-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7f6a7b
commit 30f8c31
Showing
3 changed files
with
201 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |