Reordered counters

This commit is contained in:
Eduard Urbach 2024-07-16 11:44:10 +02:00
parent 24d3e8f2be
commit 448af0707a
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 11 additions and 8 deletions

View File

@ -11,8 +11,8 @@ import (
func (f *Function) CompileCondition(condition *expression.Expression, successLabel string, failLabel string) error {
switch condition.Token.Text() {
case "||":
leftFailLabel := fmt.Sprintf("%s_false_%d", f.Name, f.count.subBranch)
f.count.subBranch++
leftFailLabel := fmt.Sprintf("%s_false_%d", f.Name, f.count.subBranch)
// Left
left := condition.Children[0]
@ -38,8 +38,8 @@ func (f *Function) CompileCondition(condition *expression.Expression, successLab
return err
case "&&":
leftSuccessLabel := fmt.Sprintf("%s_true_%d", f.Name, f.count.subBranch)
f.count.subBranch++
leftSuccessLabel := fmt.Sprintf("%s_true_%d", f.Name, f.count.subBranch)
// Left
left := condition.Children[0]

View File

@ -8,6 +8,7 @@ import (
// CompileIf compiles a branch instruction.
func (f *Function) CompileIf(branch *ast.If) error {
f.count.branch++
success := fmt.Sprintf("%s_if_%d_true", f.Name, f.count.branch)
fail := fmt.Sprintf("%s_if_%d_false", f.Name, f.count.branch)
err := f.CompileCondition(branch.Condition, success, fail)
@ -16,7 +17,6 @@ func (f *Function) CompileIf(branch *ast.If) error {
return err
}
f.count.branch++
f.AddLabel(success)
f.pushScope()
err = f.CompileAST(branch.Body)

View File

@ -9,9 +9,12 @@ import (
// CompileLoop compiles a loop instruction.
func (f *Function) CompileLoop(loop *ast.Loop) error {
f.count.loop++
label := fmt.Sprintf("%s_loop_%d", f.Name, f.count.loop)
f.AddLabel(label)
defer f.Jump(asm.JUMP, label)
f.count.loop++
return f.CompileAST(loop.Body)
f.pushScope()
err := f.CompileAST(loop.Body)
f.popScope()
f.Jump(asm.JUMP, label)
return err
}

View File

@ -38,11 +38,11 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
return nil
case token.String:
value := t.Text()[1 : len(t.Bytes)-1]
f.count.data++
label := fmt.Sprintf("%s_data_%d", f.Name, f.count.data)
value := t.Text()[1 : len(t.Bytes)-1]
f.assembler.Data[label] = []byte(value)
f.RegisterLabel(asm.MOVE, register, label)
f.count.data++
return nil
default: