diff options
| author | Marc Vertes <mvertes@free.fr> | 2024-06-19 13:55:21 +0200 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2024-06-19 13:55:21 +0200 |
| commit | 70625002b6b3ba280c700636ed8314f20e1384a7 (patch) | |
| tree | 8218f793c1fc140fc8354f1cfac162d47c424d57 /vm | |
| parent | 1a2b2cb565ebf701f43012e1fce5552398d622a9 (diff) | |
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.
Diffstat (limited to 'vm')
| -rw-r--r-- | vm/vm_test.go | 104 |
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] |
