diff --git a/src/replacements/list/$elm$core$List$append.js b/src/replacements/list/$elm$core$List$append.js new file mode 100644 index 0000000..088a054 --- /dev/null +++ b/src/replacements/list/$elm$core$List$append.js @@ -0,0 +1,12 @@ +var $elm$core$List$append = F2(function (xs, ys) { + var tmp = _List_Cons(undefined, _List_Nil); + var end = tmp; + for (; xs.b; xs = xs.b) { + var next = _List_Cons(xs.a, _List_Nil); + end.b = next; + end = next; + } + end.b = ys; + + return tmp.b; +}); diff --git a/src/replacements/list/$elm$core$List$concat.js b/src/replacements/list/$elm$core$List$concat.js new file mode 100644 index 0000000..e7c946e --- /dev/null +++ b/src/replacements/list/$elm$core$List$concat.js @@ -0,0 +1,18 @@ +var $elm$core$List$concat = function (lists) { + var tmp = _List_Cons(undefined, _List_Nil); + var end = tmp; + if (!lists.b) { + return _List_Nil; + } + for (; lists.b.b; lists = lists.b) { + var xs = lists.a; + for (; xs.b; xs = xs.b) { + var next = _List_Cons(xs.a, _List_Nil); + end.b = next; + end = next; + } + } + end.b = lists.a; + + return tmp.b; +}; diff --git a/src/replacements/list/$elm$core$List$filter.js b/src/replacements/list/$elm$core$List$filter.js new file mode 100644 index 0000000..db8085b --- /dev/null +++ b/src/replacements/list/$elm$core$List$filter.js @@ -0,0 +1,12 @@ +var $elm$core$List$filter = F2(function (f, xs) { + var tmp = _List_Cons(undefined, _List_Nil); + var end = tmp; + for (; xs.b; xs = xs.b) { + if (f(xs.a)) { + var next = _List_Cons(xs.a, _List_Nil); + end.b = next; + end = next; + } + } + return tmp.b; +}); diff --git a/src/replacements/list/$elm$core$List$indexedMap.js b/src/replacements/list/$elm$core$List$indexedMap.js new file mode 100644 index 0000000..8c4dd5d --- /dev/null +++ b/src/replacements/list/$elm$core$List$indexedMap.js @@ -0,0 +1,10 @@ +var $elm$core$List$indexedMap = F2(function (f, xs) { + var tmp = _List_Cons(undefined, _List_Nil); + var end = tmp; + for (var i = 0; xs.b; i++, xs = xs.b) { + var next = _List_Cons(A2(f, i, xs.a), _List_Nil); + end.b = next; + end = next; + } + return tmp.b; +}); diff --git a/src/replacements/list/$elm$core$List$intersperse.js b/src/replacements/list/$elm$core$List$intersperse.js new file mode 100644 index 0000000..b3908a6 --- /dev/null +++ b/src/replacements/list/$elm$core$List$intersperse.js @@ -0,0 +1,20 @@ +var $elm$core$List$intersperse = F2(function (sep, xs) { + if (!xs.b) { + return xs; + } + var tmp = _List_Cons(undefined, _List_Nil); + var end = tmp; + + end.b = _List_Cons(xs.a, _List_Nil); + end = end.b; + xs = xs.b; + + for (; xs.b; xs = xs.b) { + var valNode = _List_Cons(xs.a, _List_Nil); + var sepNode = _List_Cons(sep, valNode); + end.b = sepNode; + end = valNode; + } + + return tmp.b; +}); diff --git a/src/replacements/list/$elm$core$List$map.js b/src/replacements/list/$elm$core$List$map.js new file mode 100644 index 0000000..76b8ff3 --- /dev/null +++ b/src/replacements/list/$elm$core$List$map.js @@ -0,0 +1,10 @@ +var $elm$core$List$map = F2(function (f, xs) { + var tmp = _List_Cons(undefined, _List_Nil); + var end = tmp; + for (; xs.b; xs = xs.b) { + var next = _List_Cons(f(xs.a), _List_Nil); + end.b = next; + end = next; + } + return tmp.b; +}); diff --git a/src/replacements/list/$elm$core$List$partition.js b/src/replacements/list/$elm$core$List$partition.js new file mode 100644 index 0000000..dc25acf --- /dev/null +++ b/src/replacements/list/$elm$core$List$partition.js @@ -0,0 +1,17 @@ +var $elm$core$List$partition = F2(function (f, xs) { + var truesHead = _List_Cons(undefined, _List_Nil); + var falsesHead = _List_Cons(undefined, _List_Nil); + var truesEnd = truesHead; + var falsesEnd = falsesHead; + for (; xs.b; xs = xs.b) { + var next = _List_Cons(xs.a, _List_Nil); + if (f(xs.a)) { + truesEnd.b = next; + truesEnd = next; + } else { + falsesEnd.b = next; + falsesEnd = next; + } + } + return _Utils_Tuple2(truesHead.b, falsesHead.b); +}); diff --git a/src/replacements/list/$elm$core$List$unzip.js b/src/replacements/list/$elm$core$List$unzip.js new file mode 100644 index 0000000..865f952 --- /dev/null +++ b/src/replacements/list/$elm$core$List$unzip.js @@ -0,0 +1,18 @@ +var $elm$core$List$unzip = function (pairs) { + var aHead = _List_Cons(undefined, _List_Nil); + var bHead = _List_Cons(undefined, _List_Nil); + var aEnd = aHead; + var bEnd = bHead; + for (; pairs.b; pairs = pairs.b) { + var tuple = pairs.a; + + var aNext = _List_Cons(tuple.a, _List_Nil); + aEnd.b = aNext; + aEnd = aNext; + + var bNext = _List_Cons(tuple.b, _List_Nil); + bEnd.b = bNext; + bEnd = bNext; + } + return _Utils_Tuple2(aHead.b, bHead.b); +}; diff --git a/src/replacements/string/$elm$core$String$join.js b/src/replacements/string/$elm$core$String$join.js new file mode 100644 index 0000000..3560010 --- /dev/null +++ b/src/replacements/string/$elm$core$String$join.js @@ -0,0 +1,13 @@ +var $elm$core$String$join = F2(function (sep, strs) { + if (!strs.b) { + return ''; + } + var acc = '' + strs.a; + strs = strs.b; + + for (; strs.b; strs = strs.b) { + acc = '' + acc + sep + strs.a; + } + + return acc; +}); diff --git a/src/transforms/replace.ts b/src/transforms/replace.ts index 8e7544c..3349701 100644 --- a/src/transforms/replace.ts +++ b/src/transforms/replace.ts @@ -1,6 +1,7 @@ import ts, { isIdentifier } from 'typescript'; import { ast, astNodes } from './utils/create'; +import { readFilesSync } from '../fs_util'; export const replace = ( replacements: { [name: string]: string } @@ -26,3 +27,13 @@ export const replace = ( return ts.visitNode(sourceFile, visitor); }; }; + + +export const from_file = (path: string) => { + const read = readFilesSync(__dirname + path) + let replacements = {} + if (read) { + replacements = read + } + return replace(replacements) +} \ No newline at end of file diff --git a/src/transforms/replaceListFunctions.ts b/src/transforms/replaceListFunctions.ts deleted file mode 100644 index 9d817ec..0000000 --- a/src/transforms/replaceListFunctions.ts +++ /dev/null @@ -1,172 +0,0 @@ -import ts, { isIdentifier } from 'typescript'; -import { ast } from './utils/create'; - -const $elm$core$List$map = ` -var $elm$core$List$map = F2(function (f, xs) { - var tmp = _List_Cons(undefined, _List_Nil); - var end = tmp; - for (; xs.b; xs = xs.b) { - var next = _List_Cons(f(xs.a), _List_Nil); - end.b = next; - end = next; - } - return tmp.b; -}); -`; - -const $elm$core$List$indexedMap = ` -var $elm$core$List$indexedMap = F2(function (f, xs) { - var tmp = _List_Cons(undefined, _List_Nil); - var end = tmp; - for (var i = 0; xs.b; i++, xs = xs.b) { - var next = _List_Cons(A2(f, i, xs.a), _List_Nil); - end.b = next; - end = next; - } - return tmp.b; -}); -`; - -const $elm$core$List$filter = ` -var $elm$core$List$filter = F2(function (f, xs) { - var tmp = _List_Cons(undefined, _List_Nil); - var end = tmp; - for (; xs.b; xs = xs.b) { - if (f(xs.a)) { - var next = _List_Cons(xs.a, _List_Nil); - end.b = next; - end = next; - } - } - return tmp.b; -}); -`; - -const $elm$core$List$append = ` -var $elm$core$List$append = F2(function (xs, ys) { - var tmp = _List_Cons(undefined, _List_Nil); - var end = tmp; - for (; xs.b; xs = xs.b) { - var next = _List_Cons(xs.a, _List_Nil); - end.b = next; - end = next; - } - end.b = ys; - - return tmp.b; -}); -`; - -const $elm$core$List$concat = ` -var $elm$core$List$concat = function (lists) { - var tmp = _List_Cons(undefined, _List_Nil); - var end = tmp; - if (!lists.b) { - return _List_Nil; - } - for (; lists.b.b; lists = lists.b) { - var xs = lists.a; - for (; xs.b; xs = xs.b) { - var next = _List_Cons(xs.a, _List_Nil); - end.b = next; - end = next; - } - } - end.b = lists.a; - - return tmp.b; -}; -`; - -const $elm$core$List$intersperse = ` -var $elm$core$List$intersperse = F2(function (sep, xs) { - if (!xs.b) { - return xs; - } - var tmp = _List_Cons(undefined, _List_Nil); - var end = tmp; - - end.b = _List_Cons(xs.a, _List_Nil); - end = end.b; - xs = xs.b; - - for (; xs.b; xs = xs.b) { - var valNode = _List_Cons(xs.a, _List_Nil); - var sepNode = _List_Cons(sep, valNode); - end.b = sepNode; - end = valNode; - } - - return tmp.b; -}); -`; - -const $elm$core$List$partition = ` -var $elm$core$List$partition = F2(function (f, xs) { - var truesHead = _List_Cons(undefined, _List_Nil); - var falsesHead = _List_Cons(undefined, _List_Nil); - var truesEnd = truesHead; - var falsesEnd = falsesHead; - for (; xs.b; xs = xs.b) { - var next = _List_Cons(xs.a, _List_Nil); - if (f(xs.a)) { - truesEnd.b = next; - truesEnd = next; - } else { - falsesEnd.b = next; - falsesEnd = next; - } - } - return _Utils_Tuple2(truesHead.b, falsesHead.b); -}); -`; - -const $elm$core$List$unzip = ` -var $elm$core$List$unzip = function (pairs) { - var aHead = _List_Cons(undefined, _List_Nil); - var bHead = _List_Cons(undefined, _List_Nil); - var aEnd = aHead; - var bEnd = bHead; - for (; pairs.b; pairs = pairs.b) { - var tuple = pairs.a; - - var aNext = _List_Cons(tuple.a, _List_Nil); - aEnd.b = aNext; - aEnd = aNext; - - var bNext = _List_Cons(tuple.b, _List_Nil); - bEnd.b = bNext; - bEnd = bNext; - } - return _Utils_Tuple2(aHead.b, bHead.b); -}; -`; - - -const replacements = { - $elm$core$List$map, - $elm$core$List$indexedMap, - $elm$core$List$filter, - $elm$core$List$append, - $elm$core$List$concat, - $elm$core$List$intersperse, - $elm$core$List$partition, - $elm$core$List$unzip, -}; - -export const replaceListFunctions: ts.TransformerFactory = ( - context -) => (sourceFile) => { - const visitor = (node: ts.Node): ts.VisitResult => { - if (ts.isVariableStatement(node)) { - const name = node.declarationList.declarations[0]?.name; - if (isIdentifier(name) && name.text in replacements) { - const key = name.text as keyof typeof replacements; - return ast(replacements[key]); - } - } - return ts.visitEachChild(node, visitor, context); - }; - - return ts.visitNode(sourceFile, visitor); -}; diff --git a/src/transforms/replaceStringFunctions.ts b/src/transforms/replaceStringFunctions.ts deleted file mode 100644 index bd80298..0000000 --- a/src/transforms/replaceStringFunctions.ts +++ /dev/null @@ -1,40 +0,0 @@ -import ts, { isIdentifier } from 'typescript'; -import { ast } from './utils/create'; - -const $elm$core$String$join = ` -var $elm$core$String$join = F2(function (sep, strs) { - if (!strs.b) { - return ""; - } - var acc = strs.a; - strs = strs.b; - - for (; strs.b; strs = strs.b) { - acc = acc + sep + strs.a; - } - - return acc; -}; -`; - - -const replacements = { - $elm$core$String$join, -}; - -export const replaceStringFunctions: ts.TransformerFactory = ( - context -) => (sourceFile) => { - const visitor = (node: ts.Node): ts.VisitResult => { - if (ts.isVariableStatement(node)) { - const name = node.declarationList.declarations[0]?.name; - if (isIdentifier(name) && name.text in replacements) { - const key = name.text as keyof typeof replacements; - return ast(replacements[key]); - } - } - return ts.visitEachChild(node, visitor, context); - }; - - return ts.visitNode(sourceFile, visitor); -};