@grey-ts/transpiler 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/index.js +107 -5
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -18,4 +18,4 @@ bun/npx greyts transpile pathToMyFile.ts
|
|
|
18
18
|
```
|
|
19
19
|
|
|
20
20
|
|
|
21
|
-
|
|
21
|
+
To easily start writing your code for GreyHack, use [this template](https://github.com/OkkaOk/grey-ts-template) instead of using this manually.
|
package/dist/index.js
CHANGED
|
@@ -195,6 +195,7 @@ var propertyAccessReplacements = {
|
|
|
195
195
|
"Math.log": "log",
|
|
196
196
|
"String.prototype": "string",
|
|
197
197
|
"Number.prototype": "number",
|
|
198
|
+
"Boolean.prototype": "number",
|
|
198
199
|
"Object.prototype": "map",
|
|
199
200
|
"Array.prototype": "list",
|
|
200
201
|
"Function.prototype": "funcRef"
|
|
@@ -511,6 +512,52 @@ var utilFunctions2 = {
|
|
|
511
512
|
"\treturn out",
|
|
512
513
|
"end function"
|
|
513
514
|
].join(`
|
|
515
|
+
`),
|
|
516
|
+
array_push: [
|
|
517
|
+
"array_push = function(target, items)",
|
|
518
|
+
"\tfor item in items",
|
|
519
|
+
"\t\ttarget.push(item)",
|
|
520
|
+
"\tend for",
|
|
521
|
+
"\treturn target.len",
|
|
522
|
+
"end function"
|
|
523
|
+
].join(`
|
|
524
|
+
`),
|
|
525
|
+
array_unshift: [
|
|
526
|
+
"array_unshift = function(target, items)",
|
|
527
|
+
"\tif not items.len then return target.len",
|
|
528
|
+
"\tfor i in range(items.len-1)",
|
|
529
|
+
"\t\ttarget.insert(0, items[i])",
|
|
530
|
+
"\tend for",
|
|
531
|
+
"\treturn target.len",
|
|
532
|
+
"end function"
|
|
533
|
+
].join(`
|
|
534
|
+
`),
|
|
535
|
+
str_starts_with: [
|
|
536
|
+
"str_starts_with = function(str, search, pos = 0)",
|
|
537
|
+
"\tif pos < 0 then pos = 0",
|
|
538
|
+
"\treturn str.indexOf(search) == pos",
|
|
539
|
+
"end function"
|
|
540
|
+
].join(`
|
|
541
|
+
`),
|
|
542
|
+
str_ends_with: [
|
|
543
|
+
"str_ends_with = function(str, search, pos = null)",
|
|
544
|
+
"\tif pos == null then pos = str.len",
|
|
545
|
+
"\tif pos < 0 then pos = 0",
|
|
546
|
+
"\treturn str.indexOf(search) + search.len == pos",
|
|
547
|
+
"end function"
|
|
548
|
+
].join(`
|
|
549
|
+
`),
|
|
550
|
+
str_repeat: [
|
|
551
|
+
"str_repeat = function(str, count = 0)",
|
|
552
|
+
'\tif count <= 0 then return ""',
|
|
553
|
+
"\tif count == 1 then return str",
|
|
554
|
+
"\tout = str",
|
|
555
|
+
"\tfor i in range(count-2)",
|
|
556
|
+
"\t\tout = out + str",
|
|
557
|
+
"\tend for",
|
|
558
|
+
"\treturn out",
|
|
559
|
+
"end function"
|
|
560
|
+
].join(`
|
|
514
561
|
`),
|
|
515
562
|
math_min: [
|
|
516
563
|
"math_min = function(numbers)",
|
|
@@ -707,7 +754,7 @@ NodeHandler.register(ts5.SyntaxKind.SetAccessor, (node) => {
|
|
|
707
754
|
return "";
|
|
708
755
|
const body = NodeHandler.handle(node.body);
|
|
709
756
|
const params = node.parameters.map((param) => NodeHandler.handle(param));
|
|
710
|
-
return
|
|
757
|
+
return `set_${NodeHandler.handle(node.name)} = function(${params.join(", ")})
|
|
711
758
|
${body}
|
|
712
759
|
end function`;
|
|
713
760
|
});
|
|
@@ -762,6 +809,40 @@ CallTransformer.register("Array.every", (name, args) => {
|
|
|
762
809
|
throw "Invalid argument count";
|
|
763
810
|
return callUtilFunction("array_every", name.slice(0, name.lastIndexOf(".")), args[0]);
|
|
764
811
|
});
|
|
812
|
+
CallTransformer.register("Array.slice", (name, args) => {
|
|
813
|
+
return name.slice(0, name.lastIndexOf(".")) + `[${args[0] ?? ""}:${args[1] ?? ""}]`;
|
|
814
|
+
});
|
|
815
|
+
CallTransformer.register("Array.push", (name, args) => {
|
|
816
|
+
if (!args.length)
|
|
817
|
+
throw "Invalid argument count";
|
|
818
|
+
return callUtilFunction("array_push", name.slice(0, name.lastIndexOf(".")), args[0]);
|
|
819
|
+
});
|
|
820
|
+
CallTransformer.register("Array.unshift", (name, args) => {
|
|
821
|
+
if (!args.length)
|
|
822
|
+
throw "Invalid argument count";
|
|
823
|
+
return callUtilFunction("array_unshift", name.slice(0, name.lastIndexOf(".")), args[0]);
|
|
824
|
+
});
|
|
825
|
+
CallTransformer.register("Array.shift", (name) => {
|
|
826
|
+
return `${name.slice(0, name.lastIndexOf("."))}.pull`;
|
|
827
|
+
});
|
|
828
|
+
CallTransformer.register("String.startsWith", (name, args) => {
|
|
829
|
+
if (!args.length)
|
|
830
|
+
throw "Invalid argument count";
|
|
831
|
+
return callUtilFunction("str_starts_with", name.slice(0, name.lastIndexOf(".")), ...args);
|
|
832
|
+
});
|
|
833
|
+
CallTransformer.register("String.endsWith", (name, args) => {
|
|
834
|
+
if (!args.length)
|
|
835
|
+
throw "Invalid argument count";
|
|
836
|
+
return callUtilFunction("str_ends_with", name.slice(0, name.lastIndexOf(".")), ...args);
|
|
837
|
+
});
|
|
838
|
+
CallTransformer.register("String.repeat", (name, args) => {
|
|
839
|
+
if (!args.length)
|
|
840
|
+
throw "Invalid argument count";
|
|
841
|
+
return callUtilFunction("str_repeat", name.slice(0, name.lastIndexOf(".")), ...args);
|
|
842
|
+
});
|
|
843
|
+
CallTransformer.register("String.slice", (name, args) => {
|
|
844
|
+
return name.slice(0, name.lastIndexOf(".")) + `[${args[0] ?? ""}:${args[1] ?? ""}]`;
|
|
845
|
+
});
|
|
765
846
|
CallTransformer.register("Math.min", (name, args) => {
|
|
766
847
|
return callUtilFunction("math_min", `${args.join(",")}`);
|
|
767
848
|
});
|
|
@@ -794,6 +875,21 @@ CallTransformer.register("GreyHack.include", (name, args, node, ctx) => {
|
|
|
794
875
|
}
|
|
795
876
|
return "";
|
|
796
877
|
});
|
|
878
|
+
CallTransformer.register("Boolean", (name, args) => {
|
|
879
|
+
if (!args.length)
|
|
880
|
+
return "0";
|
|
881
|
+
return `(not (not ${args[0]}))`;
|
|
882
|
+
});
|
|
883
|
+
CallTransformer.register("Number", (name, args) => {
|
|
884
|
+
if (!args.length)
|
|
885
|
+
return "0";
|
|
886
|
+
return `str(${args[0]}).val`;
|
|
887
|
+
});
|
|
888
|
+
CallTransformer.register("String", (name, args) => {
|
|
889
|
+
if (!args.length)
|
|
890
|
+
return "";
|
|
891
|
+
return `str(${args[0]})`;
|
|
892
|
+
});
|
|
797
893
|
|
|
798
894
|
// src/visitors/expressions.ts
|
|
799
895
|
function hasRestParam(params) {
|
|
@@ -915,11 +1011,17 @@ NodeHandler.register(ts7.SyntaxKind.BinaryExpression, (node) => {
|
|
|
915
1011
|
if (ts7.isPropertyAccessExpression(node.left)) {
|
|
916
1012
|
const leftType = checker.getTypeAtLocation(node.left.expression);
|
|
917
1013
|
const symbol = leftType.getProperty(node.left.name.text);
|
|
918
|
-
if (symbol?.declarations
|
|
919
|
-
|
|
920
|
-
|
|
1014
|
+
if (symbol?.declarations?.some((decl) => ts7.isSetAccessor(decl))) {
|
|
1015
|
+
const objName = NodeHandler.handle(node.left.expression);
|
|
1016
|
+
const key = node.left.name.text;
|
|
1017
|
+
if (operatorToken !== "=" && symbol.declarations.some((decl) => ts7.isGetAccessor(decl)))
|
|
1018
|
+
throw `Can't handle '${operatorToken}' because '${objName}' doesn't have a getter '${key}'`;
|
|
1019
|
+
if (operatorToken === "+=" || operatorToken === "-=") {
|
|
1020
|
+
right = `${objName}.${key} ${operatorToken[0]} ${right}`;
|
|
1021
|
+
} else if (operatorToken !== "=") {
|
|
1022
|
+
throw `The transpiler can't handle ${operatorToken} operator for setters yet`;
|
|
921
1023
|
}
|
|
922
|
-
return `${
|
|
1024
|
+
return `${objName}.set_${key}(${right})`;
|
|
923
1025
|
}
|
|
924
1026
|
}
|
|
925
1027
|
let left = NodeHandler.handle(node.left);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@grey-ts/transpiler",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"author": "Okka",
|
|
5
5
|
"module": "src/index.ts",
|
|
6
6
|
"bin": {
|
|
@@ -39,6 +39,6 @@
|
|
|
39
39
|
"typescript": "^5"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@grey-ts/types": "
|
|
42
|
+
"@grey-ts/types": "^1.3.0"
|
|
43
43
|
}
|
|
44
44
|
}
|