60 lines
1.1 KiB
Go
60 lines
1.1 KiB
Go
|
|
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
|
||
|
|
}
|