mevento 1.8.0 → 1.9.1

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 ||
@@ -883,11 +883,14 @@ class Parser {
883
883
  _objectProperties() {
884
884
  const properties = [];
885
885
  if (this.currentToken?.type != TokenType.rbrace) {
886
+ this._eatEOL();
886
887
  properties.push(this._property());
887
888
  }
888
889
  while (this.currentToken?.type === TokenType.comma) {
889
890
  this._eat(TokenType.comma);
891
+ this._eatEOL();
890
892
  properties.push(this._property());
893
+ this._eatEOL();
891
894
  }
892
895
  return properties;
893
896
  }
@@ -1257,14 +1260,58 @@ class NodeVisitor extends ANodeVisitor {
1257
1260
  else { }
1258
1261
  }
1259
1262
  }
1263
+ class MEventScope {
1264
+ constructor(name, memory, parent) {
1265
+ this.memory = {};
1266
+ this.name = name;
1267
+ this.memory = memory;
1268
+ this.parent = parent;
1269
+ }
1270
+ resolve(key) {
1271
+ return Object.keys(this.memory).includes(key) ? this.memory[key] : (this.parent?.resolve(key) ?? null);
1272
+ }
1273
+ change(key, value, declare = true) {
1274
+ if (Object.keys(this.memory).includes(key)) {
1275
+ // change here
1276
+ this.memory[key] = value;
1277
+ return true;
1278
+ }
1279
+ if (this.parent && this.parent.change(key, value, false)) {
1280
+ return true;
1281
+ }
1282
+ if (!declare) {
1283
+ return false;
1284
+ }
1285
+ this.memory[key] = value;
1286
+ return true;
1287
+ }
1288
+ }
1260
1289
  class MEvento extends NodeVisitor {
1261
1290
  constructor() {
1262
1291
  super();
1263
- this._memory = {};
1292
+ this.rootScope = new MEventScope("Program", {});
1293
+ this.currentScope = this.rootScope;
1264
1294
  this.debug = false;
1265
1295
  this._functionsRegistry = {};
1266
1296
  this._functionsRegistry = { ...MEvento._globalFunctionsRegistry };
1267
1297
  }
1298
+ resolve(name) {
1299
+ return this.currentScope?.resolve(name) ?? null;
1300
+ }
1301
+ changeVariable(name, value) {
1302
+ if (this.currentScope?.change(name, value)) {
1303
+ return value;
1304
+ }
1305
+ else
1306
+ return null;
1307
+ }
1308
+ pushScope(name) {
1309
+ const scope = new MEventScope(name, {}, this.currentScope);
1310
+ this.currentScope = scope;
1311
+ }
1312
+ popScope() {
1313
+ this.currentScope = this.currentScope?.parent;
1314
+ }
1268
1315
  log(message) {
1269
1316
  if (this.debug) {
1270
1317
  console.log(message);
@@ -1281,17 +1328,19 @@ class MEvento extends NodeVisitor {
1281
1328
  visitBlockStatementAST(node) {
1282
1329
  const list = node.body;
1283
1330
  let last;
1331
+ this.pushScope("Block");
1284
1332
  for (const n of list) {
1285
1333
  last = this.visit(n);
1286
1334
  if (last instanceof LoopControl) {
1287
- return last;
1335
+ break;
1288
1336
  }
1289
1337
  }
1338
+ this.popScope();
1290
1339
  return last;
1291
1340
  }
1292
1341
  visitIdentifierAST(node) {
1293
1342
  const id = node.value;
1294
- return this._memory[id];
1343
+ return this?.resolve(id) ?? null;
1295
1344
  }
1296
1345
  visitLiteralAST(node) {
1297
1346
  const value = node.value;
@@ -1309,7 +1358,7 @@ class MEvento extends NodeVisitor {
1309
1358
  }
1310
1359
  else if (id instanceof IdentifierAST) {
1311
1360
  value = this.visit(init);
1312
- this._memory[id.value] = value;
1361
+ this.changeVariable(id.value, value);
1313
1362
  }
1314
1363
  return value;
1315
1364
  }
@@ -1479,51 +1528,59 @@ class MEvento extends NodeVisitor {
1479
1528
  if (ret instanceof BreakBranch) {
1480
1529
  break;
1481
1530
  }
1482
- if (ret instanceof ContinueAST) {
1531
+ if (ret instanceof ContinueBranch) {
1483
1532
  continue;
1484
1533
  }
1485
1534
  retainer?.push(ret);
1486
1535
  }
1487
- return retainer || null;
1536
+ return retainer ?? null;
1488
1537
  }
1489
1538
  visitForLoopStatement(node) {
1490
1539
  const retainer = node.retain ? [] : undefined;
1491
1540
  const loopIdentifier = node.init.identifier;
1492
1541
  if (!(loopIdentifier instanceof IdentifierAST))
1493
1542
  throw new Error("Unexpected identifer found");
1543
+ this.pushScope("ForLoopStatement");
1494
1544
  const initialValue = this.visit(node.init.init);
1495
- this._memory[loopIdentifier.value] = initialValue;
1545
+ this.changeVariable(loopIdentifier.value, initialValue);
1496
1546
  retainer?.push(initialValue);
1497
1547
  const test = () => {
1498
1548
  const ret = this.visit(node.test);
1499
1549
  if (isNum(ret)) {
1500
- const tmp = this._memory[loopIdentifier.value];
1550
+ const tmp = this.resolve(loopIdentifier.value);
1501
1551
  return node.direction.type === TokenType.up ? ret >= tmp : ret <= tmp;
1502
1552
  }
1503
1553
  return _boolValue(ret);
1504
1554
  };
1555
+ const update = () => {
1556
+ const updateValue = this.visit(node.update);
1557
+ if (isNum(updateValue)) {
1558
+ // auto handle
1559
+ const tmp = this.resolve(loopIdentifier.value);
1560
+ if (!isNum(tmp))
1561
+ throw Error("Cant update value");
1562
+ this.changeVariable(loopIdentifier.value, node.direction.type === TokenType.up ? tmp + updateValue : tmp - updateValue);
1563
+ // console.log('Update loop from ' + tmp + ' to ' + this.resolve(loopIdentifier.value))
1564
+ }
1565
+ else {
1566
+ throw Error("Update value cant be non number");
1567
+ }
1568
+ };
1505
1569
  while (test()) {
1506
1570
  const ret = this.visit(node.body);
1507
1571
  if (ret instanceof BreakBranch) {
1508
1572
  break;
1509
1573
  }
1510
- if (ret instanceof ContinueAST) {
1574
+ if (ret instanceof ContinueBranch) {
1575
+ update();
1511
1576
  continue;
1512
1577
  }
1513
1578
  retainer?.push(ret);
1514
1579
  // 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
- }
1580
+ update();
1525
1581
  }
1526
- return retainer || null;
1582
+ this.popScope();
1583
+ return retainer ?? null;
1527
1584
  }
1528
1585
  visitForOfStatement(node) {
1529
1586
  const collection = this.visit(node.collection);
@@ -1531,18 +1588,20 @@ class MEvento extends NodeVisitor {
1531
1588
  throw Error("Can iterate non array object");
1532
1589
  }
1533
1590
  const retainer = node.retain ? [] : undefined;
1591
+ this.pushScope("ForOfStatement");
1534
1592
  for (const it of collection) {
1535
1593
  this._declareForIdentifier(node.identifier, it);
1536
1594
  const ret = this.visit(node.body);
1537
1595
  if (ret instanceof BreakBranch) {
1538
1596
  break;
1539
1597
  }
1540
- if (ret instanceof ContinueAST) {
1598
+ if (ret instanceof ContinueBranch) {
1541
1599
  continue;
1542
1600
  }
1543
1601
  retainer?.push(ret);
1544
1602
  }
1545
- return retainer || null;
1603
+ this.popScope();
1604
+ return retainer ?? null;
1546
1605
  }
1547
1606
  visitContinueAST(node) {
1548
1607
  return new ContinueBranch();
@@ -1552,12 +1611,10 @@ class MEvento extends NodeVisitor {
1552
1611
  if (!Array.isArray(value)) {
1553
1612
  throw Error("Unable to make a tuple from non Array element");
1554
1613
  }
1555
- this._memory[node.first.value] = value[0];
1556
- this._memory[node.second.value] = value[1];
1557
- null;
1614
+ this.changeVariable(node.first.value, value[0]);
1615
+ this.changeVariable(node.second.value, value[1]);
1558
1616
  }
1559
- this._memory[node.value] = value;
1560
- null;
1617
+ this.changeVariable(node.value, value);
1561
1618
  }
1562
1619
  resolveArguments(args) {
1563
1620
  return args.map((it) => {
@@ -1606,12 +1663,12 @@ class MEvento extends NodeVisitor {
1606
1663
  clone() {
1607
1664
  var ret = new MEvento();
1608
1665
  ret._functionsRegistry = { ...this._functionsRegistry };
1609
- ret._memory = { ...this._memory };
1666
+ ret.rootScope.memory = { ...this.rootScope.memory };
1610
1667
  return ret;
1611
1668
  }
1612
1669
  newAsyncInstance() {
1613
1670
  const asyncIns = MEventoAsync.newInstance();
1614
- asyncIns._memory = this._memory;
1671
+ asyncIns.rootScope.memory = this.rootScope.memory;
1615
1672
  /// ????
1616
1673
  asyncIns._functionsRegistry = this._functionsRegistry;
1617
1674
  return asyncIns;
@@ -1635,17 +1692,19 @@ class MEventoAsync extends MEvento {
1635
1692
  async visitBlockStatementAST(node) {
1636
1693
  const list = node.body;
1637
1694
  let last;
1695
+ this.pushScope("Block");
1638
1696
  for (const n of list) {
1639
1697
  last = await this.visit(n);
1640
1698
  if (last instanceof LoopControl) {
1641
- return last;
1699
+ break;
1642
1700
  }
1643
1701
  }
1702
+ this.popScope();
1644
1703
  return last;
1645
1704
  }
1646
1705
  async visitIdentifierAST(node) {
1647
1706
  const id = node.value;
1648
- return this._memory[id];
1707
+ return this.resolve(id);
1649
1708
  }
1650
1709
  async visitLiteralAST(node) {
1651
1710
  const value = node.value;
@@ -1663,7 +1722,7 @@ class MEventoAsync extends MEvento {
1663
1722
  }
1664
1723
  else if (id instanceof IdentifierAST) {
1665
1724
  value = await this.visit(init);
1666
- this._memory[id.value] = value;
1725
+ this.changeVariable(id.value, value);
1667
1726
  }
1668
1727
  return value;
1669
1728
  }
@@ -1753,6 +1812,9 @@ class MEventoAsync extends MEvento {
1753
1812
  const arg = node.argument;
1754
1813
  const op = node.operation;
1755
1814
  const argValue = await this.visit(arg);
1815
+ if (op.type === TokenType.not) {
1816
+ return _boolValue(argValue) === false;
1817
+ }
1756
1818
  if (isNum(argValue)) {
1757
1819
  throw new Error(`Operation ${op.value} not allowed no num value`);
1758
1820
  }
@@ -1822,12 +1884,12 @@ class MEventoAsync extends MEvento {
1822
1884
  async visitWhileLoopStatement(node) {
1823
1885
  this.log(`WhileLoopStatement ${node.test} ${node.body}`);
1824
1886
  const retainer = node.retain ? [] : undefined;
1825
- while (_boolValue(this.visit(node.test))) {
1887
+ while (_boolValue(await this.visit(node.test))) {
1826
1888
  const ret = await this.visit(node.body);
1827
1889
  if (ret instanceof BreakBranch) {
1828
1890
  break;
1829
1891
  }
1830
- if (ret instanceof ContinueAST) {
1892
+ if (ret instanceof ContinueBranch) {
1831
1893
  continue;
1832
1894
  }
1833
1895
  retainer?.push(ret);
@@ -1839,57 +1901,66 @@ class MEventoAsync extends MEvento {
1839
1901
  const loopIdentifier = node.init.identifier;
1840
1902
  if (!(loopIdentifier instanceof IdentifierAST))
1841
1903
  throw new Error("Unexpected identifer found");
1842
- const initialValue = this.visit(node.init.init);
1843
- this._memory[loopIdentifier.value] = initialValue;
1904
+ this.pushScope("ForLoopStatement");
1905
+ const initialValue = await this.visit(node.init.init);
1906
+ this.changeVariable(loopIdentifier.value, initialValue);
1844
1907
  retainer?.push(initialValue);
1845
- const test = () => {
1846
- const ret = this.visit(node.test);
1908
+ const test = async () => {
1909
+ const ret = await this.visit(node.test);
1847
1910
  if (isNum(ret)) {
1848
- const tmp = this._memory[loopIdentifier.value];
1911
+ const tmp = this.resolve(loopIdentifier.value);
1849
1912
  return node.direction.type === TokenType.up ? ret >= tmp : ret <= tmp;
1850
1913
  }
1851
1914
  return _boolValue(ret);
1852
1915
  };
1853
- while (test()) {
1916
+ const update = async () => {
1917
+ const updateValue = await this.visit(node.update);
1918
+ if (isNum(updateValue)) {
1919
+ // auto handle
1920
+ const tmp = this.resolve(loopIdentifier.value);
1921
+ if (!isNum(tmp))
1922
+ throw Error("Cant update value");
1923
+ this.changeVariable(loopIdentifier.value, node.direction.type === TokenType.up ? tmp + updateValue : tmp - updateValue);
1924
+ }
1925
+ else {
1926
+ throw Error("Update value cant be non number");
1927
+ }
1928
+ };
1929
+ while (await test()) {
1854
1930
  const ret = await this.visit(node.body);
1855
1931
  if (ret instanceof BreakBranch) {
1856
1932
  break;
1857
1933
  }
1858
- if (ret instanceof ContinueAST) {
1934
+ if (ret instanceof ContinueBranch) {
1935
+ await update();
1859
1936
  continue;
1860
1937
  }
1861
1938
  retainer?.push(ret);
1862
1939
  // 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
- }
1940
+ await update();
1873
1941
  }
1874
- return retainer;
1942
+ this.popScope();
1943
+ return retainer ?? null;
1875
1944
  }
1876
1945
  async visitForOfStatement(node) {
1877
- const collection = this.visit(node.collection);
1946
+ const collection = await this.visit(node.collection);
1878
1947
  if (!Array.isArray(collection)) {
1879
1948
  throw Error("Can iterate non array object");
1880
1949
  }
1881
1950
  const retainer = node.retain ? [] : undefined;
1951
+ this.pushScope("ForOfStatement");
1882
1952
  for (const it of collection) {
1883
1953
  this._declareForIdentifier(node.identifier, it);
1884
1954
  const ret = await this.visit(node.body);
1885
1955
  if (ret instanceof BreakBranch) {
1886
1956
  break;
1887
1957
  }
1888
- if (ret instanceof ContinueAST) {
1958
+ if (ret instanceof ContinueBranch) {
1889
1959
  continue;
1890
1960
  }
1891
1961
  retainer?.push(ret);
1892
1962
  }
1963
+ this.popScope();
1893
1964
  return retainer;
1894
1965
  }
1895
1966
  async resolveArgumentsAsync(args) {
@@ -1918,7 +1989,7 @@ class MEventoAsync extends MEvento {
1918
1989
  clone() {
1919
1990
  var ret = new MEventoAsync();
1920
1991
  ret._functionsRegistry = { ...this._functionsRegistry };
1921
- ret._memory = { ...this._memory };
1992
+ ret.rootScope.memory = { ...this.rootScope.memory };
1922
1993
  return ret;
1923
1994
  }
1924
1995
  }
@@ -1931,5 +2002,5 @@ class MEventoAsync extends MEvento {
1931
2002
  * Generated bundle index. Do not edit.
1932
2003
  */
1933
2004
 
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 };
2005
+ 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
2006
  //# sourceMappingURL=mevento.mjs.map