diff options
| author | Marc Vertes <mvertes@free.fr> | 2026-01-27 16:37:02 +0100 |
|---|---|---|
| committer | Marc Vertes <mvertes@free.fr> | 2026-01-27 16:37:02 +0100 |
| commit | 60f6ebc8d8369721e105d826145af2b8856ac67e (patch) | |
| tree | 002073b75edc68aba95399db89f50b24ebb6935f /comp | |
| parent | aa5861917ac2543f85bf4cfefbb69cf501d4de41 (diff) | |
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.
Diffstat (limited to 'comp')
| -rw-r--r-- | comp/compiler.go | 28 |
1 files changed, 19 insertions, 9 deletions
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] |
