summaryrefslogtreecommitdiff
path: root/vm/vm_test.go
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2024-07-18 12:56:29 +0200
committerGitHub <noreply@github.com>2024-07-18 12:56:29 +0200
commitdabd9e5eb81bbc9aeaeb32fb3e3ce83eef258a77 (patch)
tree32be6a4cecf83487dd6a91e8a9aa1da709840b0f /vm/vm_test.go
parent1a2b2cb565ebf701f43012e1fce5552398d622a9 (diff)
fix (parser): don't panic if assign of define untyped value (#10)
* fix (parser): don't panic if assign of define untyped value In case of defining or assigning to untyped value, the type has to be taken from the source value instead of the target value. The vm test coverage has also been slightly improved. * fix and simplify Token.Name() * improve parser errors
Diffstat (limited to 'vm/vm_test.go')
-rw-r--r--vm/vm_test.go104
1 files changed, 94 insertions, 10 deletions
diff --git a/vm/vm_test.go b/vm/vm_test.go
index 8fa8e71..43333ad 100644
--- a/vm/vm_test.go
+++ b/vm/vm_test.go
@@ -3,6 +3,7 @@ package vm
import (
"fmt"
"log"
+ "reflect"
"testing"
)
@@ -61,7 +62,90 @@ var tests = []struct {
{0, Exit},
},
start: 0, end: 1, mem: "[3]",
-}, { // #01 -- Calling a function defined outside the VM.
+}, { // #01 -- A simple subtraction.
+ code: [][]int64{
+ {0, Push, 2},
+ {0, Push, 3},
+ {0, Sub},
+ {0, Exit},
+ },
+ start: 0, end: 1, mem: "[1]",
+}, { // #02 -- A simple multiplication.
+ code: [][]int64{
+ {0, Push, 3},
+ {0, Push, 2},
+ {0, Mul},
+ {0, Exit},
+ },
+ start: 0, end: 1, mem: "[6]",
+}, { // #03 -- lower.
+ code: [][]int64{
+ {0, Push, 3},
+ {0, Push, 2},
+ {0, Lower},
+ {0, Exit},
+ },
+ start: 0, end: 1, mem: "[true]",
+}, { // #04 -- greater.
+ code: [][]int64{
+ {0, Push, 2},
+ {0, Push, 3},
+ {0, Greater},
+ {0, Exit},
+ },
+ start: 0, end: 1, mem: "[true]",
+}, { // #05 -- equal.
+ code: [][]int64{
+ {0, Push, 2},
+ {0, Push, 3},
+ {0, Equal},
+ {0, Exit},
+ },
+ start: 0, end: 1, mem: "[false]",
+}, { // #06 -- equalSet.
+ code: [][]int64{
+ {0, Push, 2},
+ {0, Push, 3},
+ {0, EqualSet},
+ {0, Exit},
+ },
+ start: 0, end: 2, mem: "[2 false]",
+}, { // #07 -- equalSet.
+ code: [][]int64{
+ {0, Push, 3},
+ {0, Push, 3},
+ {0, EqualSet},
+ {0, Exit},
+ },
+ start: 0, end: 1, mem: "[true]",
+}, { // #08 not.
+ code: [][]int64{
+ {0, Push, 3},
+ {0, Push, 3},
+ {0, Equal},
+ {0, Not},
+ {0, Exit},
+ },
+ start: 0, end: 1, mem: "[false]",
+}, { // #09 pop.
+ code: [][]int64{
+ {0, Push, 3},
+ {0, Push, 2},
+ {0, Pop, 1},
+ {0, Exit},
+ },
+ start: 0, end: 1, mem: "[3]",
+}, { // #10 -- Assign a variable.
+ sym: []Value{{Type: TypeOf(0), Data: reflect.ValueOf(0)}},
+ code: [][]int64{
+ {0, Grow, 1},
+ {0, New, 2, 0},
+ {0, Push, 2},
+ {0, Assign, 1},
+ {0, Exit},
+ },
+ start: 1, end: 2, mem: "[2]",
+}, { // #11 -- Calling a function defined outside the VM.
sym: []Value{ValueOf(fmt.Println), ValueOf("Hello")},
code: [][]int64{
{0, Dup, 0},
@@ -69,7 +153,7 @@ var tests = []struct {
{0, Exit},
},
start: 1, end: 3, mem: "[6 <nil>]",
-}, { // #02 -- Defining and calling a function in VM.
+}, { // #12 -- Defining and calling a function in VM.
code: [][]int64{
{0, Jump, 3}, // 0
{0, Push, 3}, // 1
@@ -79,7 +163,7 @@ var tests = []struct {
{0, Exit}, // 5
},
start: 0, end: 1, mem: "[3]",
-}, { // #03 -- Defining and calling a function in VM.
+}, { // #13 -- Defining and calling a function in VM.
code: [][]int64{
{0, Jump, 3}, // 0
{0, Push, 3}, // 1
@@ -90,7 +174,7 @@ var tests = []struct {
{0, Exit}, // 6
},
start: 0, end: 1, mem: "[3]",
-}, { // #04 -- Defining and calling a function in VM.
+}, { // #14 -- Defining and calling a function in VM.
code: [][]int64{
{0, Jump, 5}, // 0
{0, Push, 3}, // 1
@@ -103,20 +187,20 @@ var tests = []struct {
{0, Exit}, // 8
},
start: 0, end: 1, mem: "[3]",
-}, { // #05 -- Fibonacci numbers, hand written. Showcase recursivity.
+}, { // #15 -- Fibonacci numbers, hand written. Showcase recursivity.
code: [][]int64{
{0, Jump, 19}, // 0
{0, Push, 2}, // 2 [2]
{0, Fdup, -2}, // 1 [2 i]
{0, Lower}, // 3 [true/false]
{0, JumpTrue, 13}, // 4 [], goto 17
- {0, Push, 2}, // 6 [i 2]
- {0, Fdup, -2}, // 5 [i]
+ {0, Push, 2}, // 5 [i 2]
+ {0, Fdup, -2}, // 6 [i]
{0, Sub}, // 7 [(i-2)]
{0, Push, 1}, // 8
{0, Call}, // 9 [fib(i-2)]
- {0, Push, 1}, // 11 [(i-2) i 1]
- {0, Fdup, -2}, // 10 [fib(i-2) i]
+ {0, Push, 1}, // 10 [(i-2) i 1]
+ {0, Fdup, -2}, // 11 [fib(i-2) i]
{0, Sub}, // 12 [(i-2) (i-1)]
{0, Push, 1}, // 13
{0, Call}, // 14 [fib(i-2) fib(i-1)]
@@ -130,7 +214,7 @@ var tests = []struct {
{0, Exit}, // 22
},
start: 0, end: 1, mem: "[8]",
-}, { // #06 -- Fibonacci with some immediate instructions.
+}, { // #16 -- Fibonacci with some immediate instructions.
code: [][]int64{
{0, Jump, 14}, // 0
{0, Fdup, -2}, // 1 [i]