Files
TransPyC/Test/EnumTest/App/enumtest.vp
T
TermiNexus 6644d6ce3b test: TestProject/2/3 拆分为 10 个具体测试项目; ZlibTest 并入标准 zlib 库
新项目(各含 project.json + App/main.vp):
- LoopMathTest/LogicFlowTest/ClassArrayTest/NumpyTest (原 TestProject)
- HashLibTest (原 TestProject2 去 zlib)
- GenericOopTest/EnumTest/MpoolOopTest/VectorGenericTest/FileIoTest (原 TestProject3)
ZlibTest 增加"标准 zlib 库(zlib.pyzlib)" section 与 __zlib 并存验证。
2026-08-09 04:06:26 +08:00

96 lines
2.0 KiB
Plaintext

import t, c
from t import CInt, CPtr, CChar, CInt32T, CExport
import viperlib
import stdlib
class Color(t.CEnum):
Red = 0
Green = 1
Blue = 2
class Option(t.REnum):
class Some:
value: CInt
Empty = 1
class Result(t.REnum):
class Ok:
value: CInt
class Err:
msg: t.CChar | CPtr
class GeoShape(t.REnum):
class GCircle:
radius: CInt
class GRect:
w: CInt
h: CInt
class GPoint:
x: CInt
y: CInt
def enum_main() -> CInt:
buf: CChar | CPtr = CPtr(stdlib.malloc(256))
c1: CInt = Color.Red
c2: CInt = Color.Green
c3: CInt = Color.Blue
viperlib.snprintf(buf, 256, "CEnum: Red=%d Green=%d Blue=%d", c1, c2, c3)
print(buf)
opt: Option = Option.Some(42)
match opt:
case Option.Some(v):
viperlib.snprintf(buf, 256, "Option.Some: value=%d", v)
print(buf)
case Option.Empty():
pass
res1: Result = Result.Ok(100)
match res1:
case Result.Ok(v):
viperlib.snprintf(buf, 256, "Result.Ok: value=%d", v)
print(buf)
case Result.Err(e):
pass
res2: Result = Result.Err("not found")
match res2:
case Result.Ok(v):
pass
case Result.Err(e):
viperlib.snprintf(buf, 256, "Result.Err: msg=%s", e)
print(buf)
s1: GeoShape = GeoShape.GCircle(5)
match s1:
case GeoShape.GCircle(r):
viperlib.snprintf(buf, 256, "GeoShape.GCircle: radius=%d", r)
print(buf)
case _:
pass
s2: GeoShape = GeoShape.GRect(10, 20)
match s2:
case GeoShape.GRect(w, h):
viperlib.snprintf(buf, 256, "GeoShape.GRect: w=%d h=%d", w, h)
print(buf)
case _:
pass
s3: GeoShape = GeoShape.GPoint(3, 4)
match s3:
case GeoShape.GPoint(x, y):
viperlib.snprintf(buf, 256, "GeoShape.GPoint: x=%d y=%d", x, y)
print(buf)
case _:
pass
stdlib.free(buf)
return 0