修复了大量存在的问题,增加了假鸭子类型等等机制

This commit is contained in:
2026-06-25 14:49:46 +08:00
parent 19f2787db0
commit d88d11b646
827 changed files with 32617 additions and 18316 deletions

View File

@@ -0,0 +1,260 @@
import t
import t as tt
import stdio
import testcheck
from t import CEnum as en
from t import CUnion as un
from t import REnum as ren
from t import CStruct as cs
from t import CInt as myint
from t import CChar, CPtr
# ============================================================
# Test 1: from t import CEnum as en; class X(en)
# ============================================================
class Color(en):
Red = 0
Green = 1
Blue = 2
# ============================================================
# Test 2: import t as tt; class X(tt.CEnum)
# ============================================================
class Status(tt.CEnum):
Ok = 0
Error = 1
# ============================================================
# Test 3: from t import CUnion as un; class X(un)
# ============================================================
class Data(un):
i_val: t.CInt
f_val: t.CFloat
# ============================================================
# Test 4: from t import REnum as ren; class X(ren)
# ============================================================
class Opt(ren):
class Some:
val: myint
None_ = 0
# ============================================================
# Test 5: 前向引用检测 — _class_graph 正确识别父类
# (注:检测已修复,但成员继承仍需父类先处理)
# ============================================================
class Parent:
a: t.CInt
def get_a(self) -> t.CInt:
return self.a
class Child(Parent):
b: t.CInt
def get_sum(self) -> t.CInt:
return self.a + self.b
# ============================================================
# Test 6: 多层继承 A -> B -> CCVTable 向上传播
# ============================================================
class Base:
x: t.CInt
def base_val(self) -> t.CInt:
return self.x
class Mid(Base):
y: t.CInt
def mid_val(self) -> t.CInt:
return self.y
class Leaf(Mid):
z: t.CInt
def leaf_val(self) -> t.CInt:
return self.z
# ============================================================
# Test 7: 类型别名用于注解
# ============================================================
val: myint = 42
# ============================================================
# Test 8: BigEndian 字节序
# ============================================================
class NetHeader:
magic: t.CInt | t.BigEndian
length: t.CShort | t.BigEndian
normal: t.CInt
# ============================================================
# Test 9: import t as tt; tt.CInt 作为类型注解
# ============================================================
# class TtStruct:
# x: tt.CInt
# y: tt.CFloat
# ============================================================
# Test 10: import t as tt; class X(tt.CStruct)
# ============================================================
class TtPoint(tt.CStruct):
x: t.CInt
y: t.CInt
# ============================================================
# Test 11: import t as tt; @tt.Object 装饰器
# ============================================================
@tt.Object
class TtObject:
val: t.CInt
def get_val(self) -> t.CInt:
return self.val
# ============================================================
# Test 12: from t import CStruct as cs; class X(cs)
# ============================================================
class CsPoint(cs):
x: t.CInt
y: t.CInt
# ============================================================
# Test 13: import t as tt; class X(tt.CUnion)
# ============================================================
class TtUnion(tt.CUnion):
a: t.CInt
b: t.CFloat
# ============================================================
# Test 14: @tt.Object on class without methods (should fail?)
# ============================================================
@tt.Object
class TtEmptyObject:
val: t.CInt
def main() -> t.CInt:
testcheck.begin("EdgeCaseTest: 类型别名与边界情况测试")
# Test 1: CEnum alias
testcheck.section("Test 1: CEnum alias")
c: t.CInt = Color.Green
stdio.printf("Test1 CEnum alias: %d\n", c)
testcheck.ok("CEnum alias (Color.Green=1)")
# Test 2: module alias + CEnum
testcheck.section("Test 2: module alias + CEnum")
s: t.CInt = Status.Error
stdio.printf("Test2 t as tt.CEnum: %d\n", s)
testcheck.ok("t as tt.CEnum (Status.Error=1)")
# Test 3: CUnion alias
testcheck.section("Test 3: CUnion alias")
d: Data = Data()
d.i_val = 10
d.f_val = 3.14
stdio.printf("Test3 CUnion alias: i=%d f=%f\n", d.i_val, d.f_val)
testcheck.ok("CUnion alias (i=10 f=3.14)")
# Test 4: REnum alias
testcheck.section("Test 4: REnum alias")
o: Opt = Opt.Some(99)
match o:
case Opt.Some(v):
stdio.printf("Test4 REnum alias: val=%d\n", v)
case Opt.None_():
stdio.printf("Test4 REnum alias: None\n")
testcheck.ok("REnum alias (Opt.Some(99))")
# Test 5: forward reference
testcheck.section("Test 5: forward reference")
ch: Child = Child()
ch.a = 100
ch.b = 200
stdio.printf("Test5 forward ref: a=%d b=%d sum=%d\n", ch.a, ch.b, ch.get_sum())
stdio.printf("Test5 parent method: %d\n", ch.get_a())
testcheck.ok("forward ref (a=100 b=200 sum=300)")
# Test 6: multi-level inheritance
testcheck.section("Test 6: multi-level inheritance")
lf: Leaf = Leaf()
lf.x = 1
lf.y = 2
lf.z = 3
stdio.printf("Test6 multi-inherit: base=%d mid=%d leaf=%d\n",
lf.base_val(), lf.mid_val(), lf.leaf_val())
testcheck.ok("multi-inherit (base=1 mid=2 leaf=3)")
# Test 7: type alias in annotation
testcheck.section("Test 7: type alias in annotation")
stdio.printf("Test7 type alias: val=%d\n", val)
testcheck.ok("type alias (val=42)")
# Test 8: BigEndian
testcheck.section("Test 8: BigEndian")
nh: NetHeader = NetHeader()
nh.magic = 0x12345678
nh.length = 256
nh.normal = 42
stdio.printf("Test8 BigEndian: magic=0x%X length=%d normal=%d\n",
nh.magic, nh.length, nh.normal)
testcheck.ok("BigEndian (magic=0x12345678 length=256 normal=42)")
# Test 10: tt.CStruct as base class
testcheck.section("Test 10: tt.CStruct as base class")
tp: TtPoint = TtPoint()
tp.x = 100
tp.y = 200
stdio.printf("Test10 tt.CStruct: x=%d y=%d\n", tp.x, tp.y)
testcheck.ok("tt.CStruct (x=100 y=200)")
# Test 11: @tt.Object decorator
testcheck.section("Test 11: @tt.Object decorator")
to: TtObject = TtObject()
to.val = 999
stdio.printf("Test11 @tt.Object: val=%d\n", to.get_val())
testcheck.ok("@tt.Object (val=999)")
# Test 12: from t import CStruct as cs
testcheck.section("Test 12: from t import CStruct as cs")
cp: CsPoint = CsPoint()
cp.x = 11
cp.y = 22
stdio.printf("Test12 cs alias: x=%d y=%d\n", cp.x, cp.y)
testcheck.ok("cs alias (x=11 y=22)")
# Test 13: tt.CUnion
testcheck.section("Test 13: tt.CUnion")
tu: TtUnion = TtUnion()
tu.a = 77
stdio.printf("Test13 tt.CUnion: a=%d\n", tu.a)
testcheck.ok("tt.CUnion (a=77)")
# Test 14: @tt.Object on empty class
testcheck.section("Test 14: @tt.Object on empty class")
teo: TtEmptyObject = TtEmptyObject()
teo.val = 555
stdio.printf("Test14 @tt.Object empty: val=%d\n", teo.val)
testcheck.ok("@tt.Object empty (val=555)")
return testcheck.end()

View File

@@ -0,0 +1 @@
{"stdio": "73edbcf76e32d00b", "testcheck": "9dbecd0942a39782", "main": "ac0d686cb2cc783b"}

View File

@@ -0,0 +1 @@
{"stdio": "73edbcf76e32d00b", "testcheck": "9dbecd0942a39782"}

View File

@@ -0,0 +1,29 @@
{
"$schema": "https://raw.githubusercontent.com/TermiNexus/TransPyC/main/schemas/project-schema.json",
"name": "EdgeCaseTest",
"version": "1.0.0",
"source_dir": "./App",
"temp_dir": "./temp",
"output_dir": "./output",
"compiler": {
"cmd": "llc",
"flags": ["-filetype=obj", "-relocation-model=pic"]
},
"linker": {
"cmd": "clang++",
"flags": ["-lmsvcrt", "-lucrt", "-lpthread", "-lmingwex", "-lkernel32", "-Wl,--allow-multiple-definition"],
"output": "EdgeCaseTest.exe"
},
"includes": [
"../../includes"
],
"target": {
"triple": "x86_64-pc-windows-gnu",
"datalayout": "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
},
"options": {
"slice_level": 3,
"target": "llvm",
"strict_mode": true
}
}

View File

@@ -0,0 +1,28 @@
"""
Auto-generated Python stub file from stdio.py
Module: stdio
"""
import t, c
def printf(fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
def fprintf(stream: t.CVoid | t.CPtr, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
def sprintf(buf: t.CChar | t.CPtr, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
def snprintf(buf: t.CChar | t.CPtr, size: t.CSizeT, fmt: t.CChar | t.CConst | t.CPtr, *args) -> t.CInt | t.CExtern | t.CExport: pass
def puts(s: t.CChar | t.CConst | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
def fputs(s: t.CChar | t.CConst | t.CPtr, stream: t.CVoid | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
def fgets(buf: t.CChar | t.CPtr, size: t.CInt, stream: t.CVoid | t.CPtr) -> t.CChar | t.CPtr | t.CExtern | t.CExport: pass
def fflush(stream: t.CVoid | t.CPtr) -> t.CInt | t.CExtern | t.CExport: pass
stdin: t.CExtern | t.CVoid | t.CPtr
stdout: t.CExtern | t.CVoid | t.CPtr
stderr: t.CExtern | t.CVoid | t.CPtr

View File

@@ -0,0 +1,25 @@
"""
Auto-generated Python stub file from testcheck.py
Module: testcheck
"""
import t, c
import stdio
_pass_count: t.CExtern | t.CInt
_fail_count: t.CExtern | t.CInt
def begin(name: str) -> t.CInt: pass
def section(name: str) -> t.CInt: pass
def ok(msg: str) -> t.CInt: pass
def fail(msg: str) -> t.CInt: pass
def check(cond: t.CInt, ok_msg: str, fail_msg: str) -> t.CInt: pass
def info(msg: str) -> t.CInt: pass
def end() -> t.CInt: pass

View File

@@ -0,0 +1,3 @@
73edbcf76e32d00b:includes/stdio.py
9dbecd0942a39782:includes/testcheck.py
ac0d686cb2cc783b:main.py

View File

@@ -0,0 +1,73 @@
"""
Auto-generated Python stub file from main.py
Module: main
"""
import c
import t
import t as tt
import stdio
import testcheck
from t import CEnum as en
from t import CUnion as un
from t import REnum as ren
from t import CStruct as cs
from t import CInt as myint
from t import CChar, CPtr
class Color(en):
Red = 0
Green = 1
Blue = 2
class Status(tt.CEnum):
Ok = 0
Error = 1
class Data(un):
i_val: t.CInt
f_val: t.CFloat
class Opt(ren):
class Some:
val: myint
None_ = 0
class Parent:
a: t.CInt
def get_a(self: Parent) -> t.CInt: pass
class Child(Parent):
b: t.CInt
def get_sum(self: Child) -> t.CInt: pass
class Base:
x: t.CInt
def base_val(self: Base) -> t.CInt: pass
class Mid(Base):
y: t.CInt
def mid_val(self: Mid) -> t.CInt: pass
class Leaf(Mid):
z: t.CInt
def leaf_val(self: Leaf) -> t.CInt: pass
val: t.CExtern | myint
class NetHeader:
magic: t.CInt | t.BigEndian
length: t.CShort | t.BigEndian
normal: t.CInt
class TtPoint(tt.CStruct):
x: t.CInt
y: t.CInt
@tt.Object
class TtObject:
val: t.CInt
def get_val(self: TtObject) -> t.CInt: pass
class CsPoint(cs):
x: t.CInt
y: t.CInt
class TtUnion(tt.CUnion):
a: t.CInt
b: t.CFloat
@tt.Object
class TtEmptyObject:
val: t.CInt
def main() -> t.CInt: pass