parse calls, returns, and expressions
This commit is contained in:
243
ast/ast.go
243
ast/ast.go
@@ -3,18 +3,22 @@ package ast
|
||||
import (
|
||||
"bytes"
|
||||
"monkey/token"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// The base Node interface
|
||||
type Node interface {
|
||||
TokenLiteral() string
|
||||
String() string
|
||||
}
|
||||
|
||||
// All statement nodes implement this
|
||||
type Statement interface {
|
||||
Node
|
||||
statementNode()
|
||||
}
|
||||
|
||||
// All expression nodes implement this
|
||||
type Expression interface {
|
||||
Node
|
||||
expressionNode()
|
||||
@@ -42,67 +46,15 @@ func (p *Program) String() string {
|
||||
return out.String()
|
||||
}
|
||||
|
||||
type Identifier struct {
|
||||
Token token.Token // the token.Ident token
|
||||
Value string
|
||||
}
|
||||
|
||||
// Statements
|
||||
type LetStatement struct {
|
||||
Token token.Token // the token.Let token
|
||||
Token token.Token // the token.LET token
|
||||
Name *Identifier
|
||||
Value Expression
|
||||
}
|
||||
|
||||
type ReturnStatement struct {
|
||||
Token token.Token // the 'return token
|
||||
ReturnValue Expression
|
||||
}
|
||||
|
||||
type ExpressionStatement struct {
|
||||
Token token.Token // the first token of the expression
|
||||
Expression Expression
|
||||
}
|
||||
|
||||
type IntegerLiteral struct {
|
||||
Token token.Token
|
||||
Value int64
|
||||
}
|
||||
|
||||
type PrefixExpression struct {
|
||||
Token token.Token
|
||||
Operator string
|
||||
Right Expression
|
||||
}
|
||||
|
||||
type InfixExpression struct {
|
||||
Token token.Token
|
||||
Left Expression
|
||||
Operator string
|
||||
Right Expression
|
||||
}
|
||||
|
||||
type Boolean struct {
|
||||
Token token.Token
|
||||
Value bool
|
||||
}
|
||||
|
||||
type BlockStatement struct {
|
||||
Token token.Token // the { token
|
||||
Statements []Statement
|
||||
}
|
||||
|
||||
type IfExpression struct {
|
||||
Token token.Token // The 'if' token
|
||||
Condition Expression
|
||||
Consequence *BlockStatement
|
||||
Alternative *BlockStatement
|
||||
}
|
||||
|
||||
func (ls *LetStatement) statementNode() {
|
||||
}
|
||||
func (ls *LetStatement) TokenLiteral() string {
|
||||
return ls.Token.Literal
|
||||
}
|
||||
func (ls *LetStatement) statementNode() {}
|
||||
func (ls *LetStatement) TokenLiteral() string { return ls.Token.Literal }
|
||||
func (ls *LetStatement) String() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
@@ -119,18 +71,13 @@ func (ls *LetStatement) String() string {
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func (i *Identifier) expressionNode() {
|
||||
type ReturnStatement struct {
|
||||
Token token.Token // the 'return' token
|
||||
ReturnValue Expression
|
||||
}
|
||||
func (i *Identifier) TokenLiteral() string {
|
||||
return i.Token.Literal
|
||||
}
|
||||
func (i *Identifier) String() string { return i.Value }
|
||||
|
||||
func (rs *ReturnStatement) statementNode() {
|
||||
}
|
||||
func (rs *ReturnStatement) TokenLiteral() string {
|
||||
return rs.Token.Literal
|
||||
}
|
||||
func (rs *ReturnStatement) statementNode() {}
|
||||
func (rs *ReturnStatement) TokenLiteral() string { return rs.Token.Literal }
|
||||
func (rs *ReturnStatement) String() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
@@ -145,32 +92,73 @@ func (rs *ReturnStatement) String() string {
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func (es *ExpressionStatement) statementNode() {
|
||||
}
|
||||
func (es *ExpressionStatement) TokenLiteral() string {
|
||||
return es.Token.Literal
|
||||
type ExpressionStatement struct {
|
||||
Token token.Token // the first token of the expression
|
||||
Expression Expression
|
||||
}
|
||||
|
||||
func (es *ExpressionStatement) statementNode() {}
|
||||
func (es *ExpressionStatement) TokenLiteral() string { return es.Token.Literal }
|
||||
func (es *ExpressionStatement) String() string {
|
||||
if es.Expression != nil {
|
||||
return es.Expression.String()
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (il *IntegerLiteral) expressionNode() {
|
||||
}
|
||||
func (il *IntegerLiteral) TokenLiteral() string {
|
||||
return il.Token.Literal
|
||||
}
|
||||
func (il *IntegerLiteral) String() string {
|
||||
return il.Token.Literal
|
||||
type BlockStatement struct {
|
||||
Token token.Token // the { token
|
||||
Statements []Statement
|
||||
}
|
||||
|
||||
func (pe *PrefixExpression) expressionNode() {}
|
||||
func (pe *PrefixExpression) TokenLiteral() string {
|
||||
return pe.Token.Literal
|
||||
func (bs *BlockStatement) statementNode() {}
|
||||
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()
|
||||
}
|
||||
|
||||
// Expressions
|
||||
type Identifier struct {
|
||||
Token token.Token // the token.IDENT token
|
||||
Value string
|
||||
}
|
||||
|
||||
func (i *Identifier) expressionNode() {}
|
||||
func (i *Identifier) TokenLiteral() string { return i.Token.Literal }
|
||||
func (i *Identifier) String() string { return i.Value }
|
||||
|
||||
type Boolean struct {
|
||||
Token token.Token
|
||||
Value bool
|
||||
}
|
||||
|
||||
func (b *Boolean) expressionNode() {}
|
||||
func (b *Boolean) TokenLiteral() string { return b.Token.Literal }
|
||||
func (b *Boolean) String() string { return b.Token.Literal }
|
||||
|
||||
type IntegerLiteral struct {
|
||||
Token token.Token
|
||||
Value int64
|
||||
}
|
||||
|
||||
func (il *IntegerLiteral) expressionNode() {}
|
||||
func (il *IntegerLiteral) TokenLiteral() string { return il.Token.Literal }
|
||||
func (il *IntegerLiteral) String() string { return il.Token.Literal }
|
||||
|
||||
type PrefixExpression struct {
|
||||
Token token.Token // The prefix token, e.g. !
|
||||
Operator string
|
||||
Right Expression
|
||||
}
|
||||
|
||||
func (pe *PrefixExpression) expressionNode() {}
|
||||
func (pe *PrefixExpression) TokenLiteral() string { return pe.Token.Literal }
|
||||
func (pe *PrefixExpression) String() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
@@ -182,10 +170,15 @@ func (pe *PrefixExpression) String() string {
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func (ie *InfixExpression) expressionNode() {}
|
||||
func (ie *InfixExpression) TokenLiteral() string {
|
||||
return ie.Token.Literal
|
||||
type InfixExpression struct {
|
||||
Token token.Token // The operator token, e.g. +
|
||||
Left Expression
|
||||
Operator string
|
||||
Right Expression
|
||||
}
|
||||
|
||||
func (ie *InfixExpression) expressionNode() {}
|
||||
func (ie *InfixExpression) TokenLiteral() string { return ie.Token.Literal }
|
||||
func (ie *InfixExpression) String() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
@@ -198,32 +191,16 @@ func (ie *InfixExpression) String() string {
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func (b Boolean) TokenLiteral() string {
|
||||
return b.Token.Literal
|
||||
type IfExpression struct {
|
||||
Token token.Token // The 'if' token
|
||||
Condition Expression
|
||||
Consequence *BlockStatement
|
||||
Alternative *BlockStatement
|
||||
}
|
||||
func (b Boolean) String() string {
|
||||
return b.Token.Literal
|
||||
}
|
||||
func (b Boolean) expressionNode() {}
|
||||
|
||||
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() {}
|
||||
|
||||
func (ie IfExpression) TokenLiteral() string {
|
||||
return ie.Token.Literal
|
||||
}
|
||||
func (ie IfExpression) String() string {
|
||||
func (ie *IfExpression) expressionNode() {}
|
||||
func (ie *IfExpression) TokenLiteral() string { return ie.Token.Literal }
|
||||
func (ie *IfExpression) String() string {
|
||||
var out bytes.Buffer
|
||||
|
||||
out.WriteString("if")
|
||||
@@ -238,4 +215,52 @@ func (ie IfExpression) String() string {
|
||||
|
||||
return out.String()
|
||||
}
|
||||
func (ie IfExpression) expressionNode() {}
|
||||
|
||||
type FunctionLiteral struct {
|
||||
Token token.Token // The 'fn' token
|
||||
Parameters []*Identifier
|
||||
Body *BlockStatement
|
||||
}
|
||||
|
||||
func (fl *FunctionLiteral) expressionNode() {}
|
||||
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()
|
||||
}
|
||||
|
||||
type CallExpression struct {
|
||||
Token token.Token // The '(' token
|
||||
Function Expression // Identifier or FunctionLiteral
|
||||
Arguments []Expression
|
||||
}
|
||||
|
||||
func (ce *CallExpression) expressionNode() {}
|
||||
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()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user