From b0173ccd751f9b43d85f00898e5f9141fe27889e Mon Sep 17 00:00:00 2001 From: Lahoucine EL ADDALI Date: Sat, 27 Apr 2024 01:56:59 +0100 Subject: [PATCH] fix: parse class literal parsing --- internal/parser/data/test_data.go | 2 +- internal/parser/parser.go | 6 +++++- internal/parser/parser_test.go | 33 +++++++++++++++---------------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/internal/parser/data/test_data.go b/internal/parser/data/test_data.go index 4ab0f47..27c1ca7 100644 --- a/internal/parser/data/test_data.go +++ b/internal/parser/data/test_data.go @@ -122,7 +122,7 @@ var ( Arrays = "[1,12 - 8 ,7]" ArrayIndex = "nums[7-4]" - ClassExp = `class helloworld() { + ClassExp = `class helloworld { def var1= 444; def var2=777; diff --git a/internal/parser/parser.go b/internal/parser/parser.go index eed33cd..1b7c01a 100644 --- a/internal/parser/parser.go +++ b/internal/parser/parser.go @@ -59,6 +59,7 @@ func InitParser(l *lexer.Lexer) *Parser { }, p.parsePrefixExpression) p.addPrefixFn(token.IF, p.parseIfExpression) p.addPrefixFn(token.FUNCTION, p.parseFunctionExpression) + p.addPrefixFn(token.CLASS, p.parseClass) p.addPrefixFn(token.FOR, p.parseForLoopExpression) p.addPrefixFn(token.STRING, p.parseStringLit) p.addPrefixFn(token.LB, p.parseArrayLit) @@ -138,7 +139,7 @@ func (p *Parser) parseStmt() ast.Statement { } } -func (p *Parser) parseClass() *ast.ClassLiteral { +func (p *Parser) parseClass() ast.Expression { exp := &ast.ClassLiteral{ Token: p.currToken, // the class key word @@ -161,15 +162,18 @@ func (p *Parser) parseClass() *ast.ClassLiteral { var methods []*ast.FunctionExp for p.peekTokenEquals(token.DEF) { + p.Next() st := p.parseDefStmt() fields = append(fields, st) } if p.peekTokenEquals(token.CONSTRUCTOR) { + p.Next() exp.Constructor = p.parseConstructor(exp.ClassName) } for p.peekTokenEquals(token.FUNCTION) { + p.Next() m := p.parseFunctionExpression() methods = append(methods, m.(*ast.FunctionExp)) } diff --git a/internal/parser/parser_test.go b/internal/parser/parser_test.go index e325bd6..f2782ec 100644 --- a/internal/parser/parser_test.go +++ b/internal/parser/parser_test.go @@ -443,23 +443,22 @@ func TestArrayIndexExp(t *testing.T) { } } -// func TestClassExpression(t *testing.T) { -// input := data.ClassExp -// -// pr, parser := getProg(input) -// -// checkParserErrors(parser, t) -// checkIsProgramStmLengthValid(pr, t, 1) -// -// stm, ok := pr.Statements[0].(*ast.ExpressionStatement) -// -// if !ok { -// t.Fatalf("pr.Statements[0] is not of type *ast.ExpressionStatements instead got %T ", stm) -// } -// -// fmt.Printf("program is %s", pr.ToString()) -// -// } +func TestClassExpression(t *testing.T) { + input := data.ClassExp + + pr, parser := getProg(input) + + checkParserErrors(parser, t) + checkIsProgramStmLengthValid(pr, t, 1) + + stm, ok := pr.Statements[0].(*ast.ExpressionStatement) + + if !ok { + t.Fatalf("pr.Statements[0] is not of type *ast.ExpressionStatements instead got %T ", stm) + } + + fmt.Printf("program is %s", pr.ToString()) +} // Tests helper functions func checkIsProgramStmLengthValid(program *ast.Program, t *testing.T, length int) {