From f81aa97baa72c3d701ddf693e317204ba9a458bf Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Tue, 21 Apr 2026 18:12:34 +0100 Subject: [PATCH] fix: another compilation errors found after testing --- .../compilation-engine/compilation-engine.go | 70 +++++++++++++------ 1 file changed, 48 insertions(+), 22 deletions(-) diff --git a/internal/compilation-engine/compilation-engine.go b/internal/compilation-engine/compilation-engine.go index 11bce2d..b2dcdad 100644 --- a/internal/compilation-engine/compilation-engine.go +++ b/internal/compilation-engine/compilation-engine.go @@ -13,6 +13,7 @@ func compileClassVarDec(output *strings.Builder, tokens []tokenizer.Token, index return nil } + output.WriteString("\n") output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ @@ -51,9 +52,11 @@ func compileClassVarDec(output *strings.Builder, tokens []tokenizer.Token, index return errors.New("Missing semicolon!") } - output.WriteString(" " + tokens[*index].Value + " \n") + output.WriteString(" " + tokens[*index].Value + " \n") (*index)++ + output.WriteString("\n") + return compileClassVarDec(output, tokens, index) } @@ -199,6 +202,8 @@ func compileLetStatement(output *strings.Builder, tokens []tokenizer.Token, inde return errors.New("Invalid let statement!") } + output.WriteString("\n") + output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ @@ -206,11 +211,7 @@ func compileLetStatement(output *strings.Builder, tokens []tokenizer.Token, inde return errors.New("Invalid variable name!") } - output.WriteString("\n") - output.WriteString("\n") output.WriteString(" " + tokens[*index].Value + " \n") - output.WriteString("\n") - output.WriteString("\n") *(index)++ if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "=" { @@ -231,6 +232,8 @@ func compileLetStatement(output *strings.Builder, tokens []tokenizer.Token, inde output.WriteString(" " + tokens[*index].Value + " \n") (*index)++ + output.WriteString("\n") + return nil } @@ -239,6 +242,8 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index return errors.New("Invalid if statement!") } + output.WriteString("\n") + output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ @@ -267,10 +272,14 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ + output.WriteString("\n") + if err := compileStatements(output, tokens, index); err != nil { return err } + output.WriteString("\n") + if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" { return errors.New("Missing if statement closing curly brace!") } @@ -279,7 +288,7 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index *(index)++ if tokens[*index].Type == tokenizer.KEYWORD && tokens[*index].Value == "else" { - output.WriteString(" " + tokens[*index].Value + " \n") + output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "{" { @@ -289,10 +298,14 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ + output.WriteString("\n") + if err := compileStatements(output, tokens, index); err != nil { return err } + output.WriteString("\n") + if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" { return errors.New("Missing if statement closing curly brace!") } @@ -301,6 +314,8 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index *(index)++ } + output.WriteString("\n") + return nil } @@ -309,6 +324,8 @@ func compileWhileStatement(output *strings.Builder, tokens []tokenizer.Token, in return errors.New("Invalid while statement!") } + output.WriteString("\n") + output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ @@ -337,10 +354,14 @@ func compileWhileStatement(output *strings.Builder, tokens []tokenizer.Token, in output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ + output.WriteString("\n") + if err := compileStatements(output, tokens, index); err != nil { return err } + output.WriteString("\n") + if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" { return errors.New("Missing while statement closing curly brace!") } @@ -348,6 +369,8 @@ func compileWhileStatement(output *strings.Builder, tokens []tokenizer.Token, in output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ + output.WriteString("\n") + return nil } @@ -356,6 +379,8 @@ func compileDoStatement(output *strings.Builder, tokens []tokenizer.Token, index return errors.New("Invalid do statement!") } + output.WriteString("\n") + output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ @@ -367,6 +392,7 @@ func compileDoStatement(output *strings.Builder, tokens []tokenizer.Token, index *(index)++ if tokens[*index].Type == tokenizer.SYMBOL && tokens[*index].Value == "." { + output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ if tokens[*index].Type != tokenizer.IDENTIFIER { @@ -384,10 +410,14 @@ func compileDoStatement(output *strings.Builder, tokens []tokenizer.Token, index output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ + output.WriteString("\n") + if err := compileExpressionList(output, tokens, index); err != nil { return err } + output.WriteString("\n") + if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != ")" { return errors.New("Missing subroutine call closing parenthese!") } @@ -402,6 +432,8 @@ func compileDoStatement(output *strings.Builder, tokens []tokenizer.Token, index output.WriteString(" " + tokens[*index].Value + " \n") (*index)++ + output.WriteString("\n") + return nil } @@ -410,6 +442,8 @@ func compileReturnStatement(output *strings.Builder, tokens []tokenizer.Token, i return errors.New("Invalid return statement!") } + output.WriteString("\n") + output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ @@ -426,6 +460,8 @@ func compileReturnStatement(output *strings.Builder, tokens []tokenizer.Token, i output.WriteString(" " + tokens[*index].Value + " \n") (*index)++ + output.WriteString("\n") + return nil } @@ -434,10 +470,6 @@ func compileStatements(output *strings.Builder, tokens []tokenizer.Token, index return nil } - output.WriteString("<") - output.WriteString(tokens[*index].Value) - output.WriteString("Statement>\n") - switch tokens[*index].Value { case "let": if err := compileLetStatement(output, tokens, index); err != nil { @@ -459,12 +491,10 @@ func compileStatements(output *strings.Builder, tokens []tokenizer.Token, index if err := compileReturnStatement(output, tokens, index); err != nil { return err } + default: + return errors.New("Invalid statement!") } - output.WriteString("\n") - return compileStatements(output, tokens, index) } @@ -495,6 +525,8 @@ func compileSubroutineDeclaration(output *strings.Builder, tokens []tokenizer.To return nil } + output.WriteString("\n") + output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ @@ -559,7 +591,8 @@ func compileSubroutineDeclaration(output *strings.Builder, tokens []tokenizer.To output.WriteString(" " + tokens[*index].Value + " \n") *(index)++ - output.WriteString("\n") + output.WriteString("\n") + output.WriteString("\n") return compileSubroutineDeclaration(output, tokens, index) } @@ -590,21 +623,14 @@ func compileClass(output *strings.Builder, tokens []tokenizer.Token) error { output.WriteString(" " + tokens[index].Value + " \n") index++ - output.WriteString("\n") - if err := compileClassVarDec(output, tokens, &index); err != nil { return err } - output.WriteString("\n") - output.WriteString("\n") - if err := compileSubroutineDeclaration(output, tokens, &index); err != nil { return err } - output.WriteString("\n") - if tokens[index].Type != tokenizer.SYMBOL || tokens[index].Value != "}" { return errors.New("Missing class closing curly brace!") }