aboutsummaryrefslogtreecommitdiff
path: root/pkg/lang/vm
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/lang/vm')
-rw-r--r--pkg/lang/vm/code/op.go4
-rw-r--r--pkg/lang/vm/exec.go53
-rw-r--r--pkg/lang/vm/text/decompiler.go4
-rw-r--r--pkg/lang/vm/text/op.go3
-rw-r--r--pkg/lang/vm/vm.go5
-rw-r--r--pkg/lang/vm/vm_test.go47
6 files changed, 34 insertions, 82 deletions
diff --git a/pkg/lang/vm/code/op.go b/pkg/lang/vm/code/op.go
index 8a5c6b5..0f268d6 100644
--- a/pkg/lang/vm/code/op.go
+++ b/pkg/lang/vm/code/op.go
@@ -55,8 +55,4 @@ const (
OpJf
OpRet
-
- // Temporary operations, which will be removed with the advent of methods.
- OpTempArrLen
- OpTempArrPush
)
diff --git a/pkg/lang/vm/exec.go b/pkg/lang/vm/exec.go
index 0ace13a..bca4e03 100644
--- a/pkg/lang/vm/exec.go
+++ b/pkg/lang/vm/exec.go
@@ -1077,56 +1077,3 @@ func (vm *VM) execRet() error {
vm.setPos(pos)
return nil
}
-
-func (vm *VM) execTempArrLen() error {
- a, err := vm.stack.Pop()
- if err != nil {
- return err
- }
-
- switch a.Type() {
- case value.ArrayType:
- arr := a.Data().(value.ArrayData)
- len, err := arr.Len(vm.memory)
- if err != nil {
- return err
- }
- res := value.NewInt(int64(len))
- vm.stack.Push(res)
-
- if err := a.Drop(vm.memory); err != nil {
- return err
- }
- default:
- return ErrInvalidOperandTypes{
- Op: code.OpTempArrLen,
- X: a.Type(),
- }
- }
-
- return nil
-}
-
-func (vm *VM) execTempArrPush() error {
- e, err := vm.popAndDrop()
- if err != nil {
- return err
- }
- a, err := vm.popAndDrop()
- if err != nil {
- return err
- }
-
- switch a.Type() {
- case value.ArrayType:
- arr := a.Data().(value.ArrayData)
- arr.Push(vm.memory, e)
- default:
- return ErrInvalidOperandType{
- Op: code.OpTempArrPush,
- X: a.Type(),
- }
- }
-
- return nil
-}
diff --git a/pkg/lang/vm/text/decompiler.go b/pkg/lang/vm/text/decompiler.go
index ed48412..ba08f1c 100644
--- a/pkg/lang/vm/text/decompiler.go
+++ b/pkg/lang/vm/text/decompiler.go
@@ -78,9 +78,7 @@ func (d *Decompiler) decompileInstruction(bc code.Raw) (string, code.Raw) {
code.OpGte,
code.OpIndex,
code.OpSetAtIndex,
- code.OpRet,
- code.OpTempArrLen,
- code.OpTempArrPush:
+ code.OpRet:
return opString, bc[1:]
// Operations that take an int.
diff --git a/pkg/lang/vm/text/op.go b/pkg/lang/vm/text/op.go
index 213656b..d94ff1b 100644
--- a/pkg/lang/vm/text/op.go
+++ b/pkg/lang/vm/text/op.go
@@ -46,9 +46,6 @@ var (
code.OpJt: "jt",
code.OpJf: "jf",
code.OpRet: "ret",
-
- code.OpTempArrPush: "temp_arr_push",
- code.OpTempArrLen: "temp_arr_len",
}
stringToOp = reverseMap(opToString)
)
diff --git a/pkg/lang/vm/vm.go b/pkg/lang/vm/vm.go
index b18e7cb..452cde6 100644
--- a/pkg/lang/vm/vm.go
+++ b/pkg/lang/vm/vm.go
@@ -303,11 +303,6 @@ func (vm *VM) step(module *code.Code, op code.Op) (stepDecision, error) {
case code.OpRet:
err = vm.execRet()
- case code.OpTempArrLen:
- err = vm.execTempArrLen()
- case code.OpTempArrPush:
- err = vm.execTempArrPush()
-
default:
err = ErrInvalidOp{Op: uint8(op)}
}
diff --git a/pkg/lang/vm/vm_test.go b/pkg/lang/vm/vm_test.go
index ea4123f..a8b479b 100644
--- a/pkg/lang/vm/vm_test.go
+++ b/pkg/lang/vm/vm_test.go
@@ -39,16 +39,20 @@ func TestFibonacci(t *testing.T) {
push_array
get_local 0
+ get_member "push"
push_int 1
- temp_arr_push
+ call 1
get_local 0
+ get_member "push"
push_int 1
- temp_arr_push
+ call 1
+ drop 2
@fib_loop:
get_local 0
- temp_arr_len
+ get_member "length"
+ call 0
push_int 1
@@ -56,6 +60,8 @@ func TestFibonacci(t *testing.T) {
sub
get_local 0
+ get_member "push"
+
get_local 0
# This is the last element
get_local 1
@@ -73,13 +79,15 @@ func TestFibonacci(t *testing.T) {
index
add
- temp_arr_push
+ call 1
+ drop 1
# Drop local 1, which was the length of the array, which we no longer need
drop 1
get_local 0
- temp_arr_len
+ get_member "length"
+ call 0
push_int 10
@@ -151,19 +159,23 @@ func TestEscapedEnv(t *testing.T) {
push_array
get_local 1
+ get_member "push"
get_local 0
call 0
- temp_arr_push
+ call 1
get_local 1
+ get_member "push"
get_local 0
call 0
- temp_arr_push
+ call 1
get_local 1
+ get_member "push"
get_local 0
call 0
- temp_arr_push
+ call 1
+ drop 3
halt
@create:
@@ -229,16 +241,20 @@ func TestMember(t *testing.T) {
push_array
get_local 0
+ get_member "push"
push_int 1
- temp_arr_push
+ call 1
get_local 0
+ get_member "push"
push_int 2
- temp_arr_push
+ call 1
get_local 0
+ get_member "push"
push_int 3
- temp_arr_push
+ call 1
+ drop 3
get_member "length"
call 0
@@ -393,7 +409,6 @@ func TestPrimes(t *testing.T) {
lte
jf @prime_check
-
get_local 0
get_local 3
mod
@@ -404,8 +419,10 @@ func TestPrimes(t *testing.T) {
jf @factor_loop_inc
get_local 2
+ get_member "push"
get_local 3
- temp_arr_push
+ call 1
+ drop 1
@factor_loop_inc:
get_local 3
@@ -424,8 +441,10 @@ func TestPrimes(t *testing.T) {
jf @main_loop_inc
get_local 1
+ get_member "push"
get_local 0
- temp_arr_push
+ call 1
+ drop 1
@main_loop_inc:
get_local 0