prefix parsing
This commit is contained in:
104
ast/ast.go
104
ast/ast.go
@@ -1,9 +1,13 @@
|
|||||||
package ast
|
package ast
|
||||||
|
|
||||||
import "monkey/token"
|
import (
|
||||||
|
"bytes"
|
||||||
|
"monkey/token"
|
||||||
|
)
|
||||||
|
|
||||||
type Node interface {
|
type Node interface {
|
||||||
TokenLiteral() string
|
TokenLiteral() string
|
||||||
|
String() string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Statement interface {
|
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 {
|
type Identifier struct {
|
||||||
Token token.Token // the token.Ident token
|
Token token.Token // the token.Ident token
|
||||||
Value string
|
Value string
|
||||||
@@ -44,20 +58,102 @@ type ReturnStatement struct {
|
|||||||
ReturnValue Expression
|
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) statementNode() {
|
||||||
}
|
}
|
||||||
func (ls *LetStatement) TokenLiteral() string {
|
func (ls *LetStatement) TokenLiteral() string {
|
||||||
return ls.Token.Literal
|
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) expressionNode() {
|
||||||
}
|
}
|
||||||
func (i *Identifier) TokenLiteral() string {
|
func (i *Identifier) TokenLiteral() string {
|
||||||
return i.Token.Literal
|
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 {
|
func (rs *ReturnStatement) TokenLiteral() string {
|
||||||
return i.Token.Literal
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,6 +5,18 @@ import (
|
|||||||
"monkey/ast"
|
"monkey/ast"
|
||||||
"monkey/lexer"
|
"monkey/lexer"
|
||||||
"monkey/token"
|
"monkey/token"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
_ int = iota
|
||||||
|
LOWEST
|
||||||
|
EQUALS // ==
|
||||||
|
LESSGREATER // > or <
|
||||||
|
SUM // +
|
||||||
|
PRODUCT // *
|
||||||
|
PREFIX // -X or !X
|
||||||
|
CALL // myFunction(X)
|
||||||
)
|
)
|
||||||
|
|
||||||
type Parser struct {
|
type Parser struct {
|
||||||
@@ -13,8 +25,16 @@ type Parser struct {
|
|||||||
|
|
||||||
curToken token.Token
|
curToken token.Token
|
||||||
peekToken token.Token
|
peekToken token.Token
|
||||||
|
|
||||||
|
prefixParseFns map[token.TokenType]prefixParseFn
|
||||||
|
infixParseFns map[token.TokenType]infixParseFn
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type (
|
||||||
|
prefixParseFn func() ast.Expression
|
||||||
|
infixParseFn func(expression ast.Expression) ast.Expression
|
||||||
|
)
|
||||||
|
|
||||||
func (p *Parser) nextToken() {
|
func (p *Parser) nextToken() {
|
||||||
p.curToken = p.peekToken
|
p.curToken = p.peekToken
|
||||||
p.peekToken = p.l.NextToken()
|
p.peekToken = p.l.NextToken()
|
||||||
@@ -26,6 +46,12 @@ func New(l *lexer.Lexer) *Parser {
|
|||||||
errors: []string{},
|
errors: []string{},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p.prefixParseFns = make(map[token.TokenType]prefixParseFn)
|
||||||
|
p.registerPrefix(token.IDENT, p.parseIdentifier)
|
||||||
|
p.registerPrefix(token.INT, p.parseIntegerLiteral)
|
||||||
|
p.registerPrefix(token.BANG, p.parsePrefixExpression)
|
||||||
|
p.registerPrefix(token.MINUS, p.parsePrefixExpression)
|
||||||
|
|
||||||
// Read two token, so curToken and peekToken are both set
|
// Read two token, so curToken and peekToken are both set
|
||||||
p.nextToken()
|
p.nextToken()
|
||||||
p.nextToken()
|
p.nextToken()
|
||||||
@@ -55,7 +81,7 @@ func (p *Parser) parseStatement() ast.Statement {
|
|||||||
case token.RETURN:
|
case token.RETURN:
|
||||||
return p.parseReturnStatement()
|
return p.parseReturnStatement()
|
||||||
default:
|
default:
|
||||||
return nil
|
return p.parseExpressionsStatement()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,3 +149,74 @@ func (p *Parser) parseReturnStatement() *ast.ReturnStatement {
|
|||||||
|
|
||||||
return stmt
|
return stmt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseExpressionsStatement() *ast.ExpressionStatement {
|
||||||
|
stmt := &ast.ExpressionStatement{Token: p.curToken}
|
||||||
|
|
||||||
|
stmt.Expression = p.parseExpression(LOWEST)
|
||||||
|
|
||||||
|
if p.peekTokenIs(token.SEMICOLON) {
|
||||||
|
p.nextToken()
|
||||||
|
}
|
||||||
|
|
||||||
|
return stmt
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseExpression(precedence int) ast.Expression {
|
||||||
|
prefix := p.prefixParseFns[p.curToken.Type]
|
||||||
|
if prefix == nil {
|
||||||
|
p.noPrefixParseFnError(p.curToken.Type)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
leftExp := prefix()
|
||||||
|
|
||||||
|
return leftExp
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseIntegerLiteral() ast.Expression {
|
||||||
|
lit := &ast.IntegerLiteral{Token: p.curToken}
|
||||||
|
|
||||||
|
value, err := strconv.ParseInt(p.curToken.Literal, 0, 64)
|
||||||
|
if err != nil {
|
||||||
|
msg := fmt.Sprintf("could not parse %q as integer", p.curToken.Literal)
|
||||||
|
p.errors = append(p.errors, msg)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
lit.Value = value
|
||||||
|
|
||||||
|
return lit
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) registerPrefix(tokenType token.TokenType, fn prefixParseFn) {
|
||||||
|
p.prefixParseFns[tokenType] = fn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) registerInfix(tokenType token.TokenType, fn infixParseFn) {
|
||||||
|
p.infixParseFns[tokenType] = fn
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parseIdentifier() ast.Expression {
|
||||||
|
return &ast.Identifier{
|
||||||
|
Token: p.curToken,
|
||||||
|
Value: p.curToken.Literal,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) noPrefixParseFnError(t token.TokenType) {
|
||||||
|
msg := fmt.Sprintf("no prefix parse function for %s found", t)
|
||||||
|
p.errors = append(p.errors, msg)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Parser) parsePrefixExpression() ast.Expression {
|
||||||
|
expression := &ast.PrefixExpression{
|
||||||
|
Token: p.curToken,
|
||||||
|
Operator: p.curToken.Literal,
|
||||||
|
}
|
||||||
|
|
||||||
|
p.nextToken()
|
||||||
|
|
||||||
|
expression.Right = p.parseExpression(PREFIX)
|
||||||
|
|
||||||
|
return expression
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package parser
|
package parser
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"monkey/ast"
|
"monkey/ast"
|
||||||
"monkey/lexer"
|
"monkey/lexer"
|
||||||
"testing"
|
"testing"
|
||||||
@@ -73,3 +74,116 @@ func testLetStatements(t *testing.T, s ast.Statement, name string) bool {
|
|||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestIdentifierExpressions(t *testing.T) {
|
||||||
|
input := "foobar;"
|
||||||
|
|
||||||
|
l := lexer.New(input)
|
||||||
|
p := New(l)
|
||||||
|
program := p.ParseProgram()
|
||||||
|
checkParserErrors(t, p)
|
||||||
|
|
||||||
|
if len(program.Statements) != 1 {
|
||||||
|
t.Fatalf("program has not enough statements. got=%d", len(program.Statements))
|
||||||
|
}
|
||||||
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
ident, ok := stmt.Expression.(*ast.Identifier)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("exp is not *ast.Identifier. got=%T", stmt.Expression)
|
||||||
|
}
|
||||||
|
if ident.Value != "foobar" {
|
||||||
|
t.Errorf("ident.Value not %s. got=%s", "foobar", ident.Value)
|
||||||
|
}
|
||||||
|
if ident.TokenLiteral() != "foobar" {
|
||||||
|
t.Errorf("ident.TokenLiteral not %s. got=%s", "foobar", ident.TokenLiteral())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIntegerLiteralExpressions(t *testing.T) {
|
||||||
|
input := "5;"
|
||||||
|
|
||||||
|
l := lexer.New(input)
|
||||||
|
p := New(l)
|
||||||
|
program := p.ParseProgram()
|
||||||
|
checkParserErrors(t, p)
|
||||||
|
|
||||||
|
if len(program.Statements) != 1 {
|
||||||
|
t.Fatalf("program has not enough statements. got=%d", len(program.Statements))
|
||||||
|
}
|
||||||
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
literal, ok := stmt.Expression.(*ast.IntegerLiteral)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("exp is not *ast.IntegerLiteral. got=%T", stmt.Expression)
|
||||||
|
}
|
||||||
|
if literal.Value != 5 {
|
||||||
|
t.Errorf("ident.Value not %d. got=%d", 5, literal.Value)
|
||||||
|
}
|
||||||
|
if literal.TokenLiteral() != "5" {
|
||||||
|
t.Errorf("ident.TokenLiteral not %s. got=%s", "5", literal.TokenLiteral())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParsingPrefixExpressions(t *testing.T) {
|
||||||
|
prefixTests := []struct {
|
||||||
|
input string
|
||||||
|
operator string
|
||||||
|
integerValue int64
|
||||||
|
}{
|
||||||
|
{"!5;", "!", 5},
|
||||||
|
{"-15;", "-", 15},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range prefixTests {
|
||||||
|
l := lexer.New(tt.input)
|
||||||
|
p := New(l)
|
||||||
|
program := p.ParseProgram()
|
||||||
|
checkParserErrors(t, p)
|
||||||
|
|
||||||
|
if len(program.Statements) != 1 {
|
||||||
|
t.Fatalf("program.Statements does not contain %d statements. got=%d\n", 1, len(program.Statements))
|
||||||
|
}
|
||||||
|
stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("program.Statements[0] is not ast.ExpressionStatement. got=%T", program.Statements[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
exp, ok := stmt.Expression.(*ast.PrefixExpression)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("exp is not *ast.PrefixExpression. got=%T", stmt.Expression)
|
||||||
|
}
|
||||||
|
if exp.Operator != tt.operator {
|
||||||
|
t.Fatalf("exp.Operator is not '%s'. got=%s", tt.operator, exp.Operator)
|
||||||
|
}
|
||||||
|
if !testIntegerLiteral(t, exp.Right, tt.integerValue) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func testIntegerLiteral(t *testing.T, il ast.Expression, value int64) bool {
|
||||||
|
integ, ok := il.(*ast.IntegerLiteral)
|
||||||
|
if !ok {
|
||||||
|
t.Errorf("il not *ast.IntegerLiteral. got=%T", il)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if integ.Value != value {
|
||||||
|
t.Errorf("integ.Value not %d. got=%d", value, integ.Value)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
if integ.TokenLiteral() != fmt.Sprintf("%d", value) {
|
||||||
|
t.Errorf("integ.TopkenLiteral not %d. got=%s", value, integ.TokenLiteral())
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user