1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-08-13 19:52:52 +02:00

Organize imports across integration and unit test files for consistent structure. Initialize functions map in SymbolTable safely and refactor disassembler label handling logic.

This commit is contained in:
Tim Voronov
2025-07-07 11:27:22 -04:00
parent 8893605445
commit 924c5282cf
12 changed files with 32 additions and 13 deletions

View File

@@ -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++
}

View File

@@ -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) {

View File

@@ -2,6 +2,7 @@ package asm
import (
"fmt"
"github.com/MontFerret/ferret/pkg/runtime"
"github.com/MontFerret/ferret/pkg/vm"
)

View File

@@ -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

View File

@@ -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 {

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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"

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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) {