starting to be something xDDDDD

This commit is contained in:
Rolf Martin Glomsrud 2023-12-11 19:38:30 +01:00
parent 50deec77f4
commit ccd6a2ec94
5 changed files with 2361 additions and 2226 deletions

29
package-lock.json generated
View file

@ -10,13 +10,13 @@
"@babel/generator": "^7.23.0", "@babel/generator": "^7.23.0",
"@babel/parser": "^7.23.0", "@babel/parser": "^7.23.0",
"@babel/traverse": "^7.23.0", "@babel/traverse": "^7.23.0",
"@types/babel-traverse": "^6.25.10",
"babel": "^6.23.0", "babel": "^6.23.0",
"bun": "^1.0.4", "bun": "^1.0.4",
"ts-node": "^10.9.1" "ts-node": "^10.9.1"
}, },
"devDependencies": { "devDependencies": {
"@swc/cli": "^0.1.62", "@swc/cli": "^0.1.62",
"@swc/core": "^1.3.83",
"@types/node": "^20.5.9", "@types/node": "^20.5.9",
"bun-types": "latest", "bun-types": "latest",
"typescript": "^5.2.2" "typescript": "^5.2.2"
@ -414,6 +414,7 @@
"integrity": "sha512-PccHDgGQlFjpExgJxH91qA3a4aifR+axCFJ4RieCoiI0m5gURE4nBhxzTBY5YU/YKTBmPO8Gc5Q6inE3+NquWg==", "integrity": "sha512-PccHDgGQlFjpExgJxH91qA3a4aifR+axCFJ4RieCoiI0m5gURE4nBhxzTBY5YU/YKTBmPO8Gc5Q6inE3+NquWg==",
"devOptional": true, "devOptional": true,
"hasInstallScript": true, "hasInstallScript": true,
"peer": true,
"dependencies": { "dependencies": {
"@swc/types": "^0.1.4" "@swc/types": "^0.1.4"
}, },
@ -457,6 +458,7 @@
"os": [ "os": [
"darwin" "darwin"
], ],
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -473,6 +475,7 @@
"os": [ "os": [
"darwin" "darwin"
], ],
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -489,6 +492,7 @@
"os": [ "os": [
"linux" "linux"
], ],
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -505,6 +509,7 @@
"os": [ "os": [
"linux" "linux"
], ],
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -521,6 +526,7 @@
"os": [ "os": [
"linux" "linux"
], ],
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -537,6 +543,7 @@
"os": [ "os": [
"linux" "linux"
], ],
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -553,6 +560,7 @@
"os": [ "os": [
"linux" "linux"
], ],
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -569,6 +577,7 @@
"os": [ "os": [
"win32" "win32"
], ],
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -585,6 +594,7 @@
"os": [ "os": [
"win32" "win32"
], ],
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -601,6 +611,7 @@
"os": [ "os": [
"win32" "win32"
], ],
"peer": true,
"engines": { "engines": {
"node": ">=10" "node": ">=10"
} }
@ -609,7 +620,8 @@
"version": "0.1.4", "version": "0.1.4",
"resolved": "https://registry.npmjs.org/@swc/types/-/types-0.1.4.tgz", "resolved": "https://registry.npmjs.org/@swc/types/-/types-0.1.4.tgz",
"integrity": "sha512-z/G02d+59gyyUb7KYhKi9jOhicek6QD2oMaotUyG+lUkybpXoV49dY9bj7Ah5Q+y7knK2jU67UTX9FyfGzaxQg==", "integrity": "sha512-z/G02d+59gyyUb7KYhKi9jOhicek6QD2oMaotUyG+lUkybpXoV49dY9bj7Ah5Q+y7knK2jU67UTX9FyfGzaxQg==",
"devOptional": true "devOptional": true,
"peer": true
}, },
"node_modules/@szmarczak/http-timer": { "node_modules/@szmarczak/http-timer": {
"version": "4.0.6", "version": "4.0.6",
@ -649,6 +661,19 @@
"resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz",
"integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==" "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA=="
}, },
"node_modules/@types/babel-traverse": {
"version": "6.25.10",
"resolved": "https://registry.npmjs.org/@types/babel-traverse/-/babel-traverse-6.25.10.tgz",
"integrity": "sha512-B3XitTFG8YeXb5lr7Nj62t1DikCDuAJ/4BDeK6GCuWREEmdunI9DWnv+81oHl2yQBQPWY/C1PmV3vaRZB4LQmw==",
"dependencies": {
"@types/babel-types": "*"
}
},
"node_modules/@types/babel-types": {
"version": "7.0.15",
"resolved": "https://registry.npmjs.org/@types/babel-types/-/babel-types-7.0.15.tgz",
"integrity": "sha512-JUgfZHUOMbtjopxiOQaaF+Uovk5wpDqpXR+XLWiOivCWSy1FccO30lvNNpCt8geFwq8VmGT2y9OMkOpA0g5O5g=="
},
"node_modules/@types/cacheable-request": { "node_modules/@types/cacheable-request": {
"version": "6.0.3", "version": "6.0.3",
"resolved": "https://registry.npmjs.org/@types/cacheable-request/-/cacheable-request-6.0.3.tgz", "resolved": "https://registry.npmjs.org/@types/cacheable-request/-/cacheable-request-6.0.3.tgz",

View file

@ -16,6 +16,7 @@
"@babel/generator": "^7.23.0", "@babel/generator": "^7.23.0",
"@babel/parser": "^7.23.0", "@babel/parser": "^7.23.0",
"@babel/traverse": "^7.23.0", "@babel/traverse": "^7.23.0",
"@types/babel-traverse": "^6.25.10",
"babel": "^6.23.0", "babel": "^6.23.0",
"bun": "^1.0.4", "bun": "^1.0.4",
"ts-node": "^10.9.1" "ts-node": "^10.9.1"

View file

@ -1,10 +1,11 @@
import * as babelparser from "../babel/packages/babel-parser"; import * as babelparser from "../babel/packages/babel-parser";
import { parseApplicableTo } from "./parser/parse";
import { TransformRecipe, transform } from "./transform/transform";
/* /*
proposal await_to_promise { proposal await_to_promise {
applicable to { applicable to {
let a = await b(); let a = await b();
console.log(b); <<CONSUME>>
} }
transform to { transform to {
@ -14,13 +15,22 @@ proposal await_to_promise {
} }
} }
*/ */
const transformExample:TransformRecipe = {
applicableTo: `let a = await b();<<REST_BLOCK:rest>>`,
consumeBlock: true,
identifiers: ["b", "a", "rest"],
transformTo: "b().then((a) => {<<REST_BLOCK:rest>>})"
}
const code = "let a = await b(); console.log(a);"
const main = ( const main = (): void => {
transform(transformExample, code);
): void => {};
};
main(); main();

50
src/parser/parse.ts Normal file
View file

@ -0,0 +1,50 @@
// This needs to support multiple commands in future
export interface Command{
commandName: string,
commandIdentifier: string,
}
export interface ApplicableToResult{
commands: Command[],
applicableTo: string,
}
export function parseApplicableTo(applicableTo:string) :ApplicableToResult {
let applicableToIter = applicableTo[Symbol.iterator]();
let applicableToOut = "";
let commands:Command[] = [];
let curCommandName = "";
let curCommandIdentifier = "";
let nextIter;
while(!(nextIter = applicableToIter.next()).done){
if (nextIter.value === "<" && applicableToIter.next().value === "<") {
let commandChar;
let commandName = "";
while((commandChar = applicableToIter.next()).value != ":"){
commandName += commandChar.value;
}
let commandIdentifier = "";
while((commandChar = applicableToIter.next()).value != ">"){
commandIdentifier += commandChar.value;
}
let _ = applicableToIter.next();
commands.push({commandIdentifier, commandName});
}else{
applicableToOut += nextIter.value;
}
}
return {applicableTo:applicableToOut, commands};
}

View file

@ -0,0 +1,49 @@
import * as babelparser from "@babel/parser";
import traverse from "@babel/traverse";
import * as t from "@babel/types";
import { parseApplicableTo } from "../parser/parse";
export interface TransformRecipe{
applicableTo: string,
identifiers: string[],
consumeBlock: boolean,
transformTo: string,
}
export function transform(recipe: TransformRecipe, code:string){
let {commands, applicableTo} = parseApplicableTo(recipe.applicableTo);
//console.log(applicableTo);
let applicableToAST = babelparser.parse(applicableTo, {allowAwaitOutsideFunction: true});
console.log(applicableToAST);
let codeAST = babelparser.parse(code, {allowAwaitOutsideFunction: true});
console.log(codeAST);
traverse(applicableToAST, {
enter(path:any) {
traverse(codeAST, {
enter(codePath:any){
if (codePath.node.type === "Program"){
return;
}
if (codePath.node.type === path.node.type){
console.log("We found a match with");
console.log(codePath.node);
}
}
})
}
})
}