mirror of
https://github.com/hazemKrimi/jack-compiler.git
synced 2026-05-01 17:48:57 +00:00
chore: finish compilation engine scaffolding with some fixes
This commit is contained in:
@@ -51,12 +51,15 @@ 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")
|
||||||
|
(*index)++
|
||||||
|
|
||||||
return compileClassVarDec(output, tokens, index)
|
return compileClassVarDec(output, tokens, index)
|
||||||
}
|
}
|
||||||
|
|
||||||
func compileParameterList(output *strings.Builder, tokens []tokenizer.Token, index *int) error {
|
func compileParameterList(output *strings.Builder, tokens []tokenizer.Token, index *int) error {
|
||||||
if !slices.Contains([]tokenizer.TokenType{tokenizer.KEYWORD, tokenizer.IDENTIFIER}, tokens[*index].Type) || !slices.Contains([]string{"int", "char", "boolean"}, tokens[*index].Value) {
|
if !slices.Contains([]tokenizer.TokenType{tokenizer.KEYWORD, tokenizer.IDENTIFIER}, tokens[*index].Type) || !slices.Contains([]string{"int", "char", "boolean"}, tokens[*index].Value) {
|
||||||
return errors.New("Invalid variable type name!")
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if tokens[*index].Type == tokenizer.KEYWORD {
|
if tokens[*index].Type == tokenizer.KEYWORD {
|
||||||
@@ -142,7 +145,11 @@ 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 != "=" {
|
||||||
@@ -171,16 +178,141 @@ func compileLetStatement(output *strings.Builder, tokens []tokenizer.Token, inde
|
|||||||
}
|
}
|
||||||
|
|
||||||
func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index *int) error {
|
func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index *int) error {
|
||||||
|
if tokens[*index].Type != tokenizer.KEYWORD || tokens[*index].Value != "if" {
|
||||||
|
return errors.New("Invalid if statement!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "(" {
|
||||||
|
return errors.New("Missing if statement opening parenthese!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
// TODO: Change to expression compilation
|
||||||
|
if tokens[*index].Type != tokenizer.IDENTIFIER {
|
||||||
|
return errors.New("Invalid variable name!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<expression>\n")
|
||||||
|
output.WriteString("<term>\n")
|
||||||
|
output.WriteString("<identifier> " + tokens[*index].Value + " </identifier>\n")
|
||||||
|
output.WriteString("</term>\n")
|
||||||
|
output.WriteString("</expression>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != ")" {
|
||||||
|
return errors.New("Missing if statement closing parenthese!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "{" {
|
||||||
|
return errors.New("Missing if statement opening curly brace!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if err := compileStatements(output, tokens, index); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" {
|
||||||
|
return errors.New("Missing if statement closing curly brace!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if tokens[*index].Type == tokenizer.KEYWORD || tokens[*index].Value == "else" {
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "{" {
|
||||||
|
return errors.New("Missing if statement opening curly brace!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if err := compileStatements(output, tokens, index); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" {
|
||||||
|
return errors.New("Missing if statement closing curly brace!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func compileWhileStatement(output *strings.Builder, tokens []tokenizer.Token, index *int) error {
|
func compileWhileStatement(output *strings.Builder, tokens []tokenizer.Token, index *int) error {
|
||||||
|
if tokens[*index].Type != tokenizer.KEYWORD || tokens[*index].Value != "while" {
|
||||||
|
return errors.New("Invalid while statement!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "(" {
|
||||||
|
return errors.New("Missing while statement opening parenthese!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
// TODO: Change to expression compilation
|
||||||
|
if tokens[*index].Type != tokenizer.IDENTIFIER {
|
||||||
|
return errors.New("Invalid variable name!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<expression>\n")
|
||||||
|
output.WriteString("<term>\n")
|
||||||
|
output.WriteString("<identifier> " + tokens[*index].Value + " </identifier>\n")
|
||||||
|
output.WriteString("</term>\n")
|
||||||
|
output.WriteString("</expression>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != ")" {
|
||||||
|
return errors.New("Missing while statement closing parenthese!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "{" {
|
||||||
|
return errors.New("Missing while statement opening curly brace!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
|
if err := compileStatements(output, tokens, index); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "}" {
|
||||||
|
return errors.New("Missing while statement closing curly brace!")
|
||||||
|
}
|
||||||
|
|
||||||
|
output.WriteString("<symbol> " + tokens[*index].Value + " </symbol>\n")
|
||||||
|
*(index)++
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func compileDoStatement(output *strings.Builder, tokens []tokenizer.Token, index *int) error {
|
func compileDoStatement(output *strings.Builder, tokens []tokenizer.Token, index *int) error {
|
||||||
if tokens[*index].Type != tokenizer.KEYWORD || tokens[*index].Value != "do" {
|
if tokens[*index].Type != tokenizer.KEYWORD || tokens[*index].Value != "do" {
|
||||||
return errors.New("Invalid return statement!")
|
return errors.New("Invalid do statement!")
|
||||||
}
|
}
|
||||||
|
|
||||||
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
output.WriteString("<keyword> " + tokens[*index].Value + " </keyword>\n")
|
||||||
@@ -199,7 +331,11 @@ func compileReturnStatement(output *strings.Builder, tokens []tokenizer.Token, i
|
|||||||
|
|
||||||
// TODO: Change to expression compilation
|
// TODO: Change to expression compilation
|
||||||
if tokens[*index].Type == tokenizer.IDENTIFIER {
|
if tokens[*index].Type == tokenizer.IDENTIFIER {
|
||||||
|
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)++
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -406,37 +542,37 @@ func ParseTokens(tokens []tokenizer.Token) (string, error) {
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
output.WriteString("<tokens>\n")
|
// output.WriteString("<tokens>\n")
|
||||||
|
|
||||||
for _, token := range tokens {
|
// for _, token := range tokens {
|
||||||
switch token.Type {
|
// switch token.Type {
|
||||||
case tokenizer.SYMBOL:
|
// case tokenizer.SYMBOL:
|
||||||
var value string
|
// var value string
|
||||||
|
//
|
||||||
|
// switch token.Value {
|
||||||
|
// case "<":
|
||||||
|
// value = "<"
|
||||||
|
// case ">":
|
||||||
|
// value = ">"
|
||||||
|
// case "&":
|
||||||
|
// value = "&"
|
||||||
|
// default:
|
||||||
|
// value = token.Value
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// output.WriteString("<symbol> " + value + " </symbol>\n")
|
||||||
|
// case tokenizer.KEYWORD:
|
||||||
|
// output.WriteString("<keyword> " + token.Value + " </keyword>\n")
|
||||||
|
// case tokenizer.IDENTIFIER:
|
||||||
|
// output.WriteString("<identifier> " + token.Value + " </identifier>\n")
|
||||||
|
// case tokenizer.INT_CONST:
|
||||||
|
// output.WriteString("<integerConstant> " + token.Value + " </integerConstant>\n")
|
||||||
|
// case tokenizer.STR_CONST:
|
||||||
|
// output.WriteString("<stringConstant> " + token.Value + " </stringConstant>\n")
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
switch token.Value {
|
// output.WriteString("</tokens>\n")
|
||||||
case "<":
|
|
||||||
value = "<"
|
|
||||||
case ">":
|
|
||||||
value = ">"
|
|
||||||
case "&":
|
|
||||||
value = "&"
|
|
||||||
default:
|
|
||||||
value = token.Value
|
|
||||||
}
|
|
||||||
|
|
||||||
output.WriteString("<symbol> " + value + " </symbol>\n")
|
|
||||||
case tokenizer.KEYWORD:
|
|
||||||
output.WriteString("<keyword> " + token.Value + " </keyword>\n")
|
|
||||||
case tokenizer.IDENTIFIER:
|
|
||||||
output.WriteString("<identifier> " + token.Value + " </identifier>\n")
|
|
||||||
case tokenizer.INT_CONST:
|
|
||||||
output.WriteString("<integerConstant> " + token.Value + " </integerConstant>\n")
|
|
||||||
case tokenizer.STR_CONST:
|
|
||||||
output.WriteString("<stringConstant> " + token.Value + " </stringConstant>\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
output.WriteString("</tokens>\n")
|
|
||||||
|
|
||||||
return output.String(), nil
|
return output.String(), nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user