Skip to content

Commit

Permalink
parsing return statement!!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonak-Adipta-Kalita committed Apr 7, 2023
1 parent 9bb9777 commit 677ac0c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 12 deletions.
11 changes: 11 additions & 0 deletions ast/returnStatement.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ast

import "github.com/Jonak-Adipta-Kalita/JAK-Programming-Language/token"

type ReturnStatement struct {
Token token.Token
ReturnValue Expression
}

func (rs *ReturnStatement) statementNode() {}
func (rs *ReturnStatement) TokenLiteral() string { return rs.Token.Literal }
15 changes: 15 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ func (p *Parser) parseStatement() ast.Statement {
switch p.curToken.Type {
case token.VAR:
return p.parseVarStatement()
case token.RETURN:
return p.parseReturnStatement()
default:
return nil
}
Expand Down Expand Up @@ -82,6 +84,8 @@ func (p *Parser) peekError(t token.TokenType) {
p.errors = append(p.errors, msg)
}

// Parsing Statements

func (p *Parser) parseVarStatement() *ast.VarStatement {
stmt := &ast.VarStatement{Token: p.curToken}
if !p.expectPeek(token.IDENTIFIER) {
Expand All @@ -100,3 +104,14 @@ func (p *Parser) parseVarStatement() *ast.VarStatement {

return stmt
}

func (p *Parser) parseReturnStatement() *ast.ReturnStatement {
stmt := &ast.ReturnStatement{Token: p.curToken}
p.nextToken()

for !p.curTokenIs(token.SEMICOLON) {
p.nextToken()
}

return stmt
}
54 changes: 42 additions & 12 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,26 @@ import (
"github.com/Jonak-Adipta-Kalita/JAK-Programming-Language/ast"
"github.com/Jonak-Adipta-Kalita/JAK-Programming-Language/lexer"
)

func checkParserErrors(t *testing.T, p *Parser) {
errors := p.Errors()
if len(errors) == 0 {
return
}

t.Errorf("parser has %d errors", len(errors))
for _, msg := range errors {
t.Errorf("parser error: %q", msg)
}

t.FailNow()
}

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

l := lexer.New(input)
Expand Down Expand Up @@ -70,16 +85,31 @@ func testVarStatement(t *testing.T, s ast.Statement, name string) bool {
return true
}

func checkParserErrors(t *testing.T, p *Parser) {
errors := p.Errors()
if len(errors) == 0 {
return
}
func TestReturnStatements(t *testing.T) {
input := `return 5;`

t.Errorf("parser has %d errors", len(errors))
for _, msg := range errors {
t.Errorf("parser error: %q", msg)
l := lexer.New(input)
p := New(l)
program := p.ParseProgram()

checkParserErrors(t, p)

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

t.FailNow()
for _, stmt := range program.Statements {
returnStmt, ok := stmt.(*ast.ReturnStatement)
if !ok {
t.Errorf("stmt not *ast.returnStatement. got=%T", stmt)
continue
}

if returnStmt.TokenLiteral() != "return" {
t.Errorf("returnStmt.TokenLiteral not 'return', got %q",
returnStmt.TokenLiteral())
}
}
}

0 comments on commit 677ac0c

Please sign in to comment.