From 448af0707afab634a9e73a97e4485e8688e3f9a5 Mon Sep 17 00:00:00 2001 From: Eduard Urbach Date: Tue, 16 Jul 2024 11:44:10 +0200 Subject: [PATCH] Reordered counters --- src/build/core/CompileCondition.go | 4 ++-- src/build/core/CompileIf.go | 2 +- src/build/core/CompileLoop.go | 9 ++++++--- src/build/core/TokenToRegister.go | 4 ++-- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/build/core/CompileCondition.go b/src/build/core/CompileCondition.go index 84c4a38..b257cee 100644 --- a/src/build/core/CompileCondition.go +++ b/src/build/core/CompileCondition.go @@ -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] diff --git a/src/build/core/CompileIf.go b/src/build/core/CompileIf.go index 3146cc4..14422cc 100644 --- a/src/build/core/CompileIf.go +++ b/src/build/core/CompileIf.go @@ -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) diff --git a/src/build/core/CompileLoop.go b/src/build/core/CompileLoop.go index 7bf00c4..7553394 100644 --- a/src/build/core/CompileLoop.go +++ b/src/build/core/CompileLoop.go @@ -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 } diff --git a/src/build/core/TokenToRegister.go b/src/build/core/TokenToRegister.go index 1d3640c..afedcb5 100644 --- a/src/build/core/TokenToRegister.go +++ b/src/build/core/TokenToRegister.go @@ -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: