diff --git a/pkg/asm/disassembler.go b/pkg/asm/disassembler.go index ac682ec3..81fe09ea 100644 --- a/pkg/asm/disassembler.go +++ b/pkg/asm/disassembler.go @@ -55,14 +55,18 @@ func collectLabels(bytecode []vm.Instruction, names map[int]string) map[int]stri labels := make(map[int]string) counter := 0 + // Iterate through the labels in the program to initialize the labels map + for target, name := range names { + labels[target] = fmt.Sprintf("@%s", name) + } + + // Collect unmarked jump targets for _, instr := range bytecode { switch instr.Opcode { case vm.OpJump, vm.OpJumpIfFalse, vm.OpJumpIfTrue, vm.OpIterNext, vm.OpIterSkip, vm.OpIterLimit: target := int(instr.Operands[0]) - if name, ok := names[target]; ok && name != "" { - labels[target] = fmt.Sprintf("@%s", name) - } else if _, - ok := labels[target]; !ok { + + if name, ok := names[target]; !ok || name == "" { labels[target] = fmt.Sprintf("@L%d", counter) counter++ } diff --git a/pkg/asm/disassembler_test.go b/pkg/asm/disassembler_test.go index 50856016..1e97f826 100644 --- a/pkg/asm/disassembler_test.go +++ b/pkg/asm/disassembler_test.go @@ -1,9 +1,10 @@ package asm import ( - "github.com/MontFerret/ferret/pkg/vm" "reflect" "testing" + + "github.com/MontFerret/ferret/pkg/vm" ) func TestDisassemble(t *testing.T) { diff --git a/pkg/asm/formatter.go b/pkg/asm/formatter.go index 034ee066..8a1e5d8a 100644 --- a/pkg/asm/formatter.go +++ b/pkg/asm/formatter.go @@ -2,6 +2,7 @@ package asm import ( "fmt" + "github.com/MontFerret/ferret/pkg/runtime" "github.com/MontFerret/ferret/pkg/vm" ) diff --git a/pkg/compiler/internal/core/symbols.go b/pkg/compiler/internal/core/symbols.go index 2adda710..87c82d69 100644 --- a/pkg/compiler/internal/core/symbols.go +++ b/pkg/compiler/internal/core/symbols.go @@ -118,6 +118,10 @@ func (st *SymbolTable) BindParam(name string) vm.Operand { } func (st *SymbolTable) BindFunction(name string, args int) { + if st.functions == nil { + st.functions = make(map[string]int) + } + if currArgs, exists := st.functions[name]; exists { // we need to ensure that the number of arguments is not greater than the current one // if it is not, we will not override the current one diff --git a/test/integration/compiler/assertions.go b/test/integration/compiler/assertions.go index 876f046b..03c60763 100644 --- a/test/integration/compiler/assertions.go +++ b/test/integration/compiler/assertions.go @@ -2,8 +2,10 @@ package compiler_test import ( "fmt" - "github.com/MontFerret/ferret/pkg/vm" + "github.com/smartystreets/goconvey/convey" + + "github.com/MontFerret/ferret/pkg/vm" ) func CastToProgram(prog any) *vm.Program { diff --git a/test/integration/compiler/compiler_for_while_distinct_test.go b/test/integration/compiler/compiler_for_while_distinct_test.go index b82e0f0c..7e4d8e69 100644 --- a/test/integration/compiler/compiler_for_while_distinct_test.go +++ b/test/integration/compiler/compiler_for_while_distinct_test.go @@ -1,8 +1,9 @@ package compiler_test import ( - "github.com/MontFerret/ferret/pkg/vm" "testing" + + "github.com/MontFerret/ferret/pkg/vm" ) func TestForWhileDistinct(t *testing.T) { diff --git a/test/integration/compiler/compiler_op_logical_test.go b/test/integration/compiler/compiler_op_logical_test.go index 45403818..5bb9fed0 100644 --- a/test/integration/compiler/compiler_op_logical_test.go +++ b/test/integration/compiler/compiler_op_logical_test.go @@ -1,8 +1,9 @@ package compiler_test import ( - "github.com/MontFerret/ferret/pkg/vm" "testing" + + "github.com/MontFerret/ferret/pkg/vm" ) func TestLogicalOperators(t *testing.T) { diff --git a/test/integration/compiler/test_case.go b/test/integration/compiler/test_case.go index ff621d08..04d8f2bc 100644 --- a/test/integration/compiler/test_case.go +++ b/test/integration/compiler/test_case.go @@ -2,10 +2,11 @@ package compiler_test import ( "fmt" - "github.com/MontFerret/ferret/pkg/asm" "strings" "testing" + "github.com/MontFerret/ferret/pkg/asm" + "github.com/smartystreets/goconvey/convey" "github.com/MontFerret/ferret/pkg/compiler" diff --git a/test/integration/vm/vm_for_while_collect_agg_test.go b/test/integration/vm/vm_for_while_collect_agg_test.go index c4de4b96..44bceda0 100644 --- a/test/integration/vm/vm_for_while_collect_agg_test.go +++ b/test/integration/vm/vm_for_while_collect_agg_test.go @@ -1,8 +1,9 @@ package vm_test import ( - "github.com/MontFerret/ferret/pkg/vm" "testing" + + "github.com/MontFerret/ferret/pkg/vm" ) func TestForWhileCollectAggregate(t *testing.T) { diff --git a/test/integration/vm/vm_for_while_collect_test.go b/test/integration/vm/vm_for_while_collect_test.go index 1d86deeb..c40c29d9 100644 --- a/test/integration/vm/vm_for_while_collect_test.go +++ b/test/integration/vm/vm_for_while_collect_test.go @@ -1,8 +1,9 @@ package vm_test import ( - "github.com/MontFerret/ferret/pkg/vm" "testing" + + "github.com/MontFerret/ferret/pkg/vm" ) func TestForWhileCollect(t *testing.T) { diff --git a/test/integration/vm/vm_for_while_distinct_test.go b/test/integration/vm/vm_for_while_distinct_test.go index 9dd1cdef..5271232d 100644 --- a/test/integration/vm/vm_for_while_distinct_test.go +++ b/test/integration/vm/vm_for_while_distinct_test.go @@ -1,8 +1,9 @@ package vm_test import ( - "github.com/MontFerret/ferret/pkg/vm" "testing" + + "github.com/MontFerret/ferret/pkg/vm" ) func TestForWhileDistinct(t *testing.T) { diff --git a/test/integration/vm/vm_for_while_nested_test.go b/test/integration/vm/vm_for_while_nested_test.go index 5d268f7d..7036818a 100644 --- a/test/integration/vm/vm_for_while_nested_test.go +++ b/test/integration/vm/vm_for_while_nested_test.go @@ -1,8 +1,9 @@ package vm_test import ( - "github.com/MontFerret/ferret/pkg/vm" "testing" + + "github.com/MontFerret/ferret/pkg/vm" ) func TestForWhileNested(t *testing.T) {