ast

package
v0.0.0-...-b6e707d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2018 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package ast provides AST definition for GoCaml.

Example
package main

import (
	"fmt"
	"github.com/rhysd/gocaml/token"
	"github.com/rhysd/locerr"
)

// Visitor which counts number of nodes in AST
type printPath struct {
	total int
}

// VisitTopdown method is called before children are visited
func (v *printPath) VisitTopdown(e Expr) Visitor {
	fmt.Printf("\n -> %s (topdown)", e.Name())
	return v
}

// VisitBottomup method is called after children were visited
func (v *printPath) VisitBottomup(e Expr) {
	fmt.Printf("\n -> %s (bottomup)", e.Name())
}

func main() {
	src := locerr.NewDummySource("")

	// AST which usually comes from syntax.Parse() function.
	rootOfAST := &Let{
		LetToken: &token.Token{File: src},
		Symbol:   NewSymbol("test"),
		Bound: &Int{
			Token: &token.Token{File: src},
			Value: 42,
		},
		Body: &Add{
			Left: &VarRef{
				Token:  &token.Token{File: src},
				Symbol: NewSymbol("test"),
			},
			Right: &Float{
				Token: &token.Token{File: src},
				Value: 3.14,
			},
		},
	}

	ast := &AST{Root: rootOfAST}

	// Apply visitor to root node of AST
	v := &printPath{0}
	fmt.Println("ROOT")

	Visit(v, ast.Root)
	
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fprint

func Fprint(out io.Writer, a *AST)

Fprint outputs a structure of AST to given io.Writer object

func Print

func Print(a *AST)

Print outputs a structure of AST to stdout.

func Println

func Println(a *AST)

Println does the same as Print and append newline at the end of output.

func Visit

func Visit(vis Visitor, e Expr)

Visit visits the tree with the visitor.

Types

type AST

type AST struct {
	Root      Expr
	TypeDecls []*TypeDecl
	Externals []*External
}

func (*AST) File

func (a *AST) File() *locerr.Source

type Add

type Add struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*Add) End

func (e *Add) End() locerr.Pos

func (*Add) Name

func (e *Add) Name() string

func (*Add) Pos

func (e *Add) Pos() locerr.Pos

type And

type And struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*And) End

func (e *And) End() locerr.Pos

func (*And) Name

func (e *And) Name() string

func (*And) Pos

func (e *And) Pos() locerr.Pos

type Apply

type Apply struct {
	Callee Expr
	Args   []Expr
}

AST node which meets Expr interface

func (*Apply) End

func (e *Apply) End() locerr.Pos

func (*Apply) Name

func (e *Apply) Name() string

func (*Apply) Pos

func (e *Apply) Pos() locerr.Pos

type ArrayGet

type ArrayGet struct {
	Array, Index Expr
}

AST node which meets Expr interface

func (*ArrayGet) End

func (e *ArrayGet) End() locerr.Pos

func (*ArrayGet) Name

func (e *ArrayGet) Name() string

func (*ArrayGet) Pos

func (e *ArrayGet) Pos() locerr.Pos

type ArrayLit

type ArrayLit struct {
	StartToken *token.Token
	EndToken   *token.Token
	Elems      []Expr
}

AST node which meets Expr interface

func (*ArrayLit) End

func (e *ArrayLit) End() locerr.Pos

func (*ArrayLit) Name

func (e *ArrayLit) Name() string

func (*ArrayLit) Pos

func (e *ArrayLit) Pos() locerr.Pos

type ArrayMake

type ArrayMake struct {
	ArrayToken *token.Token
	Size, Elem Expr
}

AST node which meets Expr interface

func (*ArrayMake) End

func (e *ArrayMake) End() locerr.Pos

func (*ArrayMake) Name

func (e *ArrayMake) Name() string

func (*ArrayMake) Pos

func (e *ArrayMake) Pos() locerr.Pos

type ArrayPut

type ArrayPut struct {
	Array, Index, Assignee Expr
}

AST node which meets Expr interface

func (*ArrayPut) End

func (e *ArrayPut) End() locerr.Pos

func (*ArrayPut) Name

func (e *ArrayPut) Name() string

func (*ArrayPut) Pos

func (e *ArrayPut) Pos() locerr.Pos

type ArraySize

type ArraySize struct {
	ArrayToken *token.Token
	Target     Expr
}

AST node which meets Expr interface

func (*ArraySize) End

func (e *ArraySize) End() locerr.Pos

func (*ArraySize) Name

func (e *ArraySize) Name() string

func (*ArraySize) Pos

func (e *ArraySize) Pos() locerr.Pos

type Bool

type Bool struct {
	Token *token.Token
	Value bool
}

AST node which meets Expr interface

func (*Bool) End

func (e *Bool) End() locerr.Pos

func (*Bool) Name

func (e *Bool) Name() string

func (*Bool) Pos

func (e *Bool) Pos() locerr.Pos

type CtorType

type CtorType struct {
	StartToken *token.Token // Maybe nil
	EndToken   *token.Token
	ParamTypes []Expr
	Ctor       *Symbol
}

Note: `int` has no param

func (*CtorType) End

func (e *CtorType) End() locerr.Pos

func (*CtorType) Name

func (e *CtorType) Name() string

func (*CtorType) Pos

func (e *CtorType) Pos() locerr.Pos

type Div

type Div struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*Div) End

func (e *Div) End() locerr.Pos

func (*Div) Name

func (e *Div) Name() string

func (*Div) Pos

func (e *Div) Pos() locerr.Pos

type Eq

type Eq struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*Eq) End

func (e *Eq) End() locerr.Pos

func (*Eq) Name

func (e *Eq) Name() string

func (*Eq) Pos

func (e *Eq) Pos() locerr.Pos

type Expr

type Expr interface {
	Pos() locerr.Pos
	End() locerr.Pos
	Name() string
}

Expr is an interface for node of GoCaml AST. All nodes have its position and name.

type External

type External struct {
	StartToken *token.Token
	EndToken   *token.Token
	Ident      *Symbol
	Type       Expr
	C          string
}

AST node which meets Expr interface

func (*External) End

func (e *External) End() locerr.Pos

func (*External) Name

func (e *External) Name() string

func (*External) Pos

func (e *External) Pos() locerr.Pos

type FAdd

type FAdd struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*FAdd) End

func (e *FAdd) End() locerr.Pos

func (*FAdd) Name

func (e *FAdd) Name() string

func (*FAdd) Pos

func (e *FAdd) Pos() locerr.Pos

type FDiv

type FDiv struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*FDiv) End

func (e *FDiv) End() locerr.Pos

func (*FDiv) Name

func (e *FDiv) Name() string

func (*FDiv) Pos

func (e *FDiv) Pos() locerr.Pos

type FMul

type FMul struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*FMul) End

func (e *FMul) End() locerr.Pos

func (*FMul) Name

func (e *FMul) Name() string

func (*FMul) Pos

func (e *FMul) Pos() locerr.Pos

type FNeg

type FNeg struct {
	MinusToken *token.Token
	Child      Expr
}

AST node which meets Expr interface

func (*FNeg) End

func (e *FNeg) End() locerr.Pos

func (*FNeg) Name

func (e *FNeg) Name() string

func (*FNeg) Pos

func (e *FNeg) Pos() locerr.Pos

type FSub

type FSub struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*FSub) End

func (e *FSub) End() locerr.Pos

func (*FSub) Name

func (e *FSub) Name() string

func (*FSub) Pos

func (e *FSub) Pos() locerr.Pos

type Float

type Float struct {
	Token *token.Token
	Value float64
}

AST node which meets Expr interface

func (*Float) End

func (e *Float) End() locerr.Pos

func (*Float) Name

func (e *Float) Name() string

func (*Float) Pos

func (e *Float) Pos() locerr.Pos

type FuncDef

type FuncDef struct {
	Symbol  *Symbol
	Params  []Param
	Body    Expr
	RetType Expr
}

func (*FuncDef) ParamSymbols

func (d *FuncDef) ParamSymbols() []*Symbol

type FuncType

type FuncType struct {
	ParamTypes []Expr
	RetType    Expr
}

AST node which meets Expr interface

func (*FuncType) End

func (e *FuncType) End() locerr.Pos

func (*FuncType) Name

func (e *FuncType) Name() string

func (*FuncType) Pos

func (e *FuncType) Pos() locerr.Pos

type Greater

type Greater struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*Greater) End

func (e *Greater) End() locerr.Pos

func (*Greater) Name

func (e *Greater) Name() string

func (*Greater) Pos

func (e *Greater) Pos() locerr.Pos

type GreaterEq

type GreaterEq struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*GreaterEq) End

