From 3dd32df2c183a7da0850d1a6b54b27c6a36000a5 Mon Sep 17 00:00:00 2001 From: polsevev Date: Fri, 31 May 2024 16:09:49 +0200 Subject: [PATCH] cleanup --- README.md | 10 ++- demo-site/index.html | 19 ----- dsl_files/multi_stmt_test.jstql | 16 ---- dsl_files/pipelineDeep.jstql | 14 ---- dsl_files/star.jstql | 17 ---- dsl_files/test_single_stmt.jstql | 10 --- output_files/output.js | 94 --------------------- output_files/output_await_to_promise.js | 9 -- output_files/output_do.js | 12 --- output_files/output_multi.js | 2 - output_files/output_pipeline.js | 4 - output_files/pipeline_out.js | 4 - output_files/test2.js | 104 ----------------------- output_files/test_2.js | 94 --------------------- output_files/testing.js | 9 -- output_files/testingLOL.js | 98 ---------------------- src/evalRunner.ts | 66 +++++++++++++++ src/index.ts | 105 ++++++++---------------- test_files/multi_stmt_test.js | 7 -- test_files/single_stmt.js | 8 -- test_files/star_test.js | 4 - 21 files changed, 109 insertions(+), 597 deletions(-) delete mode 100644 demo-site/index.html delete mode 100644 dsl_files/multi_stmt_test.jstql delete mode 100644 dsl_files/pipelineDeep.jstql delete mode 100644 dsl_files/star.jstql delete mode 100644 dsl_files/test_single_stmt.jstql delete mode 100644 output_files/output.js delete mode 100644 output_files/output_await_to_promise.js delete mode 100644 output_files/output_do.js delete mode 100644 output_files/output_multi.js delete mode 100644 output_files/output_pipeline.js delete mode 100644 output_files/pipeline_out.js delete mode 100644 output_files/test2.js delete mode 100644 output_files/test_2.js delete mode 100644 output_files/testing.js delete mode 100644 output_files/testingLOL.js create mode 100644 src/evalRunner.ts delete mode 100644 test_files/multi_stmt_test.js delete mode 100644 test_files/single_stmt.js delete mode 100644 test_files/star_test.js diff --git a/README.md b/README.md index d3582dc..2204472 100644 --- a/README.md +++ b/README.md @@ -1 +1,9 @@ -# didactic-chainsaw \ No newline at end of file +# JSTQL JavaScript Transform tool + +This tool is created to transform JavaScript based on definitions written in a custom DSL called JSTQL + +## JSTQL + +This is a DSL created to define proposal transformations using templates. A transformation is defined by two templates, a template of what code snippets to search for **applicable to** and a template of how to transform those snippets **transform to**. + +Examples of definitions for proposals can be found in [dsl_files](./dsl_files/) diff --git a/demo-site/index.html b/demo-site/index.html deleted file mode 100644 index ed0b63f..0000000 --- a/demo-site/index.html +++ /dev/null @@ -1,19 +0,0 @@ - - - -Page Title - - - - - - -

Demo site

-

