diff --git a/_demo/go/gotoken/main.go b/_demo/go/gotoken/main.go new file mode 100644 index 00000000..219384ee --- /dev/null +++ b/_demo/go/gotoken/main.go @@ -0,0 +1,242 @@ +package main + +import ( + "fmt" + "go/token" +) + +func main() { + fmt.Println("=== Comprehensive go/token Package Demo ===\n") + + testPos() + testToken() + testFileSet() + testFile() + testPosition() + testTokenPrecedence() + testTokenKeywords() + + fmt.Println("\n=== All go/token tests completed successfully! ===") +} + +func testPos() { + fmt.Println("=== Test Pos ===") + + pos1 := token.Pos(100) + pos2 := token.Pos(200) + + fmt.Printf("Pos1: %d, Pos2: %d\n", pos1, pos2) + fmt.Printf("Pos1.IsValid(): %v\n", pos1.IsValid()) + + noPos := token.NoPos + fmt.Printf("NoPos: %d, IsValid: %v\n", noPos, noPos.IsValid()) + + fmt.Println("SUCCESS: Pos operations work correctly\n") +} + +func testToken() { + fmt.Println("=== Test Token Types ===") + + tokens := []token.Token{ + token.ILLEGAL, + token.EOF, + token.COMMENT, + token.IDENT, + token.INT, + token.FLOAT, + token.IMAG, + token.CHAR, + token.STRING, + token.ADD, + token.SUB, + token.MUL, + token.QUO, + token.REM, + token.AND, + token.OR, + token.XOR, + token.SHL, + token.SHR, + token.AND_NOT, + token.ADD_ASSIGN, + token.SUB_ASSIGN, + token.MUL_ASSIGN, + token.QUO_ASSIGN, + token.REM_ASSIGN, + token.AND_ASSIGN, + token.OR_ASSIGN, + token.XOR_ASSIGN, + token.SHL_ASSIGN, + token.SHR_ASSIGN, + token.AND_NOT_ASSIGN, + token.LAND, + token.LOR, + token.ARROW, + token.INC, + token.DEC, + token.EQL, + token.LSS, + token.GTR, + token.ASSIGN, + token.NOT, + token.NEQ, + token.LEQ, + token.GEQ, + token.DEFINE, + token.ELLIPSIS, + token.LPAREN, + token.LBRACK, + token.LBRACE, + token.COMMA, + token.PERIOD, + token.RPAREN, + token.RBRACK, + token.RBRACE, + token.SEMICOLON, + token.COLON, + } + + for _, tok := range tokens { + fmt.Printf("Token: %s (String: %q)\n", tok, tok.String()) + } + + fmt.Println("SUCCESS: Token types work correctly\n") +} + +func testTokenKeywords() { + fmt.Println("=== Test Keywords ===") + + keywords := []token.Token{ + token.BREAK, + token.CASE, + token.CHAN, + token.CONST, + token.CONTINUE, + token.DEFAULT, + token.DEFER, + token.ELSE, + token.FALLTHROUGH, + token.FOR, + token.FUNC, + token.GO, + token.GOTO, + token.IF, + token.IMPORT, + token.INTERFACE, + token.MAP, + token.PACKAGE, + token.RANGE, + token.RETURN, + token.SELECT, + token.STRUCT, + token.SWITCH, + token.TYPE, + token.VAR, + } + + for _, kw := range keywords { + fmt.Printf("Keyword: %s, IsKeyword: %v\n", kw, kw.IsKeyword()) + } + + fmt.Println("SUCCESS: Keyword checks work correctly\n") +} + +func testTokenPrecedence() { + fmt.Println("=== Test Token Precedence ===") + + operators := []token.Token{ + token.ADD, + token.SUB, + token.MUL, + token.QUO, + token.REM, + token.LAND, + token.LOR, + token.EQL, + token.LSS, + token.GTR, + } + + for _, op := range operators { + fmt.Printf("Operator: %s, Precedence: %d\n", op, op.Precedence()) + } + + fmt.Println("SUCCESS: Precedence operations work correctly\n") +} + +func testFileSet() { + fmt.Println("=== Test FileSet ===") + + fset := token.NewFileSet() + + file1 := fset.AddFile("file1.go", -1, 1000) + file2 := fset.AddFile("file2.go", -1, 2000) + + fmt.Printf("Added file1: %s, Base: %d, Size: %d\n", file1.Name(), file1.Base(), file1.Size()) + fmt.Printf("Added file2: %s, Base: %d, Size: %d\n", file2.Name(), file2.Base(), file2.Size()) + + pos := file1.Pos(100) + retrievedFile := fset.File(pos) + if retrievedFile != file1 { + panic("FileSet.File failed to retrieve correct file") + } + + position := fset.Position(pos) + fmt.Printf("Position at offset 100: %s\n", position) + + fmt.Println("SUCCESS: FileSet operations work correctly\n") +} + +func testFile() { + fmt.Println("=== Test File ===") + + fset := token.NewFileSet() + file := fset.AddFile("test.go", -1, 1000) + + file.AddLine(0) + file.AddLine(50) + file.AddLine(100) + + fmt.Printf("File name: %s\n", file.Name()) + fmt.Printf("File base: %d\n", file.Base()) + fmt.Printf("File size: %d\n", file.Size()) + fmt.Printf("File line count: %d\n", file.LineCount()) + + pos := file.Pos(50) + fmt.Printf("Pos at offset 50: %d\n", pos) + + offset := file.Offset(pos) + fmt.Printf("Offset of pos: %d\n", offset) + + line := file.Line(pos) + fmt.Printf("Line number at pos: %d\n", line) + + lineStart := file.LineStart(2) + fmt.Printf("Line 2 starts at pos: %d\n", lineStart) + + position := file.Position(pos) + fmt.Printf("Position: %s\n", position) + + fmt.Println("SUCCESS: File operations work correctly\n") +} + +func testPosition() { + fmt.Println("=== Test Position ===") + + pos := token.Position{ + Filename: "test.go", + Offset: 100, + Line: 5, + Column: 10, + } + + fmt.Printf("Position: %s\n", pos.String()) + fmt.Printf("Filename: %s, Line: %d, Column: %d, Offset: %d\n", + pos.Filename, pos.Line, pos.Column, pos.Offset) + fmt.Printf("IsValid: %v\n", pos.IsValid()) + + invalidPos := token.Position{} + fmt.Printf("Invalid position IsValid: %v\n", invalidPos.IsValid()) + + fmt.Println("SUCCESS: Position operations work correctly\n") +} diff --git a/_demo/go/gotypes/main.go b/_demo/go/gotypes/main.go new file mode 100644 index 00000000..0786c796 --- /dev/null +++ b/_demo/go/gotypes/main.go @@ -0,0 +1,260 @@ +package main + +import ( + "fmt" + "go/ast" + "go/token" + "go/types" +) + +func main() { + fmt.Println("=== Comprehensive go/types Package Demo ===\n") + + testBasicTypes() + testObjects() + testScope() + testPackage() + testNamed() + testInterface() + testStruct() + testSignature() + testTuple() + testArray() + testSlice() + testPointer() + testMap() + testChan() + + fmt.Println("\n=== All go/types tests completed successfully! ===") +} + +func testBasicTypes() { + fmt.Println("=== Test Basic Types ===") + + intType := types.Typ[types.Int] + fmt.Printf("Int type: %v, Kind: %v\n", intType, intType.Kind()) + + stringType := types.Typ[types.String] + fmt.Printf("String type: %v, Kind: %v\n", stringType, stringType.Kind()) + + boolType := types.Typ[types.Bool] + fmt.Printf("Bool type: %v, Kind: %v\n", boolType, boolType.Kind()) + + float64Type := types.Typ[types.Float64] + fmt.Printf("Float64 type: %v, Kind: %v\n", float64Type, float64Type.Kind()) + + fmt.Println("SUCCESS: Basic types work correctly\n") +} + +func testObjects() { + fmt.Println("=== Test Objects (Var, Const, Func, TypeName) ===") + + varObj := types.NewVar(token.NoPos, nil, "x", types.Typ[types.Int]) + fmt.Printf("Var: Name=%s, Type=%v\n", varObj.Name(), varObj.Type()) + + constObj := types.NewConst(token.NoPos, nil, "pi", types.Typ[types.Float64], nil) + fmt.Printf("Const: Name=%s, Type=%v\n", constObj.Name(), constObj.Type()) + + sig := types.NewSignatureType(nil, nil, nil, nil, nil, false) + funcObj := types.NewFunc(token.NoPos, nil, "foo", sig) + fmt.Printf("Func: Name=%s, Type=%v\n", funcObj.Name(), funcObj.Type()) + + typeObj := types.NewTypeName(token.NoPos, nil, "MyInt", types.Typ[types.Int]) + fmt.Printf("TypeName: Name=%s, Type=%v\n", typeObj.Name(), typeObj.Type()) + + var obj types.Object = varObj + if obj.Name() != "x" { + panic("Object interface conversion failed") + } + fmt.Println("SUCCESS: Object interface works correctly\n") +} + +func testScope() { + fmt.Println("=== Test Scope ===") + + scope := types.NewScope(nil, 0, 0, "test") + obj := types.NewVar(0, nil, "x", types.Typ[types.Int]) + + scope.Insert(obj) + result := scope.Lookup("x") + if result != obj { + panic("Scope.Lookup failed") + } + + names := scope.Names() + if len(names) != 1 || names[0] != "x" { + panic("Scope.Names failed") + } + + num := scope.Len() + if num != 1 { + panic("Scope.Len failed") + } + + fmt.Printf("Scope contains %d object(s): %v\n", num, names) + fmt.Println("SUCCESS: Scope operations work correctly\n") +} + +func testPackage() { + fmt.Println("=== Test Package ===") + + pkg := types.NewPackage("example.com/test", "test") + fmt.Printf("Package: Path=%s, Name=%s\n", pkg.Path(), pkg.Name()) + + scope := pkg.Scope() + if scope == nil { + panic("Package.Scope returned nil") + } + + varObj := types.NewVar(token.NoPos, pkg, "x", types.Typ[types.Int]) + scope.Insert(varObj) + + result := pkg.Scope().Lookup("x") + if result != varObj { + panic("Package scope lookup failed") + } + + fmt.Println("SUCCESS: Package operations work correctly\n") +} + +func testNamed() { + fmt.Println("=== Test Named Types ===") + + pkg := types.NewPackage("example.com/test", "test") + typeName := types.NewTypeName(token.NoPos, pkg, "MyInt", nil) + named := types.NewNamed(typeName, types.Typ[types.Int], nil) + + fmt.Printf("Named type: %v, Underlying: %v\n", named, named.Underlying()) + + if named.Obj() != typeName { + panic("Named.Obj failed") + } + + fmt.Println("SUCCESS: Named type operations work correctly\n") +} + +func testInterface() { + fmt.Println("=== Test Interface ===") + + pkg := types.NewPackage("example.com/test", "test") + + posMethod := types.NewFunc(token.NoPos, pkg, "Pos", types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(types.NewVar(0, pkg, "", types.Typ[types.Int])), false)) + endMethod := types.NewFunc(token.NoPos, pkg, "End", types.NewSignatureType(nil, nil, nil, nil, types.NewTuple(types.NewVar(0, pkg, "", types.Typ[types.Int])), false)) + + methods := []*types.Func{posMethod, endMethod} + iface := types.NewInterfaceType(methods, nil) + iface.Complete() + + fmt.Printf("Interface with %d methods\n", iface.NumMethods()) + + method := iface.Method(0) + fmt.Printf("Method 0: %s\n", method.Name()) + + fmt.Println("SUCCESS: Interface operations work correctly\n") +} + +func testStruct() { + fmt.Println("=== Test Struct ===") + + fields := []*types.Var{ + types.NewField(token.NoPos, nil, "X", types.Typ[types.Int], false), + types.NewField(token.NoPos, nil, "Y", types.Typ[types.String], false), + } + + structType := types.NewStruct(fields, nil) + fmt.Printf("Struct with %d fields\n", structType.NumFields()) + + field0 := structType.Field(0) + fmt.Printf("Field 0: Name=%s, Type=%v\n", field0.Name(), field0.Type()) + + fmt.Println("SUCCESS: Struct operations work correctly\n") +} + +func testSignature() { + fmt.Println("=== Test Signature ===") + + params := types.NewTuple( + types.NewVar(token.NoPos, nil, "x", types.Typ[types.Int]), + types.NewVar(token.NoPos, nil, "y", types.Typ[types.String]), + ) + + results := types.NewTuple( + types.NewVar(token.NoPos, nil, "", types.Typ[types.Bool]), + ) + + sig := types.NewSignatureType(nil, nil, nil, params, results, false) + + fmt.Printf("Signature: %d params, %d results\n", sig.Params().Len(), sig.Results().Len()) + + param0 := sig.Params().At(0) + fmt.Printf("Param 0: Name=%s, Type=%v\n", param0.Name(), param0.Type()) + + fmt.Println("SUCCESS: Signature operations work correctly\n") +} + +func testTuple() { + fmt.Println("=== Test Tuple ===") + + tuple := types.NewTuple( + types.NewVar(token.NoPos, nil, "a", types.Typ[types.Int]), + types.NewVar(token.NoPos, nil, "b", types.Typ[types.String]), + ) + + fmt.Printf("Tuple length: %d\n", tuple.Len()) + + var0 := tuple.At(0) + fmt.Printf("Element 0: Name=%s, Type=%v\n", var0.Name(), var0.Type()) + + fmt.Println("SUCCESS: Tuple operations work correctly\n") +} + +func testArray() { + fmt.Println("=== Test Array ===") + + arrayType := types.NewArray(types.Typ[types.Int], 10) + fmt.Printf("Array type: %v, Elem: %v, Len: %d\n", arrayType, arrayType.Elem(), arrayType.Len()) + + fmt.Println("SUCCESS: Array operations work correctly\n") +} + +func testSlice() { + fmt.Println("=== Test Slice ===") + + sliceType := types.NewSlice(types.Typ[types.String]) + fmt.Printf("Slice type: %v, Elem: %v\n", sliceType, sliceType.Elem()) + + fmt.Println("SUCCESS: Slice operations work correctly\n") +} + +func testPointer() { + fmt.Println("=== Test Pointer ===") + + ptrType := types.NewPointer(types.Typ[types.Int]) + fmt.Printf("Pointer type: %v, Elem: %v\n", ptrType, ptrType.Elem()) + + fmt.Println("SUCCESS: Pointer operations work correctly\n") +} + +func testMap() { + fmt.Println("=== Test Map ===") + + mapType := types.NewMap(types.Typ[types.String], types.Typ[types.Int]) + fmt.Printf("Map type: %v, Key: %v, Elem: %v\n", mapType, mapType.Key(), mapType.Elem()) + + fmt.Println("SUCCESS: Map operations work correctly\n") +} + +func testChan() { + fmt.Println("=== Test Chan ===") + + chanType := types.NewChan(types.SendRecv, types.Typ[types.Int]) + fmt.Printf("Chan type: %v, Dir: %v, Elem: %v\n", chanType, chanType.Dir(), chanType.Elem()) + + sendChan := types.NewChan(types.SendOnly, types.Typ[types.String]) + fmt.Printf("SendOnly chan: %v, Dir: %v\n", sendChan, sendChan.Dir()) + + recvChan := types.NewChan(types.RecvOnly, types.Typ[types.Bool]) + fmt.Printf("RecvOnly chan: %v, Dir: %v\n", recvChan, recvChan.Dir()) + + fmt.Println("SUCCESS: Chan operations work correctly\n") +} diff --git a/_demo/go/issue1370_case1/main.go b/_demo/go/issue1370_case1/main.go deleted file mode 100644 index a5ef8b99..00000000 --- a/_demo/go/issue1370_case1/main.go +++ /dev/null @@ -1,58 +0,0 @@ -package main - -import ( - "fmt" - "go/types" -) - -func main() { - testScopeInsertLookup() - testScopeLookupNonExistent() - testScopeMultipleObjects() -} - -func testScopeInsertLookup() { - fmt.Println("=== Test Scope Insert and Lookup ===") - scope := types.NewScope(nil, 0, 0, "test") - obj := types.NewVar(0, nil, "x", types.Typ[types.Int]) - - scope.Insert(obj) - - result := scope.Lookup("x") - if result != obj { - panic(fmt.Sprintf("Lookup returned wrong object: expected %v, got %v", obj, result)) - } - fmt.Println("SUCCESS: Scope.Insert and Lookup work correctly") -} - -func testScopeLookupNonExistent() { - fmt.Println("\n=== Test Scope Lookup Non-Existent ===") - scope := types.NewScope(nil, 0, 0, "test") - - result := scope.Lookup("nonexistent") - if result != nil { - panic(fmt.Sprintf("Lookup should return nil for non-existent name, got %v", result)) - } - fmt.Println("SUCCESS: Lookup returns nil for non-existent name") -} - -func testScopeMultipleObjects() { - fmt.Println("\n=== Test Scope Multiple Objects ===") - scope := types.NewScope(nil, 0, 0, "test") - obj1 := types.NewVar(0, nil, "x", types.Typ[types.Int]) - obj2 := types.NewVar(0, nil, "y", types.Typ[types.String]) - - scope.Insert(obj1) - scope.Insert(obj2) - - result1 := scope.Lookup("x") - if result1 != obj1 { - panic(fmt.Sprintf("Lookup('x') returned wrong object: expected %v, got %v", obj1, result1)) - } - - result2 := scope.Lookup("y") - if result2 != obj2 { - panic(fmt.Sprintf("Lookup('y') returned wrong object: expected %v, got %v", obj2, result2)) - } - fmt.Println("SUCCESS: Multiple objects handled correctly") -} diff --git a/_demo/go/issue1370_case2/main.go b/_demo/go/issue1370_case2/main.go deleted file mode 100644 index 02a8acce..00000000 --- a/_demo/go/issue1370_case2/main.go +++ /dev/null @@ -1,63 +0,0 @@ -package main - -import ( - "fmt" - "go/ast" - "go/token" -) - -func main() { - testIdentToExpr() - testExprPos() - testExprEnd() -} - -func testIdentToExpr() { - fmt.Println("=== Test Ident to Expr Conversion ===") - ident := &ast.Ident{ - NamePos: token.Pos(1), - Name: "foo", - } - - var expr ast.Expr = ident - if expr == nil { - panic("Interface conversion failed: expr is nil") - } - fmt.Printf("Identifier: %s\n", ident.Name) - fmt.Println("SUCCESS: ast.Expr interface conversion works correctly") -} - -func testExprPos() { - fmt.Println("\n=== Test Expr.Pos() Method ===") - expectedPos := token.Pos(42) - ident := &ast.Ident{ - NamePos: expectedPos, - Name: "bar", - } - - var expr ast.Expr = ident - actualPos := expr.Pos() - if actualPos != expectedPos { - panic(fmt.Sprintf("Pos() returned wrong position: expected %d, got %d", expectedPos, actualPos)) - } - fmt.Printf("Position: %d\n", actualPos) - fmt.Println("SUCCESS: Expr.Pos() returns correct position") -} - -func testExprEnd() { - fmt.Println("\n=== Test Expr.End() Method ===") - name := "testvar" - ident := &ast.Ident{ - NamePos: token.Pos(10), - Name: name, - } - - var expr ast.Expr = ident - expectedEnd := token.Pos(10 + len(name)) - actualEnd := expr.End() - if actualEnd != expectedEnd { - panic(fmt.Sprintf("End() returned wrong position: expected %d, got %d", expectedEnd, actualEnd)) - } - fmt.Printf("End position: %d\n", actualEnd) - fmt.Println("SUCCESS: Expr.End() returns correct position") -}