Skip to content

Commit

Permalink
fix: parse class literal parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
houcine7 committed Apr 27, 2024
1 parent d7bf105 commit b0173cc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
2 changes: 1 addition & 1 deletion internal/parser/data/test_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 5 additions & 1 deletion internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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))
}
Expand Down
33 changes: 16 additions & 17 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit b0173cc

Please sign in to comment.