Replies: 1 comment 3 replies
-
I happened to look at #1529 and saw a simple fix that seems to work: func rangeInt(n *node) {
index0 := n.child[0].findex // array index location in frame
index2 := index0 - 1 // max
fnext := getExec(n.fnext)
tnext := getExec(n.tnext)
var value func(*frame) reflect.Value
mxn := n.child[1]
value = genValue(mxn)
n.exec = func(f *frame) bltn {
v0 := f.data[index0]
// NEW CODE HERE:
nv := reflect.New(v0.Type()).Elem() // make a new value for each iteration
f.data[index0] = nv
nv.SetInt(v0.Int() + 1)
if int(nv.Int()) >= int(f.data[index2].Int()) {
return fnext
}
return tnext
}
// Init sequence
next := n.exec
index := index0
n.child[0].exec = func(f *frame) bltn {
f.data[index2] = value(f) // set max
f.data[index].SetInt(-1) // assing index value
return next
}
} I don't know if there might be some kind of memory leak associated with this but hopefully not? I guess I can test that.. :) Also see #1644 for the impl of range-over-int. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I would like to implement the per-loop variables as in go 1.22: https://go.googlesource.com/proposal/+/master/design/60078-loopvar.md
I already managed to implement the new
for i := range n
construct (PR forthcoming), so I have some familiarity with the guts at this point, but I'm not quite sure how to inject a new per-loop variable.for a simple test case:
The ast tree looks like this:
The gen runner is:
but this is storing the one index variable in
n.child[0]
, whereas I need to somehow create a new per-iteration copy of that inside theblockStmt
. Is it necessary to inject a new AST element at the start of the block, or can I somehow just inject a new variable into the scope for that block? The existing range code does something like this for the range variable:cfg.go:240 (in my code)
but the
k
here the .child[0] index node -- do we need to anchor a scope symbol to a node? I guess someone has to keep track of thefindex
? is there somewhere else to keep track of that?I guess the ast injection would need to happen in ast.Inspect, similar to this code:
we would need to add this for standard for loops too.. would be easier if we didn't have to make a new node for the var(s) within the block..
anyway, thanks for any tips! and obviously if you're planning to implement this anytime soon, that would be awesome!
Beta Was this translation helpful? Give feedback.
All reactions