mirror of
https://github.com/hazemKrimi/jack-compiler.git
synced 2026-05-01 17:48:57 +00:00
fix: regression with second test compilation
This commit is contained in:
@@ -315,7 +315,7 @@ func compileSubroutineCall(output *strings.Builder, tokens []tokenizer.Token, in
|
|||||||
subroutine = tokens[*index].Value
|
subroutine = tokens[*index].Value
|
||||||
function = class + "." + subroutine
|
function = class + "." + subroutine
|
||||||
|
|
||||||
// This assumes that the called subroutine is a method for now.
|
// Assumes that the called subroutine of a class instance is a method.
|
||||||
if found {
|
if found {
|
||||||
code.WritePush(output, segmentFromVariableKind(variable.Kind), variable.Count)
|
code.WritePush(output, segmentFromVariableKind(variable.Kind), variable.Count)
|
||||||
instanceCallingMethod = true
|
instanceCallingMethod = true
|
||||||
@@ -412,6 +412,7 @@ 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 {
|
||||||
|
endLabel := "IF_END_" + fmt.Sprint(ifLabelIndex)
|
||||||
elseLabel := "IF_ELSE_" + fmt.Sprint(ifLabelIndex)
|
elseLabel := "IF_ELSE_" + fmt.Sprint(ifLabelIndex)
|
||||||
|
|
||||||
ifLabelIndex++
|
ifLabelIndex++
|
||||||
@@ -459,9 +460,8 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
|
|
||||||
if tokens[*index].Value != "else" {
|
if tokens[*index].Value != "else" {
|
||||||
code.WriteLabel(output, elseLabel)
|
code.WriteLabel(output, elseLabel)
|
||||||
}
|
code.WriteGoto(output, endLabel)
|
||||||
|
} else if tokens[*index].Type == tokenizer.KEYWORD && tokens[*index].Value == "else" {
|
||||||
if tokens[*index].Type == tokenizer.KEYWORD && tokens[*index].Value == "else" {
|
|
||||||
(*index)++
|
(*index)++
|
||||||
|
|
||||||
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "{" {
|
if tokens[*index].Type != tokenizer.SYMBOL || tokens[*index].Value != "{" {
|
||||||
@@ -470,6 +470,7 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
|
|
||||||
(*index)++
|
(*index)++
|
||||||
|
|
||||||
|
code.WriteGoto(output, endLabel)
|
||||||
code.WriteLabel(output, elseLabel)
|
code.WriteLabel(output, elseLabel)
|
||||||
|
|
||||||
if err := compileStatements(output, tokens, index); err != nil {
|
if err := compileStatements(output, tokens, index); err != nil {
|
||||||
@@ -483,6 +484,8 @@ func compileIfStatement(output *strings.Builder, tokens []tokenizer.Token, index
|
|||||||
(*index)++
|
(*index)++
|
||||||
}
|
}
|
||||||
|
|
||||||
|
code.WriteLabel(output, endLabel)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user