Repository: goplus/gop
Pull request #2008 (2024-11-07 08:02:40)

cl: compileNumberUnitLit


Patch:
diff --git a/cl/expr.go b/cl/expr.go
index dec29bf9..56ce39c1 100644
--- a/cl/expr.go
+++ b/cl/expr.go
@@ -813,12 +813,15 @@ func compileCallArgs(ctx *blockCtx, pfn *gogen.Element, fn *fnType, v *ast.CallE
 			if typetype {
 				return
 			}
+		case *ast.NumberUnitLit:
+			compileNumberUnitLit(ctx, expr, fn.arg(i, ellipsis))
 		default:
 			compileExpr(ctx, arg)
 		}
 	}
 	if needInferFunc {
-		typ, err := gogen.InferFunc(ctx.pkg, pfn, fn.sig, nil, ctx.cb.InternalStack().GetArgs(len(v.Args)), flags)
+		args := ctx.cb.InternalStack().GetArgs(len(v.Args))
+		typ, err := gogen.InferFunc(ctx.pkg, pfn, fn.sig, nil, args, flags)
 		if err != nil {
 			return err
 		}
@@ -977,6 +980,12 @@ func compileFuncLit(ctx *blockCtx, v *ast.FuncLit) {
 	}
 }
 
+func compileNumberUnitLit(ctx *blockCtx, v *ast.NumberUnitLit, expected types.Type) {
+	ctx.cb.ValWithUnit(
+		&goast.BasicLit{ValuePos: v.ValuePos, Kind: gotoken.Token(v.Kind), Value: v.Value},
+		expected, v.Unit)
+}
+
 func compileBasicLit(ctx *blockCtx, v *ast.BasicLit) {
 	cb := ctx.cb
 	switch kind := v.Kind; kind {
diff --git a/go.mod b/go.mod
index bf802983..f3c2d9ca 100644
--- a/go.mod
+++ b/go.mod
@@ -3,8 +3,8 @@ module github.com/goplus/gop
 go 1.18
 
 require (
+	github.com/goplus/gogen v1.16.2-0.20241107075831-8fd274c85e06
 	github.com/fsnotify/fsnotify v1.8.0
-	github.com/goplus/gogen v1.16.1
 	github.com/goplus/llgo v0.9.7
 	github.com/goplus/mod v0.13.12
 	github.com/qiniu/x v1.13.10
diff --git a/go.sum b/go.sum
index 579abc97..b3a5c491 100644
--- a/go.sum
+++ b/go.sum
@@ -1,7 +1,7 @@
+github.com/goplus/gogen v1.16.2-0.20241107075831-8fd274c85e06 h1:TZhHNYHia8bP8JoTO5utGWFTLBp2JrdyWukX/tnA/n4=
+github.com/goplus/gogen v1.16.2-0.20241107075831-8fd274c85e06/go.mod h1:6TQYbabXDF9LCdDkOOzHmfg1R4ENfXQ3XpHa9RhTSD8=
 github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
 github.com/fsnotify/fsnotify v1.8.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
-github.com/goplus/gogen v1.16.1 h1:f55LIfyTO5GcXxCfwXKqLKiQV3AORvn9PSGCncWvB1A=
-github.com/goplus/gogen v1.16.1/go.mod h1:6TQYbabXDF9LCdDkOOzHmfg1R4ENfXQ3XpHa9RhTSD8=
 github.com/goplus/llgo v0.9.7 h1:LRF2Fq9ts4QrVxOPZufexalbIoJ1oiBERjCWQ45wxbg=
 github.com/goplus/llgo v0.9.7/go.mod h1:5Fs+08NslqofJ7xtOiIXugkurYOoQvY02ZkFNWA1uEI=
 github.com/goplus/mod v0.13.12 h1:Trwk6j3i9VvBuW6/9ZxmkoFlEL2v3HKQu0Na1c6DAdw=
diff --git a/scanner/scanner.go b/scanner/scanner.go
index 4d4ff8cc..3842de77 100644
--- a/scanner/scanner.go
+++ b/scanner/scanner.go
@@ -510,7 +510,7 @@ func (s *Scanner) scanNumber() (token.Token, string) {
 	} else if s.ch == 'r' {
 		tok = token.RAT
 		s.next()
-	} else if s.ch >= 'a' && s.ch <= 'z' || s.ch >= 'A' && s.ch <= 'Z' {
+	} else if isLetter(s.ch) {
 		s.needUnit = true
 	}
 


Test patch:
diff --git a/cl/_testgop/matrix/in.gop b/cl/_testgop/_matrix/in.gop
similarity index 100%
rename from cl/_testgop/matrix/in.gop
rename to cl/_testgop/_matrix/in.gop
diff --git a/cl/_testgop/unit/in.gop b/cl/_testgop/unit/in.gop
new file mode 100644
index 00000000..e4977a1c
--- /dev/null
+++ b/cl/_testgop/unit/in.gop
@@ -0,0 +1,5 @@
+import "time"
+
+func Wait(time.Duration) {}
+
+wait 0.5µs
diff --git a/cl/_testgop/unit/out.go b/cl/_testgop/unit/out.go
new file mode 100644
index 00000000..d55d250c
--- /dev/null
+++ b/cl/_testgop/unit/out.go
@@ -0,0 +1,9 @@
+package main
+
+import "time"
+
+func Wait(time.Duration) {
+}
+func main() {
+	Wait(500)
+}
diff --git a/cl/compile_testdir_test.go b/cl/compile_testdir_test.go
index 122cf720..f41cd71c 100644
--- a/cl/compile_testdir_test.go
+++ b/cl/compile_testdir_test.go
@@ -22,6 +22,10 @@ import (
 	"github.com/goplus/gop/cl/cltest"
 )
 
+func TestTestgop(t *testing.T) {
+	cltest.FromDir(t, "", "./_testgop")
+}
+
 func TestTestc(t *testing.T) {
 	cltest.FromDir(t, "", "./_testc")
 }
diff --git a/parser/_testdata/unit/parser.expect b/parser/_testdata/unit/parser.expect
index e71a0367..5f01ff09 100644
--- a/parser/_testdata/unit/parser.expect
+++ b/parser/_testdata/unit/parser.expect
@@ -18,9 +18,9 @@ ast.FuncDecl:
             ast.CallExpr:
               Fun:
                 ast.Ident:
-                  Name: step
+                  Name: wait
               Args:
                 ast.NumberUnitLit:
                   Kind: INT
                   Value: 1
-                  Unit: cm
+                  Unit: µs
diff --git a/parser/_testdata/unit/step.gop b/parser/_testdata/unit/step.gop
index 5588cd87..b0f1553d 100644
--- a/parser/_testdata/unit/step.gop
+++ b/parser/_testdata/unit/step.gop
@@ -1,1 +1,1 @@
-step 1cm
+wait 1µs