diff --git a/includes/ast/parser.vp b/includes/ast/parser.vp index 462c9b8..f1136a1 100644 --- a/includes/ast/parser.vp +++ b/includes/ast/parser.vp @@ -2,6 +2,8 @@ import t, c from stdint import * import memhub import string +import stdio +import stdlib from .tokens import (Token, TokenType, Keyword, TokOp) from .base import ( AST, ASTCtx, ASTKind, OpKind, ASTFlag, @@ -165,7 +167,39 @@ def _new_const_int(pool: memhub.MemManager | t.CPtr, val: t.CInt64T, def _new_const_float(pool: memhub.MemManager | t.CPtr, val: t.CDouble, lineno: t.CInt, col: t.CInt) -> AST | t.CPtr: - return Constant(pool, CONST_FLOAT, 0, val, None, lineno, col) + global _g_dbg_pts, _g_dbg_vals, _g_dbg_cnt + new_c: Constant | t.CPtr = Constant(pool, CONST_FLOAT, 0, val, None, lineno, col) + # OVW-DBG: 检查之前构造的浮点节点是否已被覆写(定位覆写者) + if _g_dbg_cnt > 0 and _g_dbg_pts is not None and _g_dbg_vals is not None: + chk: t.CSizeT = 0 + while chk < _g_dbg_cnt: + old_ptr: t.CUInt64T = _g_dbg_pts[chk] + if old_ptr != 0: + old_val: t.CDouble = _g_dbg_vals[chk] + old_c: Constant | t.CPtr = (Constant | t.CPtr)(t.CVoid(old_ptr, t.CPtr)) + if old_c.float_val != old_val: + stdio.printf("[OVW-DBG] 构造新字面量 val=%.17g 后,之前节点 ptr=%p 的 float_val 从 %.17g 变 %.17g\n", + val, t.CUInt64T(old_ptr), old_val, old_c.float_val) + _g_dbg_pts[chk] = 0 + chk += 1 + # 记录当前节点 + if _g_dbg_pts is None: + _g_dbg_pts = stdlib.malloc(512 * 8) + _g_dbg_vals = stdlib.malloc(512 * 8) + if _g_dbg_cnt < 512 and _g_dbg_pts is not None and _g_dbg_vals is not None: + _g_dbg_pts[_g_dbg_cnt] = t.CUInt64T(new_c) + _g_dbg_vals[_g_dbg_cnt] = val + _g_dbg_cnt += 1 + # NEWF-DBG: 打印浮点常量构造值+节点指针+存储后读回(定位浮点 CDefine 值错误) + stdio.printf("[NEWF-DBG] val=%.17g ptr=%p readback=%.17g int_val=%lld\n", + val, t.CSizeT(new_c), new_c.float_val, new_c.int_val) + return new_c + + +# OVW-DBG: 浮点节点监控表(stdlib 分配,避免污染 pool) +_g_dbg_pts: t.CUInt64T | t.CPtr = None +_g_dbg_vals: t.CDouble | t.CPtr = None +_g_dbg_cnt: int = 0 def _new_const_str(pool: memhub.MemManager | t.CPtr, val: str, diff --git a/includes/string.vp b/includes/string.vp index 6ad1adb..e44a386 100644 --- a/includes/string.vp +++ b/includes/string.vp @@ -365,6 +365,10 @@ def atof(src: str) -> t.CDouble: while exp < 0: result = result / 10.0 exp += 1 + # ATOF-DBG: 打印长浮点字面量解析过程(定位浮点 CDefine 值错误) + if src is not None and strlen(src) >= 12: + stdio.printf("[ATOF-DBG] src=%s int=%.17g frac=%.17g div=%.17g result=%.17g\n", + src, int_part, frac_part, frac_div, result) return result def split(s: str, delim: str, result: t.CArray[str]) -> int: