适配 STRING 名字参数 + 核心修复验证:锚点宏 dp≠0 守恒;expand_node_to_data private 后走 .var 公开路径;测试用 #define private public

This commit is contained in:
hermes committed 2026-08-26 13:14:51 +00:00
1 parent 6c74a1de4f
commit ec8c07e11f
3 files changed
+42 -17

No files matched your search

+5 -5
View File
@@ -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", {}}}}
}};
+26 -9
View File
@@ -1,11 +1,18 @@
#include "r8asm/r8asm.hh"
// 测试需要直接读写 ast 内部状态(scopes/macros/dataptr),生产代码保持 private。
// 仅在测试 TU 里临时放开访问权限;r8asm.hh 的依赖头必须先包含完,
// 防止 #define 污染标准库内部。
#include <cstddef>
#include <cstdint>
#include <exception>
#include <iostream>
#include <map>
#include <string>
#include <vector>
#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) {
+11 -3
View File
@@ -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)."""