diff options
Diffstat (limited to 'pkg/lang/compiler/compiler_test.go')
| -rw-r--r-- | pkg/lang/compiler/compiler_test.go | 80 |
1 files changed, 79 insertions, 1 deletions
diff --git a/pkg/lang/compiler/compiler_test.go b/pkg/lang/compiler/compiler_test.go index 9c21a3b..04bf425 100644 --- a/pkg/lang/compiler/compiler_test.go +++ b/pkg/lang/compiler/compiler_test.go @@ -457,6 +457,7 @@ func TestSimpleFunction(t *testing.T) { } func TestFunctionArgs(t *testing.T) { + // TODO: Are arguments in the correct order? src := ` fn add(a, b) { return a + b @@ -477,8 +478,8 @@ func TestFunctionArgs(t *testing.T) { halt @add: - get_local 0 get_local 1 + get_local 0 add ret ` @@ -523,6 +524,83 @@ func TestClosureEnv(t *testing.T) { mustCompileTo(t, src, expected) } +func TestType(t *testing.T) { + src := ` + type Cat { + (name, age) { + this.name = name + this.age = age + } + + fn meow(this) { + return this.name + " says Meow!" + } + } + + var kitty = Cat("Kitty", 3) + kitty.meow() + ` + + expected := ` + push_type "Cat" + + get_local 0 + get_member "$add_method" + + push_string "$init" + push_function @Cat:$init + set_arg_count 2 + + call 2 + + get_local 0 + get_member "$add_method" + + push_string "meow" + push_function @Cat:meow + + call 2 + + get_local 0 + push_string "Kitty" + push_int 3 + call 2 + + get_local 1 + get_member "meow" + call 0 + drop 1 + + halt + + @Cat:$init: + push_object + get_env 0 + anchor_type + + get_local 2 + get_local 1 + set_member "name" + + get_local 2 + get_local 0 + set_member "age" + + get_local 2 + ret + @Cat:meow: + get_env 0 + get_member "name" + + push_string " says Meow!" + + add + ret + ` + + mustCompileTo(t, src, expected) +} + func mustCompileTo(t *testing.T, src, expected string) { scanner := scanner.New(strings.NewReader(src)) tokens, err := scanner.Scan() |
