2022-05-23 09:26:34 -05:00
|
|
|
package ast
|
|
|
|
|
2022-05-30 00:51:27 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"monkey/token"
|
2022-07-31 16:44:35 -05:00
|
|
|
"strings"
|
2022-05-30 00:51:27 -05:00
|
|
|
)
|
2022-05-23 09:26:34 -05:00
|
|
|
|
|
|
|
type Node interface {
|
2022-05-23 12:35:55 -05:00
|
|
|
TokenLiteral() string
|
2022-05-30 00:51:27 -05:00
|
|
|
String() string
|
2022-05-23 09:26:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type Statement interface {
|
|
|
|
Node
|
|
|
|
statement_node()
|
|
|
|
}
|
|
|
|
|
|
|
|
type Expression interface {
|
|
|
|
Node
|
|
|
|
expression_node()
|
|
|
|
}
|
|
|
|
|
2022-05-30 00:51:27 -05:00
|
|
|
func (l_program *Program) String() string {
|
|
|
|
var out bytes.Buffer
|
|
|
|
for _, s := range l_program.Statements {
|
|
|
|
out.WriteString(s.String())
|
|
|
|
}
|
|
|
|
return out.String()
|
|
|
|
}
|
|
|
|
|
2022-05-23 09:26:34 -05:00
|
|
|
type Program struct {
|
|
|
|
Statements []Statement
|
|
|
|
}
|
|
|
|
|
2022-07-31 16:44:35 -05:00
|
|
|
// Let Statements
|
2022-05-23 09:26:34 -05:00
|
|
|
type LetStatement struct {
|
2022-05-27 10:07:19 -05:00
|
|
|
Token token.Token // token.LET token
|
2022-05-23 09:26:34 -05:00
|
|
|
Name *Identifier
|
|
|
|
Value Expression
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LetStatement) statement_node() {}
|
|
|
|
|
|
|
|
func (ls *LetStatement) TokenLiteral() string {
|
|
|
|
return ls.Token.Literal
|
|
|
|
}
|
|
|
|
|
2022-05-30 00:51:27 -05:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Identifier
|
2022-07-31 16:44:35 -05:00
|
|
|
type Identifier struct {
|
|
|
|
Token token.Token // the token.IDENT token
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
2022-05-23 09:26:34 -05:00
|
|
|
func (i *Identifier) expression_node() {}
|
|
|
|
|
|
|
|
func (i *Identifier) TokenLiteral() string {
|
|
|
|
return i.Token.Literal
|
|
|
|
}
|
|
|
|
|
2022-05-30 00:51:27 -05:00
|
|
|
func (i *Identifier) String() string {
|
|
|
|
return i.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Program
|
2022-05-23 09:26:34 -05:00
|
|
|
func (p *Program) TokenLiteral() string {
|
|
|
|
if len(p.Statements) > 0 {
|
|
|
|
return p.Statements[0].TokenLiteral()
|
|
|
|
} else {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
2022-05-27 10:07:19 -05:00
|
|
|
|
2022-05-30 00:51:27 -05:00
|
|
|
// Return Statements
|
2022-07-31 16:44:35 -05:00
|
|
|
type ReturnStatement struct {
|
|
|
|
Token token.Token // token.RETURN token
|
|
|
|
ReturnValue Expression
|
|
|
|
}
|
|
|
|
|
2022-05-27 10:07:19 -05:00
|
|
|
func (rs *ReturnStatement) statement_node() {}
|
|
|
|
|
|
|
|
func (rs *ReturnStatement) TokenLiteral() string {
|
|
|
|
return rs.Token.Literal
|
|
|
|
}
|
2022-05-30 00:51:27 -05:00
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Expression Statement
|
2022-07-31 16:44:35 -05:00
|
|
|
type ExpressionStatement struct {
|
|
|
|
Token token.Token // the first token in the expression
|
|
|
|
Expression Expression
|
|
|
|
}
|
|
|
|
|
2022-05-30 00:51:27 -05:00
|
|
|
func (es *ExpressionStatement) statement_node() {}
|
|
|
|
|
|
|
|
func (es *ExpressionStatement) TokenLiteral() string {
|
|
|
|
return es.Token.Literal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *ExpressionStatement) String() string {
|
|
|
|
if es.Expression != nil {
|
|
|
|
return es.Expression.String()
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2022-05-30 12:40:03 -05:00
|
|
|
|
|
|
|
// IntegerLiteral
|
2022-07-31 16:44:35 -05:00
|
|
|
type IntegerLiteral struct {
|
|
|
|
Token token.Token
|
|
|
|
Value int64
|
|
|
|
}
|
|
|
|
|
2022-05-30 12:40:03 -05:00
|
|
|
func (il *IntegerLiteral) expression_node() {}
|
|
|
|
|
|
|
|
func (il *IntegerLiteral) TokenLiteral() string {
|
|
|
|
return il.Token.Literal
|
|
|
|
}
|
|
|
|
|
|
|
|
func (il *IntegerLiteral) String() string {
|
|
|
|
return il.Token.Literal
|
|
|
|
}
|
2022-05-31 06:32:43 -05:00
|
|
|
|
|
|
|
// PrefixExpression
|
2022-07-31 16:44:35 -05:00
|
|
|
type PrefixExpression struct {
|
|
|
|
Token token.Token // prefix token i.e. !
|
|
|
|
Operator string
|
|
|
|
Right Expression
|
|
|
|
}
|
|
|
|
|
2022-05-31 06:32:43 -05:00
|
|
|
func (pe *PrefixExpression) expression_node() {}
|
|
|
|
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()
|
|
|
|
}
|
2022-05-31 10:01:43 -05:00
|
|
|
|
|
|
|
// Infix Expression
|
2022-07-31 16:44:35 -05:00
|
|
|
type InfixExpression struct {
|
|
|
|
Token token.Token // operator tokens i.e. +, -, *, /
|
|
|
|
Left Expression
|
|
|
|
Operator string
|
|
|
|
Right Expression
|
|
|
|
}
|
|
|
|
|
2022-05-31 10:01:43 -05:00
|
|
|
func (ie *InfixExpression) expression_node() {}
|
|
|
|
func (ie *InfixExpression) TokenLiteral() string { return ie.Token.Literal }
|
|
|
|
func (ie *InfixExpression) String() string {
|
|
|
|
var out bytes.Buffer
|
|
|
|
out.WriteString("(")
|
|
|
|
out.WriteString(ie.Left.String())
|
|
|
|
out.WriteString(" " + ie.Operator + " ")
|
|
|
|
out.WriteString(ie.Right.String())
|
|
|
|
out.WriteString(")")
|
|
|
|
|
|
|
|
return out.String()
|
|
|
|
}
|
2022-07-14 02:26:34 -05:00
|
|
|
|
|
|
|
// Booleans
|
2022-07-31 16:44:35 -05:00
|
|
|
type Boolean struct {
|
|
|
|
Token token.Token
|
|
|
|
Value bool
|
|
|
|
}
|
|
|
|
|
2022-07-14 07:23:22 -05:00
|
|
|
func (b *Boolean) expression_node() {}
|
2022-07-14 02:26:34 -05:00
|
|
|
func (b *Boolean) TokenLiteral() string { return b.Token.Literal }
|
2022-07-14 07:23:22 -05:00
|
|
|
func (b *Boolean) String() string { return b.Token.Literal }
|
2022-07-31 16:44:35 -05:00
|
|
|
|
|
|
|
// If Expression
|
|
|
|
type IfExpression struct {
|
|
|
|
Token token.Token // the 'if' token
|
|
|
|
Condition Expression
|
|
|
|
Consequence *BlockStatement
|
|
|
|
Alternative *BlockStatement
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ie *IfExpression) expression_node() {}
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block Statements
|
|
|
|
type BlockStatement struct {
|
|
|
|
Token token.Token // the { token
|
|
|
|
Statements []Statement
|
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BlockStatement) statement_node() {}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function literals
|
|
|
|
type FunctionLiteral struct {
|
|
|
|
Token token.Token // the 'fn' token
|
|
|
|
Parameters []*Identifier
|
|
|
|
Body *BlockStatement
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fl *FunctionLiteral) expression_node() {}
|
|
|
|
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()
|
|
|
|
}
|