|
| 1 | +// Copyright (c) 2024 Roberto Raggi <[email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +import { groupNodesByBaseType } from "./groupNodesByBaseType.js"; |
| 22 | +import { AST, getASTNodes } from "./parseAST.js"; |
| 23 | +import { cpy_header } from "./cpy_header.js"; |
| 24 | +import * as fs from "fs"; |
| 25 | + |
| 26 | +export function gen_ast_visitor_cc({ |
| 27 | + ast, |
| 28 | + output, |
| 29 | +}: { |
| 30 | + ast: AST; |
| 31 | + output: string; |
| 32 | +}) { |
| 33 | + const code: string[] = []; |
| 34 | + const emit = (line = "") => code.push(line); |
| 35 | + |
| 36 | + const by_base = groupNodesByBaseType(ast); |
| 37 | + |
| 38 | + const types = new Set<string>(); |
| 39 | + |
| 40 | + ast.nodes.forEach((node) => { |
| 41 | + node.members.forEach((m) => { |
| 42 | + if (m.kind === "node" || m.kind === "node-list") types.add(m.type); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + by_base.forEach((nodes) => { |
| 47 | + nodes.forEach(({ name, members }) => { |
| 48 | + members = getASTNodes(members); |
| 49 | + |
| 50 | + emit(); |
| 51 | + emit(`void ASTVisitor::visit(${name}* ast) {`); |
| 52 | + members.forEach((m) => { |
| 53 | + if (m.kind === "node") { |
| 54 | + emit(`accept(ast->${m.name});`); |
| 55 | + } else if (m.kind === "node-list") { |
| 56 | + emit(`for (auto node : ListView{ast->${m.name}}) {`); |
| 57 | + emit(`accept(node);`); |
| 58 | + emit(`}`); |
| 59 | + } |
| 60 | + }); |
| 61 | + emit(`}`); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + const out = `${cpy_header} |
| 66 | +#include <cxx/ast_visitor.h> |
| 67 | +
|
| 68 | +// cxx |
| 69 | +#include <cxx/ast.h> |
| 70 | +
|
| 71 | +namespace cxx { |
| 72 | +
|
| 73 | +auto ASTVisitor::preVisit(AST*) -> bool { |
| 74 | + return true; |
| 75 | +} |
| 76 | +
|
| 77 | +void ASTVisitor::postVisit(AST*) {} |
| 78 | +
|
| 79 | +void ASTVisitor::accept(AST* ast) { |
| 80 | + if (!ast) return; |
| 81 | + if (preVisit(ast)) ast->accept(this); |
| 82 | + postVisit(ast); |
| 83 | +} |
| 84 | +
|
| 85 | +${code.join("\n")} |
| 86 | +
|
| 87 | +} // namespace cxx |
| 88 | +`; |
| 89 | + |
| 90 | + fs.writeFileSync(output, out); |
| 91 | +} |
0 commit comments