Repository: goplus/gop
Pull request #1150 (2022-04-22 01:19:22)

add Uint128


Patch:
diff --git a/builtin/big/big.go b/builtin/big/big.go
index 289304b3..e7d04f04 100644
--- a/builtin/big/big.go
+++ b/builtin/big/big.go
@@ -47,17 +47,21 @@ type UntypedRat_Default = Rat
 type UntypedFloat_Default = Float
 
 func UntypedInt_Init__0(x int) UntypedInt {
-	panic("make compiler happy")
+	panic(makeCompilerHappy)
 }
 
 func UntypedRat_Init__0(x int) UntypedRat {
-	panic("make compiler happy")
+	panic(makeCompilerHappy)
 }
 
 func UntypedRat_Init__1(x UntypedInt) UntypedRat {
-	panic("make compiler happy")
+	panic(makeCompilerHappy)
 }
 
+const (
+	makeCompilerHappy = "make compiler happy"
+)
+
 // -----------------------------------------------------------------------------
 // type bigint
 
diff --git a/builtin/ng/uint128.go b/builtin/ng/uint128.go
index 9f3d5d19..0f8520f7 100644
--- a/builtin/ng/uint128.go
+++ b/builtin/ng/uint128.go
@@ -14,6 +14,19 @@ type Uint128 struct {
 	hi, lo uint64
 }
 
+// Uint128_Cast: func uint128.init(v int) uint128
+func Uint128_Init__0(v int) (out Uint128) {
+	if v < 0 {
+		panic("TODO: can't init uint128 from a negative integer")
+	}
+	return Uint128{lo: uint64(v)}
+}
+
+// Uint128_Cast: func uint128.init(v uint64) uint128
+func Uint128_Init__1(v uint64) Uint128 {
+	return Uint128{lo: v}
+}
+
 // Uint128_Cast: func uint128(hi, lo uint64) uint128
 func Uint128_Cast__0(hi, lo uint64) Uint128 {
 	return Uint128{hi: hi, lo: lo}
@@ -44,7 +57,7 @@ func Uint128_Cast__5(v uint8) Uint128 {
 	return Uint128{lo: uint64(v)}
 }
 
-// Uint128_Cast: func uint128(v int64) (uint128, bool)
+// Uint128_Cast: func uint128(v int64) uint128
 func Uint128_Cast__6(v int64) (out Uint128) {
 	if v < 0 {
 		return Uint128{hi: ^uint64(0), lo: uint64(v)}
diff --git a/cl/builtin.go b/cl/builtin.go
index c7bf9af1..150a5ae7 100644
--- a/cl/builtin.go
+++ b/cl/builtin.go
@@ -32,7 +32,7 @@ func initMathBig(pkg gox.PkgImporter, conf *gox.Config, big *gox.PkgRef) {
 	conf.UntypedBigFloat = big.Ref("UntypedFloat").Type().(*types.Named)
 }
 
-func initBuiltin(pkg gox.PkgImporter, builtin *types.Package, fmt, big, buil *gox.PkgRef) {
+func initBuiltin(pkg gox.PkgImporter, builtin *types.Package, fmt, big, ng, buil *gox.PkgRef) {
 	scope := builtin.Scope()
 	typs := []string{"int", "rat", "float"}
 	for _, typ := range typs {
@@ -49,6 +49,7 @@ func initBuiltin(pkg gox.PkgImporter, builtin *types.Package, fmt, big, buil *go
 		scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, fn, fmt.Ref(fnTitle)))
 	}
 	scope.Insert(gox.NewOverloadFunc(token.NoPos, builtin, "newRange", buil.Ref("NewRange__0")))
+	scope.Insert(types.NewTypeName(token.NoPos, builtin, "uint128", ng.Ref("Uint128").Type()))
 	scope.Insert(types.NewTypeName(token.NoPos, builtin, "any", gox.TyEmptyInterface))
 }
 
@@ -57,10 +58,11 @@ func newBuiltinDefault(pkg gox.PkgImporter, conf *gox.Config) *types.Package {
 	fmt := pkg.Import("fmt")
 	buil := pkg.Import("github.com/goplus/gop/builtin")
 	big := pkg.Import("github.com/goplus/gop/builtin/big")
+	ng := pkg.Import("github.com/goplus/gop/builtin/ng")
 	pkg.Import("strconv")
 	pkg.Import("strings")
 	initMathBig(pkg, conf, big)
-	initBuiltin(pkg, builtin, fmt, big, buil)
+	initBuiltin(pkg, builtin, fmt, big, ng, buil)
 	gox.InitBuiltin(pkg, builtin, conf)
 	return builtin
 }
diff --git a/go.mod b/go.mod
index 53c54dd0..e1fa44ca 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,7 @@ module github.com/goplus/gop
 go 1.16
 
 require (
-	github.com/goplus/gox v1.10.4
+	github.com/goplus/gox v1.10.5
 	github.com/qiniu/x v1.11.5
 	golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3
 	golang.org/x/tools v0.1.10
diff --git a/go.sum b/go.sum
index 2263b0ad..cd22fe97 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-github.com/goplus/gox v1.10.4 h1:u7YZnuJHcugzaoe7mkxqGG4xXciubGobDRjz//hdtyY=
-github.com/goplus/gox v1.10.4/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/KatM=
+github.com/goplus/gox v1.10.5 h1:Ztgx1nyJ1R52VeeNmddX/JCmHHy+j4Ql7rmcbXXMD7A=
+github.com/goplus/gox v1.10.5/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/KatM=
 github.com/qiniu/x v1.11.5 h1:TYr5cl4g2yoHAZeDK4MTjKF6CMoG+IHlCDvvM5qym6U=
 github.com/qiniu/x v1.11.5/go.mod h1:03Ni9tj+N2h2aKnAz+6N0Xfl8FwMEDRC2PAlxekASDs=
 github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=


Test patch:
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 80698bff..125c8504 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -1192,6 +1192,19 @@ var x, y = 1, "Hi"
 `)
 }
 
+func TestUint128Add(t *testing.T) {
+	gopClTest(t, `
+var x, y uint128
+var z uint128 = x + y
+`, `package main
+
+import ng "github.com/goplus/gop/builtin/ng"
+
+var x, y ng.Uint128
+var z ng.Uint128 = x.Gop_Add__0(y)
+`)
+}
+
 func TestBigIntAdd(t *testing.T) {
 	gopClTest(t, `
 var x, y bigint
@@ -1219,6 +1232,17 @@ var x = big.Int_Init__1(big1.NewInt(1))
 `)
 }
 
+func TestUint128Lit(t *testing.T) {
+	gopClTest(t, `
+var x uint128 = 1
+`, `package main
+
+import ng "github.com/goplus/gop/builtin/ng"
+
+var x ng.Uint128 = ng.Uint128_Init__0(1)
+`)
+}
+
 func TestBigRatLit(t *testing.T) {
 	gopClTest(t, `
 var x = 1/2r