Files
Viper_RegressionTesting/EnumTest/App/enumtest.vp
T
2026-10-17 21:57:03 +08:00

91 lines
1.9 KiB
Plaintext

import viperlib
import stdlib
enum Color:
Red, Green, Blue
renum Option:
class Some:
value: int
Empty = 1
renum Result:
class Ok:
value: int
class Err:
msg: str
renum GeoShape:
class GCircle:
radius: int
class GRect:
w: int
h: int
class GPoint:
x: int
y: int
def enum_main() -> int:
buf: str = stdlib.malloc(256)
c1: int = Color.Red
c2: int = Color.Green
c3: int = 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