Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: value decl loop #2074

Merged
merged 12 commits into from
Sep 12, 2024
30 changes: 26 additions & 4 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,37 @@ func PredefineFileSet(store Store, pn *PackageNode, fset *FileSet) {
// Finally, predefine other decls and
// preprocess ValueDecls..
for _, fn := range fset.Files {
for i := 0; i < len(fn.Decls); i++ {
lenDecls := len(fn.Decls)
for i := 0; i < lenDecls; i++ {
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
d := fn.Decls[i]
if d.GetAttribute(ATTR_PREDEFINED) == true {
// skip declarations already predefined (e.g.
// through recursion for a dependent)
} else {
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
// recursively predefine dependencies.
d2, _ := predefineNow(store, fn, d)
fn.Decls[i] = d2
if vd, ok := d.(*ValueDecl); ok && len(vd.NameExprs) > 1 {
split := make([]Decl, len(vd.NameExprs))

for j := 0; j < len(vd.NameExprs); j++ {
base := vd.Copy().(*ValueDecl)
base.NameExprs = NameExprs{NameExpr{
Attributes: base.NameExprs[j].Attributes,
Path: base.NameExprs[j].Path,
Name: base.NameExprs[j].Name,
}}
base.Values = Exprs{base.Values[j].Copy().(Expr)}

split[j], _ = predefineNow(store, fn, base)
}

fn.Decls = append(fn.Decls[:i], append(split, fn.Decls[i+1:]...)...)
petar-dambovaliev marked this conversation as resolved.
Show resolved Hide resolved
i += len(vd.NameExprs)
lenDecls += len(vd.NameExprs) - 1
} else {
// recursively predefine dependencies.
d2, _ := predefineNow(store, fn, d)

fn.Decls[i] = d2
}
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions gnovm/tests/files/var19.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

func main() {
println(a)
println(b)
println(c)
}

var a, b, c = 1, a + 1, b + 1

// Output:
// 1
// 2
// 3
17 changes: 17 additions & 0 deletions gnovm/tests/files/var20.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package main

func main() {
println(a)
println(b)
println(c)
println(d)
}

var a, b, c = 1, a + d, 3
var d = a

// Output:
// 1
// 2
// 3
// 1
Loading