func (e *GreaterEq) End() locerr.Pos

func (*GreaterEq) Name

func (e *GreaterEq) Name() string

func (*GreaterEq) Pos

func (e *GreaterEq) Pos() locerr.Pos

type If

type If struct {
	IfToken          *token.Token
	Cond, Then, Else Expr
}

AST node which meets Expr interface

func (*If) End

func (e *If) End() locerr.Pos

func (*If) Name

func (e *If) Name() string

func (*If) Pos

func (e *If) Pos() locerr.Pos

type Int

type Int struct {
	Token *token.Token
	Value int64
}

AST node which meets Expr interface

func (*Int) End

func (e *Int) End() locerr.Pos

func (*Int) Name

func (e *Int) Name() string

func (*Int) Pos

func (e *Int) Pos() locerr.Pos

type Less

type Less struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*Less) End

func (e *Less) End() locerr.Pos

func (*Less) Name

func (e *Less) Name() string

func (*Less) Pos

func (e *Less) Pos() locerr.Pos

type LessEq

type LessEq struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*LessEq) End

func (e *LessEq) End() locerr.Pos

func (*LessEq) Name

func (e *LessEq) Name() string

func (*LessEq) Pos

func (e *LessEq) Pos() locerr.Pos

type Let

type Let struct {
	LetToken    *token.Token
	Symbol      *Symbol
	Bound, Body Expr
	Type        Expr // Maybe nil
}

AST node which meets Expr interface

func (*Let) End

func (e *Let) End() locerr.Pos

func (*Let) Name

func (e *Let) Name() string

func (*Let) Pos

func (e *Let) Pos() locerr.Pos

type LetRec

type LetRec struct {
	LetToken *token.Token
	Func     *FuncDef
	Body     Expr
}

AST node which meets Expr interface

func (*LetRec) End

func (e *LetRec) End() locerr.Pos

func (*LetRec) Name

func (e *LetRec) Name() string

func (*LetRec) Pos

func (e *LetRec) Pos() locerr.Pos

type LetTuple

type LetTuple struct {
	LetToken    *token.Token
	Symbols     []*Symbol
	Bound, Body Expr
	Type        Expr // Maybe nil
}

AST node which meets Expr interface

func (*LetTuple) End

func (e *LetTuple) End() locerr.Pos

func (*LetTuple) Name

func (e *LetTuple) Name() string

func (*LetTuple) Pos

func (e *LetTuple) Pos() locerr.Pos

type Match

type Match struct {
	StartToken     *token.Token
	Target         Expr
	IfSome, IfNone Expr
	SomeIdent      *Symbol
	EndPos         locerr.Pos
}

AST node which meets Expr interface

func (*Match) End

func (e *Match) End() locerr.Pos

func (*Match) Name

func (e *Match) Name() string

func (*Match) Pos

func (e *Match) Pos() locerr.Pos

type Mod

type Mod struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*Mod) End

func (e *Mod) End() locerr.Pos

func (*Mod) Name

func (e *Mod) Name() string

func (*Mod) Pos

func (e *Mod) Pos() locerr.Pos

type Mul

type Mul struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*Mul) End

func (e *Mul) End() locerr.Pos

func (*Mul) Name

func (e *Mul) Name() string

func (*Mul) Pos

func (e *Mul) Pos() locerr.Pos

type Neg

type Neg struct {
	MinusToken *token.Token
	Child      Expr
}

AST node which meets Expr interface

func (*Neg) End

func (e *Neg) End() locerr.Pos

func (*Neg) Name

func (e *Neg) Name() string

func (*Neg) Pos

func (e *Neg) Pos() locerr.Pos

type None

type None struct {
	Token *token.Token
}

AST node which meets Expr interface

func (*None) End

func (e *None) End() locerr.Pos

func (*None) Name

func (e *None) Name() string

func (*None) Pos

func (e *None) Pos() locerr.Pos

type Not

type Not struct {
	OpToken *token.Token
	Child   Expr
}

AST node which meets Expr interface

func (*Not) End

func (e *Not) End() locerr.Pos

func (*Not) Name

func (e *Not) Name() string

func (*Not) Pos

func (e *Not) Pos() locerr.Pos

type NotEq

type NotEq struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*NotEq) End

func (e *NotEq) End() locerr.Pos

func (*NotEq) Name

func (e *NotEq) Name() string

func (*NotEq) Pos

