summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc Vertes <mvertes@free.fr>2026-01-23 09:23:51 +0100
committerMarc Vertes <mvertes@free.fr>2026-01-23 09:23:51 +0100
commit79eaad4bbb778b04501fca724ce2641e25b33f3a (patch)
tree7bfb83681bdfd4af8836f9001ed65221105d2145
parent305e4774d543dab27d665c39fd750eb8b701948e (diff)
chore: use parser.Token.Arg to store generated parameters
-rw-r--r--comp/compiler.go17
-rw-r--r--interp/interpreter_test.go1
-rw-r--r--parser/parse.go3
3 files changed, 10 insertions, 11 deletions
diff --git a/comp/compiler.go b/comp/compiler.go
index 614e80a..ba114da 100644
--- a/comp/compiler.go
+++ b/comp/compiler.go
@@ -319,8 +319,7 @@ func (c *Compiler) Generate(tokens parser.Tokens) (err error) {
case lang.JumpFalse:
var i int
if s, ok := c.Symbols[t.Str]; !ok {
- // t.Beg contains the position in code which needs to be fixed.
- t.Beg = len(c.Code)
+ t.Arg = []any{len(c.Code)} // current code location
fixList = append(fixList, t)
} else {
i = int(s.Value.Int()) - len(c.Code)
@@ -330,8 +329,7 @@ func (c *Compiler) Generate(tokens parser.Tokens) (err error) {
case lang.JumpSetFalse:
var i int
if s, ok := c.Symbols[t.Str]; !ok {
- // t.Beg contains the position in code which needs to be fixed.
- t.Beg = len(c.Code)
+ t.Arg = []any{len(c.Code)} // current code location
fixList = append(fixList, t)
} else {
i = int(s.Value.Int()) - len(c.Code)
@@ -341,8 +339,7 @@ func (c *Compiler) Generate(tokens parser.Tokens) (err error) {
case lang.JumpSetTrue:
var i int
if s, ok := c.Symbols[t.Str]; !ok {
- // t.Beg contains the position in code which needs to be fixed.
- t.Beg = len(c.Code)
+ t.Arg = []any{len(c.Code)} // current code location
fixList = append(fixList, t)
} else {
i = int(s.Value.Int()) - len(c.Code)
@@ -352,7 +349,7 @@ func (c *Compiler) Generate(tokens parser.Tokens) (err error) {
case lang.Goto:
var i int
if s, ok := c.Symbols[t.Str]; !ok {
- t.Beg = len(c.Code)
+ t.Arg = []any{len(c.Code)} // current code location
fixList = append(fixList, t)
} else {
i = int(s.Value.Int()) - len(c.Code)
@@ -430,7 +427,8 @@ func (c *Compiler) Generate(tokens parser.Tokens) (err error) {
emit(t, vm.Stop)
case lang.Return:
- emit(t, vm.Return, t.Beg, t.End)
+ numOut, numIn := t.Arg[0].(int), t.Arg[1].(int)
+ emit(t, vm.Return, numOut, numIn)
case lang.Slice:
if stack[len(stack)-3].IsInt() {
@@ -452,7 +450,8 @@ func (c *Compiler) Generate(tokens parser.Tokens) (err error) {
if !ok {
return fmt.Errorf("label not found: %q", t.Str)
}
- c.Code[t.Beg].Arg[0] = int(s.Value.Int()) - t.Beg
+ loc := t.Arg[0].(int)
+ c.Code[loc].Arg[0] = int(s.Value.Int()) - loc // relative code position
}
return err
}
diff --git a/interp/interpreter_test.go b/interp/interpreter_test.go
index f5c347b..89ab4ae 100644
--- a/interp/interpreter_test.go
+++ b/interp/interpreter_test.go
@@ -128,6 +128,7 @@ func TestFor(t *testing.T) {
{src: "func f() int {a := 0; for {a = a+1; if a < 3 {continue}; break}; return a}; f()", res: "3"}, // #04
{src: "a := []int{1,2,3,4}; b := 0; for i := range a {b = b+i}; b", res: "6"}, // #05
{src: "func f() int {a := []int{1,2,3,4}; b := 0; for i := range a {b = b+i}; return b}; f()", res: "6"}, // #06
+ // {src: "a := []int{1,2,3,4}; b := 0; for i, e := range a {b = b+i+e}; b", res: "6"}, // #07
})
}
diff --git a/parser/parse.go b/parser/parse.go
index 95179be..daf30ca 100644
--- a/parser/parse.go
+++ b/parser/parse.go
@@ -497,8 +497,7 @@ func (p *Parser) parseReturn(in Tokens) (out Tokens, err error) {
// TODO: the function symbol should be already present in the parser context.
// otherwise no way to handle anonymous func.
s := p.function
- in[0].Beg = s.Type.Rtype.NumOut()
- in[0].End = s.Type.Rtype.NumIn()
+ in[0].Arg = []any{s.Type.Rtype.NumOut(), s.Type.Rtype.NumIn()}
out = append(out, in[0])
return out, err
}