Skip to content

Commit

Permalink
parsing var statements!!
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonak-Adipta-Kalita committed Apr 7, 2023
1 parent c7f6a7b commit 30f8c31
Show file tree
Hide file tree
Showing 3 changed files with 201 additions and 0 deletions.
46 changes: 46 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ast

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

type Node interface {
TokenLiteral() string
}
type Statement interface {
Node
statementNode()
}
type Expression interface {
Node
expressionNode()
}

type Program struct {
Statements []Statement
}

func (p *Program) TokenLiteral() string {
if len(p.Statements) > 0 {
return p.Statements[0].TokenLiteral()
} else {
return ""
}
}

type VarStatement struct {
Token token.Token
Name *Identifier
Value Expression
}

func (ls *VarStatement) statementNode() {}

func (ls *VarStatement) TokenLiteral() string { return ls.Token.Literal }

type Identifier struct {
Token token.Token
Value string
}

func (i *Identifier) expressionNode() {}

func (i *Identifier) TokenLiteral() string { return i.Token.Literal }
85 changes: 85 additions & 0 deletions parser/parser.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package parser

import (
"github.com/Jonak-Adipta-Kalita/JAK-Programming-Language/ast"
"github.com/Jonak-Adipta-Kalita/JAK-Programming-Language/lexer"
"github.com/Jonak-Adipta-Kalita/JAK-Programming-Language/token"
)

type Parser struct {
l *lexer.Lexer
curToken token.Token
peekToken token.Token
}

func New(l *lexer.Lexer) *Parser {
p := &Parser{l: l}
p.nextToken()
p.nextToken()
return p
}

func (p *Parser) nextToken() {
p.curToken = p.peekToken
p.peekToken = p.l.NextToken()
}

func (p *Parser) ParseProgram() *ast.Program {
program := &ast.Program{}
program.Statements = []ast.Statement{}

for p.curToken.Type != token.EOF {
stmt := p.parseStatement()
if stmt != nil {
program.Statements = append(program.Statements, stmt)
}
p.nextToken()
}

return program
}

func (p *Parser) parseStatement() ast.Statement {
switch p.curToken.Type {
case token.VAR:
return p.parseVarStatement()
default:
return nil
}
}

func (p *Parser) parseVarStatement() *ast.VarStatement {
stmt := &ast.VarStatement{Token: p.curToken}
if !p.expectPeek(token.IDENTIFIER) {
return nil
}

stmt.Name = &ast.Identifier{Token: p.curToken, Value: p.curToken.Literal}

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

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

return stmt
}

func (p *Parser) curTokenIs(t token.TokenType) bool {
return p.curToken.Type == t
}

func (p *Parser) peekTokenIs(t token.TokenType) bool {
return p.peekToken.Type == t
}

func (p *Parser) expectPeek(t token.TokenType) bool {
if p.peekTokenIs(t) {
p.nextToken()
return true
} else {
return false
}
}
70 changes: 70 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package parser

import (
"testing"

"github.com/Jonak-Adipta-Kalita/JAK-Programming-Language/ast"
"github.com/Jonak-Adipta-Kalita/JAK-Programming-Language/lexer"
)
func TestVarStatements(t *testing.T) {
input := `
var x = 5;
var y = 10;
var foobar = 838383;
`

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

program := p.ParseProgram()

if program == nil {
t.Fatalf("ParseProgram() returned nil")
}

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

tests := []struct {
expectedIdentifier string
}{
{"x"},
{"y"},
{"foobar"},
}

for i, tt := range tests {
stmt := program.Statements[i]
if !testVarStatement(t, stmt, tt.expectedIdentifier) {
return
}
}
}

func testVarStatement(t *testing.T, s ast.Statement, name string) bool {
if s.TokenLiteral() != "var" {
t.Errorf("s.TokenLiteral not 'var'. got=%q", s.TokenLiteral())
return false
}

varStmt, ok := s.(*ast.VarStatement)

if !ok {
t.Errorf("s not *ast.VarStatement. got=%T", s)
return false
}

if varStmt.Name.Value != name {
t.Errorf("varStmt.Name.Value not '%s'. got=%s", name, varStmt.Name.Value)
return false
}

if varStmt.Name.TokenLiteral() != name {
t.Errorf("s.Name not '%s'. got=%s", name, varStmt.Name)
return false
}

return true
}

0 comments on commit 30f8c31

Please sign in to comment.