From ec8c07e11f51bf0c5abf58a13b4bfdf97a100c9d Mon Sep 17 00:00:00 2001 From: hermes Date: Wed, 26 Aug 2026 13:14:51 +0000 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=20STRING=20=E5=90=8D?= =?UTF-8?q?=E5=AD=97=E5=8F=82=E6=95=B0=20+=20=E6=A0=B8=E5=BF=83=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E9=AA=8C=E8=AF=81:=E9=94=9A=E7=82=B9=E5=AE=8F=20dp?= =?UTF-8?q?=E2=89=A00=20=E5=AE=88=E6=81=92;expand=5Fnode=5Fto=5Fdata=20pri?= =?UTF-8?q?vate=20=E5=90=8E=E8=B5=B0=20.var=20=E5=85=AC=E5=BC=80=E8=B7=AF?= =?UTF-8?q?=E5=BE=84;=E6=B5=8B=E8=AF=95=E7=94=A8=20#define=20private=20pub?= =?UTF-8?q?lic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/macros_gen.inc | 10 +++++----- tests/r8_test.cc | 35 ++++++++++++++++++++++++++--------- tools/gen_macros.py | 14 +++++++++++--- 3 files changed, 42 insertions(+), 17 deletions(-) diff --git a/tests/macros_gen.inc b/tests/macros_gen.inc index f0cd2d7..e9df560 100644 --- a/tests/macros_gen.inc +++ b/tests/macros_gen.inc @@ -22,7 +22,7 @@ ast_node{ast_node_t::REPEAT, "", {ast_node{ast_node_t::VARIABLE, "_ROL_STEP", {}}, ast_node{ast_node_t::CODEBLOCK, "", {ast_node{ast_node_t::ROT8, "rot8_ror", {}}}}}} }}; a.macros["clb_bit_mov"] = ast_node{ast_node_t::CODEBLOCK, "", { - ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::VARIABLE, "before_clb_bit_mov", {}}}}, + ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::STRING, "before_clb_bit_mov", {}}}}, ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "1", {}}}}, ast_node{ast_node_t::MACROCALL, "set_0", {}}, ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_clb_bit_mov", {}}}}, @@ -35,7 +35,7 @@ ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_clb_bit_mov", {}}}} }}; a.macros["bit_copy"] = ast_node{ast_node_t::CODEBLOCK, "", { - ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::VARIABLE, "before_bit_copy", {}}}}, + ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::STRING, "before_bit_copy", {}}}}, ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "1", {}}}}, ast_node{ast_node_t::MACROCALL, "set_0", {}}, ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "2", {}}}}, @@ -54,18 +54,18 @@ ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_bit_copy", {}}}} }}; a.macros["copy"] = ast_node{ast_node_t::CODEBLOCK, "", { - ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::VARIABLE, "before_copy", {}}}}, + ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::STRING, "before_copy", {}}}}, ast_node{ast_node_t::REPEAT, "", {ast_node{ast_node_t::VARIABLE, "_CELL_SIZE", {}}, ast_node{ast_node_t::CODEBLOCK, "", {ast_node{ast_node_t::MACROCALL, "bit_copy", {ast_node{ast_node_t::PARAMETER, "1", {}}, ast_node{ast_node_t::PARAMETER, "2", {}}}}, ast_node{ast_node_t::ROT8, "rot8_ror", {}}, ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "1", {}}}}, ast_node{ast_node_t::ROT8, "rot8_ror", {}}, ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::PARAMETER, "2", {}}}}, ast_node{ast_node_t::ROT8, "rot8_ror", {}}, ast_node{ast_node_t::BUILTIN, "builtin_tp", {ast_node{ast_node_t::VARIABLE, "before_copy", {}}}}}}}} }}; a.macros["bit_copy_to_buffer"] = ast_node{ast_node_t::CODEBLOCK, "", { ast_node{ast_node_t::ROT8, "rot8_stp", {}}, - ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::VARIABLE, "bit_copy_to_buffer_buf_1", {}}}}, + ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::STRING, "bit_copy_to_buffer_buf_1", {}}}}, ast_node{ast_node_t::ROT8, "rot8_btp", {}}, ast_node{ast_node_t::MACROCALL, "bit_copy", {ast_node{ast_node_t::VARIABLE, "bit_copy_to_buffer_buf_1", {}}, ast_node{ast_node_t::PARAMETER, "1", {}}}} }}; a.macros["copy_to_buffer"] = ast_node{ast_node_t::CODEBLOCK, "", { ast_node{ast_node_t::ROT8, "rot8_stp", {}}, - ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::VARIABLE, "copy_to_buffer_buf_1", {}}}}, + ast_node{ast_node_t::BUILTIN, "builtin_anchor", {ast_node{ast_node_t::STRING, "copy_to_buffer_buf_1", {}}}}, ast_node{ast_node_t::ROT8, "rot8_btp", {}}, ast_node{ast_node_t::MACROCALL, "copy", {ast_node{ast_node_t::VARIABLE, "copy_to_buffer_buf_1", {}}, ast_node{ast_node_t::PARAMETER, "1", {}}}} }}; diff --git a/tests/r8_test.cc b/tests/r8_test.cc index 98fafbf..11d4d40 100644 --- a/tests/r8_test.cc +++ b/tests/r8_test.cc @@ -1,11 +1,18 @@ -#include "r8asm/r8asm.hh" - +// 测试需要直接读写 ast 内部状态(scopes/macros/dataptr),生产代码保持 private。 +// 仅在测试 TU 里临时放开访问权限;r8asm.hh 的依赖头必须先包含完, +// 防止 #define 污染标准库内部。 +#include #include #include #include +#include #include #include +#define private public +#include "r8asm/r8asm.hh" +#undef private + using ast = r8asm::ast; using ast_node = r8asm::ast_node; using ast_node_t = r8asm::ast_node_t; @@ -82,19 +89,29 @@ int main() { {ast_node_t::CODEBLOCK, "", {{ast_node_t::ROT8, "rot8_stp", {}}}}}}}}, 6, false, 3); - // nested macros: assert non-empty + dataptr conserved, print actual length + // 顺序即状态流:dataptr 在用例间传递(mixed codeblock 之后锚点宏从 dp=3 起步)。 + // 锚点宏的展开指令数依赖起始 dataptr,所以精确计数只在当前顺序下有效—— + // 重排用例需重标;conserve 检查(回到起点)是不依赖顺序的真正不变量。 test("bit_copy 3 5", mcall("bit_copy", {cst(3), cst(5)}), 0, true); test("copy 3 5", mcall("copy", {cst(3), cst(5)}), 0, true); test("bit_copy_to_buffer 5", mcall("bit_copy_to_buffer", {cst(5)}), 0, true); test("copy_to_buffer 5", mcall("copy_to_buffer", {cst(5)}), 0, true); - // expand_node_to_data: expression evaluation with op_ prefixed operators - auto data_test = [&](const char *name, ast_node node, int32_t expect) { + // expand_node_to_data 已 private,走公开路径:通过 .var 展开表达式, + // 再从作用域读结果(顺带覆盖 STRING 名字参数 + VAR 写入路径) + auto data_test = [&](const char *name, ast_node expr, int32_t expect) { + static int seq = 0; + std::string vn = "t" + std::to_string(++seq); try { - int32_t v = a.expand_node_to_data(node); - bool ok = (v == expect); - std::cout << "expr " << name << ": " << v << (ok ? "" : " FAIL") - << " (expect " << expect << ")\n"; + a.expand_node_to_ins( + {ast_node_t::BUILTIN, "builtin_var", + {{ast_node_t::STRING, vn, {}}, expr}}); + auto it = a.scopes[0].find(vn); + bool ok = (it != a.scopes[0].end()) && (it->second == expect); + std::cout << "expr " << name << ": " + << (it != a.scopes[0].end() ? std::to_string(it->second) + : "未写入") + << (ok ? "" : " FAIL") << " (expect " << expect << ")\n"; if (!ok) fail++; } catch (const std::exception &e) { diff --git a/tools/gen_macros.py b/tools/gen_macros.py index b088e5c..989db4f 100644 --- a/tools/gen_macros.py +++ b/tools/gen_macros.py @@ -30,16 +30,24 @@ def expr(tok): def line_to_node(tokens): head = tokens[0] - args = ", ".join(expr(t) for t in tokens[1:]) + args = [] if head.startswith("."): - return f'ast_node{{ast_node_t::BUILTIN, "builtin_{head[1:]}", {{{args}}}}}' + for i, t in enumerate(tokens[1:]): + # .anchor/.var 的第一个参数是名字字面量 -> STRING 节点 + if head in (".anchor", ".var") and i == 0: + args.append(f'ast_node{{ast_node_t::STRING, "{t}", {{}}}}') + else: + args.append(expr(t)) + return f'ast_node{{ast_node_t::BUILTIN, "builtin_{head[1:]}", {{{", ".join(args)}}}}}' if head in ROT8: if len(tokens) > 1: body = (f'ast_node{{ast_node_t::CODEBLOCK, "", ' f'{{ast_node{{ast_node_t::ROT8, "rot8_{head}", {{}}}}}}}}') return f'ast_node{{ast_node_t::REPEAT, "", {{{expr(tokens[1])}, {body}}}}}' return f'ast_node{{ast_node_t::ROT8, "rot8_{head}", {{}}}}' - return f'ast_node{{ast_node_t::MACROCALL, "{head}", {{{args}}}}}' + for t in tokens[1:]: + args.append(expr(t)) + return f'ast_node{{ast_node_t::MACROCALL, "{head}", {{{", ".join(args)}}}}}' def parse_body(lines): """Lines of a macro body -> list of node initializer strings (%rep handled)."""