| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- package ast
- import (
- "bytes"
- "github/runnignwater/monkey/token"
- "strings"
- )
- // Node @Author: simon
- // @Author: ynwdlxm@163.com
- // @Date: 2022/10/2 下午8:58
- type Node interface {
- TokenLiteral() string
- // String This will allow us to print AST nodes for debugging and to compare them with other AST nodes.
- // This is going to be really handy in tests!
- String() string
- }
- // Statement expressions produce values, statements don't
- type Statement interface {
- Node
- statementNode()
- }
- type Expression interface {
- Node
- expressionNode()
- }
- // ---------------------implementation of Node----------------------------------BEGIN-----------------------------------
- // Program 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 ""
- }
- }
- func (p *Program) String() string {
- var out bytes.Buffer
- for _, s := range p.Statements {
- out.WriteString(s.String())
- }
- return out.String()
- }
- //---------------------implementation of Node-----------------------------------END-------------------------------------
- // LetStatement let <identifier> = <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) String() string {
- var out bytes.Buffer
- out.WriteString(ls.TokenLiteral() + " ")
- out.WriteString(ls.Name.String())
- out.WriteString(" = ")
- if ls.Value != nil {
- out.WriteString(ls.Value.String())
- }
- out.WriteString(";")
- return out.String()
- }
- 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) String() string {
- return i.Value
- }
- func (i *Identifier) TokenLiteral() string {
- return i.Token.Literal
- }
- // ReturnStatement return <expression>;
- type ReturnStatement struct {
- Token token.Token // the token.RETURN
- ReturnValue Expression
- }
- func (rs *ReturnStatement) TokenLiteral() string {
- return rs.Token.Literal
- }
- func (rs *ReturnStatement) String() string {
- var out bytes.Buffer
- out.WriteString(rs.TokenLiteral() + " ")
- if rs.ReturnValue != nil {
- out.WriteString(rs.ReturnValue.String())
- }
- out.WriteString(";")
- return out.String()
- }
- func (rs *ReturnStatement) statementNode() {
- panic("implement me")
- }
- type ExpressionStatement struct {
- Token token.Token // the first token of the expression
- Expression Expression
- }
- func (es *ExpressionStatement) TokenLiteral() string {
- return es.Token.Literal
- }
- func (es *ExpressionStatement) String() string {
- if es.Expression != nil {
- return es.Expression.String()
- }
- return ""
- }
- func (es *ExpressionStatement) statementNode() {}
- // IntegerLiteral integer value e.g. 5 6 ...
- type IntegerLiteral struct {
- Token token.Token
- Value int64
- }
- func (i *IntegerLiteral) TokenLiteral() string { return i.Token.Literal }
- func (i *IntegerLiteral) String() string { return i.Token.Literal }
- func (i *IntegerLiteral) expressionNode() {}
- // PrefixExpression 前缀表示式 e.g. !a -6
- type PrefixExpression struct {
- Token token.Token // The prefix token, e.g. !
- Operator string
- Right Expression
- }
- func (pe *PrefixExpression) TokenLiteral() string { return pe.Token.Literal }
- func (pe *PrefixExpression) String() string {
- var out bytes.Buffer
- out.WriteString("(")
- out.WriteString(pe.Operator)
- out.WriteString(pe.Right.String())
- out.WriteString(")")
- return out.String()
- }
- func (pe *PrefixExpression) expressionNode() {}
- // InfixExpression <expression> <infix operator> <expression>
- type InfixExpression struct {
- Token token.Token // The operator token, e.g. + - * /
- Left Expression
- Operator string
- Right Expression
- }
- func (oe *InfixExpression) TokenLiteral() string { return oe.Token.Literal }
- func (oe *InfixExpression) String() string {
- var out bytes.Buffer
- out.WriteString("(")
- out.WriteString(oe.Left.String())
- out.WriteString(" " + oe.Operator + " ")
- out.WriteString(oe.Right.String())
- out.WriteString(")")
- return out.String()
- }
- func (oe *InfixExpression) expressionNode() {}
- // Boolean bool 表达式 e.g. true/false
- type Boolean struct {
- Token token.Token
- Value bool
- }
- func (b *Boolean) TokenLiteral() string {
- return b.Token.Literal
- }
- func (b *Boolean) String() string {
- return b.Token.Literal
- }
- func (b *Boolean) expressionNode() {}
- // IfExpression if (<condition>) <consequence> else <alternative>
- type IfExpression struct {
- Token token.Token // The 'if' token
- Condition Expression
- Consequence *BlockStatement
- Alternative *BlockStatement
- }
- func (ie *IfExpression) TokenLiteral() string {
- return ie.Token.Literal
- }
- func (ie *IfExpression) String() string {
- var out bytes.Buffer
- out.WriteString("if")
- out.WriteString(ie.Condition.String())
- out.WriteString(" ")
- out.WriteString(ie.Consequence.String())
- if ie.Alternative != nil {
- out.WriteString("else ")
- out.WriteString(ie.Alternative.String())
- }
- return out.String()
- }
- func (ie *IfExpression) expressionNode() {}
- type BlockStatement struct {
- Token token.Token // the { token
- Statements []Statement
- }
- func (bs *BlockStatement) TokenLiteral() string {
- return bs.Token.Literal
- }
- func (bs *BlockStatement) String() string {
- var out bytes.Buffer
- for _, s := range bs.Statements {
- out.WriteString(s.String())
- }
- return out.String()
- }
- func (bs *BlockStatement) statementNode() {}
- // FunctionLiteral fn <parameters> <block statement>
- type FunctionLiteral struct {
- Token token.Token // The 'fn' token
- Parameters []*Identifier
- Body *BlockStatement
- }
- func (fl *FunctionLiteral) TokenLiteral() string {
- return fl.Token.Literal
- }
- func (fl *FunctionLiteral) String() string {
- var out bytes.Buffer
- params := []string{}
- for _, p := range fl.Parameters {
- params = append(params, p.String())
- }
- out.WriteString(fl.TokenLiteral())
- out.WriteString("(")
- out.WriteString(strings.Join(params, ", "))
- out.WriteString(")")
- out.WriteString(fl.Body.String())
- return out.String()
- }
- func (fl *FunctionLiteral) expressionNode() {}
- // CallExpression <expression>(<comma separated expressions>);
- type CallExpression struct {
- Token token.Token // the '(' token
- Function Expression
- Arguments []Expression
- }
- func (ce *CallExpression) TokenLiteral() string {
- return ce.Token.Literal
- }
- func (ce *CallExpression) String() string {
- var out bytes.Buffer
- args := []string{}
- for _, a := range ce.Arguments {
- args = append(args, a.String())
- }
- out.WriteString(ce.Function.String())
- out.WriteString("(")
- out.WriteString(strings.Join(args, ", "))
- out.WriteString(")")
- return out.String()
- }
- func (ce *CallExpression) expressionNode() {}
|