chore(comments): Test 仓注释英文化(R11)
把 81 个 .vp 文件的中文 # 注释与三引号文档串改写为简洁英文,每个连续注释块 <=2 行;运行时中文字符串字面量(testcheck.begin/print 等用户可见消息)保持原样。骨架比对 HEAD 与工作区 112 文件漂移 0,全量 77/77 顺序回归通过。
This commit is contained in:
1 parent
aa5a7e7250
commit
ee6461be46
81 files changed
+1231
-1925
No files matched your search
+32
-74
@@ -2,22 +2,10 @@ import t
|
||||
from stdio import printf
|
||||
import testcheck
|
||||
|
||||
# ============================================================
|
||||
# 自定义装饰器处理函数
|
||||
# 调用约定 (v4):
|
||||
# i32 decor_name(i8* ctx, i8* func_name, i32 phase,
|
||||
# i8* args_ptr, i8* ret_ptr, i8* decor_args_ptr)
|
||||
#
|
||||
# ctx: 栈帧局部上下文(32字节),每次调用独立,线程/递归安全
|
||||
# args_ptr: 可读写,装饰器可修改函数入参
|
||||
# ret_ptr: 后置阶段可读写,装饰器可修改返回值
|
||||
# 返回值(前置阶段):
|
||||
# 0 = 跳过原函数调用
|
||||
# 1 = 正常调用一次
|
||||
# N = 循环调用 N 次
|
||||
# ============================================================
|
||||
# Custom decorator handler calling convention (v4): i32 decor(ctx, func_name, phase, args_ptr, ret_ptr, decor_args_ptr).
|
||||
# ctx: per-call frame-local context (32 bytes); args_ptr/ret_ptr are read-write; pre-phase returns 0=skip, 1=call once, N=loop N.
|
||||
|
||||
# @log 装饰器:打印函数进入/退出,返回 1(正常调用一次)
|
||||
# @log: print enter/exit, return 1 (call once)
|
||||
def log(ctx: t.CPtr, func_name: str, phase: int,
|
||||
args_ptr: t.CPtr,
|
||||
ret_ptr: t.CPtr,
|
||||
@@ -29,7 +17,7 @@ def log(ctx: t.CPtr, func_name: str, phase: int,
|
||||
printf("[LOG] << %s exit\n", func_name)
|
||||
return 0
|
||||
|
||||
# @trace 装饰器:详细追踪
|
||||
# @trace: verbose tracing
|
||||
def trace(ctx: t.CPtr, func_name: str, phase: int,
|
||||
args_ptr: t.CPtr,
|
||||
ret_ptr: t.CPtr,
|
||||
@@ -41,10 +29,8 @@ def trace(ctx: t.CPtr, func_name: str, phase: int,
|
||||
printf("[TRACE] %s returned\n", func_name)
|
||||
return 0
|
||||
|
||||
# @timing 装饰器:使用全局变量记录计时
|
||||
# 注意:理想情况下应使用 ctx 栈帧局部上下文替代全局变量,
|
||||
# 但 Viper 当前前端尚不支持 i8* 到 i32* 的指针转型,
|
||||
# 因此暂时使用全局变量演示计时逻辑
|
||||
# @timing: measures elapsed time using a global counter.
|
||||
# A global is used because i8* to i32* pointer casts are not supported yet.
|
||||
_timing_tmp: int = 0
|
||||
|
||||
def timing(ctx: t.CPtr, func_name: str, phase: int,
|
||||
@@ -61,7 +47,7 @@ def timing(ctx: t.CPtr, func_name: str, phase: int,
|
||||
printf("[TIMING] %s took %d ms\n", func_name, elapsed)
|
||||
return 0
|
||||
|
||||
# @repeat 装饰器(带参数,流程劫持):循环调用原函数 N 次
|
||||
# @repeat (with args, flow hijack): call the original function N times
|
||||
def repeat(ctx: t.CPtr, func_name: str, phase: int,
|
||||
args_ptr: t.CPtr,
|
||||
ret_ptr: t.CPtr,
|
||||
@@ -73,7 +59,7 @@ def repeat(ctx: t.CPtr, func_name: str, phase: int,
|
||||
printf("[REPEAT] %s all iterations done\n", func_name)
|
||||
return 0
|
||||
|
||||
# @skip 装饰器:跳过原函数调用(流程劫持,返回 0)
|
||||
# @skip: skip the original call (flow hijack, returns 0)
|
||||
_skip_count: int = 0
|
||||
|
||||
def skip(ctx: t.CPtr, func_name: str, phase: int,
|
||||
@@ -89,7 +75,7 @@ def skip(ctx: t.CPtr, func_name: str, phase: int,
|
||||
printf("[SKIP] %s post (ret_ptr=%p)\n", func_name, ret_ptr)
|
||||
return 0
|
||||
|
||||
# @count_calls 装饰器:统计函数被调用次数(递归保护测试用)
|
||||
# @count_calls: count invocations (used to test recursion guard)
|
||||
_call_count: int = 0
|
||||
|
||||
def count_calls(ctx: t.CPtr, func_name: str, phase: int,
|
||||
@@ -106,19 +92,14 @@ def count_calls(ctx: t.CPtr, func_name: str, phase: int,
|
||||
return 0
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 1:单个装饰器 @log
|
||||
# ============================================================
|
||||
# --- Test 1: single @log ---
|
||||
|
||||
@log
|
||||
def add(a: int, b: int) -> int | t.CExport:
|
||||
return a + b
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 2:链式装饰器 @log @trace => log(trace(f))
|
||||
# 执行顺序:log_pre → trace_pre → f → trace_post → log_post
|
||||
# ============================================================
|
||||
# --- Test 2: chained @log @trace (log(trace(f))) ---
|
||||
|
||||
@log
|
||||
@trace
|
||||
@@ -126,45 +107,35 @@ def multiply(a: int, b: int) -> int | t.CExport:
|
||||
return a * b
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 3:装饰器修饰 void 返回函数
|
||||
# ============================================================
|
||||
# --- Test 3: @log on void function ---
|
||||
|
||||
@log
|
||||
def greet(name: str) -> void | t.CExport:
|
||||
printf("Hello, %s!\n", name)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 4:装饰器修饰无参数函数
|
||||
# ============================================================
|
||||
# --- Test 4: @log on no-arg function ---
|
||||
|
||||
@log
|
||||
def get_value() -> int | t.CExport:
|
||||
return 42
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 5:@timing 装饰器
|
||||
# ============================================================
|
||||
# --- Test 5: @timing ---
|
||||
|
||||
@timing
|
||||
export def compute(x: int) -> int:
|
||||
return x * x + 1
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 6:带参数装饰器 @repeat(3) — 流程劫持,循环调用 3 次
|
||||
# ============================================================
|
||||
# --- Test 6: @repeat(3) flow hijack ---
|
||||
|
||||
@repeat(3)
|
||||
export def echo(msg: str):
|
||||
printf(" echo: %s\n", msg)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 7:链式 @timing @log
|
||||
# ============================================================
|
||||
# --- Test 7: chained @timing @log ---
|
||||
|
||||
@timing
|
||||
@log
|
||||
@@ -175,9 +146,7 @@ export def power(base: int, exp: int) -> int:
|
||||
return result
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 8:@skip 装饰器 — 流程劫持,跳过原函数调用
|
||||
# ============================================================
|
||||
# --- Test 8: @skip flow hijack ---
|
||||
|
||||
@skip
|
||||
def dangerous() -> int | t.CExport:
|
||||
@@ -185,9 +154,7 @@ def dangerous() -> int | t.CExport:
|
||||
return 999
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 9:@repeat(3) 修饰有返回值的函数
|
||||
# ============================================================
|
||||
# --- Test 9: @repeat(3) with return value ---
|
||||
|
||||
@repeat(3)
|
||||
def accumulate(x: int) -> int | t.CExport:
|
||||
@@ -195,11 +162,7 @@ def accumulate(x: int) -> int | t.CExport:
|
||||
return x * 10
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 10:递归装饰保护 — @count_calls 修饰递归函数
|
||||
# v4 递归保护:最外层 wrapper 检查全局标志位,
|
||||
# 递归调用时跳过所有装饰逻辑,仅最外层调用触发装饰器
|
||||
# ============================================================
|
||||
# --- Test 10: recursion guard, @count_calls on recursive fn ---
|
||||
|
||||
@count_calls
|
||||
def factorial(n: int) -> int | t.CExport:
|
||||
@@ -208,10 +171,7 @@ def factorial(n: int) -> int | t.CExport:
|
||||
return n * factorial(n - 1)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 测试 11:@log 修饰递归函数 — 验证递归保护
|
||||
# 递归保护确保只有最外层调用打印 LOG,内部递归不触发
|
||||
# ============================================================
|
||||
# --- Test 11: @log on recursive fib ---
|
||||
|
||||
@log
|
||||
def fib(n: int) -> int | t.CExport:
|
||||
@@ -220,66 +180,64 @@ def fib(n: int) -> int | t.CExport:
|
||||
return fib(n - 1) + fib(n - 2)
|
||||
|
||||
|
||||
# ============================================================
|
||||
# 主函数
|
||||
# ============================================================
|
||||
# --- main ---
|
||||
|
||||
def main() -> int | t.CExport:
|
||||
testcheck.begin("Decorator Test v4")
|
||||
|
||||
# 测试 1:单个 @log
|
||||
# Test 1: single @log
|
||||
testcheck.section("Test 1: single @log")
|
||||
r1: int = add(3, 4)
|
||||
printf("add(3,4) = %d\n", r1)
|
||||
testcheck.check(r1 == 7, "add(3,4) = 7", "add(3,4) expect 7")
|
||||
|
||||
# 测试 2:链式 @log @trace
|
||||
# Test 2: chained @log @trace
|
||||
testcheck.section("Test 2: chained @log @trace")
|
||||
r2: int = multiply(5, 6)
|
||||
printf("multiply(5,6) = %d\n", r2)
|
||||
testcheck.check(r2 == 30, "multiply(5,6) = 30", "multiply(5,6) expect 30")
|
||||
|
||||
# 测试 3:void 返回
|
||||
# Test 3: void return
|
||||
testcheck.section("Test 3: @log on void function")
|
||||
greet("Viper")
|
||||
testcheck.ok("@log on void function greet")
|
||||
|
||||
# 测试 4:无参数
|
||||
# Test 4: no arguments
|
||||
testcheck.section("Test 4: @log on no-arg function")
|
||||
r4: int = get_value()
|
||||
printf("get_value() = %d\n", r4)
|
||||
testcheck.check(r4 == 42, "get_value() = 42", "get_value() expect 42")
|
||||
|
||||
# 测试 5:@timing
|
||||
# Test 5: @timing
|
||||
testcheck.section("Test 5: @timing")
|
||||
r5: int = compute(7)
|
||||
printf("compute(7) = %d\n", r5)
|
||||
testcheck.check(r5 == 50, "compute(7) = 50", "compute(7) expect 50")
|
||||
|
||||
# 测试 6:@repeat(3) — 流程劫持,循环 3 次
|
||||
# Test 6: @repeat(3) flow hijack, loops 3 times
|
||||
testcheck.section("Test 6: @repeat(3) flow hijack")
|
||||
echo("hello")
|
||||
testcheck.ok("@repeat(3) flow hijack echo")
|
||||
|
||||
# 测试 7:链式 @timing @log
|
||||
# Test 7: chained @timing @log
|
||||
testcheck.section("Test 7: chained @timing @log")
|
||||
r7: int = power(2, 10)
|
||||
printf("power(2,10) = %d\n", r7)
|
||||
testcheck.check(r7 == 1024, "power(2,10) = 1024", "power(2,10) expect 1024")
|
||||
|
||||
# 测试 8:@skip — 跳过原函数调用
|
||||
# Test 8: @skip, skips the original call
|
||||
testcheck.section("Test 8: @skip flow hijack")
|
||||
r8: int = dangerous()
|
||||
printf("dangerous() = %d (should be 0, skipped)\n", r8)
|
||||
testcheck.check(r8 == 0, "dangerous() skipped = 0", "dangerous() expect 0")
|
||||
|
||||
# 测试 9:@repeat(3) 有返回值
|
||||
# Test 9: @repeat(3) with return value
|
||||
testcheck.section("Test 9: @repeat(3) with return value")
|
||||
r9: int = accumulate(5)
|
||||
printf("accumulate(5) = %d (last iteration result)\n", r9)
|
||||
testcheck.check(r9 == 50, "accumulate(5) = 50", "accumulate(5) expect 50")
|
||||
|
||||
# 测试 10:递归装饰保护 — @count_calls
|
||||
# Test 10: recursion guard, @count_calls
|
||||
testcheck.section("Test 10: recursive decoration protection")
|
||||
global _call_count
|
||||
_call_count = 0
|
||||
@@ -288,7 +246,7 @@ def main() -> int | t.CExport:
|
||||
printf(" decorator call count = %d (should be 1, not 5)\n", _call_count)
|
||||
testcheck.check(r10 == 120 and _call_count == 1, "factorial(5)=120, calls=1", "factorial(5) expect 120, calls=1")
|
||||
|
||||
# 测试 11:@log 递归保护 — fib
|
||||
# Test 11: @log recursion guard, fib
|
||||
testcheck.section("Test 11: @log on recursive fib")
|
||||
r11: int = fib(6)
|
||||
printf("fib(6) = %d (should be 8)\n", r11)
|
||||
|
||||
Reference in new issue
Block a user