fix wasm and tests
All checks were successful
/ test (push) Successful in 51s

This commit is contained in:
Neemek 2026-07-15 21:46:14 +02:00
parent 575fd8e37a
commit 7879184d56
Signed by: neemek
GPG key ID: 84FFE4D7D40AB25E
4 changed files with 40 additions and 59 deletions

View file

@ -14,11 +14,9 @@ type AllTestCase struct {
func GetAllTestCases() map[string]AllTestCase { func GetAllTestCases() map[string]AllTestCase {
return map[string]AllTestCase{ return map[string]AllTestCase{
"constant_number": { "constant_number": {
"a := 1", "a := 1\na",
[]Value{&IntegerValue{new(big.Int).SetInt64(1)}}, []Value{&IntegerValue{new(big.Int).SetInt64(1)}},
[]map[string]Value{ []map[string]Value{},
{"a": &IntegerValue{new(big.Int).SetInt64(1)}},
},
}, },
"func": { "func": {
"fn sum(a: int, b: int) -> int {\n\treturn a + b\n}\nres := sum(1, 2)", "fn sum(a: int, b: int) -> int {\n\treturn a + b\n}\nres := sum(1, 2)",
@ -26,50 +24,36 @@ func GetAllTestCases() map[string]AllTestCase {
[]map[string]Value{}, []map[string]Value{},
}, },
"list": { "list": {
"a := [1.0, 2.0]\n{}", "a := [1.0, 2.0]\na",
[]Value{&NilValue{}}, []Value{&ListValue{
[]map[string]Value{
{"a": &ListValue{
[]Value{ []Value{
&FloatValue{1}, &FloatValue{1},
&FloatValue{2}, &FloatValue{2},
}, },
}}, }},
}, []map[string]Value{},
}, },
"constant_list_concat": { "constant_list_concat": {
"a := [1, 2] + [3]\n{}", "a := [1, 2] + [3]\na",
[]Value{&NilValue{}}, []Value{&ListValue{
[]map[string]Value{
{"a": &ListValue{
[]Value{ []Value{
&IntegerValue{big.NewInt(1)}, &IntegerValue{big.NewInt(1)},
&IntegerValue{big.NewInt(2)}, &IntegerValue{big.NewInt(2)},
&IntegerValue{big.NewInt(3)}, &IntegerValue{big.NewInt(3)},
}, },
}}, }},
}, []map[string]Value{},
}, },
"list_concat": { "list_concat": {
"a := [1.0, 2.0]\nb := a + [3.0]\nnil", "a := [1.0, 2.0]\na + [3.0]",
[]Value{&NilValue{}}, []Value{&ListValue{
[]map[string]Value{
{
"a": &ListValue{
[]Value{
&FloatValue{1},
&FloatValue{2},
},
},
"b": &ListValue{
[]Value{ []Value{
&FloatValue{1}, &FloatValue{1},
&FloatValue{2}, &FloatValue{2},
&FloatValue{3}, &FloatValue{3},
}, },
}, }},
}, []map[string]Value{},
},
}, },
} }
} }
@ -104,7 +88,7 @@ func TestAll(t *testing.T) {
c := NewCompiler([]rune(tc.src)) c := NewCompiler([]rune(tc.src))
t.Log("Compiling parse tree") t.Log("Compiling parse tree")
err = c.Compile(tree) _, err = c.Compile(tree)
if err != nil { if err != nil {
t.Fatalf("Compiler had an error: %s", err) t.Fatalf("Compiler had an error: %s", err)
} }
@ -143,7 +127,7 @@ func BenchmarkAll(b *testing.B) {
tree, _ := p.Parse(tc.src) tree, _ := p.Parse(tc.src)
c := NewCompiler([]rune(tc.src)) c := NewCompiler([]rune(tc.src))
_ = c.Compile(tree) _, _ = c.Compile(tree)
vm := NewVM(c.Chunk, 256, 256) vm := NewVM(c.Chunk, 256, 256)

View file

@ -37,7 +37,6 @@ func GetCompileTestData() map[string]CompileTestData {
return map[string]CompileTestData{ return map[string]CompileTestData{
"constant_string": { "constant_string": {
&Program{ &Program{
[]Import{},
&BlockNode{ &BlockNode{
[]Node{ []Node{
&AssignNode{ &AssignNode{
@ -50,17 +49,14 @@ func GetCompileTestData() map[string]CompileTestData {
true, true,
0, 0, 0, 0,
}, },
&ReferenceNode{"a", 0, 0},
}, },
0, 0, 0, 0,
}, },
"", "",
}, },
[]Value{&StringValue{"Hello world!"}}, []Value{&StringValue{"Hello world!"}},
[]map[string]Value{ []map[string]Value{},
{
"a": &StringValue{"Hello world!"},
},
},
}, },
/* these tests are so fucking unmaintainable /* these tests are so fucking unmaintainable
"conditional_false": { "conditional_false": {
@ -626,7 +622,7 @@ func TestCompile(t *testing.T) {
c := NewCompiler([]rune(testCase.program.String())) c := NewCompiler([]rune(testCase.program.String()))
t.Log("Compiling node tree") t.Log("Compiling node tree")
err := c.Compile(testCase.program) _, err := c.Compile(testCase.program)
if err != nil { if err != nil {
t.Fatalf("Compiling failed: %v", err) t.Fatalf("Compiling failed: %v", err)
} }
@ -654,7 +650,7 @@ func BenchmarkCompile(b *testing.B) {
b.Run(name, func(b *testing.B) { b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
c := NewCompiler([]rune{}) c := NewCompiler([]rune{})
_ = c.Compile(testCase.program) _, _ = c.Compile(testCase.program)
} }
}) })
} }
@ -683,7 +679,7 @@ func TestCompiler_CleanStack(t *testing.T) {
for name, tc := range cases { for name, tc := range cases {
t.Run(name, func(t *testing.T) { t.Run(name, func(t *testing.T) {
c := NewCompiler([]rune(tc.program.String())) c := NewCompiler([]rune(tc.program.String()))
err := c.Compile(tc.program) _, err := c.Compile(tc.program)
if err != nil { if err != nil {
t.Fatalf("Compiling failed: %v", err) t.Fatalf("Compiling failed: %v", err)
} }
@ -696,7 +692,7 @@ func TestCompiler_CleanStack(t *testing.T) {
for i := 1; i < int(vm.Stack.Current); i++ { for i := 1; i < int(vm.Stack.Current); i++ {
v := vm.Stack.items[i] v := vm.Stack.items[i]
if v == nil || v.Type() != VariableValueType { if v == nil {
t.Errorf("Unclean stack! value %v at %d on the stack is intermediary", v.String(), i) t.Errorf("Unclean stack! value %v at %d on the stack is intermediary", v.String(), i)
} }
} }

View file

@ -1,10 +1,10 @@
assertEq(typeof(1), "int") assertEq(typeof(1), "int")
assertEq(typeof("Hello"), "string") assertEq(typeof("Hello"), "str")
assertEq(typeof(true), "boolean") assertEq(typeof(true), "boolean")
# lists # lists
assertEq(typeof(["Hello", "world"]), "[string]") assertEq(typeof(["Hello", "world"]), "[str]")
assertEq(typeof([0, 1]), "[int]") assertEq(typeof([0, 1]), "[int]")
assertEq(typeof((0, 1)), "(int, int)") assertEq(typeof((0, 1)), "(int, int)")

View file

@ -5,8 +5,9 @@ package main
import ( import (
"errors" "errors"
"log" "log"
"neemek.com/anglais/core"
"syscall/js" "syscall/js"
"neemek.com/anglais/core"
) )
type JsResolver struct { type JsResolver struct {
@ -91,7 +92,7 @@ func run(_ js.Value, args []js.Value) interface{} {
} }
}() }()
err = compiler.Compile(tree) _, err = compiler.Compile(tree)
if err != nil { if err != nil {
var e core.CompilerError var e core.CompilerError
if errors.As(err, &e) { if errors.As(err, &e) {