2024-02-24 21:30:30 +00:00
|
|
|
import * as t from "@babel/types";
|
|
|
|
|
|
|
|
import * as babelparser from "@babel/parser";
|
|
|
|
|
|
|
|
import {
|
|
|
|
TreeNode,
|
|
|
|
makeTree,
|
|
|
|
showTree,
|
|
|
|
showTreePaired,
|
|
|
|
} from "../data_structures/tree";
|
|
|
|
import { InternalDSLVariable } from "../parser/parse";
|
2024-05-11 14:09:03 +00:00
|
|
|
import { Match, MatchedTreeNode, PairedNodes } from "../matcher/matcher";
|
2024-02-24 21:30:30 +00:00
|
|
|
import traverse from "@babel/traverse";
|
|
|
|
|
|
|
|
export function transformer(
|
2024-05-11 14:09:03 +00:00
|
|
|
matches: Match,
|
2024-02-24 21:30:30 +00:00
|
|
|
trnTo: TreeNode<t.Node>,
|
|
|
|
output: t.Node,
|
|
|
|
inputCode: t.Node
|
|
|
|
) {
|
2024-05-11 14:09:03 +00:00
|
|
|
for (let match of matches.statements) {
|
|
|
|
transformMatch(match, trnTo, output);
|
|
|
|
}
|
2024-02-24 21:30:30 +00:00
|
|
|
|
|
|
|
if (output.type == "Program") {
|
|
|
|
output = output.body[0];
|
|
|
|
}
|
2024-05-11 14:09:03 +00:00
|
|
|
|
|
|
|
for (let match of matches.statements)
|
|
|
|
traverse(inputCode, {
|
|
|
|
enter(path) {
|
|
|
|
if (path.node === match.element.codeNode) {
|
|
|
|
path.replaceWith(output);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
2024-02-24 21:30:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function transformMatch(
|
|
|
|
match: TreeNode<PairedNodes>,
|
|
|
|
trnTo: TreeNode<t.Node>,
|
|
|
|
output: t.Node
|
|
|
|
) {
|
|
|
|
if (trnTo.element.type == "Program") {
|
|
|
|
return transformMatch(match, trnTo.children[0], output);
|
|
|
|
}
|
|
|
|
|
|
|
|
let isMatch = matchNode(match.element.aplToNode, trnTo.element);
|
|
|
|
if (isMatch) {
|
|
|
|
if (trnTo.element.type == "Identifier") {
|
|
|
|
traverse(output, {
|
|
|
|
enter(path) {
|
|
|
|
if (path.isIdentifier({ name: trnTo.element.name })) {
|
|
|
|
//console.log(match.element.codeNode);
|
|
|
|
if (match.element.codeNode) {
|
|
|
|
path.replaceWith(match.element.codeNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (let match_child of match.children) {
|
|
|
|
transformMatch(match_child, trnTo, output);
|
|
|
|
}
|
|
|
|
for (let trnTo_child of trnTo.children) {
|
|
|
|
transformMatch(match, trnTo_child, output);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function matchNode(aplTo: t.Node, trnTo: t.Node): boolean {
|
|
|
|
//console.log(trnTo);
|
|
|
|
|
|
|
|
if (trnTo.type == "Identifier" && aplTo.type == "Identifier") {
|
|
|
|
let aplToName = washName(aplTo.name);
|
|
|
|
let trnToName = trnTo.name;
|
|
|
|
if (aplToName == trnToName) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (trnTo.type == "Identifier" && aplTo.type == "Identifier") {
|
|
|
|
let aplToName = washName(aplTo.name);
|
|
|
|
let trnToName = trnTo.name;
|
|
|
|
|
|
|
|
if (aplToName == trnToName) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function washName(name: string): string {
|
2024-05-11 14:09:03 +00:00
|
|
|
return name;
|
|
|
|
if (name.startsWith("_-_")) {
|
2024-02-24 21:30:30 +00:00
|
|
|
return name.slice(3);
|
|
|
|
}
|
|
|
|
return name;
|
|
|
|
}
|