This commit is contained in:
parent
575fd8e37a
commit
7879184d56
4 changed files with 40 additions and 59 deletions
|
|
@ -14,11 +14,9 @@ type AllTestCase struct {
|
|||
func GetAllTestCases() map[string]AllTestCase {
|
||||
return map[string]AllTestCase{
|
||||
"constant_number": {
|
||||
"a := 1",
|
||||
"a := 1\na",
|
||||
[]Value{&IntegerValue{new(big.Int).SetInt64(1)}},
|
||||
[]map[string]Value{
|
||||
{"a": &IntegerValue{new(big.Int).SetInt64(1)}},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"func": {
|
||||
"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{},
|
||||
},
|
||||
"list": {
|
||||
"a := [1.0, 2.0]\n{}",
|
||||
[]Value{&NilValue{}},
|
||||
[]map[string]Value{
|
||||
{"a": &ListValue{
|
||||
"a := [1.0, 2.0]\na",
|
||||
[]Value{&ListValue{
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
},
|
||||
}},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"constant_list_concat": {
|
||||
"a := [1, 2] + [3]\n{}",
|
||||
[]Value{&NilValue{}},
|
||||
[]map[string]Value{
|
||||
{"a": &ListValue{
|
||||
"a := [1, 2] + [3]\na",
|
||||
[]Value{&ListValue{
|
||||
[]Value{
|
||||
&IntegerValue{big.NewInt(1)},
|
||||
&IntegerValue{big.NewInt(2)},
|
||||
&IntegerValue{big.NewInt(3)},
|
||||
},
|
||||
}},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
"list_concat": {
|
||||
"a := [1.0, 2.0]\nb := a + [3.0]\nnil",
|
||||
[]Value{&NilValue{}},
|
||||
[]map[string]Value{
|
||||
{
|
||||
"a": &ListValue{
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
},
|
||||
},
|
||||
"b": &ListValue{
|
||||
"a := [1.0, 2.0]\na + [3.0]",
|
||||
[]Value{&ListValue{
|
||||
[]Value{
|
||||
&FloatValue{1},
|
||||
&FloatValue{2},
|
||||
&FloatValue{3},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -104,7 +88,7 @@ func TestAll(t *testing.T) {
|
|||
c := NewCompiler([]rune(tc.src))
|
||||
|
||||
t.Log("Compiling parse tree")
|
||||
err = c.Compile(tree)
|
||||
_, err = c.Compile(tree)
|
||||
if err != nil {
|
||||
t.Fatalf("Compiler had an error: %s", err)
|
||||
}
|
||||
|
|
@ -143,7 +127,7 @@ func BenchmarkAll(b *testing.B) {
|
|||
tree, _ := p.Parse(tc.src)
|
||||
|
||||
c := NewCompiler([]rune(tc.src))
|
||||
_ = c.Compile(tree)
|
||||
_, _ = c.Compile(tree)
|
||||
|
||||
vm := NewVM(c.Chunk, 256, 256)
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,6 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
return map[string]CompileTestData{
|
||||
"constant_string": {
|
||||
&Program{
|
||||
[]Import{},
|
||||
&BlockNode{
|
||||
[]Node{
|
||||
&AssignNode{
|
||||
|
|
@ -50,17 +49,14 @@ func GetCompileTestData() map[string]CompileTestData {
|
|||
true,
|
||||
0, 0,
|
||||
},
|
||||
&ReferenceNode{"a", 0, 0},
|
||||
},
|
||||
0, 0,
|
||||
},
|
||||
"",
|
||||
},
|
||||
[]Value{&StringValue{"Hello world!"}},
|
||||
[]map[string]Value{
|
||||
{
|
||||
"a": &StringValue{"Hello world!"},
|
||||
},
|
||||
},
|
||||
[]map[string]Value{},
|
||||
},
|
||||
/* these tests are so fucking unmaintainable
|
||||
"conditional_false": {
|
||||
|
|
@ -626,7 +622,7 @@ func TestCompile(t *testing.T) {
|
|||
c := NewCompiler([]rune(testCase.program.String()))
|
||||
|
||||
t.Log("Compiling node tree")
|
||||
err := c.Compile(testCase.program)
|
||||
_, err := c.Compile(testCase.program)
|
||||
if err != nil {
|
||||
t.Fatalf("Compiling failed: %v", err)
|
||||
}
|
||||
|
|
@ -654,7 +650,7 @@ func BenchmarkCompile(b *testing.B) {
|
|||
b.Run(name, func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
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 {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
c := NewCompiler([]rune(tc.program.String()))
|
||||
err := c.Compile(tc.program)
|
||||
_, err := c.Compile(tc.program)
|
||||
if err != nil {
|
||||
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++ {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,10 +1,10 @@
|
|||
|
||||
assertEq(typeof(1), "int")
|
||||
assertEq(typeof("Hello"), "string")
|
||||
assertEq(typeof("Hello"), "str")
|
||||
assertEq(typeof(true), "boolean")
|
||||
|
||||
# lists
|
||||
assertEq(typeof(["Hello", "world"]), "[string]")
|
||||
assertEq(typeof(["Hello", "world"]), "[str]")
|
||||
assertEq(typeof([0, 1]), "[int]")
|
||||
assertEq(typeof((0, 1)), "(int, int)")
|
||||
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@ package main
|
|||
import (
|
||||
"errors"
|
||||
"log"
|
||||
"neemek.com/anglais/core"
|
||||
"syscall/js"
|
||||
|
||||
"neemek.com/anglais/core"
|
||||
)
|
||||
|
||||
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 {
|
||||
var e core.CompilerError
|
||||
if errors.As(err, &e) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue