Skip to content

Commit e116031

Browse files
committed
Regenerate the ast interpreter
1 parent 7f4792d commit e116031

File tree

4 files changed

+1025
-900
lines changed

4 files changed

+1025
-900
lines changed

packages/cxx-gen-ast/src/new_ast_op_cc.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,32 @@ export function new_ast_op_cc({
4545
return name;
4646
};
4747

48+
by_base.forEach((nodes, base) => {
49+
if (base === "AST") return;
50+
emit();
51+
emit(` struct ${opName}::${chopAST(base)}Result {};`);
52+
});
53+
54+
by_base.get("AST")?.forEach(({ name }) => {
55+
emit();
56+
emit(` struct ${opName}::${chopAST(name)}Result {};`);
57+
});
58+
59+
by_base.forEach((nodes, base) => {
60+
if (!Array.isArray(nodes)) throw new Error("not an array");
61+
if (base === "AST") return;
62+
const className = chopAST(base);
63+
const resultTy = `${chopAST(base)}Result`;
64+
emit();
65+
emit(` struct ${opName}::${className}Visitor {`);
66+
emit(` ${opName}& accept;`);
67+
nodes.forEach(({ name }) => {
68+
emit();
69+
emit(` [[nodiscard]] auto operator()(${name}* ast) -> ${resultTy};`);
70+
});
71+
emit(` };`);
72+
});
73+
4874
by_base.forEach((nodes, base) => {
4975
if (base === "AST") return;
5076
const resultTy = `${chopAST(base)}Result`;
@@ -125,6 +151,8 @@ export function new_ast_op_cc({
125151
126152
namespace cxx {
127153
154+
${code.join("\n")}
155+
128156
${opName}::${opName}(TranslationUnit* unit) : unit_(unit) {}
129157
130158
${opName}::~${opName}() {}
@@ -133,8 +161,6 @@ auto ${opName}::control() const -> Control* {
133161
return unit_->control();
134162
}
135163
136-
${code.join("\n")}
137-
138164
} // namespace cxx
139165
`;
140166

packages/cxx-gen-ast/src/new_ast_op_h.ts

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,23 @@ export function new_ast_op_h({
4343
return name;
4444
};
4545

46+
emit(` // base nodes`);
4647
by_base.forEach((nodes, base) => {
4748
if (base === "AST") return;
48-
emit(` struct ${chopAST(base)}Result {};`);
49+
emit(` struct ${chopAST(base)}Result;`);
4950
});
50-
emit();
51+
52+
emit(` // misc nodes`);
5153
by_base.get("AST")?.forEach(({ name }) => {
52-
emit(` struct ${chopAST(name)}Result {};`);
54+
emit(` struct ${chopAST(name)}Result;`);
55+
});
56+
57+
emit(` // visitors`);
58+
by_base.forEach((nodes, base) => {
59+
if (!Array.isArray(nodes)) throw new Error("not an array");
60+
if (base === "AST") return;
61+
const className = chopAST(base);
62+
emit(` struct ${className}Visitor;`);
5363
});
5464

5565
emit();
@@ -63,24 +73,7 @@ export function new_ast_op_h({
6373
emit(` // run on the misc nodes`);
6474
by_base.get("AST")?.forEach(({ name }) => {
6575
const resultTy = `${chopAST(name)}Result`;
66-
emit(` auto operator()(${name}* ast) -> ${resultTy};`);
67-
});
68-
69-
emit();
70-
emit(`private:`);
71-
by_base.forEach((nodes, base) => {
72-
if (!Array.isArray(nodes)) throw new Error("not an array");
73-
if (base === "AST") return;
74-
const className = chopAST(base);
75-
const resultTy = `${chopAST(base)}Result`;
76-
emit();
77-
emit(` struct ${className}Visitor {`);
78-
emit(` ${opName}& accept;`);
79-
nodes.forEach(({ name }) => {
80-
emit();
81-
emit(` [[nodiscard]] auto operator()(${name}* ast) -> ${resultTy};`);
82-
});
83-
emit(` };`);
76+
emit(` [[nodiscard]] auto operator()(${name}* ast) -> ${resultTy};`);
8477
});
8578

8679
const out = `${cpy_header}
@@ -96,16 +89,18 @@ class Control;
9689
9790
class ${opName} {
9891
public:
99-
explicit ${opName}(TranslationUnit* unit);
100-
~${opName}();
92+
explicit ${opName}(TranslationUnit* unit);
93+
~${opName}();
10194
102-
[[nodiscard]] auto translationUnit() const -> TranslationUnit* { return unit_; }
95+
[[nodiscard]] auto translationUnit() const -> TranslationUnit* { return unit_; }
10396
104-
[[nodiscard]] auto control() const -> Control*;
97+
[[nodiscard]] auto control() const -> Control*;
10598
99+
private:
106100
${code.join("\n")}
107101
108-
TranslationUnit* unit_ = nullptr;
102+
private:
103+
TranslationUnit* unit_ = nullptr;
109104
};
110105
111106
} // namespace cxx

0 commit comments

Comments
 (0)