Repository: goplus/gop
Pull request #2116 (2025-02-22 14:50:57)

#2115 type as builtin func
#2115


Patch:
diff --git a/cl/builtin.go b/cl/builtin.go
index c61a6c5c..93445a39 100644
--- a/cl/builtin.go
+++ b/cl/builtin.go
@@ -38,7 +38,7 @@ func initBuiltinFns(builtin *types.Package, scope *types.Scope, pkg gogen.PkgRef
 	}
 }
 
-func initBuiltin(_ *gogen.Package, builtin *types.Package, os, fmt, ng, iox, buil gogen.PkgRef) {
+func initBuiltin(_ *gogen.Package, builtin *types.Package, os, fmt, ng, iox, buil, reflect gogen.PkgRef) {
 	scope := builtin.Scope()
 	if ng.Types != nil {
 		typs := []string{"bigint", "bigrat", "bigfloat"}
@@ -68,6 +68,9 @@ func initBuiltin(_ *gogen.Package, builtin *types.Package, os, fmt, ng, iox, bui
 		})
 		scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "blines", iox.Ref("BLines")))
 	}
+	if reflect.Types != nil {
+		scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "type", reflect.Ref("TypeOf")))
+	}
 	if buil.Types != nil {
 		scope.Insert(gogen.NewOverloadFunc(token.NoPos, builtin, "newRange", buil.Ref("NewRange__0")))
 	}
@@ -78,6 +81,7 @@ func newBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package {
 	builtin := types.NewPackage("", "")
 	fmt := pkg.TryImport("fmt")
 	os := pkg.TryImport("os")
+	reflect := pkg.TryImport("reflect")
 	buil := pkg.TryImport("github.com/goplus/gop/builtin")
 	ng := pkg.TryImport("github.com/goplus/gop/builtin/ng")
 	iox := pkg.TryImport("github.com/goplus/gop/builtin/iox")
@@ -88,7 +92,7 @@ func newBuiltinDefault(pkg *gogen.Package, conf *gogen.Config) *types.Package {
 	if ng.Types != nil {
 		initMathBig(pkg, conf, ng)
 	}
-	initBuiltin(pkg, builtin, os, fmt, ng, iox, buil)
+	initBuiltin(pkg, builtin, os, fmt, ng, iox, buil, reflect)
 	gogen.InitBuiltin(pkg, builtin, conf)
 	if strx.Types != nil {
 		ti := pkg.BuiltinTI(types.Typ[types.String])
diff --git a/demo/gop-typeof/typeof.gop b/demo/gop-typeof/typeof.gop
new file mode 100644
index 00000000..bfb18c50
--- /dev/null
+++ b/demo/gop-typeof/typeof.gop
@@ -0,0 +1,1 @@
+echo type([1]), type("Hi")
diff --git a/parser/parser.go b/parser/parser.go
index dd7110dc..ee6b317c 100644
--- a/parser/parser.go
+++ b/parser/parser.go
@@ -1800,8 +1800,8 @@ func (p *parser) parseOperand(lhs, allowTuple, allowCmd bool) (x ast.Expr, isTup
 			break
 		}
 		fallthrough
-	case token.GOTO, token.BREAK, token.CONTINUE, token.FALLTHROUGH:
-		// token.RANGE, token.IMPORT, token.TYPE, token.SELECT, token.INTERFACE:
+	case token.GOTO, token.TYPE, token.BREAK, token.CONTINUE, token.FALLTHROUGH:
+		// token.RANGE, token.IMPORT, token.SELECT, token.INTERFACE:
 		// Go+: allow goto() as a function
 		p.tok = token.IDENT
 		x = p.parseIdent()
@@ -2372,7 +2372,8 @@ func (p *parser) checkCmd() bool {
 	case token.IDENT, token.DRARROW,
 		token.STRING, token.CSTRING, token.PYSTRING,
 		token.INT, token.FLOAT, token.IMAG, token.CHAR, token.RAT,
-		token.FUNC, token.GOTO, token.MAP, token.INTERFACE, token.CHAN, token.STRUCT, token.ENV:
+		token.FUNC, token.GOTO, token.TYPE, token.MAP, token.INTERFACE,
+		token.CHAN, token.STRUCT, token.ENV:
 		return true
 	case token.SUB, token.AND, token.MUL, token.ARROW, token.XOR, token.ADD:
 		oldtok, oldpos := p.tok, p.pos


Test patch:
diff --git a/parser/_testdata/typeof/parser.expect b/parser/_testdata/typeof/parser.expect
new file mode 100644
index 00000000..4f5ea586
--- /dev/null
+++ b/parser/_testdata/typeof/parser.expect
@@ -0,0 +1,38 @@
+package main
+
+file typeof.gop
+noEntrypoint
+ast.FuncDecl:
+  Name:
+    ast.Ident:
+      Name: main
+  Type:
+    ast.FuncType:
+      Params:
+        ast.FieldList:
+  Body:
+    ast.BlockStmt:
+      List:
+        ast.ExprStmt:
+          X:
+            ast.CallExpr:
+              Fun:
+                ast.Ident:
+                  Name: echo
+              Args:
+                ast.CallExpr:
+                  Fun:
+                    ast.Ident:
+                      Name: type
+                  Args:
+                    ast.BasicLit:
+                      Kind: INT
+                      Value: 1
+                ast.CallExpr:
+                  Fun:
+                    ast.Ident:
+                      Name: type
+                  Args:
+                    ast.BasicLit:
+                      Kind: STRING
+                      Value: "Hi"
diff --git a/parser/_testdata/typeof/typeof.gop b/parser/_testdata/typeof/typeof.gop
new file mode 100644
index 00000000..5363bca3
--- /dev/null
+++ b/parser/_testdata/typeof/typeof.gop
@@ -0,0 +1,1 @@
+echo type(1), type("Hi")