Parsing statements (skipping expressions).

Signed-off-by: jmug <u.g.a.mariano@gmail.com>
This commit is contained in:
Mariano Uvalle 2025-01-02 19:46:45 -08:00
parent 04dfd62600
commit 577fad2da6
3 changed files with 287 additions and 0 deletions

59
pkg/ast/ast.go Normal file
View file

@ -0,0 +1,59 @@
package ast
import "code.jmug.me/jmug/interpreter-in-go/pkg/token"
type Node interface {
TokenLiteral() 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()
}
return ""
}
type LetStatement struct {
Token token.Token // TODO: This is a little redundant, figure out if I can get rid of it.
Name *Identifier
Value Expression
}
func (ls *LetStatement) statementNode() {}
func (ls *LetStatement) TokenLiteral() string {
return ls.Token.Literal
}
type ReturnStatement struct {
Token token.Token // TODO: This is a little redundant, figure out if I can get rid of it.
ReturnValue Expression
}
func (rs *ReturnStatement) statementNode() {}
func (rs *ReturnStatement) TokenLiteral() string {
return rs.Token.Literal
}
type Identifier struct {
Token token.Token
Value string
}
func (i *Identifier) expressionNode() {}
func (i *Identifier) TokenLiteral() string {
return i.Token.Literal
}