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 {
|
|
|
|
InternalDSLVariable,
|
|
|
|
parseInternal,
|
|
|
|
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 {
|
|
|
|
pairs: TransformRecipe[];
|
|
|
|
}
|
|
|
|
|
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-13 17:12:23 +00:00
|
|
|
export function transform(recipe: TransformRecipe, code: string): string {
|
2024-05-11 14:09:03 +00:00
|
|
|
if ((<SelfHostedRecipe>recipe).prelude !== undefined) {
|
|
|
|
// We are using the self hosted version
|
2024-05-12 18:06:37 +00:00
|
|
|
return transformSelfHosted(
|
|
|
|
{
|
|
|
|
applicableTo: recipe.applicableTo,
|
|
|
|
transformTo: recipe.transformTo,
|
|
|
|
},
|
|
|
|
preludeBuilder((recipe as SelfHostedRecipe).prelude),
|
|
|
|
code
|
|
|
|
);
|
2024-05-11 14:09:03 +00:00
|
|
|
} else {
|
|
|
|
// We are using JSTQL
|
2024-05-12 18:06:37 +00:00
|
|
|
// We have to parse JSTQL to the self hosted version
|
|
|
|
|
|
|
|
let { cleanedJS: applicableTo, prelude } = parseInternal(
|
|
|
|
recipe.applicableTo
|
|
|
|
);
|
|
|
|
let { cleanedJS: transformTo, prelude: _ } = parseInternal(
|
|
|
|
recipe.transformTo
|
|
|
|
);
|
|
|
|
|
|
|
|
return transformSelfHosted(
|
|
|
|
{ applicableTo, transformTo },
|
|
|
|
prelude,
|
|
|
|
code
|
|
|
|
);
|
2024-05-11 14:09:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-12 18:06:37 +00:00
|
|
|
function transformSelfHosted(
|
|
|
|
recipe: TransformRecipe,
|
|
|
|
internals: InternalDSLVariable,
|
|
|
|
code: string
|
2024-05-13 17:12:23 +00:00
|
|
|
): string {
|
2024-05-12 18:06:37 +00:00
|
|
|
console.log(recipe);
|
2024-02-24 21:30:30 +00:00
|
|
|
let codeAST = parse_with_plugins(code);
|
|
|
|
let codeTree = makeTree(codeAST);
|
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-11 14:09:03 +00:00
|
|
|
if (
|
|
|
|
codeTree == undefined ||
|
|
|
|
applicableToTree == undefined ||
|
|
|
|
transformToTree == undefined
|
|
|
|
) {
|
|
|
|
throw new Error("This no worky LOL");
|
|
|
|
}
|
|
|
|
showTree(applicableToTree);
|
2024-02-24 21:30:30 +00:00
|
|
|
|
2024-05-11 14:09:03 +00:00
|
|
|
let matches = runMatch(codeTree, applicableToTree, internals);
|
|
|
|
|
|
|
|
console.log(matches.length);
|
2024-05-12 18:06:37 +00:00
|
|
|
for (let match of matches.reverse()) {
|
2024-02-24 21:30:30 +00:00
|
|
|
//console.log(transformToTree.element);
|
2024-05-13 17:12:23 +00:00
|
|
|
// There is a bug here, for some reason it works sometimes when Program and sometimes when File, no clue why?????
|
|
|
|
let output = parse_with_plugins(recipe.transformTo).program;
|
2024-02-24 21:30:30 +00:00
|
|
|
try {
|
|
|
|
transformer(match, transformToTree, output, codeAST);
|
|
|
|
} catch (error) {
|
2024-05-13 17:12:23 +00:00
|
|
|
console.log(error);
|
2023-12-11 18:38:30 +00:00
|
|
|
}
|
2024-02-24 21:30:30 +00:00
|
|
|
}
|
|
|
|
console.log("Final generated code: \n");
|
2023-12-11 18:38:30 +00:00
|
|
|
|
2024-02-24 21:30:30 +00:00
|
|
|
let output = generate(codeAST, { topicToken: "%" }).code;
|
|
|
|
//showTree(transformToTree);
|
|
|
|
return output;
|
|
|
|
}
|