Skip to content

Commit

Permalink
First disassembly test
Browse files Browse the repository at this point in the history
  • Loading branch information
ziflex committed Oct 30, 2024
1 parent 66b14dc commit a8c4a96
Show file tree
Hide file tree
Showing 9 changed files with 1,546 additions and 1,419 deletions.
1,407 changes: 1,407 additions & 0 deletions pkg/compiler/compiler_exec_test.go

Large diffs are not rendered by default.

48 changes: 48 additions & 0 deletions pkg/compiler/compiler_setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
j "encoding/json"
"fmt"
"github.com/MontFerret/ferret/pkg/runtime/core"
"strings"
"testing"

. "github.com/smartystreets/goconvey/convey"
Expand All @@ -18,6 +20,23 @@ type UseCase struct {
Assertion Assertion
}

type ExpectedProgram struct {
Disassembly string
Constants []core.Value
Registers int
}

type ByteCodeUseCase struct {
Expression string
Expected ExpectedProgram
}

func Compile(expression string) (*runtime.Program, error) {
c := compiler.New()

return c.Compile(expression)
}

func Run(p *runtime.Program, opts ...runtime.EnvironmentOption) ([]byte, error) {
vm := runtime.NewVM(p)

Expand Down Expand Up @@ -75,6 +94,35 @@ func ShouldHaveSameItems(actual any, expected ...any) string {
return ""
}

func RunAsmUseCases(t *testing.T, useCases []ByteCodeUseCase) {
c := compiler.New()
for _, useCase := range useCases {
t.Run(fmt.Sprintf("Bytecode: %s", useCase.Expression), func(t *testing.T) {
Convey(useCase.Expression, t, func() {
assertJSON := func(actual, expected interface{}) {
actualJ, err := j.Marshal(actual)
So(err, ShouldBeNil)

expectedJ, err := j.Marshal(expected)
So(err, ShouldBeNil)

So(string(actualJ), ShouldEqualJSON, string(expectedJ))
}

prog, err := c.Compile(useCase.Expression)

So(err, ShouldBeNil)

So(strings.TrimSpace(prog.Disassemble()), ShouldEqual, strings.TrimSpace(useCase.Expected.Disassembly))

assertJSON(prog.Constants, useCase.Expected.Constants)
//assertJSON(prog.CatchTable, useCase.Expected.CatchTable)
//So(prog.Registers, ShouldEqual, useCase.Expected.Registers)
})
})
}
}

func RunUseCasesWith(t *testing.T, c *compiler.Compiler, useCases []UseCase, opts ...runtime.EnvironmentOption) {
for _, useCase := range useCases {
t.Run(useCase.Expression, func(t *testing.T) {
Expand Down
Loading

0 comments on commit a8c4a96

Please sign in to comment.