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 ¶
- func Fprint(out io.Writer, a *AST)
- func Print(a *AST)
- func Println(a *AST)
- func Visit(vis Visitor, e Expr)
- type AST
- type Add
- type And
- type Apply
- type ArrayGet
- type ArrayLit
- type ArrayMake
- type ArrayPut
- type ArraySize
- type Bool
- type CtorType
- type Div
- type Eq
- type Expr
- type External
- type FAdd
- type FDiv
- type FMul
- type FNeg
- type FSub
- type Float
- type FuncDef
- type FuncType
- type Greater
- type GreaterEq
- type If
- type Int
- type Less
- type LessEq
- type Let
- type LetRec
- type LetTuple
- type Match
- type Mod
- type Mul
- type Neg
- type None
- type Not
- type NotEq
- type Or
- type Param
- type Printer
- type Some
- type String
- type Sub
- type Symbol
- type Tuple
- type TupleType
- type TypeDecl
- type Typed
- type Unit
- type VarRef
- type Visitor
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type ArrayPut ¶
type ArrayPut struct {
Array, Index, Assignee Expr
}
AST node which meets Expr interface
type CtorType ¶
type CtorType struct { StartToken *token.Token // Maybe nil EndToken *token.Token ParamTypes []Expr Ctor *Symbol }
Note: `int` has no param
type External ¶
type External struct { StartToken *token.Token EndToken *token.Token Ident *Symbol Type Expr C string }
AST node which meets Expr interface
type FuncDef ¶
func (*FuncDef) ParamSymbols ¶
type LetTuple ¶
type LetTuple struct { LetToken *token.Token Symbols []*Symbol Bound, Body Expr Type Expr // Maybe nil }
AST node which meets Expr interface
type Match ¶
type Match struct { StartToken *token.Token Target Expr IfSome, IfNone Expr SomeIdent *Symbol EndPos locerr.Pos }
AST node which meets Expr interface
type Printer ¶
type Printer struct {
// contains filtered or unexported fields
}
Printer is a visitor to print AST to io.Writer
func (Printer) VisitBottomup ¶
func (Printer) VisitTopdown ¶
type Symbol ¶
Note: This struct cannot be replaced with string because there may be the same name symbol.
func IgnoredSymbol ¶
func IgnoredSymbol() *Symbol
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.
Click to show internal directories.
Click to hide internal directories.