mevento 1.8.0 → 1.9.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.
@@ -152,7 +152,7 @@ class Lexer {
152
152
  error(token);
153
153
  }
154
154
  const lang = idToken.value.toString();
155
- this._language = Lexer.languages.find((element) => element.lang === lang) || Lexer._defaultLanguage;
155
+ this._language = Lexer.languages.find((element) => element.lang === lang) ?? Lexer._defaultLanguage;
156
156
  token = this.nextToken();
157
157
  if (token.type !== TokenType.great) {
158
158
  error(token);
@@ -226,9 +226,9 @@ class Lexer {
226
226
  this._advance();
227
227
  }
228
228
  const translatedId = this._language
229
- ? (this._language.keywords[ret]) || ret
229
+ ? (this._language.keywords[ret]) ?? ret
230
230
  : ret;
231
- return Lexer.RESERVED[translatedId] || new Token(TokenType.id, ret, line, lastC);
231
+ return Lexer.RESERVED[translatedId] ?? new Token(TokenType.id, ret, line, lastC);
232
232
  }
233
233
  _isLineEnd(c) {
234
234
  return c === 10 ||
@@ -1257,14 +1257,58 @@ class NodeVisitor extends ANodeVisitor {
1257
1257
  else { }
1258
1258
  }
1259
1259
  }
1260
+ class MEventScope {
1261
+ constructor(name, memory, parent) {
1262
+ this.memory = {};
1263
+ this.name = name;
1264
+ this.memory = memory;
1265
+ this.parent = parent;
1266
+ }
1267
+ resolve(key) {
1268
+ return Object.keys(this.memory).includes(key) ? this.memory[key] : (this.parent ? this.parent.resolve(key) : null);
1269
+ }
1270
+ change(key, value, declare = true) {
1271
+ if (Object.keys(this.memory).includes(key)) {
1272
+ // change here
1273
+ this.memory[key] = value;
1274
+ return true;
1275
+ }
1276
+ if (this.parent && this.parent.change(key, value, false)) {
1277
+ return true;
1278
+ }
1279
+ if (!declare) {
1280
+ return false;
1281
+ }
1282
+ this.memory[key] = value;
1283
+ return true;
1284
+ }
1285
+ }
1260
1286
  class MEvento extends NodeVisitor {
1261
1287
  constructor() {
1262
1288
  super();
1263
- this._memory = {};
1289
+ this.rootScope = new MEventScope("Program", {});
1290
+ this.currentScrope = this.rootScope;
1264
1291
  this.debug = false;
1265
1292
  this._functionsRegistry = {};
1266
1293
  this._functionsRegistry = { ...MEvento._globalFunctionsRegistry };
1267
1294
  }
1295
+ resolve(name) {
1296
+ return this.currentScrope?.resolve(name) ?? null;
1297
+ }
1298
+ changeVariable(name, value) {
1299
+ if (this.currentScrope?.change(name, value)) {
1300
+ return value;
1301
+ }
1302
+ else
1303
+ return null;
1304
+ }
1305
+ pushScope(name) {
1306
+ const scope = new MEventScope(name, {}, this.currentScrope);
1307
+ this.currentScrope = scope;
1308
+ }
1309
+ popScope() {
1310
+ this.currentScrope = this.currentScrope?.parent;
1311
+ }
1268
1312
  log(message) {
1269
1313
  if (this.debug) {
1270
1314
  console.log(message);
@@ -1281,17 +1325,19 @@ class MEvento extends NodeVisitor {
1281
1325
  visitBlockStatementAST(node) {
1282
1326
  const list = node.body;
1283
1327
  let last;
1328
+ this.pushScope("Block");
1284
1329
  for (const n of list) {
1285
1330
  last = this.visit(n);
1286
1331
  if (last instanceof LoopControl) {
1287
- return last;
1332
+ break;
1288
1333
  }
1289
1334
  }
1335
+ this.popScope();
1290
1336
  return last;
1291
1337
  }
1292
1338
  visitIdentifierAST(node) {
1293
1339
  const id = node.value;
1294
- return this._memory[id];
1340
+ return this.currentScrope?.resolve(id) ?? null;
1295
1341
  }
1296
1342
  visitLiteralAST(node) {
1297
1343
  const value = node.value;
@@ -1309,7 +1355,7 @@ class MEvento extends NodeVisitor {
1309
1355
  }
1310
1356
  else if (id instanceof IdentifierAST) {
1311
1357
  value = this.visit(init);
1312
- this._memory[id.value] = value;
1358
+ this.changeVariable(id.value, value);
1313
1359
  }
1314
1360
  return value;
1315
1361
  }
@@ -1479,51 +1525,59 @@ class MEvento extends NodeVisitor {
1479
1525
  if (ret instanceof BreakBranch) {
1480
1526
  break;
1481
1527
  }
1482
- if (ret instanceof ContinueAST) {
1528
+ if (ret instanceof ContinueBranch) {
1483
1529
  continue;
1484
1530
  }
1485
1531
  retainer?.push(ret);
1486
1532
  }
1487
- return retainer || null;
1533
+ return retainer ?? null;
1488
1534
  }
1489
1535
  visitForLoopStatement(node) {
1490
1536
  const retainer = node.retain ? [] : undefined;
1491
1537
  const loopIdentifier = node.init.identifier;
1492
1538
  if (!(loopIdentifier instanceof IdentifierAST))
1493
1539
  throw new Error("Unexpected identifer found");
1540
+ this.pushScope("ForLoopStatement");
1494
1541
  const initialValue = this.visit(node.init.init);
1495
- this._memory[loopIdentifier.value] = initialValue;
1542
+ this.changeVariable(loopIdentifier.value, initialValue);
1496
1543
  retainer?.push(initialValue);
1497
1544
  const test = () => {
1498
1545
  const ret = this.visit(node.test);
1499
1546
  if (isNum(ret)) {
1500
- const tmp = this._memory[loopIdentifier.value];
1547
+ const tmp = this.resolve(loopIdentifier.value);
1501
1548
  return node.direction.type === TokenType.up ? ret >= tmp : ret <= tmp;
1502
1549
  }
1503
1550
  return _boolValue(ret);
1504
1551
  };
1552
+ const update = () => {
1553
+ const updateValue = this.visit(node.update);
1554
+ if (isNum(updateValue)) {
1555
+ // auto handle
1556
+ const tmp = this.resolve(loopIdentifier.value);
1557
+ if (!isNum(tmp))
1558
+ throw Error("Cant update value");
1559
+ this.changeVariable(loopIdentifier.value, node.direction.type === TokenType.up ? tmp + updateValue : tmp - updateValue);
1560
+ // console.log('Update loop from ' + tmp + ' to ' + this.resolve(loopIdentifier.value))
1561
+ }
1562
+ else {
1563
+ throw Error("Update value cant be non number");
1564
+ }
1565
+ };
1505
1566
  while (test()) {
1506
1567
  const ret = this.visit(node.body);
1507
1568
  if (ret instanceof BreakBranch) {
1508
1569
  break;
1509
1570
  }
1510
- if (ret instanceof ContinueAST) {
1571
+ if (ret instanceof ContinueBranch) {
1572
+ update();
1511
1573
  continue;
1512
1574
  }
1513
1575
  retainer?.push(ret);
1514
1576
  // update
1515
- if (node.update instanceof LiteralAST && isNum(node.update.value)) {
1516
- // auto handle
1517
- const tmp = this._memory[loopIdentifier.value];
1518
- if (!isNum(tmp))
1519
- throw Error("Can update value");
1520
- this._memory[loopIdentifier.value] = node.direction.type === TokenType.up ? tmp + node.update.value : tmp - node.update.value;
1521
- }
1522
- else {
1523
- this.visit(node.update);
1524
- }
1577
+ update();
1525
1578
  }
1526
- return retainer || null;
1579
+ this.popScope();
1580
+ return retainer ?? null;
1527
1581
  }
1528
1582
  visitForOfStatement(node) {
1529
1583
  const collection = this.visit(node.collection);
@@ -1531,18 +1585,20 @@ class MEvento extends NodeVisitor {
1531
1585
  throw Error("Can iterate non array object");
1532
1586
  }
1533
1587
  const retainer = node.retain ? [] : undefined;
1588
+ this.pushScope("ForOfStatement");
1534
1589
  for (const it of collection) {
1535
1590
  this._declareForIdentifier(node.identifier, it);
1536
1591
  const ret = this.visit(node.body);
1537
1592
  if (ret instanceof BreakBranch) {
1538
1593
  break;
1539
1594
  }
1540
- if (ret instanceof ContinueAST) {
1595
+ if (ret instanceof ContinueBranch) {
1541
1596
  continue;
1542
1597
  }
1543
1598
  retainer?.push(ret);
1544
1599
  }
1545
- return retainer || null;
1600
+ this.popScope();
1601
+ return retainer ?? null;
1546
1602
  }
1547
1603
  visitContinueAST(node) {
1548
1604
  return new ContinueBranch();
@@ -1552,12 +1608,10 @@ class MEvento extends NodeVisitor {
1552
1608
  if (!Array.isArray(value)) {
1553
1609
  throw Error("Unable to make a tuple from non Array element");
1554
1610
  }
1555
- this._memory[node.first.value] = value[0];
1556
- this._memory[node.second.value] = value[1];
1557
- null;
1611
+ this.changeVariable(node.first.value, value[0]);
1612
+ this.changeVariable(node.second.value, value[1]);
1558
1613
  }
1559
- this._memory[node.value] = value;
1560
- null;
1614
+ this.changeVariable(node.value, value);
1561
1615
  }
1562
1616
  resolveArguments(args) {
1563
1617
  return args.map((it) => {
@@ -1606,12 +1660,12 @@ class MEvento extends NodeVisitor {
1606
1660
  clone() {
1607
1661
  var ret = new MEvento();
1608
1662
  ret._functionsRegistry = { ...this._functionsRegistry };
1609
- ret._memory = { ...this._memory };
1663
+ ret.rootScope.memory = { ...this.rootScope.memory };
1610
1664
  return ret;
1611
1665
  }
1612
1666
  newAsyncInstance() {
1613
1667
  const asyncIns = MEventoAsync.newInstance();
1614
- asyncIns._memory = this._memory;
1668
+ asyncIns.rootScope.memory = this.rootScope.memory;
1615
1669
  /// ????
1616
1670
  asyncIns._functionsRegistry = this._functionsRegistry;
1617
1671
  return asyncIns;
@@ -1635,17 +1689,19 @@ class MEventoAsync extends MEvento {
1635
1689
  async visitBlockStatementAST(node) {
1636
1690
  const list = node.body;
1637
1691
  let last;
1692
+ this.pushScope("Block");
1638
1693
  for (const n of list) {
1639
1694
  last = await this.visit(n);
1640
1695
  if (last instanceof LoopControl) {
1641
- return last;
1696
+ break;
1642
1697
  }
1643
1698
  }
1699
+ this.popScope();
1644
1700
  return last;
1645
1701
  }
1646
1702
  async visitIdentifierAST(node) {
1647
1703
  const id = node.value;
1648
- return this._memory[id];
1704
+ return this.resolve(id);
1649
1705
  }
1650
1706
  async visitLiteralAST(node) {
1651
1707
  const value = node.value;
@@ -1663,7 +1719,7 @@ class MEventoAsync extends MEvento {
1663
1719
  }
1664
1720
  else if (id instanceof IdentifierAST) {
1665
1721
  value = await this.visit(init);
1666
- this._memory[id.value] = value;
1722
+ this.changeVariable(id.value, value);
1667
1723
  }
1668
1724
  return value;
1669
1725
  }
@@ -1753,6 +1809,9 @@ class MEventoAsync extends MEvento {
1753
1809
  const arg = node.argument;
1754
1810
  const op = node.operation;
1755
1811
  const argValue = await this.visit(arg);
1812
+ if (op.type === TokenType.not) {
1813
+ return _boolValue(argValue) === false;
1814
+ }
1756
1815
  if (isNum(argValue)) {
1757
1816
  throw new Error(`Operation ${op.value} not allowed no num value`);
1758
1817
  }
@@ -1822,12 +1881,12 @@ class MEventoAsync extends MEvento {
1822
1881
  async visitWhileLoopStatement(node) {
1823
1882
  this.log(`WhileLoopStatement ${node.test} ${node.body}`);
1824
1883
  const retainer = node.retain ? [] : undefined;
1825
- while (_boolValue(this.visit(node.test))) {
1884
+ while (_boolValue(await this.visit(node.test))) {
1826
1885
  const ret = await this.visit(node.body);
1827
1886
  if (ret instanceof BreakBranch) {
1828
1887
  break;
1829
1888
  }
1830
- if (ret instanceof ContinueAST) {
1889
+ if (ret instanceof ContinueBranch) {
1831
1890
  continue;
1832
1891
  }
1833
1892
  retainer?.push(ret);
@@ -1839,57 +1898,66 @@ class MEventoAsync extends MEvento {
1839
1898
  const loopIdentifier = node.init.identifier;
1840
1899
  if (!(loopIdentifier instanceof IdentifierAST))
1841
1900
  throw new Error("Unexpected identifer found");
1842
- const initialValue = this.visit(node.init.init);
1843
- this._memory[loopIdentifier.value] = initialValue;
1901
+ this.pushScope("ForLoopStatement");
1902
+ const initialValue = await this.visit(node.init.init);
1903
+ this.changeVariable(loopIdentifier.value, initialValue);
1844
1904
  retainer?.push(initialValue);
1845
- const test = () => {
1846
- const ret = this.visit(node.test);
1905
+ const test = async () => {
1906
+ const ret = await this.visit(node.test);
1847
1907
  if (isNum(ret)) {
1848
- const tmp = this._memory[loopIdentifier.value];
1908
+ const tmp = this.resolve(loopIdentifier.value);
1849
1909
  return node.direction.type === TokenType.up ? ret >= tmp : ret <= tmp;
1850
1910
  }
1851
1911
  return _boolValue(ret);
1852
1912
  };
1853
- while (test()) {
1913
+ const update = async () => {
1914
+ const updateValue = await this.visit(node.update);
1915
+ if (isNum(updateValue)) {
1916
+ // auto handle
1917
+ const tmp = this.resolve(loopIdentifier.value);
1918
+ if (!isNum(tmp))
1919
+ throw Error("Cant update value");
1920
+ this.changeVariable(loopIdentifier.value, node.direction.type === TokenType.up ? tmp + updateValue : tmp - updateValue);
1921
+ }
1922
+ else {
1923
+ throw Error("Update value cant be non number");
1924
+ }
1925
+ };
1926
+ while (await test()) {
1854
1927
  const ret = await this.visit(node.body);
1855
1928
  if (ret instanceof BreakBranch) {
1856
1929
  break;
1857
1930
  }
1858
- if (ret instanceof ContinueAST) {
1931
+ if (ret instanceof ContinueBranch) {
1932
+ await update();
1859
1933
  continue;
1860
1934
  }
1861
1935
  retainer?.push(ret);
1862
1936
  // update
1863
- if (node.update instanceof LiteralAST && isNum(node.update.value)) {
1864
- // auto handle
1865
- const tmp = this._memory[loopIdentifier.value];
1866
- if (!isNum(tmp))
1867
- throw Error("Can update value");
1868
- this._memory[loopIdentifier.value] = node.direction.type === TokenType.up ? tmp + node.update.value : tmp - node.update.value;
1869
- }
1870
- else {
1871
- this.visit(node.update);
1872
- }
1937
+ await update();
1873
1938
  }
1874
- return retainer;
1939
+ this.popScope();
1940
+ return retainer ?? null;
1875
1941
  }
1876
1942
  async visitForOfStatement(node) {
1877
- const collection = this.visit(node.collection);
1943
+ const collection = await this.visit(node.collection);
1878
1944
  if (!Array.isArray(collection)) {
1879
1945
  throw Error("Can iterate non array object");
1880
1946
  }
1881
1947
  const retainer = node.retain ? [] : undefined;
1948
+ this.pushScope("ForOfStatement");
1882
1949
  for (const it of collection) {
1883
1950
  this._declareForIdentifier(node.identifier, it);
1884
1951
  const ret = await this.visit(node.body);
1885
1952
  if (ret instanceof BreakBranch) {
1886
1953
  break;
1887
1954
  }
1888
- if (ret instanceof ContinueAST) {
1955
+ if (ret instanceof ContinueBranch) {
1889
1956
  continue;
1890
1957
  }
1891
1958
  retainer?.push(ret);
1892
1959
  }
1960
+ this.popScope();
1893
1961
  return retainer;
1894
1962
  }
1895
1963
  async resolveArgumentsAsync(args) {
@@ -1918,7 +1986,7 @@ class MEventoAsync extends MEvento {
1918
1986
  clone() {
1919
1987
  var ret = new MEventoAsync();
1920
1988
  ret._functionsRegistry = { ...this._functionsRegistry };
1921
- ret._memory = { ...this._memory };
1989
+ ret.rootScope.memory = { ...this.rootScope.memory };
1922
1990
  return ret;
1923
1991
  }
1924
1992
  }
@@ -1931,5 +1999,5 @@ class MEventoAsync extends MEvento {
1931
1999
  * Generated bundle index. Do not edit.
1932
2000
  */
1933
2001
 
1934
- export { AST, ArrayExpression, AssignmentExpressionAST, BinaryExpressionAST, BlockStatementAST, BreakAST, BreakBranch, CallExpressionAST, ContinueAST, ContinueBranch, ExpressionStatementAST, ForLoopStatement, ForOfStatement, IdentifierAST, IfStatementAST, IndexAccessorAST, LexerDictionary, LiteralAST, LogicalExpressionAST, LoopControl, MEvento, MEventoAsync, NodeVisitor, ObjectExpression, ObjectProperty, RootAST, Token, TokenType, TupleExpression, UnaryExpressionAST, WhileLoopStatement };
2002
+ export { AST, ArrayExpression, AssignmentExpressionAST, BinaryExpressionAST, BlockStatementAST, BreakAST, BreakBranch, CallExpressionAST, ContinueAST, ContinueBranch, ExpressionStatementAST, ForLoopStatement, ForOfStatement, IdentifierAST, IfStatementAST, IndexAccessorAST, LexerDictionary, LiteralAST, LogicalExpressionAST, LoopControl, MEventScope, MEvento, MEventoAsync, NodeVisitor, ObjectExpression, ObjectProperty, RootAST, Token, TokenType, TupleExpression, UnaryExpressionAST, WhileLoopStatement };
1935
2003
  //# sourceMappingURL=mevento.mjs.map