Skip to content

Commit

Permalink
parser variable def
Browse files Browse the repository at this point in the history
  • Loading branch information
Houcine EL ADDALI committed Dec 8, 2023
1 parent c26570a commit eb62daa
Show file tree
Hide file tree
Showing 3 changed files with 163 additions and 3 deletions.
2 changes: 1 addition & 1 deletion AST/ast_imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,4 @@ func (defSt *DefStatement) TokenLiteral() string{
return defSt.Token.Value;
}
// satisfies the statement interface
func (defSt *DefStatement) StatementNode() {}
func (defSt *DefStatement) statementNode() {}
82 changes: 80 additions & 2 deletions parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package parser

import (
"fmt"

ast "github.com/houcine7/JIPL/AST"
"github.com/houcine7/JIPL/lexer"
"github.com/houcine7/JIPL/token"
Expand Down Expand Up @@ -38,7 +40,83 @@ func (p *Parser) NextToken(){
/*
This function is to parse a given program
*/

func (p *Parser) Parse() *ast.Program{
return nil
program := &ast.Program{}
program.Statements = []ast.Statement{};

for p.currToken.Type != token.FILE_ENDED {
stm := p.parseStmt()
//fmt.Println(stm.TokenLiteral())

if stm !=nil {
program.Statements =append(program.Statements, stm )
}
// Advance with token
p.NextToken()
}

return program
}

/*
* a parser function to parse statements
* and returns the parsed statement
*/

func (p *Parser) parseStmt() ast.Statement{
switch p.currToken.Type {
case token.DEF:
return p.parseDefStmt()
default:
return nil
}
}

/*
* function used to parse def statement
*/

func (p *Parser) parseDefStmt() *ast.DefStatement {
stm := &ast.DefStatement{Token: p.currToken}

// syntax error's
if !p.expectedNextToken(token.IDENTIFIER){
return nil
}
fmt.Println("------HERE-----------")
stm.Name = &ast.Identifier{
Token: p.currToken,
Value: p.currToken.Value,
}
if !p.expectedNextToken(token.ASSIGN){
return nil
}

for !p.currentTokenEquals(token.S_COLON){
p.NextToken();
}

return stm

}

func (p *Parser) currentTokenEquals(t token.TokenType) bool{
return p.currToken.Type == t;
}

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

/*
* function checks if the given token is the next token
* if it is returns true and advances the token
* if not returns false
*/
func (p *Parser) expectedNextToken(t token.TokenType) bool{
if p.peekTokenEquals(t){
p.NextToken()
return true
}
return false
}
82 changes: 82 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package parser

import (
"fmt"
"testing"

ast "github.com/houcine7/JIPL/AST"
"github.com/houcine7/JIPL/lexer"
)

func TestDefStatement(t *testing.T) {

input := `
def num1 = 5;
def num2 = 10;
def foobar = 838383;
`
l := lexer.InitLexer(input)
fmt.Println("-----------------------")
fmt.Println("lexer", l)
parser := InitParser(l)
fmt.Println("-----------------------")
fmt.Println("parser",parser)

program := parser.Parse()

fmt.Println(program)

if program==nil{
t.Fatalf("parse returned a nil value")
}

if len(program.Statements) !=3 {
t.Fatalf("the program.Statements doesn't not contain 3 statements, instead we got %d",
len(program.Statements))
}

tests := []struct{
expectedIdentifier string
}{
{"num1"},
{"num2"},
{"foobar"},
}

for i,t1 := range tests{

stm := program.Statements[i]

if !testDefStatement(t,stm,t1.expectedIdentifier){
return
}
}

}

func testDefStatement(t *testing.T, stm ast.Statement, name string) bool {
if stm.TokenLiteral() !="def" {
t.Errorf("s.tokenLiteral is not 'def'. got instead:%q",stm.TokenLiteral())
return false
}

defStm, ok := stm.(*ast.DefStatement)

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

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

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

return true;
}

0 comments on commit eb62daa

Please sign in to comment.