mirror of
https://github.com/MontFerret/ferret.git
synced 2025-08-15 20:02:56 +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)
|
labels := make(map[int]string)
|
||||||
counter := 0
|
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 {
|
for _, instr := range bytecode {
|
||||||
switch instr.Opcode {
|
switch instr.Opcode {
|
||||||
case vm.OpJump, vm.OpJumpIfFalse, vm.OpJumpIfTrue, vm.OpIterNext, vm.OpIterSkip, vm.OpIterLimit:
|
case vm.OpJump, vm.OpJumpIfFalse, vm.OpJumpIfTrue, vm.OpIterNext, vm.OpIterSkip, vm.OpIterLimit:
|
||||||
target := int(instr.Operands[0])
|
target := int(instr.Operands[0])
|
||||||
if name, ok := names[target]; ok && name != "" {
|
|
||||||
labels[target] = fmt.Sprintf("@%s", name)
|
if name, ok := names[target]; !ok || name == "" {
|
||||||
} else if _,
|
|
||||||
ok := labels[target]; !ok {
|
|
||||||
labels[target] = fmt.Sprintf("@L%d", counter)
|
labels[target] = fmt.Sprintf("@L%d", counter)
|
||||||
counter++
|
counter++
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,10 @@
|
|||||||
package asm
|
package asm
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDisassemble(t *testing.T) {
|
func TestDisassemble(t *testing.T) {
|
||||||
|
@@ -2,6 +2,7 @@ package asm
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/MontFerret/ferret/pkg/runtime"
|
"github.com/MontFerret/ferret/pkg/runtime"
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
"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) {
|
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 {
|
if currArgs, exists := st.functions[name]; exists {
|
||||||
// we need to ensure that the number of arguments is not greater than the current one
|
// 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
|
// if it is not, we will not override the current one
|
||||||
|
@@ -2,8 +2,10 @@ package compiler_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
|
||||||
"github.com/smartystreets/goconvey/convey"
|
"github.com/smartystreets/goconvey/convey"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func CastToProgram(prog any) *vm.Program {
|
func CastToProgram(prog any) *vm.Program {
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
package compiler_test
|
package compiler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestForWhileDistinct(t *testing.T) {
|
func TestForWhileDistinct(t *testing.T) {
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
package compiler_test
|
package compiler_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLogicalOperators(t *testing.T) {
|
func TestLogicalOperators(t *testing.T) {
|
||||||
|
@@ -2,10 +2,11 @@ package compiler_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/MontFerret/ferret/pkg/asm"
|
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/asm"
|
||||||
|
|
||||||
"github.com/smartystreets/goconvey/convey"
|
"github.com/smartystreets/goconvey/convey"
|
||||||
|
|
||||||
"github.com/MontFerret/ferret/pkg/compiler"
|
"github.com/MontFerret/ferret/pkg/compiler"
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
package vm_test
|
package vm_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestForWhileCollectAggregate(t *testing.T) {
|
func TestForWhileCollectAggregate(t *testing.T) {
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
package vm_test
|
package vm_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestForWhileCollect(t *testing.T) {
|
func TestForWhileCollect(t *testing.T) {
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
package vm_test
|
package vm_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestForWhileDistinct(t *testing.T) {
|
func TestForWhileDistinct(t *testing.T) {
|
||||||
|
@@ -1,8 +1,9 @@
|
|||||||
package vm_test
|
package vm_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/MontFerret/ferret/pkg/vm"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/MontFerret/ferret/pkg/vm"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestForWhileNested(t *testing.T) {
|
func TestForWhileNested(t *testing.T) {
|
||||||
|
Reference in New Issue
Block a user