Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,30 @@ func (b *Boolean) expressionNode() {}
func (b *Boolean) TokenLiteral() string { return b.Token.Literal }
func (b *Boolean) String() string { return b.Token.Literal }

type CallExpression struct {
Token token.Token
Function Expression
Arguments []Expression
}

func (ce *CallExpression) expressionNode() {}
func (ce *CallExpression) TokenLiteral() string { return ce.Token.Literal }
func (ce *CallExpression) String() string {
var out bytes.Buffer

args := []string{}
for _, a := range ce.Arguments {
args = append(args, a.String())
}

out.WriteString(ce.Function.String())
out.WriteString("(")
out.WriteString(strings.Join(args, ", "))
out.WriteString(")")

return out.String()
}

type FunctionLiteral struct {
Token token.Token
Parameters []*Identifier
Expand Down
46 changes: 42 additions & 4 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var precedences = map[token.TokenType]int{
token.MINUS: SUM,
token.SLASH: PRODUCT,
token.ASTERISK: PRODUCT,
token.LPAREN: CALL,
}

type Parser struct {
Expand Down Expand Up @@ -74,6 +75,7 @@ func New(l *lexer.Lexer) *Parser {
p.registerInfix(token.NOT_EQ, p.parseInfixExpression)
p.registerInfix(token.LT, p.parseInfixExpression)
p.registerInfix(token.GT, p.parseInfixExpression)
p.registerInfix(token.LPAREN, p.parseCallExpression)

// read two tokens to set both curToken and peekToken
p.nextToken()
Expand Down Expand Up @@ -116,6 +118,36 @@ func (p *Parser) nextToken() {
p.peekToken = p.l.NextToken()
}

func (p *Parser) parseCallExpression(function ast.Expression) ast.Expression {
exp := &ast.CallExpression{Token: p.curToken, Function: function}
exp.Arguments = p.parseCallArguments()
return exp
}

func (p *Parser) parseCallArguments() []ast.Expression {
args := []ast.Expression{}

if p.peekTokenIs(token.RPAREN) {
p.nextToken()
return args
}

p.nextToken()
args = append(args, p.parseExpression(LOWEST))

for p.peekTokenIs(token.COMMA) {
p.nextToken()
p.nextToken()
args = append(args, p.parseExpression(LOWEST))
}

if !p.expectPeek(token.RPAREN) {
return nil
}

return args
}

func (p *Parser) ParseProgram() *ast.Program {
program := &ast.Program{}
program.Statements = []ast.Statement{}
Expand Down Expand Up @@ -316,8 +348,12 @@ func (p *Parser) parseLetStatement() *ast.LetStatement {
if !p.expectPeek(token.ASSIGN) {
return nil
}
// skip expression parsing for now
for !p.curTokenIs(token.SEMICOLON) {

p.nextToken()

stmt.Value = p.parseExpression(LOWEST)

for !p.curTokenIs(token.SEMICOLON) && !p.curTokenIs(token.EOF) {
p.nextToken()
}
return stmt
Expand All @@ -327,8 +363,10 @@ func (p *Parser) parseReturnStatement() *ast.ReturnStatement {
stmt := &ast.ReturnStatement{Token: p.curToken}

p.nextToken()
// skip expression parsing for now
for !p.curTokenIs(token.SEMICOLON) {

stmt.ReturnValue = p.parseExpression(LOWEST)

Comment thread
Matthew-Grayson marked this conversation as resolved.
for !p.curTokenIs(token.SEMICOLON) && !p.curTokenIs(token.EOF) {
p.nextToken()
}

Expand Down
90 changes: 70 additions & 20 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,41 @@ func TestBooleanExpression(t *testing.T) {
}
}

func TestCallExpressionParsing(t *testing.T) {
input := "add(1, 2 * 3, 4 + 5);"

l := lexer.New(input)
p := New(l)
program := p.ParseProgram()
checkParserErrors(t, p)

if len(program.Statements) != 1 {
t.Fatalf("program does not contain %d statements. got=%d", 1, len(program.Statements))
}

stmt, ok := program.Statements[0].(*ast.ExpressionStatement)
if !ok {
t.Fatalf("stmt is not ast.ExpressionStatement. got=%T", program.Statements[0])
}

exp, ok := stmt.Expression.(*ast.CallExpression)
if !ok {
t.Fatalf("stmt.Expression is not ast.CallExpression. got=%T", stmt.Expression)
}

if !testIdentifier(t, exp.Function, "add") {
return
}

if len(exp.Arguments) != 3 {
t.Fatalf("wrong number of arguments. got=%d", len(exp.Arguments))
}

testLiteralExpression(t, exp.Arguments[0], 1)
testInfixExpression(t, exp.Arguments[1], 2, "*", 3)
testInfixExpression(t, exp.Arguments[2], 4, "+", 5)
}

func TestIdentifierExpression(t *testing.T) {
input := "foobar;"

Expand Down Expand Up @@ -544,6 +579,18 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
"!(true == true)",
"(!(true == true))",
},
{
"a + add(b * c) + d",
"((a + add((b * c))) + d)",
},
{
"add(a, b, 1, 2 * 3, 4 + 5, add(6, 7 * 8))",
"add(a, b, 1, (2 * 3), (4 + 5), add(6, (7 * 8)))",
},
{
"add(a + b + c * d / f + g)",
"add((((a + b) + ((c * d) / f)) + g))",
},
}
for _, tt := range tests {
l := lexer.New(tt.input)
Expand All @@ -559,35 +606,38 @@ func TestOperatorPrecedenceParsing(t *testing.T) {
}

func TestLetStatements(t *testing.T) {
input := `
let x = 5;
let y = 10;
let foobar = 838383;
`

l := lexer.New(input)
p := New(l)

program := p.ParseProgram()
checkParserErrors(t, p)
if program == nil {
t.Fatalf("ParseProgram() returned nil")
}

tests := []struct {
input string
expectedIdentifier string
expectedValue interface{}
}{
{"x"},
{"y"},
{"foobar"},
{"let x = 5;", "x", 5},
{"let y = true;", "y", true},
{"let foobar = y;", "foobar", "y"},
}

for i, tt := range tests {
stmt := program.Statements[i]
for _, tt := range tests {
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 1 statement. got=%d",
len(program.Statements))
}

stmt := program.Statements[0]
if !testLetStatement(t, stmt, tt.expectedIdentifier) {
return
}

val := stmt.(*ast.LetStatement).Value
if !testLiteralExpression(t, val, tt.expectedValue) {
return
}
}

}
Comment thread
Matthew-Grayson marked this conversation as resolved.

func testLetStatement(t *testing.T, s ast.Statement, name string) bool {
Expand Down