Basic AST to represent expressions, statement, identifiers
I now understand how it works. git-svn-id: https://svn.tlawal.org/svn/monkey@10 f6afcba9-9ef1-4bdd-9b72-7484f5705bac
This commit is contained in:
parent
69ffeb209c
commit
f8f7ec04c7
50
ast/ast.go
50
ast/ast.go
@ -1,50 +0,0 @@
|
|||||||
package ast
|
|
||||||
|
|
||||||
import "monkey/token"
|
|
||||||
|
|
||||||
type Node interface {
|
|
||||||
TokenLiteral() string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Statement interface {
|
|
||||||
Node
|
|
||||||
statementNode()
|
|
||||||
}
|
|
||||||
|
|
||||||
type Expression interface {
|
|
||||||
Node
|
|
||||||
expressionNode()
|
|
||||||
}
|
|
||||||
|
|
||||||
type Identifier struct {
|
|
||||||
Token token.Token // the token.IDENT token
|
|
||||||
Value string
|
|
||||||
}
|
|
||||||
|
|
||||||
type Program struct {
|
|
||||||
Statements []Statement
|
|
||||||
}
|
|
||||||
|
|
||||||
type LetStatement struct {
|
|
||||||
Token token.Token // the token.LET token
|
|
||||||
Name *Identifier
|
|
||||||
Value Expression
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ls *LetStatement) stamementNode() {}
|
|
||||||
func (ls *LetStatement) TokenLiteral() string {
|
|
||||||
return ls.Token.Literal
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Identifier) expressionNode() {}
|
|
||||||
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