diff --git a/.gitattributes b/.gitattributes index adc4144ffa3..6876fc55353 100644 --- a/.gitattributes +++ b/.gitattributes @@ -3,3 +3,5 @@ go.sum linguist-generated text gnovm/stdlibs/generated.go linguist-generated gnovm/tests/stdlibs/generated.go linguist-generated +*.gen.gno *.gen_test.gno linguist-generated +*.gen.go *.gen_test.go linguist-generated \ No newline at end of file diff --git a/examples/gno.land/p/moul/xmath/generate.go b/examples/gno.land/p/moul/xmath/generate.go index dd921b2ab44..ad70adb06bd 100644 --- a/examples/gno.land/p/moul/xmath/generate.go +++ b/examples/gno.land/p/moul/xmath/generate.go @@ -1,183 +1,3 @@ -package main +package xmath -//go:generate go run generate.go - -import ( - "bytes" - "fmt" - "go/format" - "log" - "os" - "strings" - "text/template" -) - -type Type struct { - Name string - ZeroValue string - Signed bool - Float bool -} - -var types = []Type{ - {"Int8", "0", true, false}, - {"Int16", "0", true, false}, - {"Int32", "0", true, false}, - {"Int64", "0", true, false}, - {"Int", "0", true, false}, - {"Uint8", "0", false, false}, - {"Uint16", "0", false, false}, - {"Uint32", "0", false, false}, - {"Uint64", "0", false, false}, - {"Uint", "0", false, false}, - {"Float32", "0.0", true, true}, - {"Float64", "0.0", true, true}, -} - -const sourceTpl = `package xmath - -{{ range .Types }} -// {{.Name}} helpers -func Max{{.Name}}(a, b {{.Name | lower}}) {{.Name | lower}} { - if a > b { - return a - } - return b -} - -func Min{{.Name}}(a, b {{.Name | lower}}) {{.Name | lower}} { - if a < b { - return a - } - return b -} - -func Clamp{{.Name}}(value, min, max {{.Name | lower}}) {{.Name | lower}} { - if value < min { - return min - } - if value > max { - return max - } - return value -} -{{if .Signed}} -func Abs{{.Name}}(x {{.Name | lower}}) {{.Name | lower}} { - if x < 0 { - return -x - } - return x -} - -func Sign{{.Name}}(x {{.Name | lower}}) {{.Name | lower}} { - if x < 0 { - return -1 - } - if x > 0 { - return 1 - } - return 0 -} -{{end}} -{{end}} -` - -const testTpl = `package xmath - -import "testing" - -{{range .Types}} -func Test{{.Name}}Helpers(t *testing.T) { - // Test Max{{.Name}} - if Max{{.Name}}(1, 2) != 2 { - t.Error("Max{{.Name}}(1, 2) should be 2") - } - {{if .Signed}}if Max{{.Name}}(-1, -2) != -1 { - t.Error("Max{{.Name}}(-1, -2) should be -1") - }{{end}} - - // Test Min{{.Name}} - if Min{{.Name}}(1, 2) != 1 { - t.Error("Min{{.Name}}(1, 2) should be 1") - } - {{if .Signed}}if Min{{.Name}}(-1, -2) != -2 { - t.Error("Min{{.Name}}(-1, -2) should be -2") - }{{end}} - - // Test Clamp{{.Name}} - if Clamp{{.Name}}(5, 1, 3) != 3 { - t.Error("Clamp{{.Name}}(5, 1, 3) should be 3") - } - if Clamp{{.Name}}(0, 1, 3) != 1 { - t.Error("Clamp{{.Name}}(0, 1, 3) should be 1") - } - if Clamp{{.Name}}(2, 1, 3) != 2 { - t.Error("Clamp{{.Name}}(2, 1, 3) should be 2") - } - {{if .Signed}} - // Test Abs{{.Name}} - if Abs{{.Name}}(-5) != 5 { - t.Error("Abs{{.Name}}(-5) should be 5") - } - if Abs{{.Name}}(5) != 5 { - t.Error("Abs{{.Name}}(5) should be 5") - } - - // Test Sign{{.Name}} - if Sign{{.Name}}(-5) != -1 { - t.Error("Sign{{.Name}}(-5) should be -1") - } - if Sign{{.Name}}(5) != 1 { - t.Error("Sign{{.Name}}(5) should be 1") - } - if Sign{{.Name}}({{.ZeroValue}}) != 0 { - t.Error("Sign{{.Name}}({{.ZeroValue}}) should be 0") - } - {{end}} -} -{{end}} -` - -func main() { - funcMap := template.FuncMap{ - "lower": strings.ToLower, - } - - // Generate source file - sourceTmpl := template.Must(template.New("source").Funcs(funcMap).Parse(sourceTpl)) - var sourceOut bytes.Buffer - if err := sourceTmpl.Execute(&sourceOut, struct{ Types []Type }{types}); err != nil { - log.Fatal(err) - } - - // Format the generated code - formattedSource, err := format.Source(sourceOut.Bytes()) - if err != nil { - log.Fatal(err) - } - - // Write source file - if err := os.WriteFile("xmath.gno", formattedSource, 0644); err != nil { - log.Fatal(err) - } - - // Generate test file - testTmpl := template.Must(template.New("test").Parse(testTpl)) - var testOut bytes.Buffer - if err := testTmpl.Execute(&testOut, struct{ Types []Type }{types}); err != nil { - log.Fatal(err) - } - - // Format the generated test code - formattedTest, err := format.Source(testOut.Bytes()) - if err != nil { - log.Fatal(err) - } - - // Write test file - if err := os.WriteFile("xmath_test.gno", formattedTest, 0644); err != nil { - log.Fatal(err) - } - - fmt.Println("Generated xmath.gno and xmath_test.gno") -} +//go:generate go run generator.go diff --git a/examples/gno.land/p/moul/xmath/generator.go b/examples/gno.land/p/moul/xmath/generator.go new file mode 100644 index 00000000000..3f7c0e88310 --- /dev/null +++ b/examples/gno.land/p/moul/xmath/generator.go @@ -0,0 +1,183 @@ +//go:build ignore + +package main + +import ( + "bytes" + "fmt" + "go/format" + "log" + "os" + "strings" + "text/template" +) + +type Type struct { + Name string + ZeroValue string + Signed bool + Float bool +} + +var types = []Type{ + {"Int8", "0", true, false}, + {"Int16", "0", true, false}, + {"Int32", "0", true, false}, + {"Int64", "0", true, false}, + {"Int", "0", true, false}, + {"Uint8", "0", false, false}, + {"Uint16", "0", false, false}, + {"Uint32", "0", false, false}, + {"Uint64", "0", false, false}, + {"Uint", "0", false, false}, + {"Float32", "0.0", true, true}, + {"Float64", "0.0", true, true}, +} + +const sourceTpl = `package xmath + +{{ range .Types }} +// {{.Name}} helpers +func Max{{.Name}}(a, b {{.Name | lower}}) {{.Name | lower}} { + if a > b { + return a + } + return b +} + +func Min{{.Name}}(a, b {{.Name | lower}}) {{.Name | lower}} { + if a < b { + return a + } + return b +} + +func Clamp{{.Name}}(value, min, max {{.Name | lower}}) {{.Name | lower}} { + if value < min { + return min + } + if value > max { + return max + } + return value +} +{{if .Signed}} +func Abs{{.Name}}(x {{.Name | lower}}) {{.Name | lower}} { + if x < 0 { + return -x + } + return x +} + +func Sign{{.Name}}(x {{.Name | lower}}) {{.Name | lower}} { + if x < 0 { + return -1 + } + if x > 0 { + return 1 + } + return 0 +} +{{end}} +{{end}} +` + +const testTpl = `package xmath + +import "testing" + +{{range .Types}} +func Test{{.Name}}Helpers(t *testing.T) { + // Test Max{{.Name}} + if Max{{.Name}}(1, 2) != 2 { + t.Error("Max{{.Name}}(1, 2) should be 2") + } + {{if .Signed}}if Max{{.Name}}(-1, -2) != -1 { + t.Error("Max{{.Name}}(-1, -2) should be -1") + }{{end}} + + // Test Min{{.Name}} + if Min{{.Name}}(1, 2) != 1 { + t.Error("Min{{.Name}}(1, 2) should be 1") + } + {{if .Signed}}if Min{{.Name}}(-1, -2) != -2 { + t.Error("Min{{.Name}}(-1, -2) should be -2") + }{{end}} + + // Test Clamp{{.Name}} + if Clamp{{.Name}}(5, 1, 3) != 3 { + t.Error("Clamp{{.Name}}(5, 1, 3) should be 3") + } + if Clamp{{.Name}}(0, 1, 3) != 1 { + t.Error("Clamp{{.Name}}(0, 1, 3) should be 1") + } + if Clamp{{.Name}}(2, 1, 3) != 2 { + t.Error("Clamp{{.Name}}(2, 1, 3) should be 2") + } + {{if .Signed}} + // Test Abs{{.Name}} + if Abs{{.Name}}(-5) != 5 { + t.Error("Abs{{.Name}}(-5) should be 5") + } + if Abs{{.Name}}(5) != 5 { + t.Error("Abs{{.Name}}(5) should be 5") + } + + // Test Sign{{.Name}} + if Sign{{.Name}}(-5) != -1 { + t.Error("Sign{{.Name}}(-5) should be -1") + } + if Sign{{.Name}}(5) != 1 { + t.Error("Sign{{.Name}}(5) should be 1") + } + if Sign{{.Name}}({{.ZeroValue}}) != 0 { + t.Error("Sign{{.Name}}({{.ZeroValue}}) should be 0") + } + {{end}} +} +{{end}} +` + +func main() { + funcMap := template.FuncMap{ + "lower": strings.ToLower, + } + + // Generate source file + sourceTmpl := template.Must(template.New("source").Funcs(funcMap).Parse(sourceTpl)) + var sourceOut bytes.Buffer + if err := sourceTmpl.Execute(&sourceOut, struct{ Types []Type }{types}); err != nil { + log.Fatal(err) + } + + // Format the generated code + formattedSource, err := format.Source(sourceOut.Bytes()) + if err != nil { + log.Fatal(err) + } + + // Write source file + if err := os.WriteFile("xmath.gen.gno", formattedSource, 0644); err != nil { + log.Fatal(err) + } + + // Generate test file + testTmpl := template.Must(template.New("test").Parse(testTpl)) + var testOut bytes.Buffer + if err := testTmpl.Execute(&testOut, struct{ Types []Type }{types}); err != nil { + log.Fatal(err) + } + + // Format the generated test code + formattedTest, err := format.Source(testOut.Bytes()) + if err != nil { + log.Fatal(err) + } + + // Write test file + if err := os.WriteFile("xmath.gen_test.gno", formattedTest, 0644); err != nil { + log.Fatal(err) + } + + fmt.Println("Generated xmath.gen.gno and xmath.gen_test.gno") +} diff --git a/examples/gno.land/p/moul/xmath/xmath.gno b/examples/gno.land/p/moul/xmath/xmath.gen.gno similarity index 100% rename from examples/gno.land/p/moul/xmath/xmath.gno rename to examples/gno.land/p/moul/xmath/xmath.gen.gno diff --git a/examples/gno.land/p/moul/xmath/xmath_test.gno b/examples/gno.land/p/moul/xmath/xmath.gen_test.gno similarity index 100% rename from examples/gno.land/p/moul/xmath/xmath_test.gno rename to examples/gno.land/p/moul/xmath/xmath.gen_test.gno