From 2dcdfd55cfa7fab0ddb438b6d0046ec4013054a2 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Mon, 24 Jun 2024 16:52:58 +0800 Subject: [PATCH 1/7] naive fix --- gnovm/pkg/gnolang/gno_test.go | 84 +++++++++++++++++------------------ gnovm/pkg/gnolang/uverse.go | 5 ++- 2 files changed, 46 insertions(+), 43 deletions(-) diff --git a/gnovm/pkg/gnolang/gno_test.go b/gnovm/pkg/gnolang/gno_test.go index 54d808faefc..583a3a244ab 100644 --- a/gnovm/pkg/gnolang/gno_test.go +++ b/gnovm/pkg/gnolang/gno_test.go @@ -159,37 +159,37 @@ func main() { m.RunMain() } -func BenchmarkPreprocessForLoop(b *testing.B) { - m := NewMachine("test", nil) - c := `package test -func main() { - for i:=0; i<10000; i++ {} -}` - n := MustParseFile("main.go", c) - m.RunFiles(n) - - for i := 0; i < b.N; i++ { - m.RunMain() - } -} - -func BenchmarkIfStatement(b *testing.B) { - m := NewMachine("test", nil) - c := `package test -func main() { - for i:=0; i<10000; i++ { - if i > 10 { - - } - } -}` - n := MustParseFile("main.go", c) - m.RunFiles(n) - - for i := 0; i < b.N; i++ { - m.RunMain() - } -} +//func BenchmarkPreprocessForLoop(b *testing.B) { +// m := NewMachine("test", nil) +// c := `package test +//func main() { +// for i:=0; i<10000; i++ {} +//}` +// n := MustParseFile("main.go", c) +// m.RunFiles(n) +// +// for i := 0; i < b.N; i++ { +// m.RunMain() +// } +//} + +//func BenchmarkIfStatement(b *testing.B) { +// m := NewMachine("test", nil) +// c := `package test +//func main() { +// for i:=0; i<10000; i++ { +// if i > 10 { +// +// } +// } +//}` +// n := MustParseFile("main.go", c) +// m.RunFiles(n) +// +// for i := 0; i < b.N; i++ { +// m.RunMain() +// } +//} func TestDoOpEvalBaseConversion(t *testing.T) { m := NewMachine("test", nil) @@ -401,17 +401,17 @@ func BenchmarkBenchdata(b *testing.B) { name += "_param:" + param } b.Run(name, func(b *testing.B) { - if strings.HasPrefix(name, "matrix.gno_param") { - // CGO_ENABLED=0 go test -bench . -benchmem ./... -short -run=^$ -cpu 1,2 -count=1 ./... - // That is not just exposing test and benchmark traces as output, but these benchmarks are failing - // making the output unparseable: - /* - BenchmarkBenchdata/matrix.gno_param:3 panic: runtime error: index out of range [31] with length 25 [recovered] - panic: runtime error: index out of range [31] with length 25: - ... - */ - b.Skip("it panics causing an error when parsing benchmark results") - } + //if strings.HasPrefix(name, "matrix.gno_param") { + // // CGO_ENABLED=0 go test -bench . -benchmem ./... -short -run=^$ -cpu 1,2 -count=1 ./... + // // That is not just exposing test and benchmark traces as output, but these benchmarks are failing + // // making the output unparseable: + // /* + // BenchmarkBenchdata/matrix.gno_param:3 panic: runtime error: index out of range [31] with length 25 [recovered] + // panic: runtime error: index out of range [31] with length 25: + // ... + // */ + // b.Skip("it panics causing an error when parsing benchmark results") + //} // Gen template with N and param. var buf bytes.Buffer diff --git a/gnovm/pkg/gnolang/uverse.go b/gnovm/pkg/gnolang/uverse.go index 880a75396ca..ad886958adc 100644 --- a/gnovm/pkg/gnolang/uverse.go +++ b/gnovm/pkg/gnolang/uverse.go @@ -65,9 +65,11 @@ var ( const uversePkgPath = ".uverse" +var done bool + // Always returns a new copy from the latest state of source. func Uverse() *PackageValue { - if uverseValue == nil { + if uverseValue == nil || !done { pn := UverseNode() uverseValue = pn.NewPackage() } @@ -1017,6 +1019,7 @@ func UverseNode() *PackageNode { m.Exceptions = nil }, ) + done = true return uverseNode } From fb2f40842a52b8e26e6b58196273879806a675ea Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Thu, 22 Aug 2024 23:39:02 +0800 Subject: [PATCH 2/7] fix untyped bool --- gnovm/pkg/gnolang/preprocess.go | 3 +++ gnovm/tests/files/types/eql_0f49.gno | 21 +++++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 gnovm/tests/files/types/eql_0f49.gno diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index ba60ead28f6..d7b52e3eed9 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -1207,6 +1207,9 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { "incompatible types in binary expression: %v %v %v", lt.TypeID(), n.Op, rt.TypeID())) } + // convert untyped to typed + checkOrConvertType(store, last, &n.Left, defaultTypeOf(lt), false) + checkOrConvertType(store, last, &n.Right, defaultTypeOf(rt), false) } else { // left untyped, right typed checkOrConvertType(store, last, &n.Left, rt, false) } diff --git a/gnovm/tests/files/types/eql_0f49.gno b/gnovm/tests/files/types/eql_0f49.gno new file mode 100644 index 00000000000..b5a4bf4ed05 --- /dev/null +++ b/gnovm/tests/files/types/eql_0f49.gno @@ -0,0 +1,21 @@ +package main + +func main() { + a := "1234" + b := "1234" + + cond := a == b + println(cond) + println(cond == (a == b)) + println((a == b) == cond) + println((a == b) == (a == b)) + println(cond && (a == b)) + println(cond || (a > b)) + +} + +// true +// true +// true +// true +// true From 22f973f97d5825455ed2eebd57aec01d802ec8ff Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Tue, 27 Aug 2024 10:02:11 +0800 Subject: [PATCH 3/7] revert un-intentional change --- gnovm/pkg/gnolang/gno_test.go | 84 +++++++++++++++++------------------ gnovm/pkg/gnolang/uverse.go | 5 +-- 2 files changed, 43 insertions(+), 46 deletions(-) diff --git a/gnovm/pkg/gnolang/gno_test.go b/gnovm/pkg/gnolang/gno_test.go index 583a3a244ab..2c1ed2f003d 100644 --- a/gnovm/pkg/gnolang/gno_test.go +++ b/gnovm/pkg/gnolang/gno_test.go @@ -159,37 +159,37 @@ func main() { m.RunMain() } -//func BenchmarkPreprocessForLoop(b *testing.B) { -// m := NewMachine("test", nil) -// c := `package test -//func main() { -// for i:=0; i<10000; i++ {} -//}` -// n := MustParseFile("main.go", c) -// m.RunFiles(n) -// -// for i := 0; i < b.N; i++ { -// m.RunMain() -// } -//} - -//func BenchmarkIfStatement(b *testing.B) { -// m := NewMachine("test", nil) -// c := `package test -//func main() { -// for i:=0; i<10000; i++ { -// if i > 10 { -// -// } -// } -//}` -// n := MustParseFile("main.go", c) -// m.RunFiles(n) -// -// for i := 0; i < b.N; i++ { -// m.RunMain() -// } -//} +func BenchmarkPreprocessForLoop(b *testing.B) { + m := NewMachine("test", nil) + c := `package test +func main() { + for i:=0; i<10000; i++ {} +}` + n := MustParseFile("main.go", c) + m.RunFiles(n) + + for i := 0; i < b.N; i++ { + m.RunMain() + } +} + +func BenchmarkIfStatement(b *testing.B) { + m := NewMachine("test", nil) + c := `package test +func main() { + for i:=0; i<10000; i++ { + if i > 10 { + + } + } +}` + n := MustParseFile("main.go", c) + m.RunFiles(n) + + for i := 0; i < b.N; i++ { + m.RunMain() + } +} func TestDoOpEvalBaseConversion(t *testing.T) { m := NewMachine("test", nil) @@ -401,17 +401,17 @@ func BenchmarkBenchdata(b *testing.B) { name += "_param:" + param } b.Run(name, func(b *testing.B) { - //if strings.HasPrefix(name, "matrix.gno_param") { - // // CGO_ENABLED=0 go test -bench . -benchmem ./... -short -run=^$ -cpu 1,2 -count=1 ./... - // // That is not just exposing test and benchmark traces as output, but these benchmarks are failing - // // making the output unparseable: - // /* - // BenchmarkBenchdata/matrix.gno_param:3 panic: runtime error: index out of range [31] with length 25 [recovered] - // panic: runtime error: index out of range [31] with length 25: - // ... - // */ - // b.Skip("it panics causing an error when parsing benchmark results") - //} + if strings.HasPrefix(name, "matrix.gno_param") { + // CGO_ENABLED=0 go test -bench . -benchmem ./... -short -run=^$ -cpu 1,2 -count=1 ./... + // That is not just exposing test and benchmark traces as output, but these benchmarks are failing + // making the output unparseable: + /* + BenchmarkBenchdata/matrix.gno_param:3 panic: runtime error: index out of range [31] with length 25 [recovered] + panic: runtime error: index out of range [31] with length 25: + ... + */ + b.Skip("it panics causing an error when parsing benchmark results") + } // Gen template with N and param. var buf bytes.Buffer diff --git a/gnovm/pkg/gnolang/uverse.go b/gnovm/pkg/gnolang/uverse.go index ad886958adc..880a75396ca 100644 --- a/gnovm/pkg/gnolang/uverse.go +++ b/gnovm/pkg/gnolang/uverse.go @@ -65,11 +65,9 @@ var ( const uversePkgPath = ".uverse" -var done bool - // Always returns a new copy from the latest state of source. func Uverse() *PackageValue { - if uverseValue == nil || !done { + if uverseValue == nil { pn := UverseNode() uverseValue = pn.NewPackage() } @@ -1019,7 +1017,6 @@ func UverseNode() *PackageNode { m.Exceptions = nil }, ) - done = true return uverseNode } From 3913889649f08bce2676b9255044e3b4b44e2d3a Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Mon, 14 Oct 2024 10:51:20 +0800 Subject: [PATCH 4/7] init --- gnovm/pkg/gnolang/go2gno.go | 24 +++++++++++++++++++--- gnovm/pkg/gnolang/preprocess.go | 9 +++++++++ gnovm/pkg/gnolang/type_check.go | 1 + gnovm/tests/files/const23.gno | 36 +++++++++++++++++++++++++++++++++ gnovm/tests/files/const24.gno | 13 ++++++++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 gnovm/tests/files/const23.gno create mode 100644 gnovm/tests/files/const24.gno diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index efdfecf0289..90f4b68e7b6 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -106,6 +106,7 @@ func MustParseExpr(expr string) Expr { // resulting AST -- the resulting FileNode is returned, together with any other // error (including panics, which are recovered) from [Go2Gno]. func ParseFile(filename string, body string) (fn *FileNode, err error) { + //fmt.Println("---ParseFile, body: ", body) // Use go parser to parse the body. fs := token.NewFileSet() // TODO(morgan): would be nice to add parser.SkipObjectResolution as we don't @@ -132,6 +133,7 @@ func ParseFile(filename string, body string) (fn *FileNode, err error) { }() // parse with Go2Gno. fn = Go2Gno(fs, f).(*FileNode) + //fmt.Println("---fn after transpile: ", fn) fn.Name = Name(filename) return fn, nil } @@ -307,6 +309,7 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { Fields: toFieldsFromList(fs, gon.Fields), } case *ast.AssignStmt: + //fmt.Println("---assignStmt, gon: ", gon) return &AssignStmt{ Lhs: toExprs(fs, gon.Lhs), Op: toWord(gon.Tok), @@ -322,6 +325,7 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { Label: toName(gon.Label), } case *ast.DeclStmt: + //fmt.Println("---DeclStmt, gon: ", gon) return &DeclStmt{ Body: toSimpleDeclStmts(fs, gon.Decl.(*ast.GenDecl)), } @@ -331,6 +335,7 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { Call: *cx, } case *ast.ExprStmt: + //fmt.Println("---ExprStmt, gon: ", gon) if cx, ok := gon.X.(*ast.CallExpr); ok { if ix, ok := cx.Fun.(*ast.Ident); ok && ix.Name == "panic" { if len(cx.Args) != 1 { @@ -408,6 +413,7 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { VarName: toName(as.Lhs[0].(*ast.Ident)), } case *ast.ExprStmt: + println("---ExprStmt") return &SwitchStmt{ Init: toStmt(fs, gon.Init), X: toExpr(fs, as.X.(*ast.TypeAssertExpr).X), @@ -457,6 +463,7 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { case *ast.GenDecl: panic("unexpected *ast.GenDecl; use toDecls(fs,) instead") case *ast.File: + //println("---ast.File") pkgName := Name(gon.Name.Name) decls := make([]Decl, 0, len(gon.Decls)) for _, d := range gon.Decls { @@ -738,6 +745,7 @@ func toDecl(fs *token.FileSet, god ast.Decl) Decl { } func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { + //fmt.Println("---toDecls: ", gd.Tok.String()) ds = make([]Decl, 0, len(gd.Specs)) /* Within a parenthesized const declaration list the @@ -760,6 +768,7 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { IsAlias: alias, }) case *ast.ValueSpec: + //fmt.Println("---ValueSpec, spec: ", s) if gd.Tok == token.CONST { var names []NameExpr var tipe Expr @@ -767,13 +776,17 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { for _, id := range s.Names { names = append(names, *Nx(toName(id))) } - if s.Type == nil { - tipe = lastType - } else { + //fmt.Println("---names: ", names) + //fmt.Println("---lastType: ", lastType) + //fmt.Println("---lastValues: ", lastValues) + if s.Type != nil { tipe = toExpr(fs, s.Type) lastType = tipe } + if s.Values == nil { + // inherit type from last value + tipe = lastType values = copyExprs(lastValues) } else { values = toExprs(fs, s.Values) @@ -785,7 +798,12 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { Values: values, Const: true, } + + //println("set attr iota") cd.SetAttribute(ATTR_IOTA, si) + //fmt.Println("---cd: ", cd) + //fmt.Println("---cd.Type: ", cd.Type) + //fmt.Println("---si: ", si) setLoc(fs, s.Pos(), cd) ds = append(ds, cd) } else { diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index d7b52e3eed9..7948d442e2f 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -955,6 +955,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { return cx, TRANS_CONTINUE } if last.GetIsConst(store, n.Name) { + //fmt.Println("---trans_leave, nameExpr, ", n.Name) cx := evalConst(store, last, n) return cx, TRANS_CONTINUE } @@ -2239,12 +2240,19 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { // evaluate types and convert consts. if n.Type != nil { // only a single type can be specified. + //fmt.Println("---n.Type: ", n.Type) nt := evalStaticType(store, last, n.Type) for i := 0; i < numNames; i++ { sts[i] = nt } + //fmt.Println("---n.Values: ", n.Values) // convert if const to nt. for i := range n.Values { + //fmt.Printf("---n.Values[%d] %v \n", i, n.Values[i]) + //fmt.Println("---nt: ", nt) + //if cx, ok := n.Values[i].(*ConstExpr); ok { + //fmt.Println("---cx: ", cx) + //} checkOrConvertType(store, last, &n.Values[i], nt, false) } } else if n.Const { @@ -2632,6 +2640,7 @@ func evalConst(store Store, last BlockNode, x Expr) *ConstExpr { // is constant? From the machine? m := NewMachine(".dontcare", store) cv := m.EvalStatic(last, x) + //fmt.Println("---evalConst, cv: ", cv) m.Release() cx := &ConstExpr{ Source: x, diff --git a/gnovm/pkg/gnolang/type_check.go b/gnovm/pkg/gnolang/type_check.go index 31025fef152..6da69536d69 100644 --- a/gnovm/pkg/gnolang/type_check.go +++ b/gnovm/pkg/gnolang/type_check.go @@ -599,6 +599,7 @@ func (x *BinaryExpr) checkShiftLhs(dt Type) { // Overall,it efficiently filters out incompatible expressions, stopping before the next // checkOrConvertType() operation to optimize performance. func (x *BinaryExpr) AssertCompatible(lt, rt Type) { + //fmt.Println("---assert compatible, lt, rt: ", lt, rt) // native type will be converted to gno in latter logic, // this check logic will be conduct again from trans_leave *BinaryExpr. lnt, lin := lt.(*NativeType) diff --git a/gnovm/tests/files/const23.gno b/gnovm/tests/files/const23.gno new file mode 100644 index 00000000000..eb6684544c4 --- /dev/null +++ b/gnovm/tests/files/const23.gno @@ -0,0 +1,36 @@ +package main + +const ( + i int32 = 1 + s = "world" + c = 1 << iota + + s2 = "hey" + d, e = 1, "hello" + + m, n + + b = 1 + 1 + u = +1 +) + +func main() { + println(i) + println(s) + println(c) + println(s2) + println(d) + println(e) + println(m, n) + println(b) + println(u) +} + +// 1 +// world +// 4 +// hey +// 1 +// hello +// 2 +// 1 diff --git a/gnovm/tests/files/const24.gno b/gnovm/tests/files/const24.gno new file mode 100644 index 00000000000..91d2f3a955b --- /dev/null +++ b/gnovm/tests/files/const24.gno @@ -0,0 +1,13 @@ +package main + +const ( + s int = 1 + g = "world" + c = 1 << iota +) + +func main() { + println(c) +} + +// 4 From 33b07a0086851aaf3613c8d8ff3ea0e1077293e5 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Mon, 14 Oct 2024 16:37:35 +0800 Subject: [PATCH 5/7] fixup --- gnovm/pkg/gnolang/go2gno.go | 51 +++++++++++++++++++++------- gnovm/pkg/gnolang/preprocess.go | 60 +++++++++++++++++++++++---------- gnovm/tests/files/const12.gno | 4 ++- gnovm/tests/files/const23.gno | 8 ++--- gnovm/tests/files/const24.gno | 1 + gnovm/tests/files/const25.gno | 14 ++++++++ gnovm/tests/files/const25a.gno | 14 ++++++++ gnovm/tests/files/const26.gno | 15 +++++++++ 8 files changed, 131 insertions(+), 36 deletions(-) create mode 100644 gnovm/tests/files/const25.gno create mode 100644 gnovm/tests/files/const25a.gno create mode 100644 gnovm/tests/files/const26.gno diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 90f4b68e7b6..8753ac85e42 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -768,26 +768,33 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { IsAlias: alias, }) case *ast.ValueSpec: - //fmt.Println("---ValueSpec, spec: ", s) if gd.Tok == token.CONST { + fmt.Println("---spec: ", s) var names []NameExpr var tipe Expr var values Exprs for _, id := range s.Names { names = append(names, *Nx(toName(id))) } - //fmt.Println("---names: ", names) - //fmt.Println("---lastType: ", lastType) - //fmt.Println("---lastValues: ", lastValues) - if s.Type != nil { - tipe = toExpr(fs, s.Type) - lastType = tipe + if s.Type == nil && s.Values == nil { + tipe = lastType + } else { + lastType = toExpr(fs, s.Type) + } + + fmt.Println("---lastType: ", lastType, reflect.TypeOf(lastType)) + if nx, ok := lastType.(*NameExpr); ok { // not nil + fmt.Println("nx.Name: ", nx) + if !isPrimitiveType(string(nx.Name)) { // inherit declared type, or nil, do deduct from values + fmt.Println("---NOT PRIMITIVE: ", nx.Name) + tipe = lastType + } } if s.Values == nil { - // inherit type from last value - tipe = lastType + // inherit type from last values values = copyExprs(lastValues) + // if lastType is declaredType, inherit it } else { values = toExprs(fs, s.Values) lastValues = values @@ -798,12 +805,14 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { Values: values, Const: true, } + //if s.Values == nil { + // cd.Type = tipe + //} - //println("set attr iota") + fmt.Println("---cd.names:", cd.NameExprs) + fmt.Println("---cd.values:", cd.Values) + fmt.Println("---cd.Type:", cd.Type) cd.SetAttribute(ATTR_IOTA, si) - //fmt.Println("---cd: ", cd) - //fmt.Println("---cd.Type: ", cd.Type) - //fmt.Println("---si: ", si) setLoc(fs, s.Pos(), cd) ds = append(ds, cd) } else { @@ -937,3 +946,19 @@ func toSwitchClauseStmt(fs *token.FileSet, cc *ast.CaseClause) SwitchClauseStmt Body: toStmts(fs, cc.Body), } } +func isPrimitiveType(typeStr string) bool { + // List of primitive type prefixes + primitiveTypes := []string{ + "int", "int8", "int16", "int32", "int64", + "uint", "uint8", "uint16", "uint32", "uint64", + "float32", "float64", "string", "bool", + } + + for _, t := range primitiveTypes { + if strings.HasPrefix(typeStr, t) { + return true + } + } + + return false +} diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 7948d442e2f..9b53759dbcd 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -955,7 +955,6 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { return cx, TRANS_CONTINUE } if last.GetIsConst(store, n.Name) { - //fmt.Println("---trans_leave, nameExpr, ", n.Name) cx := evalConst(store, last, n) return cx, TRANS_CONTINUE } @@ -2237,31 +2236,57 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { } } } - // evaluate types and convert consts. - if n.Type != nil { + + if n.Const { + fmt.Println("---isConst, n: ", n) + if n.Values != nil { + if n.Type != nil { + fmt.Println("---from n.Type") + nt := evalStaticType(store, last, n.Type) + for i := 0; i < numNames; i++ { + sts[i] = nt + } + // convert if const to nt. + for i := range n.Values { + checkOrConvertType(store, last, &n.Values[i], nt, false) + } + } else { + fmt.Println("---derive from values") + // derive static type from values. + for i, vx := range n.Values { + fmt.Println("---vx: ", vx) + vt := evalStaticTypeOf(store, last, vx) + fmt.Println("---vt: ", vt) + sts[i] = vt + } + } + } else if n.Type != nil { // special case + nt := evalStaticType(store, last, n.Type) + if dt, ok := nt.(*DeclaredType); ok { + fmt.Println("---dt: ", dt) + for i := 0; i < numNames; i++ { + sts[i] = nt + } + // convert if const to nt. + for i := range n.Values { + checkOrConvertType(store, last, &n.Values[i], nt, false) + } + } else { + panic("should not happen") + } + } + } else if n.Type != nil { // non-const + fmt.Println("---not const, value decl") // only a single type can be specified. - //fmt.Println("---n.Type: ", n.Type) nt := evalStaticType(store, last, n.Type) for i := 0; i < numNames; i++ { sts[i] = nt } - //fmt.Println("---n.Values: ", n.Values) // convert if const to nt. for i := range n.Values { - //fmt.Printf("---n.Values[%d] %v \n", i, n.Values[i]) - //fmt.Println("---nt: ", nt) - //if cx, ok := n.Values[i].(*ConstExpr); ok { - //fmt.Println("---cx: ", cx) - //} checkOrConvertType(store, last, &n.Values[i], nt, false) } - } else if n.Const { - // derive static type from values. - for i, vx := range n.Values { - vt := evalStaticTypeOf(store, last, vx) - sts[i] = vt - } - } else { + } else { // not const, type is nil // convert n.Value to default type. for i, vx := range n.Values { convertIfConst(store, last, vx) @@ -2640,7 +2665,6 @@ func evalConst(store Store, last BlockNode, x Expr) *ConstExpr { // is constant? From the machine? m := NewMachine(".dontcare", store) cv := m.EvalStatic(last, x) - //fmt.Println("---evalConst, cv: ", cv) m.Release() cx := &ConstExpr{ Source: x, diff --git a/gnovm/tests/files/const12.gno b/gnovm/tests/files/const12.gno index 86dde62eb3d..c1b72ee50dd 100644 --- a/gnovm/tests/files/const12.gno +++ b/gnovm/tests/files/const12.gno @@ -6,7 +6,9 @@ const ( None Kind = 0 Left Kind = 1 << iota Right - Both Kind = Left | Right + Both Kind = Left | Right + XX int64 = 0 + ZZ ) func main() { diff --git a/gnovm/tests/files/const23.gno b/gnovm/tests/files/const23.gno index eb6684544c4..582c944c569 100644 --- a/gnovm/tests/files/const23.gno +++ b/gnovm/tests/files/const23.gno @@ -19,18 +19,18 @@ func main() { println(s) println(c) println(s2) - println(d) - println(e) + println(d, e) println(m, n) println(b) println(u) } +// Output: // 1 // world // 4 // hey -// 1 -// hello +// 1 hello +// 1 hello // 2 // 1 diff --git a/gnovm/tests/files/const24.gno b/gnovm/tests/files/const24.gno index 91d2f3a955b..56912217a24 100644 --- a/gnovm/tests/files/const24.gno +++ b/gnovm/tests/files/const24.gno @@ -10,4 +10,5 @@ func main() { println(c) } +// Output: // 4 diff --git a/gnovm/tests/files/const25.gno b/gnovm/tests/files/const25.gno new file mode 100644 index 00000000000..3a4760457ed --- /dev/null +++ b/gnovm/tests/files/const25.gno @@ -0,0 +1,14 @@ +package main + +const ( + d, e = 1, "hello" + m +) + +func main() { + println(d, e) + println(m) +} + +// Error: +// main/files/const25.gno:5:2: assignment mismatch: 1 variable(s) but 2 value(s) diff --git a/gnovm/tests/files/const25a.gno b/gnovm/tests/files/const25a.gno new file mode 100644 index 00000000000..0ac86095cd5 --- /dev/null +++ b/gnovm/tests/files/const25a.gno @@ -0,0 +1,14 @@ +package main + +const ( + d, e = 1, "hello" + m, n, l +) + +func main() { + println(d, e) + println(m) +} + +// Error: +// main/files/const25a.gno:5:2: assignment mismatch: 3 variable(s) but 2 value(s) diff --git a/gnovm/tests/files/const26.gno b/gnovm/tests/files/const26.gno new file mode 100644 index 00000000000..0b99cf1c30a --- /dev/null +++ b/gnovm/tests/files/const26.gno @@ -0,0 +1,15 @@ +package main + +const ( + d = "hello" + m +) + +func main() { + println(d) + println(m) +} + +// Output: +// hello +// hello From d265a3103e4307d297e8bc758c8aa4a0250952e0 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Mon, 14 Oct 2024 18:27:25 +0800 Subject: [PATCH 6/7] fixup --- gnovm/pkg/gnolang/go2gno.go | 18 ++---------- gnovm/pkg/gnolang/preprocess.go | 49 ++++++--------------------------- gnovm/tests/files/const12.gno | 4 +-- gnovm/tests/files/const23.gno | 13 +++++++-- 4 files changed, 23 insertions(+), 61 deletions(-) diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 8753ac85e42..73e0c5fd29c 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -745,7 +745,6 @@ func toDecl(fs *token.FileSet, god ast.Decl) Decl { } func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { - //fmt.Println("---toDecls: ", gd.Tok.String()) ds = make([]Decl, 0, len(gd.Specs)) /* Within a parenthesized const declaration list the @@ -769,24 +768,20 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { }) case *ast.ValueSpec: if gd.Tok == token.CONST { - fmt.Println("---spec: ", s) var names []NameExpr var tipe Expr var values Exprs for _, id := range s.Names { names = append(names, *Nx(toName(id))) } - if s.Type == nil && s.Values == nil { + if s.Type == nil && s.Values == nil { // inherit declared type tipe = lastType } else { lastType = toExpr(fs, s.Type) } - fmt.Println("---lastType: ", lastType, reflect.TypeOf(lastType)) - if nx, ok := lastType.(*NameExpr); ok { // not nil - fmt.Println("nx.Name: ", nx) - if !isPrimitiveType(string(nx.Name)) { // inherit declared type, or nil, do deduct from values - fmt.Println("---NOT PRIMITIVE: ", nx.Name) + if nx, ok := lastType.(*NameExpr); ok { + if !isPrimitiveType(string(nx.Name)) { tipe = lastType } } @@ -805,13 +800,6 @@ func toDecls(fs *token.FileSet, gd *ast.GenDecl) (ds Decls) { Values: values, Const: true, } - //if s.Values == nil { - // cd.Type = tipe - //} - - fmt.Println("---cd.names:", cd.NameExprs) - fmt.Println("---cd.values:", cd.Values) - fmt.Println("---cd.Type:", cd.Type) cd.SetAttribute(ATTR_IOTA, si) setLoc(fs, s.Pos(), cd) ds = append(ds, cd) diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 9b53759dbcd..5609f0c0135 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -2237,46 +2237,7 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { } } - if n.Const { - fmt.Println("---isConst, n: ", n) - if n.Values != nil { - if n.Type != nil { - fmt.Println("---from n.Type") - nt := evalStaticType(store, last, n.Type) - for i := 0; i < numNames; i++ { - sts[i] = nt - } - // convert if const to nt. - for i := range n.Values { - checkOrConvertType(store, last, &n.Values[i], nt, false) - } - } else { - fmt.Println("---derive from values") - // derive static type from values. - for i, vx := range n.Values { - fmt.Println("---vx: ", vx) - vt := evalStaticTypeOf(store, last, vx) - fmt.Println("---vt: ", vt) - sts[i] = vt - } - } - } else if n.Type != nil { // special case - nt := evalStaticType(store, last, n.Type) - if dt, ok := nt.(*DeclaredType); ok { - fmt.Println("---dt: ", dt) - for i := 0; i < numNames; i++ { - sts[i] = nt - } - // convert if const to nt. - for i := range n.Values { - checkOrConvertType(store, last, &n.Values[i], nt, false) - } - } else { - panic("should not happen") - } - } - } else if n.Type != nil { // non-const - fmt.Println("---not const, value decl") + if n.Type != nil { // only a single type can be specified. nt := evalStaticType(store, last, n.Type) for i := 0; i < numNames; i++ { @@ -2286,6 +2247,14 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { for i := range n.Values { checkOrConvertType(store, last, &n.Values[i], nt, false) } + } else if n.Const { + if n.Values != nil { + // derive static type from values. + for i, vx := range n.Values { + vt := evalStaticTypeOf(store, last, vx) + sts[i] = vt + } + } } else { // not const, type is nil // convert n.Value to default type. for i, vx := range n.Values { diff --git a/gnovm/tests/files/const12.gno b/gnovm/tests/files/const12.gno index c1b72ee50dd..86dde62eb3d 100644 --- a/gnovm/tests/files/const12.gno +++ b/gnovm/tests/files/const12.gno @@ -6,9 +6,7 @@ const ( None Kind = 0 Left Kind = 1 << iota Right - Both Kind = Left | Right - XX int64 = 0 - ZZ + Both Kind = Left | Right ) func main() { diff --git a/gnovm/tests/files/const23.gno b/gnovm/tests/files/const23.gno index 582c944c569..a4e7c8888f3 100644 --- a/gnovm/tests/files/const23.gno +++ b/gnovm/tests/files/const23.gno @@ -1,6 +1,10 @@ package main +type Integer int + const ( + i0 Integer = 0 + i int32 = 1 s = "world" c = 1 << iota @@ -10,8 +14,9 @@ const ( m, n - b = 1 + 1 - u = +1 + b = 1 + 1 + u = +1 + x, y = i0, "hello" ) func main() { @@ -23,14 +28,16 @@ func main() { println(m, n) println(b) println(u) + println(x, y) } // Output: // 1 // world -// 4 +// 8 // hey // 1 hello // 1 hello // 2 // 1 +// (0 main.Integer) hello From 674dec140e7bfaf5bad4762c9cbef50e077a6190 Mon Sep 17 00:00:00 2001 From: ltzMaxwell Date: Mon, 14 Oct 2024 18:33:57 +0800 Subject: [PATCH 7/7] fixup --- gnovm/pkg/gnolang/go2gno.go | 4 ---- gnovm/pkg/gnolang/preprocess.go | 3 --- gnovm/pkg/gnolang/type_check.go | 1 - gnovm/tests/files/types/eql_0f49.gno | 21 --------------------- 4 files changed, 29 deletions(-) delete mode 100644 gnovm/tests/files/types/eql_0f49.gno diff --git a/gnovm/pkg/gnolang/go2gno.go b/gnovm/pkg/gnolang/go2gno.go index 73e0c5fd29c..48812f7a10e 100644 --- a/gnovm/pkg/gnolang/go2gno.go +++ b/gnovm/pkg/gnolang/go2gno.go @@ -106,7 +106,6 @@ func MustParseExpr(expr string) Expr { // resulting AST -- the resulting FileNode is returned, together with any other // error (including panics, which are recovered) from [Go2Gno]. func ParseFile(filename string, body string) (fn *FileNode, err error) { - //fmt.Println("---ParseFile, body: ", body) // Use go parser to parse the body. fs := token.NewFileSet() // TODO(morgan): would be nice to add parser.SkipObjectResolution as we don't @@ -309,7 +308,6 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { Fields: toFieldsFromList(fs, gon.Fields), } case *ast.AssignStmt: - //fmt.Println("---assignStmt, gon: ", gon) return &AssignStmt{ Lhs: toExprs(fs, gon.Lhs), Op: toWord(gon.Tok), @@ -325,7 +323,6 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { Label: toName(gon.Label), } case *ast.DeclStmt: - //fmt.Println("---DeclStmt, gon: ", gon) return &DeclStmt{ Body: toSimpleDeclStmts(fs, gon.Decl.(*ast.GenDecl)), } @@ -335,7 +332,6 @@ func Go2Gno(fs *token.FileSet, gon ast.Node) (n Node) { Call: *cx, } case *ast.ExprStmt: - //fmt.Println("---ExprStmt, gon: ", gon) if cx, ok := gon.X.(*ast.CallExpr); ok { if ix, ok := cx.Fun.(*ast.Ident); ok && ix.Name == "panic" { if len(cx.Args) != 1 { diff --git a/gnovm/pkg/gnolang/preprocess.go b/gnovm/pkg/gnolang/preprocess.go index 5609f0c0135..bdda31c027a 100644 --- a/gnovm/pkg/gnolang/preprocess.go +++ b/gnovm/pkg/gnolang/preprocess.go @@ -1207,9 +1207,6 @@ func Preprocess(store Store, ctx BlockNode, n Node) Node { "incompatible types in binary expression: %v %v %v", lt.TypeID(), n.Op, rt.TypeID())) } - // convert untyped to typed - checkOrConvertType(store, last, &n.Left, defaultTypeOf(lt), false) - checkOrConvertType(store, last, &n.Right, defaultTypeOf(rt), false) } else { // left untyped, right typed checkOrConvertType(store, last, &n.Left, rt, false) } diff --git a/gnovm/pkg/gnolang/type_check.go b/gnovm/pkg/gnolang/type_check.go index 6da69536d69..31025fef152 100644 --- a/gnovm/pkg/gnolang/type_check.go +++ b/gnovm/pkg/gnolang/type_check.go @@ -599,7 +599,6 @@ func (x *BinaryExpr) checkShiftLhs(dt Type) { // Overall,it efficiently filters out incompatible expressions, stopping before the next // checkOrConvertType() operation to optimize performance. func (x *BinaryExpr) AssertCompatible(lt, rt Type) { - //fmt.Println("---assert compatible, lt, rt: ", lt, rt) // native type will be converted to gno in latter logic, // this check logic will be conduct again from trans_leave *BinaryExpr. lnt, lin := lt.(*NativeType) diff --git a/gnovm/tests/files/types/eql_0f49.gno b/gnovm/tests/files/types/eql_0f49.gno deleted file mode 100644 index b5a4bf4ed05..00000000000 --- a/gnovm/tests/files/types/eql_0f49.gno +++ /dev/null @@ -1,21 +0,0 @@ -package main - -func main() { - a := "1234" - b := "1234" - - cond := a == b - println(cond) - println(cond == (a == b)) - println((a == b) == cond) - println((a == b) == (a == b)) - println(cond && (a == b)) - println(cond || (a > b)) - -} - -// true -// true -// true -// true -// true