summaryrefslogtreecommitdiff
path: root/vm/vm.go
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2026-01-23 13:05:10 +0100
committerMarc Vertes <mvertes@free.fr>2026-01-23 13:05:10 +0100
commit3113c7f058e427c49e936f82a518766ddb869c58 (patch)
treeb79e34e358514d1e0301722251cce3b195043670 /vm/vm.go
parent2837dabf7818666a9366d659d2da3b9055140740 (diff)
feat: add Next2 and Pull2 instructions in vm.
These will be used for range clause assigning both key and value.
Diffstat (limited to 'vm/vm.go')
-rw-r--r--vm/vm.go19
1 files changed, 16 insertions, 3 deletions
diff --git a/vm/vm.go b/vm/vm.go
index 7ab5a25..fac1a53 100644
--- a/vm/vm.go
+++ b/vm/vm.go
@@ -56,11 +56,13 @@ const (
Mul // n1 n2 -- prod ; prod = n1*n2
New // -- x; mem[fp+$1] = new mem[$2]
Negate // -- ; - mem[fp]
- Next // -- ; iterator next
+ Next // -- ; iterator next, set K
+ Next2 // -- ; iterator next, set K V
Not // c -- r ; r = !c
Pop // v --
Push // -- v
Pull // a -- a s n; pull iterator next and stop function
+ Pull2 // a -- a s n; pull iterator next and stop function
Return // [r1 .. ri] -- ; exit frame: sp = fp, fp = pop
Slice // a l h -- a; a = a [l:h]
Slice3 // a l h m -- a; a = a[l:h:m]
@@ -247,8 +249,16 @@ func (m *Machine) Run() (err error) {
case Negate:
mem[sp-1] = ValueOf(-mem[sp-1].Int())
case Next:
- if v, ok := mem[sp-2].Interface().(func() (reflect.Value, bool))(); ok {
- mem[c.Arg[1]].Set(v)
+ if k, ok := mem[sp-2].Interface().(func() (reflect.Value, bool))(); ok {
+ mem[c.Arg[1]].Set(k)
+ } else {
+ ip += c.Arg[0]
+ continue
+ }
+ case Next2:
+ if k, v, ok := mem[sp-2].Interface().(func() (reflect.Value, reflect.Value, bool))(); ok {
+ mem[c.Arg[1]].Set(k)
+ mem[c.Arg[2]].Set(v)
} else {
ip += c.Arg[0]
continue
@@ -263,6 +273,9 @@ func (m *Machine) Run() (err error) {
case Pull:
next, stop := iter.Pull(mem[sp-1].Seq())
mem = append(mem, ValueOf(next), ValueOf(stop))
+ case Pull2:
+ next, stop := iter.Pull2(mem[sp-1].Seq2())
+ mem = append(mem, ValueOf(next), ValueOf(stop))
case Grow:
mem = append(mem, make([]Value, c.Arg[0])...)
case Return: