Files
Viper_RegressionTesting/NewTest/App/main.vp
T
TermiNexus 42ca901b28 test(new): 新增 NewTest 工程;两个回归脚本支持 --escape/--only 单独跑
NewTest:M2.4 new 描述符冒烟(stack→alloca、heap→malloc;memgr 段待确认管理器 API,非法组合以注释列出)。

regression_test.py / SequentialRegressionTest.py 新增 --escape(只跑生命期/逃逸 + new 相关项目)与 --only(逗号分隔指定项目);两者都会在 Test/<名> 与仓库根 <名> 查找 project.json / project.vpj,故根级 Escape* 项目也能直接跑;--escape 忽略 README 难度顺序。

README.md:新增「一·附 生命期/逃逸相关测试」一节,列出 5 个项目的期望结果与单独运行命令。
2026-10-08 17:47:24 +08:00

47 lines
1.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from stdint import *
import w32.win32console
import t, c
from t import CInt, CExport
import stdio
# =============================================================================
# M2.4 `new` 描述符分配:三来源各自落地不同(IR 差异见下方注释)
# stack → 帧内 alloca(无 malloc 调用,随帧失效)
# heap → call i8* @malloc(i64 sizeof) + bitcast 到 T*
# memgr → call i8* @<Mgr>.alloc(i64 sizeof) + bitcast 到 T*
# 本工程做冒烟验证:能编译、能跑出正确字段值。
# 描述符顺序无关;省略来源 = heap(D3),省略生命期由来源决定。
# =============================================================================
novirtual class Point:
x: int
y: int
def main() -> CInt | CExport:
w32.win32console.SetConsoleCP(65001)
w32.win32console.SetConsoleOutputCP(65001)
# (1) stack:等价既有写法 &Point(1, 2),生命期随帧
p1: Point* = new stack slife Point(1, 2)
stdio.printf("stack: %d %d\n", p1.x, p1.y)
# (2) heap:malloc(sizeof(Point)) + 构造(生命期默认 hlife)
p2: Point* = new heap Point(3, 4)
stdio.printf("heap : %d %d\n", p2.x, p2.y)
# (3) memgr(x):从 memhub 管理器取存储
# 待确认 memhub 管理器的构造 API(MemBuddy 建立方式)后打开本段:
# m: memhub.MemBuddy* = <构造管理器>
# p3: Point* = new memgr(m) hlife Point(5, 6)
# stdio.printf("memgr: %d %d\n", p3.x, p3.y)
# 非法组合(应报 LifetimeError)——要验证时打开其中一条:
# 1) 语义矛盾:bad: Point* = new stack hlife Point(7, 8)
# 2) 多来源 :new stack heap Point(7, 8) → 解析期报错
# 3) 多生命期:new heap slife hlife Point(7, 8) → 解析期报错
return 0