@grey-ts/transpiler 1.2.0 → 1.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.
Files changed (2) hide show
  1. package/dist/index.js +210 -204
  2. package/package.json +2 -2
package/dist/index.js CHANGED
@@ -25,7 +25,7 @@ class NodeHandler {
25
25
  if (!handler) {
26
26
  console.log(`Unsupported syntax ${ts.SyntaxKind[node.kind]} (kind ${node.kind}) was not transpiled: ${node.getText()}`);
27
27
  this.printLineAndCol(node);
28
- return "null";
28
+ return ts.isBlock(node.parent) || ts.isSourceFile(node.parent) ? "" : "null";
29
29
  }
30
30
  try {
31
31
  const result = handler(node, this.transpileContext);
@@ -33,7 +33,7 @@ class NodeHandler {
33
33
  } catch (error) {
34
34
  console.error(error);
35
35
  this.printLineAndCol(node);
36
- return "null";
36
+ return ts.isBlock(node.parent) || ts.isSourceFile(node.parent) ? "" : "null";
37
37
  }
38
38
  }
39
39
  static printLineAndCol(node) {
@@ -215,9 +215,11 @@ var apiNameMap = {
215
215
  "String.length": "len",
216
216
  "String.toLowerCase": "lower",
217
217
  "String.toUpperCase": "upper",
218
+ "String.repeat": "repeatSelf",
218
219
  "Object.size": "len",
219
220
  "Array.length": "len",
220
- "Array.shift": "pull"
221
+ "Array.shift": "pull",
222
+ "Array.push": "push_many"
221
223
  };
222
224
  var propertyAccessReplacements = {
223
225
  "Math.PI": "pi",
@@ -384,7 +386,7 @@ function findProjectRoot(dir, fileToSearch = "package.json") {
384
386
  return dir;
385
387
  }
386
388
  function callUtilFunction(functionName, ...params) {
387
- calledUtilFunctions.add(functionName);
389
+ utilitiesToInsert.set(functionName, utilFunctions[functionName]);
388
390
  return `${functionName}(${params.join(", ")})`;
389
391
  }
390
392
 
@@ -508,6 +510,13 @@ class CallTransformer {
508
510
  this.handlers.set(symbolFullName, handler);
509
511
  }
510
512
  static handle(symbolFullName, functionName, callArgs, node, ctx) {
513
+ if (symbolFullName in utilFunctions) {
514
+ if (symbolFullName.startsWith("Math"))
515
+ utilitiesToInsert.set("create_math", "Math = {}");
516
+ utilitiesToInsert.set(symbolFullName, utilFunctions[symbolFullName]);
517
+ const params = callArgs.length ? `(${callArgs.join(",")})` : "";
518
+ return `${functionName}${params}`;
519
+ }
511
520
  const handler = this.handlers.get(symbolFullName);
512
521
  if (!handler)
513
522
  return null;
@@ -522,12 +531,6 @@ CallTransformer.register("Function.toString", (name) => {
522
531
  const func = name.slice(0, name.lastIndexOf("."));
523
532
  return `str(@${func})`;
524
533
  });
525
- CallTransformer.register("Math.min", (name, args) => {
526
- return callUtilFunction("math_min", `${args.join(",")}`);
527
- });
528
- CallTransformer.register("Math.max", (name, args) => {
529
- return callUtilFunction("math_max", `${args.join(",")}`);
530
- });
531
534
  CallTransformer.register("GreyHack.include", (name, args, node, ctx) => {
532
535
  if (!node.arguments.length)
533
536
  return "";
@@ -771,8 +774,8 @@ NodeHandler.register(ts8.SyntaxKind.CallExpression, (node, ctx) => {
771
774
  const transformed = CallTransformer.handle(symbolFullName, name, args, node, ctx);
772
775
  if (transformed !== null)
773
776
  return transformed;
774
- if (name === "is_type" && !calledUtilFunctions.has("is_type")) {
775
- calledUtilFunctions.add("is_type");
777
+ if (name === "is_type" && !utilitiesToInsert.has("is_type")) {
778
+ utilitiesToInsert.set("is_type", utilFunctions["is_type"]);
776
779
  }
777
780
  if (!args.length)
778
781
  return name;
@@ -1376,56 +1379,13 @@ NodeHandler.register(ts13.SyntaxKind.EnumDeclaration, (node) => {
1376
1379
  });
1377
1380
 
1378
1381
  // src/call_transformers/array.ts
1379
- CallTransformer.register("Array.concat", (name, args) => {
1380
- const dotI = name.lastIndexOf(".");
1381
- const arrayName = name.slice(0, dotI);
1382
- return callUtilFunction("array_concat", arrayName, args.join(","));
1383
- });
1384
- CallTransformer.register("Array.map", (name, args) => {
1385
- if (!args.length)
1386
- throw "Invalid argument count";
1387
- return callUtilFunction("array_map", name.slice(0, name.lastIndexOf(".")), args[0]);
1388
- });
1389
- CallTransformer.register("Array.filter", (name, args) => {
1390
- if (!args.length)
1391
- throw "Invalid argument count";
1392
- return callUtilFunction("array_filter", name.slice(0, name.lastIndexOf(".")), args[0]);
1393
- });
1394
- CallTransformer.register("Array.find", (name, args) => {
1395
- if (!args.length)
1396
- throw "Invalid argument count";
1397
- return callUtilFunction("array_find", name.slice(0, name.lastIndexOf(".")), args[0]);
1398
- });
1399
- CallTransformer.register("Array.some", (name, args) => {
1400
- if (!args.length)
1401
- throw "Invalid argument count";
1402
- return callUtilFunction("array_some", name.slice(0, name.lastIndexOf(".")), args[0]);
1403
- });
1404
- CallTransformer.register("Array.every", (name, args) => {
1405
- if (!args.length)
1406
- throw "Invalid argument count";
1407
- return callUtilFunction("array_every", name.slice(0, name.lastIndexOf(".")), args[0]);
1408
- });
1409
1382
  CallTransformer.register("Array.slice", (name, args) => {
1410
1383
  return name.slice(0, name.lastIndexOf(".")) + `[${args[0] ?? ""}:${args[1] ?? ""}]`;
1411
1384
  });
1412
- CallTransformer.register("Array.push", (name, args) => {
1413
- if (!args.length)
1414
- throw "Invalid argument count";
1415
- return callUtilFunction("array_push", name.slice(0, name.lastIndexOf(".")), args[0]);
1416
- });
1417
- CallTransformer.register("Array.unshift", (name, args) => {
1418
- if (!args.length)
1419
- throw "Invalid argument count";
1420
- return callUtilFunction("array_unshift", name.slice(0, name.lastIndexOf(".")), args[0]);
1421
- });
1422
1385
  CallTransformer.register("Array.toString", (name) => {
1423
1386
  const arrayName = name.slice(0, name.lastIndexOf("."));
1424
1387
  return `str(${arrayName})`;
1425
1388
  });
1426
- CallTransformer.register("Array.reverse", (name) => {
1427
- return callUtilFunction("array_reverse", name.slice(0, name.lastIndexOf(".")));
1428
- });
1429
1389
 
1430
1390
  // src/call_transformers/object.ts
1431
1391
  CallTransformer.register("ObjectConstructor.hasOwn", (name, args) => {
@@ -1471,21 +1431,6 @@ CallTransformer.register("Object.toString", (name) => {
1471
1431
  });
1472
1432
 
1473
1433
  // src/call_transformers/string.ts
1474
- CallTransformer.register("String.startsWith", (name, args) => {
1475
- if (!args.length)
1476
- throw "Invalid argument count";
1477
- return callUtilFunction("str_starts_with", name.slice(0, name.lastIndexOf(".")), ...args);
1478
- });
1479
- CallTransformer.register("String.endsWith", (name, args) => {
1480
- if (!args.length)
1481
- throw "Invalid argument count";
1482
- return callUtilFunction("str_ends_with", name.slice(0, name.lastIndexOf(".")), ...args);
1483
- });
1484
- CallTransformer.register("String.repeat", (name, args) => {
1485
- if (!args.length)
1486
- throw "Invalid argument count";
1487
- return callUtilFunction("str_repeat", name.slice(0, name.lastIndexOf(".")), ...args);
1488
- });
1489
1434
  CallTransformer.register("String.slice", (name, args) => {
1490
1435
  return name.slice(0, name.lastIndexOf(".")) + `[${args[0] ?? ""}:${args[1] ?? ""}]`;
1491
1436
  });
@@ -1496,132 +1441,68 @@ CallTransformer.register("String.toString", (name) => {
1496
1441
  // src/transpiler.ts
1497
1442
  var program;
1498
1443
  var checker;
1499
- var anonFunctions = new Map;
1500
- var calledUtilFunctions = new Set;
1501
- var utilFunctions2 = {
1502
- get_property: [
1503
- "get_property = function(obj, key)",
1504
- "\tif not obj then return null",
1505
- "\tif obj.hasIndex(key) then return obj[key]",
1506
- "\tisaobj = obj",
1507
- '\twhile isaobj.hasIndex("__isa")',
1508
- '\t\tisaobj = obj["__isa"]',
1509
- "\t\tif isaobj.hasIndex(key) then",
1510
- "\t\t\tres = obj[key]",
1511
- '\t\t\tif typeof(@res) == "function" and str(@res)[8:][1:-1].indexOf("self") == 0 then return res(obj)',
1512
- "\t\t\treturn obj[key]",
1513
- "\t\tend if",
1514
- "\tend while",
1515
- "\treturn null",
1516
- "end function"
1517
- ].join(`
1518
- `),
1519
- assign_objects: [
1520
- "assign_objects = function(target, source1, source2, source3)",
1521
- "\tassign_to_list = function(target, source)",
1522
- "\t\tif source isa list then",
1523
- "\t\t\tfor i in range(0, source.len - 1, 1)",
1524
- "\t\t\t\tif target.len <= i then target.push(null)",
1525
- "\t\t\t\ttarget[i] = source[i]",
1526
- "\t\t\tend for",
1527
- "\t\telse if source isa map then",
1528
- "\t\t\tfor item in source",
1529
- "\t\t\t\tkey = str(item.key).to_int",
1530
- "\t\t\t\tif key isa number then target[key] = item.value",
1531
- "\t\t\tend for",
1532
- "\t\tend if",
1533
- "\t\treturn target",
1534
- "\tend function",
1535
- "\tcounter = 0",
1536
- "\tassign_object = function(target, source)",
1537
- "\t\tif target isa list then return assign_to_list(target, source)",
1538
- "\t\tif source isa list then",
1539
- "\t\t\tfor i in range(0, source.len - 1, 1)",
1540
- "\t\t\t\ttarget[str(i)] = source[i]",
1541
- "\t\t\tend for",
1542
- "\t\telse if source isa map then",
1543
- "\t\t\tfor item in source",
1544
- "\t\t\t\ttarget[item.key] = item.value",
1545
- "\t\t\tend for",
1546
- "\t\telse",
1547
- "\t\t\ttarget[str(outer.counter)] = source",
1548
- "\t\t\touter.counter = outer.counter + 1",
1549
- "\t\tend if",
1550
- "\tend function",
1551
- "\tif source1 isa list then",
1552
- "\t\tif target isa list then return assign_to_list(target, source1)",
1553
- "\t\tfor source in source1",
1554
- "\t\t\tassign_object(target, source)",
1555
- "\t\tend for",
1556
- "\t\treturn target",
1557
- "\tend if",
1558
- "\tif source1 then assign_object(target, source1)",
1559
- "\tif source2 then assign_object(target, source2)",
1560
- "\tif source3 then assign_object(target, source3)",
1561
- "\treturn target",
1562
- "end function"
1563
- ].join(`
1564
- `),
1565
- array_map: [
1566
- "array_map = function(array, callback)",
1444
+ var utilitiesToInsert = new Map;
1445
+ var extensionFunctions = {
1446
+ "Array.map": [
1447
+ "list.map = function(callback)",
1567
1448
  "\tindex = 0",
1568
1449
  "\tout = []",
1569
- "\tfor item in array",
1570
- "\t\tout.push(callback(item, index, array))",
1450
+ "\tfor item in self",
1451
+ "\t\tout.push(callback(item, index, self))",
1571
1452
  "\t\tindex = index + 1",
1572
1453
  "\tend for",
1573
1454
  "\treturn out",
1574
1455
  "end function"
1575
1456
  ].join(`
1576
1457
  `),
1577
- array_filter: [
1578
- "array_filter = function(array, predicate)",
1458
+ "Array.filter": [
1459
+ "list.filter = function(predicate)",
1579
1460
  "\tindex = 0",
1580
1461
  "\tout = []",
1581
- "\tfor item in array",
1582
- "\t\tif predicate(item, index, array) then out.push(item)",
1462
+ "\tfor item in self",
1463
+ "\t\tif predicate(item, index, self) then out.push(item)",
1583
1464
  "\t\tindex = index + 1",
1584
1465
  "\tend for",
1585
1466
  "\treturn out",
1586
1467
  "end function"
1587
1468
  ].join(`
1588
1469
  `),
1589
- array_find: [
1590
- "array_find = function(array, predicate)",
1470
+ "Array.find": [
1471
+ "list.find = function(predicate)",
1591
1472
  "\tindex = 0",
1592
- "\tfor item in array",
1593
- "\t\tif predicate(item, index, array) then return item",
1473
+ "\tfor item in self",
1474
+ "\t\tif predicate(item, index, self) then return item",
1594
1475
  "\t\tindex = index + 1",
1595
1476
  "\tend for",
1596
1477
  "\treturn null",
1597
1478
  "end function"
1598
1479
  ].join(`
1599
1480
  `),
1600
- array_some: [
1601
- "array_some = function(array, predicate)",
1481
+ "Array.some": [
1482
+ "list.some = function(predicate)",
1602
1483
  "\tindex = 0",
1603
- "\tfor item in array",
1604
- "\t\tif predicate(item, index, array) then return 1",
1484
+ "\tfor item in self",
1485
+ "\t\tif predicate(item, index, self) then return 1",
1605
1486
  "\t\tindex = index + 1",
1606
1487
  "\tend for",
1607
1488
  "\treturn 0",
1608
1489
  "end function"
1609
1490
  ].join(`
1610
1491
  `),
1611
- array_every: [
1612
- "array_every = function(array, predicate)",
1492
+ "Array.every": [
1493
+ "list.every = function(predicate)",
1613
1494
  "\tindex = 0",
1614
- "\tfor item in array",
1615
- "\t\tif not predicate(item, index, array) then return 0",
1495
+ "\tfor item in self",
1496
+ "\t\tif not predicate(item, index, self) then return 0",
1616
1497
  "\t\tindex = index + 1",
1617
1498
  "\tend for",
1618
1499
  "\treturn 1",
1619
1500
  "end function"
1620
1501
  ].join(`
1621
1502
  `),
1622
- array_concat: [
1623
- "array_concat = function(target, items)",
1624
- "\tout = target[0:]",
1503
+ "Array.concat": [
1504
+ "list.concat = function(items)",
1505
+ "\tout = self[0:]",
1625
1506
  "\tfor item in items",
1626
1507
  "\t\tif item isa list then out = out + item else out.push(item)",
1627
1508
  "\tend for",
@@ -1629,58 +1510,123 @@ var utilFunctions2 = {
1629
1510
  "end function"
1630
1511
  ].join(`
1631
1512
  `),
1632
- array_push: [
1633
- "array_push = function(target, items)",
1513
+ "Array.push": [
1514
+ "list.push_many = function(items)",
1634
1515
  "\tfor item in items[:]",
1635
- "\t\ttarget.push(@item)",
1516
+ "\t\tself.push(@item)",
1636
1517
  "\tend for",
1637
- "\treturn target.len",
1518
+ "\treturn self.len",
1638
1519
  "end function"
1639
1520
  ].join(`
1640
1521
  `),
1641
- array_unshift: [
1642
- "array_unshift = function(target, items)",
1643
- "\tif not items.len then return target.len",
1522
+ "Array.unshift": [
1523
+ "list.unshift = function(items)",
1524
+ "\tif not items.len then return self.len",
1644
1525
  "\tfor i in range(items.len-1)",
1645
- "\t\ttarget.insert(0, @items[i])",
1526
+ "\t\tself.insert(0, items[i])",
1646
1527
  "\tend for",
1647
- "\treturn target.len",
1528
+ "\treturn self.len",
1648
1529
  "end function"
1649
1530
  ].join(`
1650
1531
  `),
1651
- array_reverse: `array_reverse = function(arr)
1652
- arr.reverse
1653
- return arr
1654
- end function`,
1655
- str_starts_with: [
1656
- "str_starts_with = function(str, search, pos = 0)",
1532
+ "Array.reverse": [
1533
+ "list.old_reverse = @list.reverse",
1534
+ "list.reverse = function",
1535
+ "\tself.old_reverse",
1536
+ "\treturn self",
1537
+ "end function"
1538
+ ].join(`
1539
+ `),
1540
+ "Array.includes": [
1541
+ "list.includes = function(value, pos = 0)",
1542
+ "\tindex = self.indexOf(value)",
1543
+ "\tif index == null then return false",
1657
1544
  "\tif pos < 0 then pos = 0",
1658
- "\treturn str.indexOf(search) == pos",
1545
+ "\treturn index >= pos",
1546
+ "end function"
1547
+ ].join(`
1548
+ `),
1549
+ "Array.splice": [
1550
+ "list.splice = function(start, count)",
1551
+ "\tdeleted = []",
1552
+ "\tif start < 0 then start = self.len + start",
1553
+ "\tif start < 0 then start = 0",
1554
+ "\tif count == null then count = self.len - start",
1555
+ "\tif count <= 0 then return deleted",
1556
+ "\twhile deleted.len < count",
1557
+ "\t\tif not self.hasIndex(start) then return deleted",
1558
+ "\t\tdeleted.push(self[start])",
1559
+ "\t\tself.remove(start)",
1560
+ "\tend while",
1561
+ "\treturn deleted",
1659
1562
  "end function"
1660
1563
  ].join(`
1661
1564
  `),
1662
- str_ends_with: [
1663
- "str_ends_with = function(str, search, pos = null)",
1664
- "\tif pos == null then pos = str.len",
1565
+ "String.startsWith": [
1566
+ "string.startsWith = function(search, pos = 0)",
1665
1567
  "\tif pos < 0 then pos = 0",
1666
- "\treturn str.indexOf(search) + search.len == pos",
1568
+ "\treturn self.indexOf(search) == pos",
1667
1569
  "end function"
1668
1570
  ].join(`
1669
1571
  `),
1670
- str_repeat: [
1671
- "str_repeat = function(str, count = 0)",
1672
- '\tif count <= 0 then return ""',
1673
- "\tif count == 1 then return str",
1674
- "\tout = str",
1675
- "\tfor i in range(count-2)",
1676
- "\t\tout = out + str",
1677
- "\tend for",
1678
- "\treturn out",
1572
+ "String.endsWith": [
1573
+ "string.endsWith = function(search, pos = null)",
1574
+ "\tif pos == null then pos = self.len",
1575
+ "\tif pos < 0 then pos = 0",
1576
+ "\treturn self.indexOf(search) + search.len == pos",
1577
+ "end function"
1578
+ ].join(`
1579
+ `),
1580
+ "String.repeat": [
1581
+ "string.repeatSelf = function(count = 0)",
1582
+ "\treturn self * count",
1583
+ "end function"
1584
+ ].join(`
1585
+ `),
1586
+ "String.includes": [
1587
+ "string.includes = function(search, pos = 0)",
1588
+ "\tindex = self.indexOf(search)",
1589
+ "\tif index == null then return false",
1590
+ "\tif pos < 0 then pos = 0",
1591
+ "\treturn index >= pos",
1592
+ "end function"
1593
+ ].join(`
1594
+ `),
1595
+ "String.trimStart": [
1596
+ "string.trimStart = function()",
1597
+ '\treturn self.replace("^\\s+", "")',
1679
1598
  "end function"
1680
1599
  ].join(`
1681
1600
  `),
1682
- math_min: [
1683
- "math_min = function(numbers)",
1601
+ "String.trimEnd": [
1602
+ "string.trimEnd = function()",
1603
+ '\treturn self.replace("\\s+$", "")',
1604
+ "end function"
1605
+ ].join(`
1606
+ `),
1607
+ "Number.toFixed": [
1608
+ "number.toFixed = function(digits = 0)",
1609
+ "\tdigits = floor(digits)",
1610
+ "\tif digits <= 0 then return str(round(self))",
1611
+ "\tvalue = self",
1612
+ "\tvalue = value * (10 ^ digits)",
1613
+ "\tvalue = round(value)",
1614
+ "\tvalue = value / (10 ^ digits)",
1615
+ "\t",
1616
+ "\tstr_value = str(value)",
1617
+ '\tdot_index = str_value.indexOf(".")',
1618
+ "\tif dot_index == null then",
1619
+ '\t\tstr_value = str_value + "." + ("0" * digits)',
1620
+ "\telse if str_value[dot_index + 1:].len < digits then",
1621
+ "\t\trepeat_count = digits - str_value[dot_index + 1:].len",
1622
+ '\t\tstr_value = str_value + ("0" * repeat_count)',
1623
+ "\tend if",
1624
+ "\treturn str_value",
1625
+ "end function"
1626
+ ].join(`
1627
+ `),
1628
+ "Math.min": [
1629
+ "Math.min = function(numbers)",
1684
1630
  "\tcurr_min = null",
1685
1631
  "\tfor num in numbers",
1686
1632
  "\t\tif curr_min == null or num < curr_min then curr_min = num",
@@ -1689,8 +1635,8 @@ end function`,
1689
1635
  "end function"
1690
1636
  ].join(`
1691
1637
  `),
1692
- math_max: [
1693
- "math_max = function(numbers)",
1638
+ "Math.max": [
1639
+ "Math.max = function(numbers)",
1694
1640
  "\tcurr_max = null",
1695
1641
  "\tfor num in numbers",
1696
1642
  "\t\tif curr_max == null or num > curr_max then curr_max = num",
@@ -1698,6 +1644,71 @@ end function`,
1698
1644
  "\treturn curr_max",
1699
1645
  "end function"
1700
1646
  ].join(`
1647
+ `)
1648
+ };
1649
+ var utilFunctions = {
1650
+ get_property: [
1651
+ "get_property = function(obj, key)",
1652
+ "\tif not obj then return null",
1653
+ "\tif obj.hasIndex(key) then return obj[key]",
1654
+ "\tisaobj = obj",
1655
+ '\twhile isaobj.hasIndex("__isa")',
1656
+ '\t\tisaobj = obj["__isa"]',
1657
+ "\t\tif isaobj.hasIndex(key) then",
1658
+ "\t\t\tres = obj[key]",
1659
+ '\t\t\tif typeof(@res) == "function" and str(@res)[8:][1:-1].indexOf("self") == 0 then return res(obj)',
1660
+ "\t\t\treturn obj[key]",
1661
+ "\t\tend if",
1662
+ "\tend while",
1663
+ "\treturn null",
1664
+ "end function"
1665
+ ].join(`
1666
+ `),
1667
+ assign_objects: [
1668
+ "assign_objects = function(target, source1, source2, source3)",
1669
+ "\tassign_to_list = function(target, source)",
1670
+ "\t\tif source isa list then",
1671
+ "\t\t\tfor i in range(0, source.len - 1, 1)",
1672
+ "\t\t\t\tif target.len <= i then target.push(null)",
1673
+ "\t\t\t\ttarget[i] = source[i]",
1674
+ "\t\t\tend for",
1675
+ "\t\telse if source isa map then",
1676
+ "\t\t\tfor item in source",
1677
+ "\t\t\t\tkey = str(item.key).to_int",
1678
+ "\t\t\t\tif key isa number then target[key] = item.value",
1679
+ "\t\t\tend for",
1680
+ "\t\tend if",
1681
+ "\t\treturn target",
1682
+ "\tend function",
1683
+ "\tcounter = 0",
1684
+ "\tassign_object = function(target, source)",
1685
+ "\t\tif target isa list then return assign_to_list(target, source)",
1686
+ "\t\tif source isa list then",
1687
+ "\t\t\tfor i in range(0, source.len - 1, 1)",
1688
+ "\t\t\t\ttarget[str(i)] = source[i]",
1689
+ "\t\t\tend for",
1690
+ "\t\telse if source isa map then",
1691
+ "\t\t\tfor item in source",
1692
+ "\t\t\t\ttarget[item.key] = item.value",
1693
+ "\t\t\tend for",
1694
+ "\t\telse",
1695
+ "\t\t\ttarget[str(outer.counter)] = source",
1696
+ "\t\t\touter.counter = outer.counter + 1",
1697
+ "\t\tend if",
1698
+ "\tend function",
1699
+ "\tif source1 isa list then",
1700
+ "\t\tif target isa list then return assign_to_list(target, source1)",
1701
+ "\t\tfor source in source1",
1702
+ "\t\t\tassign_object(target, source)",
1703
+ "\t\tend for",
1704
+ "\t\treturn target",
1705
+ "\tend if",
1706
+ "\tif source1 then assign_object(target, source1)",
1707
+ "\tif source2 then assign_object(target, source2)",
1708
+ "\tif source3 then assign_object(target, source3)",
1709
+ "\treturn target",
1710
+ "end function"
1711
+ ].join(`
1701
1712
  `),
1702
1713
  nullish_coalescing_op: `nullish_coalescing_op = function(left, right)
1703
1714
  if left == null then return @right
@@ -1714,16 +1725,17 @@ end function`,
1714
1725
  conditional_expr: `conditional_expr = function(cond, when_true, when_false)
1715
1726
  if cond then return when_true
1716
1727
  return when_false
1717
- end function`
1728
+ end function`,
1729
+ ...extensionFunctions
1718
1730
  };
1719
1731
  function createAnonFunction(body, params) {
1720
1732
  const defaultParams = new Array(3).fill(0).map((_, i) => `param${i}`);
1721
- const nextName = `func_${anonFunctions.size}`;
1733
+ const nextName = `func_${utilitiesToInsert.size}`;
1722
1734
  const paramString = Object.assign(defaultParams, params).join(",");
1723
1735
  const result = `${nextName} = function(${paramString})
1724
1736
  ${body}
1725
1737
  end function`;
1726
- anonFunctions.set(nextName, result);
1738
+ utilitiesToInsert.set(nextName, result);
1727
1739
  return { name: nextName, str: result };
1728
1740
  }
1729
1741
  function transpileProgram(entryFileRelativePath) {
@@ -1764,15 +1776,9 @@ function transpileProgram(entryFileRelativePath) {
1764
1776
  process.exit(1);
1765
1777
  }
1766
1778
  transpileSourceFile(entry, ctx);
1767
- if (anonFunctions.size) {
1768
- for (const declaration of anonFunctions.values())
1769
- ctx.output.unshift(declaration);
1770
- anonFunctions.clear();
1771
- }
1772
- if (calledUtilFunctions.size) {
1773
- for (const call of calledUtilFunctions.keys())
1774
- ctx.output.unshift(utilFunctions2[call]);
1775
- calledUtilFunctions.clear();
1779
+ if (utilitiesToInsert.size) {
1780
+ ctx.output.unshift(...utilitiesToInsert.values());
1781
+ utilitiesToInsert.clear();
1776
1782
  }
1777
1783
  console.log(`Transpiling took ${Date.now() - start} ms`);
1778
1784
  return ctx.output.join(`
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@grey-ts/transpiler",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Transpiles TypeScript into GreyScript",
5
5
  "author": "Okka",
6
6
  "module": "src/index.ts",
@@ -40,6 +40,6 @@
40
40
  "typescript": "^5"
41
41
  },
42
42
  "dependencies": {
43
- "@grey-ts/types": "^2.0.1"
43
+ "@grey-ts/types": "^2.1.0"
44
44
  }
45
45
  }