Basic AST to represent expressions, statement, identifiers
I now understand how it works. git-svn-id: https://svn.tlawal.org/svn/monkey@12 f6afcba9-9ef1-4bdd-9b72-7484f5705bac
This commit is contained in:
parent
d37bdfa644
commit
4691f6c3e5
52
ast/ast.go
Normal file
52
ast/ast.go
Normal file
@ -0,0 +1,52 @@
|
||||
package ast
|
||||
|
||||
import "monkey/token"
|
||||
|
||||
type Node interface {
|
||||
TokeLiteral() string
|
||||
}
|
||||
|
||||
type Statement interface {
|
||||
Node
|
||||
statement_node()
|
||||
}
|
||||
|
||||
type Expression interface {
|
||||
Node
|
||||
expression_node()
|
||||
}
|
||||
|
||||
type Program struct {
|
||||
Statements []Statement
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func (ls *LetStatement) statement_node() {}
|
||||
|
||||
func (ls *LetStatement) TokenLiteral() string {
|
||||
return ls.Token.Literal
|
||||
}
|
||||
|
||||
func (i *Identifier) expression_node() {}
|
||||
|
||||
func (i *Identifier) TokenLiteral() string {
|
||||
return i.Token.Literal
|
||||
}
|
||||
|
||||
func (p *Program) TokenLiteral() string {
|
||||
if len(p.Statements) > 0 {
|
||||
return p.Statements[0].TokenLiteral()
|
||||
} else {
|
||||
return ""
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user