From 3113c7f058e427c49e936f82a518766ddb869c58 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Fri, 23 Jan 2026 13:05:10 +0100 Subject: feat: add Next2 and Pull2 instructions in vm. These will be used for range clause assigning both key and value. --- vm/vm.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'vm/vm.go') 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: -- cgit v1.2.3