diff options
| author | Michael Tews <michael@tews.dev> | 2024-12-13 07:16:00 +0100 |
|---|---|---|
| committer | Michael Tews <michael@tews.dev> | 2026-04-12 11:11:02 +0200 |
| commit | 68ff1414130b7bdd9cbfe0e5c1fc60278f9a48fd (patch) | |
| tree | 5baeea833552b3f60f2c9f5dbf7d82492e066358 /ast/ast.go | |
| parent | 0f51131eedb73d371bcf4d4868cb784f3592d08c (diff) | |
Signed-off-by: Michael Tews <michael@tews.dev>
Diffstat (limited to 'ast/ast.go')
| -rw-r--r-- | ast/ast.go | 91 |
1 files changed, 91 insertions, 0 deletions
@@ -1,11 +1,14 @@ package ast import ( + "bytes" + "github.com/mewsen/interpreter/token" ) type Node interface { TokenLiteral() string + String() string } type Statement interface { @@ -30,6 +33,16 @@ func (p *Program) TokenLiteral() string { } } +func (p *Program) String() string { + var out bytes.Buffer + + for _, s := range p.Statements { + out.WriteString(s.String()) + } + + return out.String() +} + type LetStatement struct { Token token.Token Name *Identifier @@ -38,6 +51,19 @@ type LetStatement struct { 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()) + } + + return out.String() +} type Identifier struct { Token token.Token @@ -46,3 +72,68 @@ type Identifier struct { func (i *Identifier) expressionNode() {} func (i *Identifier) TokenLiteral() string { return i.Token.Literal } +func (i *Identifier) String() string { return i.Value } + +type ReturnStatement struct { + Token token.Token + ReturnValue Expression +} + +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() +} + +type ExpressionStatement struct { + Token token.Token + 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 "" +} + +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 + 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 + + out.WriteString("(") + out.WriteString(pe.Operator) + out.WriteString(pe.Right.String()) + out.WriteString(")") + + return out.String() +} |
