mirror of
https://github.com/hazemKrimi/jack-compiler.git
synced 2026-05-01 17:48:57 +00:00
fix: another compilation errors found after testing
This commit is contained in:
@@ -13,6 +13,7 @@ func compileClassVarDec(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("<classVarDec>\n")
|
||||||
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
@@ -51,9 +52,11 @@ func compileClassVarDec(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
return errors.New("Missing semicolon!")
|
return errors.New("Missing semicolon!")
|
||||||
}
|
}
|
||||||
|
|
||||||
output.WriteString("<identifier> " + tokens[*index].Value + " </identifier>\n")
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
(*index)++
|
(*index)++
|
||||||
|
|
||||||
|
output.WriteString("</classVarDec>\n")
|
||||||
|
|
||||||
return compileClassVarDec(output, tokens, index)
|
return compileClassVarDec(output, tokens, index)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,6 +202,8 @@ func compileLetStatement(output *strings.Builder, tokens []tokenizer.Token, inde
|
|||||||
return errors.New("Invalid let statement!")
|
return errors.New("Invalid let statement!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("<letStatement>\n")
|
||||||
|
|
||||||
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
@@ -206,11 +211,7 @@ func compileLetStatement(output *strings.Builder, tokens []tokenizer.Token, inde
|
|||||||
return errors.New("Invalid variable name!")
|
return errors.New("Invalid variable name!")
|
||||||
}
|
}
|
||||||
|
|
||||||
output.WriteString("<expression>\n")
|
|
||||||
output.WriteString("<term>\n")
|
|
||||||
output.WriteString("<identifier> " + tokens[*index].Value + " </identifier>\n")
|
output.WriteString("<identifier> " + tokens[*index].Value + " </identifier>\n")
|
||||||
output.WriteString("</term>\n")
|
|
||||||
output.WriteString("</expression>\n")
|
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "=" {
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "=" {
|
||||||
@@ -231,6 +232,8 @@ func compileLetStatement(output *strings.Builder, tokens []tokenizer.Token, inde
|
|||||||
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
(*index)++
|
(*index)++
|
||||||
|
|
||||||
|
output.WriteString("</letStatement>\n")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,6 +242,8 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
return errors.New("Invalid if statement!")
|
return errors.New("Invalid if statement!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("<ifStatement>\n")
|
||||||
|
|
||||||
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
@@ -267,10 +272,14 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
|
output.WriteString("<statements>\n")
|
||||||
|
|
||||||
if err := compileStatements(output, tokens, index); err != nil {
|
if err := compileStatements(output, tokens, index); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("</statements>\n")
|
||||||
|
|
||||||
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" {
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" {
|
||||||
return errors.New("Missing if statement closing curly brace!")
|
return errors.New("Missing if statement closing curly brace!")
|
||||||
}
|
}
|
||||||
@@ -279,7 +288,7 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
if tokens[*index].Type == tokenizer.KEYWORD && tokens[*index].Value == "else" {
|
if tokens[*index].Type == tokenizer.KEYWORD && tokens[*index].Value == "else" {
|
||||||
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "{" {
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "{" {
|
||||||
@@ -289,10 +298,14 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
|
output.WriteString("<statements>\n")
|
||||||
|
|
||||||
if err := compileStatements(output, tokens, index); err != nil {
|
if err := compileStatements(output, tokens, index); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("</statements>\n")
|
||||||
|
|
||||||
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" {
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" {
|
||||||
return errors.New("Missing if statement closing curly brace!")
|
return errors.New("Missing if statement closing curly brace!")
|
||||||
}
|
}
|
||||||
@@ -301,6 +314,8 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
*(index)++
|
*(index)++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("</ifStatement>\n")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -309,6 +324,8 @@ func compileWhileStatement(output *strings.Builder, tokens []tokenizer.Token, in
|
|||||||
return errors.New("Invalid while statement!")
|
return errors.New("Invalid while statement!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("<whileStatement>\n")
|
||||||
|
|
||||||
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
@@ -337,10 +354,14 @@ func compileWhileStatement(output *strings.Builder, tokens []tokenizer.Token, in
|
|||||||
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
|
output.WriteString("<statements>\n")
|
||||||
|
|
||||||
if err := compileStatements(output, tokens, index); err != nil {
|
if err := compileStatements(output, tokens, index); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("</statements>\n")
|
||||||
|
|
||||||
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" {
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" {
|
||||||
return errors.New("Missing while statement closing curly brace!")
|
return errors.New("Missing while statement closing curly brace!")
|
||||||
}
|
}
|
||||||
@@ -348,6 +369,8 @@ func compileWhileStatement(output *strings.Builder, tokens []tokenizer.Token, in
|
|||||||
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
|
output.WriteString("</whileStatement>\n")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -356,6 +379,8 @@ func compileDoStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
return errors.New("Invalid do statement!")
|
return errors.New("Invalid do statement!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("<doStatement>\n")
|
||||||
|
|
||||||
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
@@ -367,6 +392,7 @@ func compileDoStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
if tokens[*index].Type == tokenizer.SYMBOL && tokens[*index].Value == "." {
|
if tokens[*index].Type == tokenizer.SYMBOL && tokens[*index].Value == "." {
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
if tokens[*index].Type != tokenizer.IDENTIFIER {
|
if tokens[*index].Type != tokenizer.IDENTIFIER {
|
||||||
@@ -384,10 +410,14 @@ func compileDoStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
|
output.WriteString("<expressionList>\n")
|
||||||
|
|
||||||
if err := compileExpressionList(output, tokens, index); err != nil {
|
if err := compileExpressionList(output, tokens, index); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("</expressionList>\n")
|
||||||
|
|
||||||
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != ")" {
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != ")" {
|
||||||
return errors.New("Missing subroutine call closing parenthese!")
|
return errors.New("Missing subroutine call closing parenthese!")
|
||||||
}
|
}
|
||||||
@@ -402,6 +432,8 @@ func compileDoStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
(*index)++
|
(*index)++
|
||||||
|
|
||||||
|
output.WriteString("</doStatement>\n")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -410,6 +442,8 @@ func compileReturnStatement(output *strings.Builder, tokens []tokenizer.Token, i
|
|||||||
return errors.New("Invalid return statement!")
|
return errors.New("Invalid return statement!")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("<returnStatement>\n")
|
||||||
|
|
||||||
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
@@ -426,6 +460,8 @@ func compileReturnStatement(output *strings.Builder, tokens []tokenizer.Token, i
|
|||||||
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
(*index)++
|
(*index)++
|
||||||
|
|
||||||
|
output.WriteString("</returnStatement>\n")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -434,10 +470,6 @@ func compileStatements(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
output.WriteString("<")
|
|
||||||
output.WriteString(tokens[*index].Value)
|
|
||||||
output.WriteString("Statement>\n")
|
|
||||||
|
|
||||||
switch tokens[*index].Value {
|
switch tokens[*index].Value {
|
||||||
case "let":
|
case "let":
|
||||||
if err := compileLetStatement(output, tokens, index); err != nil {
|
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 {
|
if err := compileReturnStatement(output, tokens, index); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
return errors.New("Invalid statement!")
|
||||||
}
|
}
|
||||||
|
|
||||||
output.WriteString("</")
|
|
||||||
output.WriteString(tokens[*index].Value)
|
|
||||||
output.WriteString("Statement>\n")
|
|
||||||
|
|
||||||
return compileStatements(output, tokens, index)
|
return compileStatements(output, tokens, index)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -495,6 +525,8 @@ func compileSubroutineDeclaration(output *strings.Builder, tokens []tokenizer.To
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
output.WriteString("<subroutineDec>\n")
|
||||||
|
|
||||||
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
@@ -559,7 +591,8 @@ func compileSubroutineDeclaration(output *strings.Builder, tokens []tokenizer.To
|
|||||||
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
*(index)++
|
*(index)++
|
||||||
|
|
||||||
output.WriteString("<subroutineBody>\n")
|
output.WriteString("</subroutineBody>\n")
|
||||||
|
output.WriteString("</subroutineDec>\n")
|
||||||
|
|
||||||
return compileSubroutineDeclaration(output, tokens, index)
|
return compileSubroutineDeclaration(output, tokens, index)
|
||||||
}
|
}
|
||||||
@@ -590,21 +623,14 @@ func compileClass(output *strings.Builder, tokens []tokenizer.Token) error {
|
|||||||
output.WriteString("<symbol> " + tokens[index].Value + " </symbol>\n")
|
output.WriteString("<symbol> " + tokens[index].Value + " </symbol>\n")
|
||||||
index++
|
index++
|
||||||
|
|
||||||
output.WriteString("<classVarDec>\n")
|
|
||||||
|
|
||||||
if err := compileClassVarDec(output, tokens, &index); err != nil {
|
if err := compileClassVarDec(output, tokens, &index); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
output.WriteString("</classVarDec>\n")
|
|
||||||
output.WriteString("<subroutineDec>\n")
|
|
||||||
|
|
||||||
if err := compileSubroutineDeclaration(output, tokens, &index); err != nil {
|
if err := compileSubroutineDeclaration(output, tokens, &index); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
output.WriteString("</subroutineVarDec>\n")
|
|
||||||
|
|
||||||
if tokens[index].Type != tokenizer.SYMBOL || tokens[index].Value != "}" {
|
if tokens[index].Type != tokenizer.SYMBOL || tokens[index].Value != "}" {
|
||||||
return errors.New("Missing class closing curly brace!")
|
return errors.New("Missing class closing curly brace!")
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user