func (e *NotEq) Pos() locerr.Pos

type Or

type Or struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*Or) End

func (e *Or) End() locerr.Pos

func (*Or) Name

func (e *Or) Name() string

func (*Or) Pos

func (e *Or) Pos() locerr.Pos

type Param

type Param struct {
	Ident *Symbol
	Type  Expr
}

type Printer

type Printer struct {
	// contains filtered or unexported fields
}

Printer is a visitor to print AST to io.Writer

func (Printer) VisitBottomup

func (p Printer) VisitBottomup(Expr)

func (Printer) VisitTopdown

func (p Printer) VisitTopdown(e Expr) Visitor

type Some

type Some struct {
	StartToken *token.Token
	Child      Expr
}

AST node which meets Expr interface

func (*Some) End

func (e *Some) End() locerr.Pos

func (*Some) Name

func (e *Some) Name() string

func (*Some) Pos

func (e *Some) Pos() locerr.Pos

type String

type String struct {
	Token *token.Token
	Value string
}

AST node which meets Expr interface

func (*String) End

func (e *String) End() locerr.Pos

func (*String) Name

func (e *String) Name() string

func (*String) Pos

func (e *String) Pos() locerr.Pos

type Sub

type Sub struct {
	Left, Right Expr
}

AST node which meets Expr interface

func (*Sub) End

func (e *Sub) End() locerr.Pos

func (*Sub) Name

func (e *Sub) Name() string

func (*Sub) Pos

func (e *Sub) Pos() locerr.Pos

type Symbol

type Symbol struct {
	DisplayName string
	Name        string
}

Note: This struct cannot be replaced with string because there may be the same name symbol.

func IgnoredSymbol

func IgnoredSymbol() *Symbol

func NewSymbol

func NewSymbol(name string) *Symbol

func (*Symbol) IsIgnored

func (s *Symbol) IsIgnored() bool

type Tuple

type Tuple struct {
	Elems []Expr
}

AST node which meets Expr interface

func (*Tuple) End

func (e *Tuple) End() locerr.Pos

func (*Tuple) Name

func (e *Tuple) Name() string

func (*Tuple) Pos

func (e *Tuple) Pos() locerr.Pos

type TupleType

type TupleType struct {
	ElemTypes []Expr
}

AST node which meets Expr interface

func (*TupleType) End

func (e *TupleType) End() locerr.Pos

func (*TupleType) Name

func (e *TupleType) Name() string

func (*TupleType) Pos

func (e *TupleType) Pos() locerr.Pos

type TypeDecl

type TypeDecl struct {
	Token *token.Token
	Ident *Symbol
	Type  Expr
}

AST node which meets Expr interface

func (*TypeDecl) End

func (e *TypeDecl) End() locerr.Pos

func (*TypeDecl) Name

func (e *TypeDecl) Name() string

func (*TypeDecl) Pos

func (e *TypeDecl) Pos() locerr.Pos

type Typed

type Typed struct {
	Child Expr
	Type  Expr
}

AST node which meets Expr interface

func (*Typed) End

func (e *Typed) End() locerr.Pos

func (*Typed) Name

func (e *Typed) Name() string

func (*Typed) Pos

func (e *Typed) Pos() locerr.Pos

type Unit

type Unit struct {
	LParenToken *token.Token
	RParenToken *token.Token
}

AST node which meets Expr interface

func (*Unit) End

func (e *Unit) End() locerr.Pos

func (*Unit) Name

func (e *Unit) Name() string

func (*Unit) Pos

func (e *Unit) Pos() locerr.Pos

type VarRef

type VarRef struct {
	Token  *token.Token
	Symbol *Symbol
}

AST node which meets Expr interface

func (*VarRef) End

func (e *VarRef) End() locerr.Pos

func (*VarRef) Name

func (e *VarRef) Name() string

func (*VarRef) Pos

func (e *VarRef) Pos() locerr.Pos

type Visitor

type Visitor interface {
	// VisitTopdown defines the process when a node is visit. This method is called before
	// children are visited.
	// Returned value is a next visitor to use for succeeding visit. When wanting to stop
	// visiting, please return nil.
	// A visitor visits in depth-first order.
	VisitTopdown(e Expr) Visitor
	// VisitBottomup defines the process when a node is visit. This method is called after
	// children were visited.
	VisitBottomup(e Expr)
}

Visitor is an interface for the structs which is used for traversing AST.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL