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:
@@ -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++
|
||||
}
|
||||
|
@@ -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) {
|
||||
|
@@ -2,6 +2,7 @@ package asm
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/MontFerret/ferret/pkg/runtime"
|
||||
"github.com/MontFerret/ferret/pkg/vm"
|
||||
)
|
||||
|
@@ -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
|
||||
|
@@ -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 {
|
||||
|
@@ -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) {
|
||||
|
@@ -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) {
|
||||
|
@@ -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"
|
||||
|
@@ -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) {
|
||||
|
@@ -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) {
|
||||
|
@@ -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) {
|
||||
|
@@ -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) {
|
||||
|
Reference in New Issue
Block a user