Files
monkey/ast/ast.go
2024-01-18 12:08:25 -05:00

242 lines
4.4 KiB
Go

package ast
import (
"bytes"
"monkey/token"
)
type Node interface {
TokenLiteral() string
String() string
}
type Statement interface {
Node
statementNode()
}
type Expression interface {
Node
expressionNode()
}
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()
}
type Identifier struct {
Token token.Token // the token.Ident token
Value string
}
type LetStatement struct {
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) 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 (i *Identifier) expressionNode() {
}
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) 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 (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
}
func (pe *PrefixExpression) expressionNode() {}
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 (ie *InfixExpression) expressionNode() {}
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()
}
func (b Boolean) TokenLiteral() string {
return b.Token.Literal
}
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 {
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() {}