diff options
| author | Marc Vertes <mvertes@free.fr> | 2024-03-14 14:37:32 +0100 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2024-03-14 14:37:32 +0100 |
| commit | 378683d25bdae89fa446b2b82f8dda5d6b34ea33 (patch) | |
| tree | cbf624e4eb38fafe51a12a718b48f43192cfcfac /vm | |
| parent | 60170bad25f912e0ba44e4c0095b54f6e26e307e (diff) | |
feat: initial support of closures
Also detection and automatic execution of main function.
Make sure that all debug is output to stderr.
Diffstat (limited to 'vm')
| -rw-r--r-- | vm/type.go | 3 | ||||
| -rw-r--r-- | vm/vm.go | 2 | ||||
| -rw-r--r-- | vm/vm_test.go | 8 |
3 files changed, 8 insertions, 5 deletions
@@ -26,6 +26,9 @@ type Value struct { // NewValue returns an addressable zero value for the specified type. func NewValue(typ *Type) Value { + if typ.Rtype.Kind() == reflect.Func { + typ = TypeOf(0) // Function value is its index in the code segment. + } return Value{Type: typ, Data: reflect.New(typ.Rtype).Elem()} } @@ -145,7 +145,7 @@ func (m *Machine) Run() (err error) { case Calli: mem = append(mem, ValueOf(ip+1), ValueOf(fp)) fp = sp + 2 - ip += int(op[2]) + ip = int(op[2]) continue case CallX: // Should be made optional. l := int(op[2]) diff --git a/vm/vm_test.go b/vm/vm_test.go index fb71176..8fa8e71 100644 --- a/vm/vm_test.go +++ b/vm/vm_test.go @@ -75,7 +75,7 @@ var tests = []struct { {0, Push, 3}, // 1 {0, Return, 1, 1}, // 2 {0, Push, 1}, // 3 - {0, Calli, -3}, // 4 + {0, Calli, 1}, // 4 {0, Exit}, // 5 }, start: 0, end: 1, mem: "[3]", @@ -138,16 +138,16 @@ var tests = []struct { {0, JumpTrue, 9}, // 3 [], goto 12 {0, Fdup, -2}, // 4 [i] {0, Subi, 2}, // 5 [(i-2)] - {0, Calli, -5}, // 6 [fib(i-2)] + {0, Calli, 1}, // 6 [fib(i-2)] {0, Fdup, -2}, // 7 [fib(i-2) i] {0, Subi, 1}, // 8 [(i-2) (i-1)] - {0, Calli, -8}, // 9 [fib(i-2) fib(i-1)], call 1 + {0, Calli, 1}, // 9 [fib(i-2) fib(i-1)], call 1 {0, Add}, // 10 [fib(i-2)+fib(i-1)] {0, Return, 1, 1}, // 11 return i {0, Fdup, -2}, // 12 [i] {0, Return, 1, 1}, // 13 return i {0, Push, 6}, // 14 [1] - {0, Calli, -14}, // 15 [fib(*1)], call 1 + {0, Calli, 1}, // 15 [fib(*1)], call 1 {0, Exit}, // 16 }, start: 0, end: 1, mem: "[8]", |