- - - \ No newline at end of file diff --git a/dsl_files/multi_stmt_test.jstql b/dsl_files/multi_stmt_test.jstql deleted file mode 100644 index dd92246..0000000 --- a/dsl_files/multi_stmt_test.jstql +++ /dev/null @@ -1,16 +0,0 @@ -proposal MultiStmt{ - case Smthn{ - applicable to{ - "let <> = <>(); - let <> = <>; - " - } - - transform to { - "const ident2 = () => { - let <> = <>(); - return <>; - }" - } - } -} \ No newline at end of file diff --git a/dsl_files/pipelineDeep.jstql b/dsl_files/pipelineDeep.jstql deleted file mode 100644 index aef9016..0000000 --- a/dsl_files/pipelineDeep.jstql +++ /dev/null @@ -1,14 +0,0 @@ -proposal Pipeline{ - - case SingleArgument { - applicable to { - "<>(<>(<>));" - } - - transform to { - "(<> |> <>(%)) |> <>(%);" - } - } - - -} \ No newline at end of file diff --git a/dsl_files/star.jstql b/dsl_files/star.jstql deleted file mode 100644 index 79f5483..0000000 --- a/dsl_files/star.jstql +++ /dev/null @@ -1,17 +0,0 @@ -proposal Star{ - case a { - applicable to { - "let <> = () => { - <> - return <>; - } - " - } - transform to { - "let <> = do { - <> - <> - }" - } - } -} \ No newline at end of file diff --git a/dsl_files/test_single_stmt.jstql b/dsl_files/test_single_stmt.jstql deleted file mode 100644 index 91aa31b..0000000 --- a/dsl_files/test_single_stmt.jstql +++ /dev/null @@ -1,10 +0,0 @@ -proposal test_single_stmt{ - pair one { - applicable to { - "let <> = <>" - } - transform to { - "let <> = 1 + <>;" - } - } -} \ No newline at end of file diff --git a/output_files/output.js b/output_files/output.js deleted file mode 100644 index 8c3c74a..0000000 --- a/output_files/output.js +++ /dev/null @@ -1,94 +0,0 @@ -function parse() { - const input = ("input" |> document.getElementById(%)).value; - const data = 32 |> input.slice(%); - const compressedData = data |> decode_base64(%); - const uncompressed = pako.inflate(compressedData, { - to: "string" - }); - const json = uncompressed |> JSON.parse(%); - json |> console.log(%); - json |> convertToDesktop(%); -} -function convertToDesktop(json) { - const newValues = { - crb: false, - newClanRaidClassId: 0, - newClanRaidClassLevel: 0, - pendingImmortalSouls: 0, - pendingRaidRubies: 0, - immortalSouls: 0, - lastPurchaseTime: 0, - lastRaidAttemptTimestamp: 0, - lastRaidRewardCheckTimestamp: 0, - shouldShowHZERoster: false, - lastBonusRewardCheckTimestamp: 0 - }; - const mappedValues = { - rubies: json.rubies / 10 |> Math.round(%) - }; - const pcSpecificValues = { - readPatchNumber: "1.0e12", - saveOrigin: "pc" - }; - const hash = "7a990d405d2c6fb93aa8fbb0ec1a3b23"; - const newData = { - ...newValues, - ...json, - ...mappedValues, - ...pcSpecificValues - }; - const compressed = pako.deflate(newData |> JSON.stringify(%), { - to: "string" - }); - const base64 = compressed |> btoa(%); - const finalSaveString = hash + base64; - ("output_output" |> document.getElementById(%)).innerText = finalSaveString; - showOutput(); -} -function showOutput() { - ("outputs" |> document.getElementById(%)).style.visibility = "visible"; -} -function copyOutput() { - const output = "output_output" |> document.getElementById(%); - output.disabled = false; - output.focus(); - output.select(); - "copy" |> document.execCommand(%); - output.disabled = true; - const successElement = "copy_success_msg" |> document.getElementById(%); - successElement.style.visibility = "visible"; - setTimeout(() => successElement.style.visibility = "hidden", 4000); -} -function decode_base64(s) { - let e = {}, - i, - k, - v = [], - r = "", - w = String.fromCharCode; - let n = [[65, 91], [97, 123], [48, 58], [43, 44], [47, 48]]; - for (z in n) { - for (i = n[z][0]; i < n[z][1]; i++) { - i |> w(%) |> v.push(%); - } - } - for (i = 0; i < 64; i++) { - e[v[i]] = i; - } - for (i = 0; i < s.length; i += 72) { - let b = 0, - c, - x, - l = 0, - o = s.substring(i, i + 72); - for (x = 0; x < o.length; x++) { - c = e[x |> o.charAt(%)]; - b = (b << 6) + c; - l += 6; - while (l >= 8) { - r += (b >>> (l -= 8)) % 256 |> w(%); - } - } - } - return r; -} \ No newline at end of file diff --git a/output_files/output_await_to_promise.js b/output_files/output_await_to_promise.js deleted file mode 100644 index dd7fdef..0000000 --- a/output_files/output_await_to_promise.js +++ /dev/null @@ -1,9 +0,0 @@ -async function something() { - let a = 100; - a *= 100000; - return fetch("https://uib.no").then(uib => { - a += 100000; - a -= 1000; - return [a, uib]; - }); -} \ No newline at end of file diff --git a/output_files/output_do.js b/output_files/output_do.js deleted file mode 100644 index 7551a1c..0000000 --- a/output_files/output_do.js +++ /dev/null @@ -1,12 +0,0 @@ -let aaaa = do { - let g = 100; - let ff = 10; - let ggg = a(b); - 100 -}; -var bbaaa = do { - let lllll = 1 + 1; - 100 + 100; - const aaaaa = aaaa(bb); - lllll -}; \ No newline at end of file diff --git a/output_files/output_multi.js b/output_files/output_multi.js deleted file mode 100644 index b4132cc..0000000 --- a/output_files/output_multi.js +++ /dev/null @@ -1,2 +0,0 @@ -let ThisTest = LOOOOOOOOL(); -let HAHHAHAH = 1 + 1; \ No newline at end of file diff --git a/output_files/output_pipeline.js b/output_files/output_pipeline.js deleted file mode 100644 index 696dfe6..0000000 --- a/output_files/output_pipeline.js +++ /dev/null @@ -1,4 +0,0 @@ -a |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%); -a |> b(%, a |> b(%, a |> b(%, a |> b(%, a |> b(%, a |> b(%, a |> b(%, b))))))); -a |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a); -b(b(b(b(a, a, a), a, a), a, a), a, a); \ No newline at end of file diff --git a/output_files/pipeline_out.js b/output_files/pipeline_out.js deleted file mode 100644 index 696dfe6..0000000 --- a/output_files/pipeline_out.js +++ /dev/null @@ -1,4 +0,0 @@ -a |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%) |> w(%); -a |> b(%, a |> b(%, a |> b(%, a |> b(%, a |> b(%, a |> b(%, a |> b(%, b))))))); -a |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a) |> b(%, a); -b(b(b(b(a, a, a), a, a), a, a), a, a); \ No newline at end of file diff --git a/output_files/test2.js b/output_files/test2.js deleted file mode 100644 index 8ff6b91..0000000 --- a/output_files/test2.js +++ /dev/null @@ -1,104 +0,0 @@ -return awaitedExpr.then(async _geometry => { - const _intersectPoint = /*@__PURE__*/new Vector3(); - const _worldScale = /*@__PURE__*/new Vector3(); - const _mvPosition = /*@__PURE__*/new Vector3(); - const _alignedPosition = /*@__PURE__*/new Vector2(); - const _rotatedPosition = /*@__PURE__*/new Vector2(); - const _viewWorldMatrix = /*@__PURE__*/new Matrix4(); - const _vA = /*@__PURE__*/new Vector3(); - const _vB = /*@__PURE__*/new Vector3(); - const _vC = /*@__PURE__*/new Vector3(); - const _uvA = /*@__PURE__*/new Vector2(); - const _uvB = /*@__PURE__*/new Vector2(); - const _uvC = /*@__PURE__*/new Vector2(); - class Sprite extends Object3D { - constructor(material = new SpriteMaterial()) { - super(); - this.isSprite = true; - this.type = "Sprite"; - if (_geometry === undefined) { - _geometry = new BufferGeometry(); - const float32Array = new Float32Array([-0.5, -0.5, 0, 0, 0, 0.5, -0.5, 0, 1, 0, 0.5, 0.5, 0, 1, 1, -0.5, 0.5, 0, 0, 1]); - const interleavedBuffer = new InterleavedBuffer(float32Array, 5); - _geometry.setIndex([0, 1, 2, 0, 2, 3]); - _geometry.setAttribute("position", new InterleavedBufferAttribute(interleavedBuffer, 3, 0, false)); - _geometry.setAttribute("uv", new InterleavedBufferAttribute(interleavedBuffer, 2, 3, false)); - } - this.geometry = _geometry; - this.material = material; - this.center = new Vector2(0.5, 0.5); - } - raycast(raycaster, intersects) { - if (raycaster.camera === null) { - console.error('THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.'); - } - _worldScale.setFromMatrixScale(this.matrixWorld); - _viewWorldMatrix.copy(raycaster.camera.matrixWorld); - this.modelViewMatrix.multiplyMatrices(raycaster.camera.matrixWorldInverse, this.matrixWorld); - _mvPosition.setFromMatrixPosition(this.modelViewMatrix); - if (raycaster.camera.isPerspectiveCamera && this.material.sizeAttenuation === false) { - _worldScale.multiplyScalar(-_mvPosition.z); - } - const rotation = this.material.rotation; - let sin, cos; - if (rotation !== 0) { - cos = Math.cos(rotation); - sin = Math.sin(rotation); - } - const center = this.center; - transformVertex(_vA.set(-0.5, -0.5, 0), _mvPosition, center, _worldScale, sin, cos); - transformVertex(_vB.set(0.5, -0.5, 0), _mvPosition, center, _worldScale, sin, cos); - transformVertex(_vC.set(0.5, 0.5, 0), _mvPosition, center, _worldScale, sin, cos); - _uvA.set(0, 0); - _uvB.set(1, 0); - _uvC.set(1, 1); - - // check first triangle - let intersect = raycaster.ray.intersectTriangle(_vA, _vB, _vC, false, _intersectPoint); - if (intersect === null) { - // check second triangle - transformVertex(_vB.set(-0.5, 0.5, 0), _mvPosition, center, _worldScale, sin, cos); - _uvB.set(0, 1); - intersect = raycaster.ray.intersectTriangle(_vA, _vC, _vB, false, _intersectPoint); - if (intersect === null) { - return; - } - } - const distance = raycaster.ray.origin.distanceTo(_intersectPoint); - if (distance < raycaster.near || distance > raycaster.far) return; - intersects.push({ - distance: distance, - point: _intersectPoint.clone(), - uv: Triangle.getInterpolation(_intersectPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2()), - face: null, - object: this - }); - } - copy(source, recursive) { - super.copy(source, recursive); - if (source.center !== undefined) this.center.copy(source.center); - this.material = source.material; - return this; - } - } - function transformVertex(vertexPosition, mvPosition, center, scale, sin, cos) { - // compute position in camera space - _alignedPosition.subVectors(vertexPosition, center).addScalar(0.5).multiply(scale); - - // to check if rotation is not zero - if (sin !== undefined) { - _rotatedPosition.x = cos * _alignedPosition.x - sin * _alignedPosition.y; - _rotatedPosition.y = sin * _alignedPosition.x + cos * _alignedPosition.y; - } else { - _rotatedPosition.copy(_alignedPosition); - } - vertexPosition.copy(mvPosition); - vertexPosition.x += _rotatedPosition.x; - vertexPosition.y += _rotatedPosition.y; - - // transform to world space - vertexPosition.applyMatrix4(_viewWorldMatrix); - } - export { Sprite }; - return returnExpr; -}); \ No newline at end of file diff --git a/output_files/test_2.js b/output_files/test_2.js deleted file mode 100644 index d0c2239..0000000 --- a/output_files/test_2.js +++ /dev/null @@ -1,94 +0,0 @@ -function parse() { - const input = ("input" |> document.getElementById(%)).value; - const data = 32 |> input.slice(%); - const compressedData = data |> decode_base64(%); - const uncompressed = compressedData |> pako.inflate(%, { - to: "string" - }); - const json = uncompressed |> JSON.parse(%); - json |> console.log(%); - json |> convertToDesktop(%); -} -function convertToDesktop(json) { - const newValues = { - crb: false, - newClanRaidClassId: 0, - newClanRaidClassLevel: 0, - pendingImmortalSouls: 0, - pendingRaidRubies: 0, - immortalSouls: 0, - lastPurchaseTime: 0, - lastRaidAttemptTimestamp: 0, - lastRaidRewardCheckTimestamp: 0, - shouldShowHZERoster: false, - lastBonusRewardCheckTimestamp: 0 - }; - const mappedValues = { - rubies: json.rubies / 10 |> Math.round(%) - }; - const pcSpecificValues = { - readPatchNumber: "1.0e12", - saveOrigin: "pc" - }; - const hash = "7a990d405d2c6fb93aa8fbb0ec1a3b23"; - const newData = { - ...newValues, - ...json, - ...mappedValues, - ...pcSpecificValues - }; - const compressed = newData |> JSON.stringify(%) |> pako.deflate(%, { - to: "string" - }); - const base64 = compressed |> btoa(%); - const finalSaveString = hash + base64; - ("output_output" |> document.getElementById(%)).innerText = finalSaveString; - showOutput(); -} -function showOutput() { - ("outputs" |> document.getElementById(%)).style.visibility = "visible"; -} -function copyOutput() { - const output = "output_output" |> document.getElementById(%); - output.disabled = false; - output.focus(); - output.select(); - "copy" |> document.execCommand(%); - output.disabled = true; - const successElement = "copy_success_msg" |> document.getElementById(%); - successElement.style.visibility = "visible"; - (() => successElement.style.visibility = "hidden") |> setTimeout(%, 4000); -} -function decode_base64(s) { - let e = {}, - i, - k, - v = [], - r = "", - w = String.fromCharCode; - let n = [[65, 91], [97, 123], [48, 58], [43, 44], [47, 48]]; - for (z in n) { - for (i = n[z][0]; i < n[z][1]; i++) { - i |> w(%) |> v.push(%); - } - } - for (i = 0; i < 64; i++) { - e[v[i]] = i; - } - for (i = 0; i < s.length; i += 72) { - let b = 0, - c, - x, - l = 0, - o = i |> s.substring(%, i + 72); - for (x = 0; x < o.length; x++) { - c = e[x |> o.charAt(%)]; - b = (b << 6) + c; - l += 6; - while (l >= 8) { - r += (b >>> (l -= 8)) % 256 |> w(%); - } - } - } - return r; -} \ No newline at end of file diff --git a/output_files/testing.js b/output_files/testing.js deleted file mode 100644 index dd7fdef..0000000 --- a/output_files/testing.js +++ /dev/null @@ -1,9 +0,0 @@ -async function something() { - let a = 100; - a *= 100000; - return fetch("https://uib.no").then(uib => { - a += 100000; - a -= 1000; - return [a, uib]; - }); -} \ No newline at end of file diff --git a/output_files/testingLOL.js b/output_files/testingLOL.js deleted file mode 100644 index 006ee6e..0000000 --- a/output_files/testingLOL.js +++ /dev/null @@ -1,98 +0,0 @@ -// "fast-glob" and `createTwoFilesPatch` are bundled here since the API uses `micromatch` and `diff` too -import { createTwoFilesPatch } from "diff/lib/patch/create.js"; -import fastGlob from "fast-glob"; -import * as vnopts from "vnopts"; -import * as errors from "./common/errors.js"; -import getFileInfoWithoutPlugins from "./common/get-file-info.js"; -import mockable from "./common/mockable.js"; -import { clearCache as clearConfigCache, resolveConfig, resolveConfigFile } from "./config/resolve-config.js"; -import * as core from "./main/core.js"; -import { formatOptionsHiddenDefaults } from "./main/normalize-format-options.js"; -import normalizeOptions from "./main/normalize-options.js"; -import * as optionCategories from "./main/option-categories.js"; -import { clearCache as clearPluginCache, loadBuiltinPlugins, loadPlugins } from "./main/plugins/index.js"; -import { getSupportInfo as getSupportInfoWithoutPlugins, normalizeOptionSettings } from "./main/support.js"; -import { createIsIgnoredFunction } from "./utils/ignore.js"; -import isNonEmptyArray from "./utils/is-non-empty-array.js"; -import omit from "./utils/object-omit.js"; -import partition from "./utils/partition.js"; - -/** - * @param {*} fn - * @param {number} [optionsArgumentIndex] - * @returns {*} - */ -function withPlugins(fn, optionsArgumentIndex = 1 // Usually `options` is the 2nd argument -) { - return async (...args) => { - const options = args[optionsArgumentIndex] ?? {}; - const { - plugins = [] - } = options; - args[optionsArgumentIndex] = { - ...options, - plugins: (await ([loadBuiltinPlugins(), plugins |> loadPlugins(%)] |> Promise.all(%))).flat() - }; - return fn(...args); - }; -} -const formatWithCursor = core.formatWithCursor |> withPlugins(%); -async function format(text, options) { - const { - formatted - } = await (text |> formatWithCursor(%, { - ...options, - cursorOffset: -1 - })); - return formatted; -} -async function check(text, options) { - return (await (text |> format(%, options))) === text; -} - -// eslint-disable-next-line require-await -async function clearCache() { - clearConfigCache(); - clearPluginCache(); -} - -/** @type {typeof getFileInfoWithoutPlugins} */ -const getFileInfo = getFileInfoWithoutPlugins |> withPlugins(%); - -/** @type {typeof getSupportInfoWithoutPlugins} */ -const getSupportInfo = getSupportInfoWithoutPlugins |> withPlugins(%, 0); - -// Internal shared with cli -const sharedWithCli = { - errors, - optionCategories, - createIsIgnoredFunction, - formatOptionsHiddenDefaults, - normalizeOptions, - getSupportInfoWithoutPlugins, - normalizeOptionSettings, - vnopts: { - ChoiceSchema: vnopts.ChoiceSchema, - apiDescriptor: vnopts.apiDescriptor - }, - fastGlob, - createTwoFilesPatch, - utils: { - isNonEmptyArray, - partition, - omit - }, - mockable -}; -const debugApis = { - parse: core.parse |> withPlugins(%), - formatAST: core.formatAst |> withPlugins(%), - formatDoc: core.formatDoc |> withPlugins(%), - printToDoc: core.printToDoc |> withPlugins(%), - printDocToString: core.printDocToString |> withPlugins(%), - mockable -}; -export { debugApis as __debug, sharedWithCli as __internal, check, clearCache as clearConfigCache, format, formatWithCursor, getFileInfo, getSupportInfo, resolveConfig, resolveConfigFile }; -export * as doc from "./document/public.js"; -export { default as version } from "./main/version.evaluate.cjs"; -export * as util from "./utils/public.js"; \ No newline at end of file diff --git a/src/evalRunner.ts b/src/evalRunner.ts new file mode 100644 index 0000000..e866042 --- /dev/null +++ b/src/evalRunner.ts @@ -0,0 +1,66 @@ +import { transform } from "./transform/transform"; +import { readdir } from "node:fs/promises"; +import { parseJSTQL } from "./langium/langiumRunner"; + +const main = async () => { + let basepathExamplesJSFiles = "../atom"; + let examples = (await readdir(basepathExamplesJSFiles, { recursive: true })) + .filter((x) => x.endsWith(".js")) + .map((x) => basepathExamplesJSFiles + "/" + x); + console.log(examples); + let result = []; + for (let proposalFile of [ + "pipeline.jstql", + "do.jstql", + "awaitToPromise.jstql", + ]) { + const jstql_file = "dsl_files/" + proposalFile; + const test_file = Bun.file(jstql_file); + const test_JSTQL = await test_file.text(); + let proposals = await parseJSTQL(test_JSTQL); + + let sum = 0; + let failures = 0; + let filesSucceeded = 0; + console.log("Scripts found ", sum, "matches!"); + let count = 0; + for (let examplesFile of examples) { + try { + if (examplesFile.split("/").includes("compiled")) { + //continue; + } + console.log(examplesFile); + let script = await Bun.file(examplesFile).text(); + let [resultString, matches] = transform( + proposals[0].cases, + script + ); + sum += matches; + console.log(matches); + if (matches > 0) { + await Bun.write( + "output_testing/" + + count + + examplesFile.split("/").join("_"), + resultString + ); + count += 1; + } + filesSucceeded += 1; + } catch (e) { + failures += 1; + //console.log(e); + } + console.log("current sum", sum); + } + result.push( + proposalFile + ", " + sum + ", " + count + ", " + filesSucceeded + ); + } + + for (let res of result) { + console.log(res); + } +}; + +main(); diff --git a/src/index.ts b/src/index.ts index 788ef18..e733ca2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -10,83 +10,46 @@ import { import { readdir } from "node:fs/promises"; import { parseJSTQL } from "./langium/langiumRunner"; -const dir = "../prettier/src"; +import { parseArgs } from "util"; -const path = "test_files/test2.js"; -const file = Bun.file(path); -const codeFromFile = await file.text(); +const options = { + o: { + type: "string", + }, +}; + +const { values: argVals, tokens: positional } = parseArgs({ + options, + tokens: true, + allowPositionals: true, +}); const main = async () => { //transform(selfHostedTransformExampleMultiStmt, codeFromFile); - - /* - console.log(codeFromFile); - const jstql_file = - "/home/rolfmg/Coding/Master/didactic-chainsaw/dsl_files/awaitToPromise.jstql"; - const test_file = Bun.file(jstql_file); - const test_JSTQL = await test_file.text(); - let proposals = await parseJSTQL(test_JSTQL); - - let [code, count] = transform(proposals[0].cases, codeFromFile); - await Bun.write("output_files/test2.js", code); - return; - */ - let basepathExamplesJSFiles = "../next.js"; - let examples = (await readdir(basepathExamplesJSFiles, { recursive: true })) - .filter((x) => x.endsWith(".js")) - .map((x) => basepathExamplesJSFiles + "/" + x); - console.log(examples); - let result = []; - for (let proposalFile of [ - "pipeline.jstql", - "do.jstql", - "awaitToPromise.jstql", - ]) { - const jstql_file = "dsl_files/" + proposalFile; - const test_file = Bun.file(jstql_file); - const test_JSTQL = await test_file.text(); - let proposals = await parseJSTQL(test_JSTQL); - - let sum = 0; - let failures = 0; - let filesSucceeded = 0; - console.log("Scripts found ", sum, "matches!"); - let count = 0; - for (let examplesFile of examples) { - try { - if (examplesFile.split("/").includes("compiled")) { - //continue; - } - console.log(examplesFile); - let script = await Bun.file(examplesFile).text(); - let [resultString, matches] = transform( - proposals[0].cases, - script - ); - sum += matches; - console.log(matches); - if (matches > 0) { - await Bun.write( - "output_testing/" + - count + - examplesFile.split("/").join("_"), - resultString - ); - count += 1; - } - filesSucceeded += 1; - } catch (e) { - failures += 1; - //console.log(e); - } - console.log("current sum", sum); - } - result.push( - proposalFile + ", " + sum + ", " + count + ", " + filesSucceeded + console.log(positional); + if (!positional[0] || !positional[0].value.endsWith(".jstql")) { + throw new Error("First positional argument is current JSTQL file"); + } + if (!positional[1] || !positional[1].value.endsWith(".js")) { + throw new Error( + "Second positional argument is JS code to be transformed" ); } + const jstql_file = positional[0].value; + const code_file = positional[1].value; - for (let res of result) { - console.log(res); + let jstqlString = await Bun.file(jstql_file).text(); + let codeString = await Bun.file(code_file).text(); + + let parsedJSTQL = await parseJSTQL(jstqlString); + + for (let proposal of parsedJSTQL) { + let [resultString, matches] = transform(proposal.cases, codeString); + + let path = "./"; + if (argVals["o"]) { + path = argVals["o"]; + } + Bun.write(path, resultString); } }; diff --git a/test_files/multi_stmt_test.js b/test_files/multi_stmt_test.js deleted file mode 100644 index 42b85cc..0000000 --- a/test_files/multi_stmt_test.js +++ /dev/null @@ -1,7 +0,0 @@ -let a = LOOOOOOOOL(); -let b = (999 * 128) / 12; - -const haha = () => { - let a = LOOOOOOOOL(); - let b = (999 * 128) / 12; -}; diff --git a/test_files/single_stmt.js b/test_files/single_stmt.js deleted file mode 100644 index d9e4cbe..0000000 --- a/test_files/single_stmt.js +++ /dev/null @@ -1,8 +0,0 @@ -let something = 1 + 1; -let yikers = hahahah; - -let lol = () => 100 + 100; - -function haha() { - let fhdsjkfhdsjkfhds = fjhdkslfjhdsklfjdskl; -} diff --git a/test_files/star_test.js b/test_files/star_test.js deleted file mode 100644 index 264faaf..0000000 --- a/test_files/star_test.js +++ /dev/null @@ -1,4 +0,0 @@ -let x = () => { - let b = 0; - return b; -};