From 60f6ebc8d8369721e105d826145af2b8856ac67e Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Tue, 27 Jan 2026 16:37:02 +0100 Subject: fix: improve multiple define The VM Vassign instruction now takes an argument to indicates the number of assignations to perform on the stack. Definitions with a function returning multiple values now work. There is still some simplifications, and also to apply the same strategy to var declarations with assign. --- comp/compiler.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'comp/compiler.go') diff --git a/comp/compiler.go b/comp/compiler.go index 37ed0bd..ebb9926 100644 --- a/comp/compiler.go +++ b/comp/compiler.go @@ -208,15 +208,25 @@ func (c *Compiler) Generate(tokens parser.Tokens) (err error) { c.emit(t, vm.Grow, t.Arg[0].(int)) case lang.Define: - rhs := pop() - typ := rhs.Type - if typ == nil { - typ = rhs.Value.Type + showStack(stack) + n := t.Arg[0].(int) + l := len(stack) + rhs := stack[l-n:] + stack = stack[:l-n] + l = len(stack) + lhs := stack[l-n:] + stack = stack[:l-n] + showStack(stack) + for i, r := range rhs { + // Propage type of rhs to lhs. + typ := r.Type + if typ == nil { + typ = r.Value.Type + } + lhs[i].Type = typ + c.Data[lhs[i].Index] = vm.NewValue(typ) } - lhs := pop() - lhs.Type = typ - c.Data[lhs.Index] = vm.NewValue(typ) - c.emit(t, vm.Vassign) + c.emit(t, vm.Vassign, n) case lang.Assign: rhs := pop() @@ -234,7 +244,7 @@ func (c *Compiler) Generate(tokens parser.Tokens) (err error) { c.Data[lhs.Index] = vm.NewValue(rhs.Type) c.Symbols[lhs.Name].Type = rhs.Type } - c.emit(t, vm.Vassign) + c.emit(t, vm.Vassign, t.Arg[0].(int)) case lang.IndexAssign: s := stack[len(stack)-3] -- cgit v1.2.3