Co-authored-by: xgopilot <noreply@goplus.org>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
luoliwoshang
2025-11-06 20:54:41 +08:00
parent 41b403aef7
commit 4e374a99ff
7 changed files with 1305 additions and 0 deletions

356
_demo/go/gotoken/main.go Normal file
View File

@@ -0,0 +1,356 @@
package main
import (
"fmt"
"go/token"
)
func main() {
testPos()
testToken()
testFileSet()
testFile()
testPosition()
testTokenPrecedence()
testTokenKeywords()
testUtilityFunctions()
}
func testPos() {
fmt.Println("=== Test Pos ===")
pos1 := token.Pos(100)
pos2 := token.Pos(200)
if pos1 != 100 {
panic(fmt.Sprintf("Expected pos1 to be 100, got %d", pos1))
}
if pos2 != 200 {
panic(fmt.Sprintf("Expected pos2 to be 200, got %d", pos2))
}
fmt.Printf("Pos1: %d, Pos2: %d\n", pos1, pos2)
if !pos1.IsValid() {
panic("Expected pos1.IsValid() to be true")
}
fmt.Printf("Pos1.IsValid(): %v\n", pos1.IsValid())
noPos := token.NoPos
if noPos != 0 {
panic(fmt.Sprintf("Expected NoPos to be 0, got %d", noPos))
}
if noPos.IsValid() {
panic("Expected NoPos.IsValid() to be false")
}
fmt.Printf("NoPos: %d, IsValid: %v\n", noPos, noPos.IsValid())
fmt.Println("SUCCESS: Pos operations work correctly\n")
}
func testToken() {
fmt.Println("\n=== Test Token Types ===")
expectedStrings := map[token.Token]string{
token.ADD: "+",
token.SUB: "-",
token.MUL: "*",
token.QUO: "/",
token.LPAREN: "(",
token.RPAREN: ")",
token.EQL: "==",
token.NEQ: "!=",
}
for tok, expected := range expectedStrings {
if tok.String() != expected {
panic(fmt.Sprintf("Expected %v.String() to be %q, got %q", tok, expected, tok.String()))
}
}
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("\n=== 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 {
if !kw.IsKeyword() {
panic(fmt.Sprintf("Expected %s to be a keyword", kw))
}
fmt.Printf("Keyword: %s, IsKeyword: %v\n", kw, kw.IsKeyword())
}
if token.ADD.IsKeyword() {
panic("Expected ADD operator to not be a keyword")
}
if token.IDENT.IsKeyword() {
panic("Expected IDENT token to not be a keyword")
}
fmt.Println("SUCCESS: Keyword checks work correctly\n")
}
func testTokenPrecedence() {
fmt.Println("\n=== Test Token Precedence ===")
if token.MUL.Precedence() <= token.ADD.Precedence() {
panic("Expected MUL to have higher precedence than ADD")
}
if token.LAND.Precedence() <= token.LOR.Precedence() {
panic("Expected LAND to have higher precedence than LOR")
}
if token.MUL.Precedence() != token.QUO.Precedence() {
panic("Expected MUL and QUO to have same precedence")
}
if token.ADD.Precedence() != token.SUB.Precedence() {
panic("Expected ADD and SUB to have same 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("\n=== 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("\n=== Test File ===")
fset := token.NewFileSet()
file := fset.AddFile("test.go", -1, 1000)
if file.Name() != "test.go" {
panic(fmt.Sprintf("Expected file name to be 'test.go', got %q", file.Name()))
}
if file.Size() != 1000 {
panic(fmt.Sprintf("Expected file size to be 1000, got %d", file.Size()))
}
file.AddLine(0)
file.AddLine(50)
file.AddLine(100)
if file.LineCount() != 3 {
panic(fmt.Sprintf("Expected line count to be 3, got %d", file.LineCount()))
}
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)
if offset != 50 {
panic(fmt.Sprintf("Expected offset to be 50, got %d", offset))
}
fmt.Printf("Offset of pos: %d\n", offset)
line := file.Line(pos)
if line != 2 {
panic(fmt.Sprintf("Expected line to be 2, got %d", line))
}
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("\n=== Test Position ===" )
pos := token.Position{
Filename: "test.go",
Offset: 100,
Line: 5,
Column: 10,
}
if !pos.IsValid() {
panic("Expected valid position to be valid")
}
if pos.Filename != "test.go" {
panic(fmt.Sprintf("Expected filename to be 'test.go', got %q", pos.Filename))
}
if pos.Line != 5 {
panic(fmt.Sprintf("Expected line to be 5, got %d", pos.Line))
}
if pos.Column != 10 {
panic(fmt.Sprintf("Expected column to be 10, got %d", pos.Column))
}
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{}
if invalidPos.IsValid() {
panic("Expected empty position to be invalid")
}
fmt.Printf("Invalid position IsValid: %v\n", invalidPos.IsValid())
fmt.Println("SUCCESS: Position operations work correctly\n")
}
func testUtilityFunctions() {
fmt.Println("\n=== Test Utility Functions ===")
fmt.Printf("IsExported(\"Foo\"): %v\n", token.IsExported("Foo"))
fmt.Printf("IsExported(\"foo\"): %v\n", token.IsExported("foo"))
fmt.Printf("IsExported(\"_foo\"): %v\n", token.IsExported("_foo"))
fmt.Printf("IsIdentifier(\"foo\"): %v\n", token.IsIdentifier("foo"))
fmt.Printf("IsIdentifier(\"foo123\"): %v\n", token.IsIdentifier("foo123"))
fmt.Printf("IsIdentifier(\"123foo\"): %v\n", token.IsIdentifier("123foo"))
fmt.Printf("IsIdentifier(\"foo-bar\"): %v\n", token.IsIdentifier("foo-bar"))
fmt.Printf("IsKeyword(\"func\"): %v\n", token.IsKeyword("func"))
fmt.Printf("IsKeyword(\"if\"): %v\n", token.IsKeyword("if"))
fmt.Printf("IsKeyword(\"foo\"): %v\n", token.IsKeyword("foo"))
lookupFunc := token.Lookup("func")
fmt.Printf("Lookup(\"func\"): %s\n", lookupFunc)
lookupIdent := token.Lookup("myVar")
fmt.Printf("Lookup(\"myVar\"): %s\n", lookupIdent)
lookupFor := token.Lookup("for")
fmt.Printf("Lookup(\"for\"): %s\n", lookupFor)
fmt.Println("SUCCESS: Utility functions work correctly\n")
}

488
_demo/go/gotypes/main.go Normal file
View File

@@ -0,0 +1,488 @@
package main
import (
"fmt"
"go/token"
"go/types"
)
func main() {
testBasicTypes()
testObjects()
testScope()
testPackage()
testNamed()
testInterface()
testStruct()
testSignature()
testTuple()
testArray()
testSlice()
testPointer()
testMap()
testChan()
testTypeComparison()
testTypeChecking()
testStringFunctions()
testLookupFunctions()
testUtilityFunctions()
}
func testBasicTypes() {
fmt.Println("=== Test Basic Types ===")
intType := types.Typ[types.Int]
fmt.Printf("Int type: %v, Kind: %v\n", intType, intType.Kind())
if intType.Kind() != types.Int {
panic(fmt.Sprintf("Int type kind mismatch: expected %v, got %v", types.Int, intType.Kind()))
}
stringType := types.Typ[types.String]
fmt.Printf("String type: %v, Kind: %v\n", stringType, stringType.Kind())
if stringType.Kind() != types.String {
panic(fmt.Sprintf("String type kind mismatch: expected %v, got %v", types.String, stringType.Kind()))
}
boolType := types.Typ[types.Bool]
fmt.Printf("Bool type: %v, Kind: %v\n", boolType, boolType.Kind())
if boolType.Kind() != types.Bool {
panic(fmt.Sprintf("Bool type kind mismatch: expected %v, got %v", types.Bool, boolType.Kind()))
}
float64Type := types.Typ[types.Float64]
fmt.Printf("Float64 type: %v, Kind: %v\n", float64Type, float64Type.Kind())
if float64Type.Kind() != types.Float64 {
panic(fmt.Sprintf("Float64 type kind mismatch: expected %v, got %v", types.Float64, float64Type.Kind()))
}
fmt.Println("SUCCESS: Basic types work correctly\n")
}
func testObjects() {
fmt.Println("\n=== 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())
if varObj.Name() != "x" {
panic(fmt.Sprintf("Var name mismatch: expected x, got %s", varObj.Name()))
}
if varObj.Type() != types.Typ[types.Int] {
panic(fmt.Sprintf("Var type mismatch: expected int, got %v", 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())
if constObj.Name() != "pi" {
panic(fmt.Sprintf("Const name mismatch: expected pi, got %s", constObj.Name()))
}
if constObj.Type() != types.Typ[types.Float64] {
panic(fmt.Sprintf("Const type mismatch: expected float64, got %v", 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())
if funcObj.Name() != "foo" {
panic(fmt.Sprintf("Func name mismatch: expected foo, got %s", funcObj.Name()))
}
typeObj := types.NewTypeName(token.NoPos, nil, "MyInt", types.Typ[types.Int])
fmt.Printf("TypeName: Name=%s, Type=%v\n", typeObj.Name(), typeObj.Type())
if typeObj.Name() != "MyInt" {
panic(fmt.Sprintf("TypeName name mismatch: expected MyInt, got %s", typeObj.Name()))
}
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("\n=== 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("\n=== Test Package ===")
pkg := types.NewPackage("example.com/test", "test")
fmt.Printf("Package: Path=%s, Name=%s\n", pkg.Path(), pkg.Name())
if pkg.Path() != "example.com/test" {
panic(fmt.Sprintf("Package path mismatch: expected example.com/test, got %s", pkg.Path()))
}
if pkg.Name() != "test" {
panic(fmt.Sprintf("Package name mismatch: expected test, got %s", 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("\n=== 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("\n=== 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())
if iface.NumMethods() != 2 {
panic(fmt.Sprintf("Interface method count mismatch: expected 2, got %d", iface.NumMethods()))
}
method := iface.Method(0)
fmt.Printf("Method 0: %s\n", method.Name())
if method.Name() != "End" && method.Name() != "Pos" {
panic(fmt.Sprintf("Unexpected method name: %s", method.Name()))
}
fmt.Println("SUCCESS: Interface operations work correctly\n")
}
func testStruct() {
fmt.Println("\n=== 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())
if structType.NumFields() != 2 {
panic(fmt.Sprintf("Struct field count mismatch: expected 2, got %d", structType.NumFields()))
}
field0 := structType.Field(0)
fmt.Printf("Field 0: Name=%s, Type=%v\n", field0.Name(), field0.Type())
if field0.Name() != "X" {
panic(fmt.Sprintf("Field 0 name mismatch: expected X, got %s", field0.Name()))
}
if field0.Type() != types.Typ[types.Int] {
panic(fmt.Sprintf("Field 0 type mismatch: expected int, got %v", field0.Type()))
}
fmt.Println("SUCCESS: Struct operations work correctly\n")
}
func testSignature() {
fmt.Println("\n=== 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())
if sig.Params().Len() != 2 {
panic(fmt.Sprintf("Signature param count mismatch: expected 2, got %d", sig.Params().Len()))
}
if sig.Results().Len() != 1 {
panic(fmt.Sprintf("Signature result count mismatch: expected 1, got %d", sig.Results().Len()))
}
param0 := sig.Params().At(0)
fmt.Printf("Param 0: Name=%s, Type=%v\n", param0.Name(), param0.Type())
if param0.Name() != "x" {
panic(fmt.Sprintf("Param 0 name mismatch: expected x, got %s", param0.Name()))
}
if param0.Type() != types.Typ[types.Int] {
panic(fmt.Sprintf("Param 0 type mismatch: expected int, got %v", param0.Type()))
}
fmt.Println("SUCCESS: Signature operations work correctly\n")
}
func testTuple() {
fmt.Println("\n=== 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())
if tuple.Len() != 2 {
panic(fmt.Sprintf("Tuple length mismatch: expected 2, got %d", tuple.Len()))
}
var0 := tuple.At(0)
fmt.Printf("Element 0: Name=%s, Type=%v\n", var0.Name(), var0.Type())
if var0.Name() != "a" {
panic(fmt.Sprintf("Tuple element 0 name mismatch: expected a, got %s", var0.Name()))
}
if var0.Type() != types.Typ[types.Int] {
panic(fmt.Sprintf("Tuple element 0 type mismatch: expected int, got %v", var0.Type()))
}
fmt.Println("SUCCESS: Tuple operations work correctly\n")
}
func testArray() {
fmt.Println("\n=== 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())
if arrayType.Len() != 10 {
panic(fmt.Sprintf("Array length mismatch: expected 10, got %d", arrayType.Len()))
}
if arrayType.Elem() != types.Typ[types.Int] {
panic(fmt.Sprintf("Array element type mismatch: expected int, got %v", arrayType.Elem()))
}
fmt.Println("SUCCESS: Array operations work correctly\n")
}
func testSlice() {
fmt.Println("\n=== Test Slice ===")
sliceType := types.NewSlice(types.Typ[types.String])
fmt.Printf("Slice type: %v, Elem: %v\n", sliceType, sliceType.Elem())
if sliceType.Elem() != types.Typ[types.String] {
panic(fmt.Sprintf("Slice element type mismatch: expected string, got %v", sliceType.Elem()))
}
fmt.Println("SUCCESS: Slice operations work correctly\n")
}
func testPointer() {
fmt.Println("\n=== Test Pointer ===")
ptrType := types.NewPointer(types.Typ[types.Int])
fmt.Printf("Pointer type: %v, Elem: %v\n", ptrType, ptrType.Elem())
if ptrType.Elem() != types.Typ[types.Int] {
panic(fmt.Sprintf("Pointer element type mismatch: expected int, got %v", ptrType.Elem()))
}
fmt.Println("SUCCESS: Pointer operations work correctly\n")
}
func testMap() {
fmt.Println("\n=== 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())
if mapType.Key() != types.Typ[types.String] {
panic(fmt.Sprintf("Map key type mismatch: expected string, got %v", mapType.Key()))
}
if mapType.Elem() != types.Typ[types.Int] {
panic(fmt.Sprintf("Map element type mismatch: expected int, got %v", mapType.Elem()))
}
fmt.Println("SUCCESS: Map operations work correctly\n")
}
func testChan() {
fmt.Println("\n=== 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())
if chanType.Dir() != types.SendRecv {
panic(fmt.Sprintf("Chan direction mismatch: expected SendRecv, got %v", chanType.Dir()))
}
if chanType.Elem() != types.Typ[types.Int] {
panic(fmt.Sprintf("Chan element type mismatch: expected int, got %v", chanType.Elem()))
}
sendChan := types.NewChan(types.SendOnly, types.Typ[types.String])
fmt.Printf("SendOnly chan: %v, Dir: %v\n", sendChan, sendChan.Dir())
if sendChan.Dir() != types.SendOnly {
panic(fmt.Sprintf("SendOnly chan direction mismatch: expected SendOnly, got %v", sendChan.Dir()))
}
recvChan := types.NewChan(types.RecvOnly, types.Typ[types.Bool])
fmt.Printf("RecvOnly chan: %v, Dir: %v\n", recvChan, recvChan.Dir())
if recvChan.Dir() != types.RecvOnly {
panic(fmt.Sprintf("RecvOnly chan direction mismatch: expected RecvOnly, got %v", recvChan.Dir()))
}
fmt.Println("SUCCESS: Chan operations work correctly\n")
}
func testTypeComparison() {
fmt.Println("\n=== Test Type Comparison Functions ===")
t1 := types.Typ[types.Int]
t2 := types.Typ[types.Int]
t3 := types.Typ[types.String]
if !types.Identical(t1, t2) {
panic("Identical failed: int should be identical to int")
}
fmt.Printf("Identical(int, int): %v\n", types.Identical(t1, t2))
fmt.Printf("Identical(int, string): %v\n", types.Identical(t1, t3))
if !types.AssignableTo(t1, t2) {
panic("AssignableTo failed")
}
fmt.Printf("AssignableTo(int, int): %v\n", types.AssignableTo(t1, t2))
fmt.Printf("AssignableTo(int, string): %v\n", types.AssignableTo(t1, t3))
fmt.Printf("Comparable(int): %v\n", types.Comparable(t1))
fmt.Printf("Comparable(string): %v\n", types.Comparable(t3))
fmt.Printf("ConvertibleTo(int, int): %v\n", types.ConvertibleTo(t1, t2))
fmt.Println("SUCCESS: Type comparison functions work correctly\n")
}
func testTypeChecking() {
fmt.Println("\n=== Test Type Checking Functions ===")
pkg := types.NewPackage("example.com/test", "test")
m1 := types.NewFunc(token.NoPos, pkg, "Method1", types.NewSignatureType(nil, nil, nil, nil, nil, false))
m2 := types.NewFunc(token.NoPos, pkg, "Method2", types.NewSignatureType(nil, nil, nil, nil, nil, false))
iface := types.NewInterfaceType([]*types.Func{m1, m2}, nil)
iface.Complete()
fields := []*types.Var{
types.NewField(token.NoPos, nil, "x", types.Typ[types.Int], false),
}
structType := types.NewStruct(fields, nil)
fmt.Printf("Implements(struct, interface): %v\n", types.Implements(structType, iface))
fmt.Printf("Implements(int, interface): %v\n", types.Implements(types.Typ[types.Int], iface))
emptyIface := types.NewInterfaceType(nil, nil)
emptyIface.Complete()
fmt.Printf("Implements(int, empty interface): %v\n", types.Implements(types.Typ[types.Int], emptyIface))
fmt.Printf("AssertableTo(interface, int): %v\n", types.AssertableTo(iface, types.Typ[types.Int]))
fmt.Println("SUCCESS: Type checking functions work correctly\n")
}
func testStringFunctions() {
fmt.Println("\n=== Test String Functions ===")
pkg := types.NewPackage("example.com/test", "test")
varObj := types.NewVar(token.NoPos, pkg, "myVar", types.Typ[types.Int])
objStr := types.ObjectString(varObj, nil)
fmt.Printf("ObjectString: %s\n", objStr)
objStrQual := types.ObjectString(varObj, types.RelativeTo(pkg))
fmt.Printf("ObjectString (qualified): %s\n", objStrQual)
typeStr := types.TypeString(types.Typ[types.Int], nil)
fmt.Printf("TypeString(int): %s\n", typeStr)
sliceType := types.NewSlice(types.Typ[types.String])
sliceStr := types.TypeString(sliceType, nil)
fmt.Printf("TypeString([]string): %s\n", sliceStr)
fmt.Println("SUCCESS: String functions work correctly\n")
}
func testLookupFunctions() {
fmt.Println("\n=== Test Lookup Functions ===")
pkg := types.NewPackage("example.com/test", "test")
fields := []*types.Var{
types.NewField(token.NoPos, pkg, "X", types.Typ[types.Int], false),
types.NewField(token.NoPos, pkg, "Y", types.Typ[types.String], false),
}
structType := types.NewStruct(fields, nil)
obj, index, indirect := types.LookupFieldOrMethod(structType, false, pkg, "X")
if obj == nil {
panic("LookupFieldOrMethod failed to find X")
}
fmt.Printf("LookupFieldOrMethod found: %s, index: %v, indirect: %v\n", obj.Name(), index, indirect)
obj2, index2, indirect2 := types.LookupFieldOrMethod(structType, false, pkg, "NonExistent")
fmt.Printf("LookupFieldOrMethod (non-existent): found=%v, index=%v, indirect=%v\n", obj2 != nil, index2, indirect2)
mset := types.NewMethodSet(structType)
fmt.Printf("NewMethodSet: %d methods\n", mset.Len())
fmt.Println("SUCCESS: Lookup functions work correctly\n")
}
func testUtilityFunctions() {
fmt.Println("\n=== Test Utility Functions ===")
pkg := types.NewPackage("example.com/test", "test")
iface := types.NewInterfaceType(nil, nil)
iface.Complete()
fmt.Printf("IsInterface(interface): %v\n", types.IsInterface(iface))
fmt.Printf("IsInterface(int): %v\n", types.IsInterface(types.Typ[types.Int]))
typedNil := types.Typ[types.UntypedNil]
defaultType := types.Default(typedNil)
fmt.Printf("Default(UntypedNil): %v\n", defaultType)
intDefault := types.Default(types.Typ[types.Int])
fmt.Printf("Default(int): %v\n", intDefault)
idStr := types.Id(pkg, "MyType")
fmt.Printf("Id(pkg, \"MyType\"): %s\n", idStr)
fmt.Println("SUCCESS: Utility functions work correctly\n")
}