package ast import "github/runnignwater/monkey/token" /** * @Author: simon * @Author: ynwdlxm@163.com * @Date: 2022/10/2 δΈ‹εˆ8:58 * @Desc: */ type Node interface { TokenLiteral() string } // expressions produce values, statements don't type Statement interface { Node statementNode() } type Expression interface { Node expressionNode() } // ---------------------implementation of Node----------------------------------BEGIN----------------------------------- // Root Node of our parser produces. type Program struct { Statements []Statement } func (p *Program) TokenLiteral() string { if len(p.Statements) > 0 { return p.Statements[0].TokenLiteral() } else { return "" } } //---------------------implementation of Node-----------------------------------END------------------------------------- // let = ; // // let x = 5 AST // |-------------------| // | *ast.Program | // |-------------------| // | Statements | // |-------------------| // ↓ // |-------------------| // | *ast.LetStatement | // |-------------------| // | Name | // |-------------------| // | Value | // |-------------------| // // *ast.Identifier *ast.Expression // type LetStatement struct { Token token.Token // the token.LET token Name *Identifier Value Expression } func (ls *LetStatement) TokenLiteral() string { return ls.Token.Literal } func (ls *LetStatement) statementNode() { panic("implement me") } type Identifier struct { Token token.Token // the token.IDENT token Value string } func (i *Identifier) expressionNode() { panic("implement me") } func (i *Identifier) TokenLiteral() string { return i.Token.Literal } // return ; type ReturnStatement struct { Token token.Token // the token.RETURN returnValue Expression } func (rs *ReturnStatement) TokenLiteral() string { return rs.Token.Literal } func (rs *ReturnStatement) statementNode() { panic("implement me") }