-
Notifications
You must be signed in to change notification settings - Fork 386
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:gnolang/gno into feat/sign-transa…
…ctions-genesis
- Loading branch information
Showing
28 changed files
with
1,718 additions
and
1,366 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package xmath | ||
|
||
//go:generate go run generator.go |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
//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 = `// Code generated by generator.go; DO NOT EDIT. | ||
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module gno.land/p/moul/xmath |
Oops, something went wrong.