@grey-ts/transpiler 0.1.0 → 0.3.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 +137 -6
- 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
|
@@ -174,8 +174,11 @@ var apiNameMap = {
|
|
|
174
174
|
"String.isMatch": "is_match",
|
|
175
175
|
"String.toInt": "to_int",
|
|
176
176
|
"String.length": "len",
|
|
177
|
+
"String.toLowerCase": "lower",
|
|
178
|
+
"String.toUpperCase": "upper",
|
|
177
179
|
"Object.size": "len",
|
|
178
|
-
"Array.length": "len"
|
|
180
|
+
"Array.length": "len",
|
|
181
|
+
"Array.shift": "pull"
|
|
179
182
|
};
|
|
180
183
|
var propertyAccessReplacements = {
|
|
181
184
|
"Math.PI": "pi",
|
|
@@ -195,6 +198,7 @@ var propertyAccessReplacements = {
|
|
|
195
198
|
"Math.log": "log",
|
|
196
199
|
"String.prototype": "string",
|
|
197
200
|
"Number.prototype": "number",
|
|
201
|
+
"Boolean.prototype": "number",
|
|
198
202
|
"Object.prototype": "map",
|
|
199
203
|
"Array.prototype": "list",
|
|
200
204
|
"Function.prototype": "funcRef"
|
|
@@ -511,6 +515,56 @@ var utilFunctions2 = {
|
|
|
511
515
|
"\treturn out",
|
|
512
516
|
"end function"
|
|
513
517
|
].join(`
|
|
518
|
+
`),
|
|
519
|
+
array_push: [
|
|
520
|
+
"array_push = function(target, items)",
|
|
521
|
+
"\tfor item in items",
|
|
522
|
+
"\t\ttarget.push(item)",
|
|
523
|
+
"\tend for",
|
|
524
|
+
"\treturn target.len",
|
|
525
|
+
"end function"
|
|
526
|
+
].join(`
|
|
527
|
+
`),
|
|
528
|
+
array_unshift: [
|
|
529
|
+
"array_unshift = function(target, items)",
|
|
530
|
+
"\tif not items.len then return target.len",
|
|
531
|
+
"\tfor i in range(items.len-1)",
|
|
532
|
+
"\t\ttarget.insert(0, items[i])",
|
|
533
|
+
"\tend for",
|
|
534
|
+
"\treturn target.len",
|
|
535
|
+
"end function"
|
|
536
|
+
].join(`
|
|
537
|
+
`),
|
|
538
|
+
array_reverse: `array_reverse = function(arr)
|
|
539
|
+
arr.reverse
|
|
540
|
+
return arr
|
|
541
|
+
end function`,
|
|
542
|
+
str_starts_with: [
|
|
543
|
+
"str_starts_with = function(str, search, pos = 0)",
|
|
544
|
+
"\tif pos < 0 then pos = 0",
|
|
545
|
+
"\treturn str.indexOf(search) == pos",
|
|
546
|
+
"end function"
|
|
547
|
+
].join(`
|
|
548
|
+
`),
|
|
549
|
+
str_ends_with: [
|
|
550
|
+
"str_ends_with = function(str, search, pos = null)",
|
|
551
|
+
"\tif pos == null then pos = str.len",
|
|
552
|
+
"\tif pos < 0 then pos = 0",
|
|
553
|
+
"\treturn str.indexOf(search) + search.len == pos",
|
|
554
|
+
"end function"
|
|
555
|
+
].join(`
|
|
556
|
+
`),
|
|
557
|
+
str_repeat: [
|
|
558
|
+
"str_repeat = function(str, count = 0)",
|
|
559
|
+
'\tif count <= 0 then return ""',
|
|
560
|
+
"\tif count == 1 then return str",
|
|
561
|
+
"\tout = str",
|
|
562
|
+
"\tfor i in range(count-2)",
|
|
563
|
+
"\t\tout = out + str",
|
|
564
|
+
"\tend for",
|
|
565
|
+
"\treturn out",
|
|
566
|
+
"end function"
|
|
567
|
+
].join(`
|
|
514
568
|
`),
|
|
515
569
|
math_min: [
|
|
516
570
|
"math_min = function(numbers)",
|
|
@@ -707,7 +761,7 @@ NodeHandler.register(ts5.SyntaxKind.SetAccessor, (node) => {
|
|
|
707
761
|
return "";
|
|
708
762
|
const body = NodeHandler.handle(node.body);
|
|
709
763
|
const params = node.parameters.map((param) => NodeHandler.handle(param));
|
|
710
|
-
return
|
|
764
|
+
return `set_${NodeHandler.handle(node.name)} = function(${params.join(", ")})
|
|
711
765
|
${body}
|
|
712
766
|
end function`;
|
|
713
767
|
});
|
|
@@ -762,6 +816,59 @@ CallTransformer.register("Array.every", (name, args) => {
|
|
|
762
816
|
throw "Invalid argument count";
|
|
763
817
|
return callUtilFunction("array_every", name.slice(0, name.lastIndexOf(".")), args[0]);
|
|
764
818
|
});
|
|
819
|
+
CallTransformer.register("Array.slice", (name, args) => {
|
|
820
|
+
return name.slice(0, name.lastIndexOf(".")) + `[${args[0] ?? ""}:${args[1] ?? ""}]`;
|
|
821
|
+
});
|
|
822
|
+
CallTransformer.register("Array.push", (name, args) => {
|
|
823
|
+
if (!args.length)
|
|
824
|
+
throw "Invalid argument count";
|
|
825
|
+
return callUtilFunction("array_push", name.slice(0, name.lastIndexOf(".")), args[0]);
|
|
826
|
+
});
|
|
827
|
+
CallTransformer.register("Array.unshift", (name, args) => {
|
|
828
|
+
if (!args.length)
|
|
829
|
+
throw "Invalid argument count";
|
|
830
|
+
return callUtilFunction("array_unshift", name.slice(0, name.lastIndexOf(".")), args[0]);
|
|
831
|
+
});
|
|
832
|
+
CallTransformer.register("Array.toString", (name) => {
|
|
833
|
+
const arrayName = name.slice(0, name.lastIndexOf("."));
|
|
834
|
+
return `str(${arrayName})`;
|
|
835
|
+
});
|
|
836
|
+
CallTransformer.register("Array.reverse", (name) => {
|
|
837
|
+
return callUtilFunction("array_reverse", name.slice(0, name.lastIndexOf(".")));
|
|
838
|
+
});
|
|
839
|
+
CallTransformer.register("Object.toString", (name) => {
|
|
840
|
+
const objectName = name.slice(0, name.lastIndexOf("."));
|
|
841
|
+
return `str(${objectName})`;
|
|
842
|
+
});
|
|
843
|
+
CallTransformer.register("Number.toString", (name) => {
|
|
844
|
+
const number = name.slice(0, name.lastIndexOf("."));
|
|
845
|
+
return `str(${number})`;
|
|
846
|
+
});
|
|
847
|
+
CallTransformer.register("Function.toString", (name) => {
|
|
848
|
+
const func = name.slice(0, name.lastIndexOf("."));
|
|
849
|
+
return `str(@${func})`;
|
|
850
|
+
});
|
|
851
|
+
CallTransformer.register("String.startsWith", (name, args) => {
|
|
852
|
+
if (!args.length)
|
|
853
|
+
throw "Invalid argument count";
|
|
854
|
+
return callUtilFunction("str_starts_with", name.slice(0, name.lastIndexOf(".")), ...args);
|
|
855
|
+
});
|
|
856
|
+
CallTransformer.register("String.endsWith", (name, args) => {
|
|
857
|
+
if (!args.length)
|
|
858
|
+
throw "Invalid argument count";
|
|
859
|
+
return callUtilFunction("str_ends_with", name.slice(0, name.lastIndexOf(".")), ...args);
|
|
860
|
+
});
|
|
861
|
+
CallTransformer.register("String.repeat", (name, args) => {
|
|
862
|
+
if (!args.length)
|
|
863
|
+
throw "Invalid argument count";
|
|
864
|
+
return callUtilFunction("str_repeat", name.slice(0, name.lastIndexOf(".")), ...args);
|
|
865
|
+
});
|
|
866
|
+
CallTransformer.register("String.slice", (name, args) => {
|
|
867
|
+
return name.slice(0, name.lastIndexOf(".")) + `[${args[0] ?? ""}:${args[1] ?? ""}]`;
|
|
868
|
+
});
|
|
869
|
+
CallTransformer.register("String.toString", (name) => {
|
|
870
|
+
return name.slice(0, name.lastIndexOf("."));
|
|
871
|
+
});
|
|
765
872
|
CallTransformer.register("Math.min", (name, args) => {
|
|
766
873
|
return callUtilFunction("math_min", `${args.join(",")}`);
|
|
767
874
|
});
|
|
@@ -781,6 +888,9 @@ CallTransformer.register("ObjectConstructor.assign", (name, args) => {
|
|
|
781
888
|
CallTransformer.register("ObjectConstructor.keys", (name, args) => {
|
|
782
889
|
return `${args[0]}.indexes`;
|
|
783
890
|
});
|
|
891
|
+
CallTransformer.register("ObjectConstructor.values", (name, args) => {
|
|
892
|
+
return `${args[0]}.values`;
|
|
893
|
+
});
|
|
784
894
|
CallTransformer.register("GreyHack.include", (name, args, node, ctx) => {
|
|
785
895
|
if (!node.arguments.length)
|
|
786
896
|
return "";
|
|
@@ -794,6 +904,21 @@ CallTransformer.register("GreyHack.include", (name, args, node, ctx) => {
|
|
|
794
904
|
}
|
|
795
905
|
return "";
|
|
796
906
|
});
|
|
907
|
+
CallTransformer.register("Boolean", (name, args) => {
|
|
908
|
+
if (!args.length)
|
|
909
|
+
return "0";
|
|
910
|
+
return `(not (not ${args[0]}))`;
|
|
911
|
+
});
|
|
912
|
+
CallTransformer.register("Number", (name, args) => {
|
|
913
|
+
if (!args.length)
|
|
914
|
+
return "0";
|
|
915
|
+
return `str(${args[0]}).val`;
|
|
916
|
+
});
|
|
917
|
+
CallTransformer.register("String", (name, args) => {
|
|
918
|
+
if (!args.length)
|
|
919
|
+
return "";
|
|
920
|
+
return `str(${args[0]})`;
|
|
921
|
+
});
|
|
797
922
|
|
|
798
923
|
// src/visitors/expressions.ts
|
|
799
924
|
function hasRestParam(params) {
|
|
@@ -915,11 +1040,17 @@ NodeHandler.register(ts7.SyntaxKind.BinaryExpression, (node) => {
|
|
|
915
1040
|
if (ts7.isPropertyAccessExpression(node.left)) {
|
|
916
1041
|
const leftType = checker.getTypeAtLocation(node.left.expression);
|
|
917
1042
|
const symbol = leftType.getProperty(node.left.name.text);
|
|
918
|
-
if (symbol?.declarations
|
|
919
|
-
|
|
920
|
-
|
|
1043
|
+
if (symbol?.declarations?.some((decl) => ts7.isSetAccessor(decl))) {
|
|
1044
|
+
const objName = NodeHandler.handle(node.left.expression);
|
|
1045
|
+
const key = node.left.name.text;
|
|
1046
|
+
if (operatorToken !== "=" && symbol.declarations.some((decl) => ts7.isGetAccessor(decl)))
|
|
1047
|
+
throw `Can't handle '${operatorToken}' because '${objName}' doesn't have a getter '${key}'`;
|
|
1048
|
+
if (operatorToken === "+=" || operatorToken === "-=") {
|
|
1049
|
+
right = `${objName}.${key} ${operatorToken[0]} ${right}`;
|
|
1050
|
+
} else if (operatorToken !== "=") {
|
|
1051
|
+
throw `The transpiler can't handle ${operatorToken} operator for setters yet`;
|
|
921
1052
|
}
|
|
922
|
-
return `${
|
|
1053
|
+
return `${objName}.set_${key}(${right})`;
|
|
923
1054
|
}
|
|
924
1055
|
}
|
|
925
1056
|
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.3.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.5.0"
|
|
43
43
|
}
|
|
44
44
|
}
|