prefix parsing
This commit is contained in:
104
ast/ast.go
104
ast/ast.go
@@ -1,9 +1,13 @@
|
||||
package ast
|
||||
|
||||
import "monkey/token"
|
||||
import (
|
||||
"bytes"
|
||||
"monkey/token"
|
||||
)
|
||||
|
||||
type Node interface {
|
||||
TokenLiteral() string
|
||||
String() string
|
||||
}
|
||||
|
||||
type Statement interface {
|
||||
@@ -28,6 +32,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 Identifier struct {
|
||||
Token token.Token // the token.Ident token
|
||||
Value string
|
||||
@@ -44,20 +58,102 @@ type ReturnStatement struct {
|
||||
ReturnValue Expression
|
||||
}
|
||||
|
||||
type ExpressionStatement struct {
|
||||
Token token.Token // the first token of the expression
|
||||
Expression Expression
|
||||
}
|
||||
|
||||
type IntegerLiteral struct {
|
||||
Token token.Token
|
||||
Value int64
|
||||
}
|
||||
|
||||
type PrefixExpression struct {
|
||||
Token token.Token
|
||||
Operator string
|
||||
Right Expression
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
|
||||
out.WriteString(";")
|
||||
|
||||
return out.String()
|
||||
}
|
||||
|
||||
func (i *Identifier) expressionNode() {
|
||||
}
|
||||
func (i *Identifier) TokenLiteral() string {
|
||||
return i.Token.Literal
|
||||
}
|
||||
func (i *Identifier) String() string { return i.Value }
|
||||
|
||||
func (i *ReturnStatement) statementNode() {
|
||||
func (rs *ReturnStatement) statementNode() {
|
||||
}
|
||||
func (i *ReturnStatement) TokenLiteral() string {
|
||||
return i.Token.Literal
|
||||
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()
|
||||
}
|
||||
|
||||
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 ""
|
||||
}
|
||||
|
||||
func (il *IntegerLiteral) expressionNode() {
|
||||
}
|
||||
func (il *IntegerLiteral) TokenLiteral() string {
|
||||
return il.Token.Literal
|
||||
}
|
||||
func (il *IntegerLiteral) String() string {
|
||||
return il.Token.Literal
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
28
ast/ast_test.go
Normal file
28
ast/ast_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package ast
|
||||
|
||||
import (
|
||||
"monkey/token"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestString(t *testing.T) {
|
||||
program := &Program{
|
||||
Statements: []Statement{
|
||||
&LetStatement{
|
||||
Token: token.Token{Type: token.LET, Literal: "let"},
|
||||
Name: &Identifier{
|
||||
Token: token.Token{Type: token.IDENT, Literal: "myVar"},
|
||||
Value: "myVar",
|
||||
},
|
||||
Value: &Identifier{
|
||||
Token: token.Token{Type: token.IDENT, Literal: "anotherVar"},
|
||||
Value: "anotherVar",
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
if program.String() != "let myVar = anotherVar;" {
|
||||
t.Errorf("program.String wrong. got=%q", program.String())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user