2023-12-11 18:38:30 +00:00
|
|
|
import traverse from "@babel/traverse";
|
|
|
|
import * as t from "@babel/types";
|
2024-02-24 21:30:30 +00:00
|
|
|
import generate from "@babel/generator";
|
2024-05-12 18:06:37 +00:00
|
|
|
import {
|
2024-05-19 17:53:14 +00:00
|
|
|
Wildcard,
|
2024-05-15 18:55:03 +00:00
|
|
|
parseInternalAplTo,
|
|
|
|
parseInternalTraTo,
|
2024-05-12 18:06:37 +00:00
|
|
|
parse_with_plugins,
|
|
|
|
} from "../parser/parse";
|
2024-02-24 21:30:30 +00:00
|
|
|
import {
|
|
|
|
TreeNode,
|
|
|
|
makeTree,
|
|
|
|
showTree,
|
|
|
|
showTreePaired,
|
|
|
|
} from "../data_structures/tree";
|
|
|
|
import { runMatch } from "../matcher/matcher";
|
|
|
|
import { transformMatch, transformer } from "./transformMatch";
|
2024-05-11 14:09:03 +00:00
|
|
|
import { preludeBuilder } from "../parser/preludeBuilder";
|
2024-05-13 17:12:23 +00:00
|
|
|
import * as babelparser from "@babel/parser";
|
2024-05-12 18:06:37 +00:00
|
|
|
export interface Proposal {
|
2024-05-19 17:53:14 +00:00
|
|
|
cases: TransformRecipe[];
|
2024-05-12 18:06:37 +00:00
|
|
|
}
|
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
export interface TransformRecipe {
|
|
|
|
applicableTo: string;
|
|
|
|
transformTo: string;
|
2023-12-11 18:38:30 +00:00
|
|
|
}
|
2024-05-11 14:09:03 +00:00
|
|
|
export interface SelfHostedRecipe extends TransformRecipe {
|
|
|
|
prelude: string;
|
|
|
|
}
|
2024-05-23 11:46:40 +00:00
|
|
|
export function transform(
|
|
|
|
recipes: TransformRecipe[],
|
|
|
|
code: string
|
|
|
|
): [string, number] {
|
2024-05-19 17:53:14 +00:00
|
|
|
let codeAST: t.Node = parse_with_plugins(code);
|
2024-05-23 11:46:40 +00:00
|
|
|
let amount = 0;
|
2024-05-19 17:53:14 +00:00
|
|
|
for (let recipe of recipes) {
|
|
|
|
if ((<SelfHostedRecipe>recipe).prelude !== undefined) {
|
|
|
|
// We are using the self hosted version
|
2024-05-23 11:46:40 +00:00
|
|
|
let temp = transformSelfHosted(
|
2024-05-19 17:53:14 +00:00
|
|
|
{
|
|
|
|
applicableTo: recipe.applicableTo,
|
|
|
|
transformTo: recipe.transformTo,
|
|
|
|
},
|
|
|
|
preludeBuilder((recipe as SelfHostedRecipe).prelude),
|
|
|
|
codeAST
|
|
|
|
);
|
2024-05-23 11:46:40 +00:00
|
|
|
codeAST = temp[0];
|
|
|
|
amount += temp[1];
|
2024-05-19 17:53:14 +00:00
|
|
|
} else {
|
|
|
|
// We are using JSTQL
|
|
|
|
// We have to parse JSTQL to the self hosted version
|
2024-05-12 18:06:37 +00:00
|
|
|
|
2024-05-19 17:53:14 +00:00
|
|
|
let { cleanedJS: applicableTo, prelude } = parseInternalAplTo(
|
|
|
|
recipe.applicableTo
|
|
|
|
);
|
|
|
|
let transformTo = parseInternalTraTo(recipe.transformTo);
|
|
|
|
|
2024-05-23 11:46:40 +00:00
|
|
|
let temp = transformSelfHosted(
|
2024-05-19 17:53:14 +00:00
|
|
|
{ applicableTo, transformTo },
|
|
|
|
prelude,
|
|
|
|
codeAST
|
|
|
|
);
|
2024-05-23 11:46:40 +00:00
|
|
|
codeAST = temp[0];
|
|
|
|
amount += temp[1];
|
2024-05-19 17:53:14 +00:00
|
|
|
}
|
2024-05-11 14:09:03 +00:00
|
|
|
}
|
2024-05-19 17:53:14 +00:00
|
|
|
|
|
|
|
let output = generate(codeAST, { topicToken: "%" }).code;
|
|
|
|
//showTree(transformToTree);
|
2024-05-23 11:46:40 +00:00
|
|
|
return [output, amount];
|
2024-05-11 14:09:03 +00:00
|
|
|
}
|
|
|
|
|
2024-05-12 18:06:37 +00:00
|
|
|
function transformSelfHosted(
|
|
|
|
recipe: TransformRecipe,
|
2024-05-19 17:53:14 +00:00
|
|
|
internals: Wildcard[],
|
|
|
|
codeAST: t.Node
|
2024-05-23 11:46:40 +00:00
|
|
|
): [t.Node, number] {
|
2024-05-19 17:53:14 +00:00
|
|
|
let codeTree = makeTree(codeAST as babelparser.ParseResult<t.File>);
|
2024-05-11 14:09:03 +00:00
|
|
|
let applicabelToAST = parse_with_plugins(recipe.applicableTo);
|
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
let applicableToTree = makeTree(applicabelToAST);
|
|
|
|
let transformTo = parse_with_plugins(recipe.transformTo);
|
|
|
|
let transformToTree = makeTree(transformTo);
|
2024-05-15 18:55:03 +00:00
|
|
|
|
2024-05-11 14:09:03 +00:00
|
|
|
if (
|
|
|
|
codeTree == undefined ||
|
|
|
|
applicableToTree == undefined ||
|
|
|
|
transformToTree == undefined
|
|
|
|
) {
|
|
|
|
throw new Error("This no worky LOL");
|
|
|
|
}
|
|
|
|
let matches = runMatch(codeTree, applicableToTree, internals);
|
2024-05-23 11:46:40 +00:00
|
|
|
|
2024-05-19 17:53:14 +00:00
|
|
|
console.log("We found", matches.length, "matches");
|
2024-05-23 11:46:40 +00:00
|
|
|
|
2024-05-27 22:22:40 +00:00
|
|
|
let outputAST = transformer(matches, transformToTree, codeAST, transformTo);
|
2024-05-23 11:46:40 +00:00
|
|
|
|
|
|
|
console.log("Finished transforming");
|
|
|
|
return [outputAST, matches.length];
|
2024-02-24 21:30:30 +00:00
|
|
|
}
|