From 61e578fe9a9f1bbfa83af216b289fe14f58b23ab Mon Sep 17 00:00:00 2001 From: TermiNexus Date: Sat, 8 Aug 2026 19:42:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(includes):=20atof=5Fout=20=E8=BE=93?= =?UTF-8?q?=E5=87=BA=E5=8F=82=E6=95=B0=E7=89=88=E7=BB=95=E5=BC=80=E8=B7=A8?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=20double=20=E8=BF=94=E5=9B=9E=20bug;=20?= =?UTF-8?q?=E6=B5=AE=E7=82=B9=E8=8A=82=E7=82=B9=E6=9E=84=E9=80=A0=E5=80=BC?= =?UTF-8?q?=E8=AE=B0=E5=BD=95+=E6=9F=A5=E8=AF=A2(parser);=20NEWF/ATOF/LKP?= =?UTF-8?q?=20=E5=AE=9A=E4=BD=8D=E6=8F=92=E6=A1=A9;=20ast=20=E5=AF=BC?= =?UTF-8?q?=E5=87=BA=E6=9F=A5=E8=A1=A8=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- includes/ast/__init__.vp | 1 + includes/ast/parser.vp | 61 ++++++++++++++++++++++++++++------------ includes/string.vp | 13 +++++++++ 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/includes/ast/__init__.vp b/includes/ast/__init__.vp index 610776c..a02ac6d 100644 --- a/includes/ast/__init__.vp +++ b/includes/ast/__init__.vp @@ -52,6 +52,7 @@ from .match import ( from .lexer import Lexer, _lexer_init, tokenize, new_lexer from .parser import Parser, _parser_init, parse_tokens, new_parser from .parser import _parse_test +from .parser import _dbg_lookup_lit_val, _dbg_lit_found # ============================================================ # Global Memory Pool Pointer (follows project rules: declare _mbuddy) diff --git a/includes/ast/parser.vp b/includes/ast/parser.vp index f1136a1..2a114f4 100644 --- a/includes/ast/parser.vp +++ b/includes/ast/parser.vp @@ -169,24 +169,21 @@ def _new_const_float(pool: memhub.MemManager | t.CPtr, val: t.CDouble, lineno: t.CInt, col: t.CInt) -> AST | t.CPtr: 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 - # 记录当前节点 + # 把数字文本存到 str_val(stdlib 分配,绕开 pool 对 float_val 字段的覆写) + # REG-SRC 用 string.atof(str_val) 恢复原始值 + lit_txt: str = stdlib.malloc(64) + if lit_txt is not None: + stdio.snprintf(lit_txt, 64, "%.17g", val) + new_c.str_val = lit_txt + # 记录浮点节点 ptr→val 到 stdlib 表(构造时值正确,供 REG-SRC 查表绕开 pool 覆写) + # 表满时重置:旧记录对应已翻译完的文件,从当前节点重新记录 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 = stdlib.malloc(4096 * 8) + _g_dbg_vals = stdlib.malloc(4096 * 8) + _g_dbg_cnt = 0 + elif _g_dbg_cnt >= 4096: + _g_dbg_cnt = 0 + if _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 @@ -196,10 +193,38 @@ def _new_const_float(pool: memhub.MemManager | t.CPtr, val: t.CDouble, return new_c -# OVW-DBG: 浮点节点监控表(stdlib 分配,避免污染 pool) +# 浮点节点构造值表(stdlib 分配,避免污染 pool) _g_dbg_pts: t.CUInt64T | t.CPtr = None _g_dbg_vals: t.CDouble | t.CPtr = None _g_dbg_cnt: int = 0 +# 最近一次 _dbg_lookup_lit_val 是否命中 +_g_dbg_lit_found: int = 0 + + +def _dbg_lookup_lit_val(ptr: t.CUInt64T) -> double: + """查浮点节点构造值表:返回 ptr 节点的原始构造值(未命中返回 0 并置 found=0) + + 用于绕开 pool 分配对 AST float_val 字段的覆写(构造时值正确,读取时可能被覆写)。 + """ + global _g_dbg_lit_found + _g_dbg_lit_found = 0 + if _g_dbg_pts is None or _g_dbg_vals is None: + stdio.printf("[LKP-DBG] pts null cnt=%d\n", _g_dbg_cnt) + return 0.0 + i: t.CSizeT = 0 + while i < _g_dbg_cnt: + if _g_dbg_pts[i] == ptr: + _g_dbg_lit_found = 1 + stdio.printf("[LKP-DBG] HIT ptr=%p val=%.17g cnt=%d\n", ptr, _g_dbg_vals[i], _g_dbg_cnt) + return _g_dbg_vals[i] + i += 1 + stdio.printf("[LKP-DBG] MISS ptr=%p cnt=%d\n", ptr, _g_dbg_cnt) + return 0.0 + + +def _dbg_lit_found() -> int: + """检查最近一次 _dbg_lookup_lit_val 是否命中""" + return _g_dbg_lit_found def _new_const_str(pool: memhub.MemManager | t.CPtr, val: str, diff --git a/includes/string.vp b/includes/string.vp index e44a386..ecdc6f0 100644 --- a/includes/string.vp +++ b/includes/string.vp @@ -371,6 +371,19 @@ def atof(src: str) -> t.CDouble: src, int_part, frac_part, frac_div, result) return result + +def atof_out(src: str, out: t.CDouble | t.CPtr) -> t.CInt: + """atof 结果写入 out[0](输出参数方式,避免跨模块 double 返回值传递 bug) + + 返回值仅表示成功与否(1=成功,0=失败),结果值通过 out 指针传递, + 不依赖 double 返回值的跨模块传递。 + """ + if out is None: + return 0 + v: t.CDouble = atof(src) + out[0] = v + return 1 + def split(s: str, delim: str, result: t.CArray[str]) -> int: count: int = 0 start: str = s