Skip to content

Commit

Permalink
test class tokenization
Browse files Browse the repository at this point in the history
  • Loading branch information
houcine7 committed Apr 27, 2024
1 parent a26cc14 commit d7bf105
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 18 deletions.
12 changes: 6 additions & 6 deletions cmd/REPL/repel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions internal/AST/ast_imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ type IndexExpression struct {
}

type ClassLiteral struct {
Token token.Token
Token token.Token // class token
ClassName *Identifier
Constructor *FunctionExp
DataMembers []*DefStatement
Expand Down Expand Up @@ -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() {}
Expand Down
50 changes: 45 additions & 5 deletions internal/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 = "=+(){},;"
)
28 changes: 23 additions & 5 deletions internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand Down Expand Up @@ -268,15 +268,15 @@ 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],
)
}

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,
)
}
Expand Down Expand Up @@ -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],
)
}
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit d7bf105

Please sign in to comment.