diff --git a/cmd/REPL/repel.go b/cmd/REPL/repel.go index 5b62acf..df7e9c1 100644 --- a/cmd/REPL/repel.go +++ b/cmd/REPL/repel.go @@ -20,14 +20,14 @@ import ( * Function to start the repl */ -const PROMPT = "🟢>_" +const PROMPT = ">_" var ctx = types.NewContext() const ( - enableCpuPr = true - enableMemPr = true - isDebugging = false + enableCpuProfiling = true + enableMemProfiling = true + isDebugging = false ) func Start(in io.Reader, out io.Writer) { @@ -43,7 +43,7 @@ func Start(in io.Reader, out io.Writer) { fmt.Println("------------- Welcome to JIPL: you can begin coding now ------------") fmt.Println(" 👋 ") - if enableCpuPr { + if enableCpuProfiling { f, err := os.Create("cpu.pprof") if err != nil { @@ -104,7 +104,7 @@ func Start(in io.Reader, out io.Writer) { } // take memory snapshot - if enableMemPr { + if enableMemProfiling { f, err := os.Create("mem.pprod") if err != nil { diff --git a/internal/AST/ast_imp.go b/internal/AST/ast_imp.go index b04d4ee..af027f8 100644 --- a/internal/AST/ast_imp.go +++ b/internal/AST/ast_imp.go @@ -120,7 +120,7 @@ type IndexExpression struct { } type ClassLiteral struct { - Token token.Token + Token token.Token // class token ClassName *Identifier Constructor *FunctionExp DataMembers []*DefStatement @@ -408,7 +408,7 @@ func (class *ClassLiteral) ToString() string { return "TODO: IMPLEMENT TO STRING FOR THE CLASS NODE" } -// expression implementaions +// expression implementations func (postfixExp *PostfixExpression) expressionNode() {} func (forExp *ForLoopExpression) expressionNode() {} func (infixExp *InfixExpression) expressionNode() {} diff --git a/internal/lexer/lexer_test.go b/internal/lexer/lexer_test.go index b2b07f2..8db33d1 100644 --- a/internal/lexer/lexer_test.go +++ b/internal/lexer/lexer_test.go @@ -41,8 +41,8 @@ func TestNext2(t *testing.T) { calculatedToken := myLexer.NextToken() // test the token type if et.expectedTokenType != calculatedToken.Type { - log.Fatalf("tests index %d -> tokenType wrong, expected:[%d] and got:[%d]", - i, et.expectedTokenType, calculatedToken.Type) + log.Fatalf("tests index %d -> token value is wrong, expected:[%q] and got:[%q] %s:%s", + i, et.expectedValue, calculatedToken.Value, et.expectedValue, calculatedToken.Value) } // test the token literal value @@ -71,8 +71,8 @@ func TestNext3(t *testing.T) { // test the token literal value if et.expectedValue != calculatedToken.Value { - log.Fatalf("tests index %d -> token value is wrong, expected:[%q] and got:[%q]", - i, et.expectedValue, calculatedToken.Value) + log.Fatalf("tests index %d -> token value is wrong, expected:[%q] and got:[%q] %s:%s", + i, et.expectedValue, calculatedToken.Value, et.expectedValue, calculatedToken.Value) } } } @@ -245,6 +245,33 @@ var ( {expectedTokenType: token.IDENTIFIER, expectedValue: "val2"}, {expectedTokenType: token.RP, expectedValue: ")"}, {expectedTokenType: token.S_COLON, expectedValue: ";"}, + + {expectedTokenType: token.CLASS, expectedValue: "class"}, + {expectedTokenType: token.IDENTIFIER, expectedValue: "helloworld"}, + {expectedTokenType: token.LCB, expectedValue: "{"}, + {expectedTokenType: token.DEF, expectedValue: "def"}, + {expectedTokenType: token.IDENTIFIER, expectedValue: "var1"}, + {expectedTokenType: token.ASSIGN, expectedValue: "="}, + {expectedTokenType: token.INT, expectedValue: "444"}, + {expectedTokenType: token.S_COLON, expectedValue: ";"}, + {expectedTokenType: token.CONSTRUCTOR, expectedValue: "constructor"}, + {expectedTokenType: token.LP, expectedValue: "("}, + {expectedTokenType: token.RP, expectedValue: ")"}, + {expectedTokenType: token.LCB, expectedValue: "{"}, + {expectedTokenType: token.RCB, expectedValue: "}"}, + {expectedTokenType: token.FUNCTION, expectedValue: "function"}, + {expectedTokenType: token.IDENTIFIER, expectedValue: "toString"}, + {expectedTokenType: token.LP, expectedValue: "("}, + {expectedTokenType: token.RP, expectedValue: ")"}, + {expectedTokenType: token.LCB, expectedValue: "{"}, + {expectedTokenType: token.IDENTIFIER, expectedValue: "out"}, + {expectedTokenType: token.LP, expectedValue: "("}, + {expectedTokenType: token.STRING, expectedValue: "hello world"}, + {expectedTokenType: token.RP, expectedValue: ")"}, + {expectedTokenType: token.S_COLON, expectedValue: ";"}, + {expectedTokenType: token.RCB, expectedValue: "}"}, + + {expectedTokenType: token.RCB, expectedValue: "}"}, } Mock2 = `def val1 = 30; @@ -278,7 +305,20 @@ var ( def add = function(x, y) { x + y; }; - def result = add(val1, val2);` + def result = add(val1, val2); + class helloworld { + + def var1=444; + + constructor() { + + } + + function toString() { + out("hello world"); + } + + }` Mock0 = "=+(){},;" ) diff --git a/internal/parser/parser_test.go b/internal/parser/parser_test.go index b5a861c..e325bd6 100644 --- a/internal/parser/parser_test.go +++ b/internal/parser/parser_test.go @@ -175,7 +175,7 @@ func TestInfixExpression(t *testing.T) { exp, ok := stm.Expression.(*ast.InfixExpression) if !ok { - t.Fatalf("stm.Expression type is not as expected insetead got= %T", stm.Expression) + t.Fatalf("stm.Expression type is not as expected instead got= %T", stm.Expression) } if !testLiteralExpression(t, exp.Left, test.Left) || @@ -268,7 +268,7 @@ func TestForLoopFunctions(t *testing.T) { stm, ok := pr.Statements[0].(*ast.ExpressionStatement) if !ok { - t.Fatalf("pr.Statments[0] type is not *ast.ExpressionStatement instead got %T", + t.Fatalf("pr.Statements[0] type is not *ast.ExpressionStatement instead got %T", pr.Statements[0], ) } @@ -276,7 +276,7 @@ func TestForLoopFunctions(t *testing.T) { exp, ok := stm.Expression.(*ast.ForLoopExpression) if !ok { - t.Fatalf("*ast.FoorLoopExpression type is not *ast.ForLoopExpression instead got %T", + t.Fatalf("*ast.ForLoopExpression type is not *ast.ForLoopExpression instead got %T", stm.Expression, ) } @@ -314,7 +314,7 @@ func TestParseFunctions(t *testing.T) { stm, ok := pr.Statements[0].(*ast.ExpressionStatement) if !ok { - t.Fatalf("pr.Statments[0] type is not *ast.ExpressionStatement instead got %T", + t.Fatalf("pr.Statements[0] type is not *ast.ExpressionStatement instead got %T", pr.Statements[0], ) } @@ -380,7 +380,7 @@ func TestAssignExpr(t *testing.T) { assignExp, ok := stm.Expression.(*ast.AssignmentExpression) if !ok { - t.Fatalf("the stm.Expression is not not of type *ast.AssignementExpression. instead got %T", + t.Fatalf("the stm.Expression is not not of type *ast.AssignmentExpression. instead got %T", stm.Expression) } @@ -443,6 +443,24 @@ 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()) +// +// } + // Tests helper functions func checkIsProgramStmLengthValid(program *ast.Program, t *testing.T, length int) { if len(program.Statements) != length {