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.
@@ -146,6 +146,7 @@ class Lexer {
146
146
  return this._source;
147
147
  }
148
148
  _resolveLanguage() {
149
+ var _a;
149
150
  let token = this.nextToken();
150
151
  if (token.type === TokenType.less) {
151
152
  // language setting
@@ -154,7 +155,7 @@ class Lexer {
154
155
  error(token);
155
156
  }
156
157
  const lang = idToken.value.toString();
157
- this._language = Lexer.languages.find((element) => element.lang === lang) || Lexer._defaultLanguage;
158
+ this._language = (_a = Lexer.languages.find((element) => element.lang === lang)) !== null && _a !== void 0 ? _a : Lexer._defaultLanguage;
158
159
  token = this.nextToken();
159
160
  if (token.type !== TokenType.great) {
160
161
  error(token);
@@ -219,6 +220,7 @@ class Lexer {
219
220
  return false;
220
221
  }
221
222
  _id() {
223
+ var _a, _b;
222
224
  let ret = "";
223
225
  const lastC = this._col;
224
226
  const lastP = this._position;
@@ -228,9 +230,9 @@ class Lexer {
228
230
  this._advance();
229
231
  }
230
232
  const translatedId = this._language
231
- ? (this._language.keywords[ret]) || ret
233
+ ? (_a = (this._language.keywords[ret])) !== null && _a !== void 0 ? _a : ret
232
234
  : ret;
233
- return Lexer.RESERVED[translatedId] || new Token(TokenType.id, ret, line, lastC);
235
+ return (_b = Lexer.RESERVED[translatedId]) !== null && _b !== void 0 ? _b : new Token(TokenType.id, ret, line, lastC);
234
236
  }
235
237
  _isLineEnd(c) {
236
238
  return c === 10 ||
@@ -891,11 +893,14 @@ class Parser {
891
893
  var _a, _b;
892
894
  const properties = [];
893
895
  if (((_a = this.currentToken) === null || _a === void 0 ? void 0 : _a.type) != TokenType.rbrace) {
896
+ this._eatEOL();
894
897
  properties.push(this._property());
895
898
  }
896
899
  while (((_b = this.currentToken) === null || _b === void 0 ? void 0 : _b.type) === TokenType.comma) {
897
900
  this._eat(TokenType.comma);
901
+ this._eatEOL();
898
902
  properties.push(this._property());
903
+ this._eatEOL();
899
904
  }
900
905
  return properties;
901
906
  }
@@ -1269,14 +1274,62 @@ class NodeVisitor extends ANodeVisitor {
1269
1274
  else { }
1270
1275
  }
1271
1276
  }
1277
+ class MEventScope {
1278
+ constructor(name, memory, parent) {
1279
+ this.memory = {};
1280
+ this.name = name;
1281
+ this.memory = memory;
1282
+ this.parent = parent;
1283
+ }
1284
+ resolve(key) {
1285
+ var _a, _b;
1286
+ return Object.keys(this.memory).includes(key) ? this.memory[key] : ((_b = (_a = this.parent) === null || _a === void 0 ? void 0 : _a.resolve(key)) !== null && _b !== void 0 ? _b : null);
1287
+ }
1288
+ change(key, value, declare = true) {
1289
+ if (Object.keys(this.memory).includes(key)) {
1290
+ // change here
1291
+ this.memory[key] = value;
1292
+ return true;
1293
+ }
1294
+ if (this.parent && this.parent.change(key, value, false)) {
1295
+ return true;
1296
+ }
1297
+ if (!declare) {
1298
+ return false;
1299
+ }
1300
+ this.memory[key] = value;
1301
+ return true;
1302
+ }
1303
+ }
1272
1304
  class MEvento extends NodeVisitor {
1273
1305
  constructor() {
1274
1306
  super();
1275
- this._memory = {};
1307
+ this.rootScope = new MEventScope("Program", {});
1308
+ this.currentScope = this.rootScope;
1276
1309
  this.debug = false;
1277
1310
  this._functionsRegistry = {};
1278
1311
  this._functionsRegistry = Object.assign({}, MEvento._globalFunctionsRegistry);
1279
1312
  }
1313
+ resolve(name) {
1314
+ var _a, _b;
1315
+ return (_b = (_a = this.currentScope) === null || _a === void 0 ? void 0 : _a.resolve(name)) !== null && _b !== void 0 ? _b : null;
1316
+ }
1317
+ changeVariable(name, value) {
1318
+ var _a;
1319
+ if ((_a = this.currentScope) === null || _a === void 0 ? void 0 : _a.change(name, value)) {
1320
+ return value;
1321
+ }
1322
+ else
1323
+ return null;
1324
+ }
1325
+ pushScope(name) {
1326
+ const scope = new MEventScope(name, {}, this.currentScope);
1327
+ this.currentScope = scope;
1328
+ }
1329
+ popScope() {
1330
+ var _a;
1331
+ this.currentScope = (_a = this.currentScope) === null || _a === void 0 ? void 0 : _a.parent;
1332
+ }
1280
1333
  log(message) {
1281
1334
  if (this.debug) {
1282
1335
  console.log(message);
@@ -1293,17 +1346,20 @@ class MEvento extends NodeVisitor {
1293
1346
  visitBlockStatementAST(node) {
1294
1347
  const list = node.body;
1295
1348
  let last;
1349
+ this.pushScope("Block");
1296
1350
  for (const n of list) {
1297
1351
  last = this.visit(n);
1298
1352
  if (last instanceof LoopControl) {
1299
- return last;
1353
+ break;
1300
1354
  }
1301
1355
  }
1356
+ this.popScope();
1302
1357
  return last;
1303
1358
  }
1304
1359
  visitIdentifierAST(node) {
1360
+ var _a;
1305
1361
  const id = node.value;
1306
- return this._memory[id];
1362
+ return (_a = this === null || this === void 0 ? void 0 : this.resolve(id)) !== null && _a !== void 0 ? _a : null;
1307
1363
  }
1308
1364
  visitLiteralAST(node) {
1309
1365
  const value = node.value;
@@ -1321,7 +1377,7 @@ class MEvento extends NodeVisitor {
1321
1377
  }
1322
1378
  else if (id instanceof IdentifierAST) {
1323
1379
  value = this.visit(init);
1324
- this._memory[id.value] = value;
1380
+ this.changeVariable(id.value, value);
1325
1381
  }
1326
1382
  return value;
1327
1383
  }
@@ -1491,51 +1547,59 @@ class MEvento extends NodeVisitor {
1491
1547
  if (ret instanceof BreakBranch) {
1492
1548
  break;
1493
1549
  }
1494
- if (ret instanceof ContinueAST) {
1550
+ if (ret instanceof ContinueBranch) {
1495
1551
  continue;
1496
1552
  }
1497
1553
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1498
1554
  }
1499
- return retainer || null;
1555
+ return retainer !== null && retainer !== void 0 ? retainer : null;
1500
1556
  }
1501
1557
  visitForLoopStatement(node) {
1502
1558
  const retainer = node.retain ? [] : undefined;
1503
1559
  const loopIdentifier = node.init.identifier;
1504
1560
  if (!(loopIdentifier instanceof IdentifierAST))
1505
1561
  throw new Error("Unexpected identifer found");
1562
+ this.pushScope("ForLoopStatement");
1506
1563
  const initialValue = this.visit(node.init.init);
1507
- this._memory[loopIdentifier.value] = initialValue;
1564
+ this.changeVariable(loopIdentifier.value, initialValue);
1508
1565
  retainer === null || retainer === void 0 ? void 0 : retainer.push(initialValue);
1509
1566
  const test = () => {
1510
1567
  const ret = this.visit(node.test);
1511
1568
  if (isNum(ret)) {
1512
- const tmp = this._memory[loopIdentifier.value];
1569
+ const tmp = this.resolve(loopIdentifier.value);
1513
1570
  return node.direction.type === TokenType.up ? ret >= tmp : ret <= tmp;
1514
1571
  }
1515
1572
  return _boolValue(ret);
1516
1573
  };
1574
+ const update = () => {
1575
+ const updateValue = this.visit(node.update);
1576
+ if (isNum(updateValue)) {
1577
+ // auto handle
1578
+ const tmp = this.resolve(loopIdentifier.value);
1579
+ if (!isNum(tmp))
1580
+ throw Error("Cant update value");
1581
+ this.changeVariable(loopIdentifier.value, node.direction.type === TokenType.up ? tmp + updateValue : tmp - updateValue);
1582
+ // console.log('Update loop from ' + tmp + ' to ' + this.resolve(loopIdentifier.value))
1583
+ }
1584
+ else {
1585
+ throw Error("Update value cant be non number");
1586
+ }
1587
+ };
1517
1588
  while (test()) {
1518
1589
  const ret = this.visit(node.body);
1519
1590
  if (ret instanceof BreakBranch) {
1520
1591
  break;
1521
1592
  }
1522
- if (ret instanceof ContinueAST) {
1593
+ if (ret instanceof ContinueBranch) {
1594
+ update();
1523
1595
  continue;
1524
1596
  }
1525
1597
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1526
1598
  // update
1527
- if (node.update instanceof LiteralAST && isNum(node.update.value)) {
1528
- // auto handle
1529
- const tmp = this._memory[loopIdentifier.value];
1530
- if (!isNum(tmp))
1531
- throw Error("Can update value");
1532
- this._memory[loopIdentifier.value] = node.direction.type === TokenType.up ? tmp + node.update.value : tmp - node.update.value;
1533
- }
1534
- else {
1535
- this.visit(node.update);
1536
- }
1599
+ update();
1537
1600
  }
1538
- return retainer || null;
1601
+ this.popScope();
1602
+ return retainer !== null && retainer !== void 0 ? retainer : null;
1539
1603
  }
1540
1604
  visitForOfStatement(node) {
1541
1605
  const collection = this.visit(node.collection);
@@ -1543,18 +1607,20 @@ class MEvento extends NodeVisitor {
1543
1607
  throw Error("Can iterate non array object");
1544
1608
  }
1545
1609
  const retainer = node.retain ? [] : undefined;
1610
+ this.pushScope("ForOfStatement");
1546
1611
  for (const it of collection) {
1547
1612
  this._declareForIdentifier(node.identifier, it);
1548
1613
  const ret = this.visit(node.body);
1549
1614
  if (ret instanceof BreakBranch) {
1550
1615
  break;
1551
1616
  }
1552
- if (ret instanceof ContinueAST) {
1617
+ if (ret instanceof ContinueBranch) {
1553
1618
  continue;
1554
1619
  }
1555
1620
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1556
1621
  }
1557
- return retainer || null;
1622
+ this.popScope();
1623
+ return retainer !== null && retainer !== void 0 ? retainer : null;
1558
1624
  }
1559
1625
  visitContinueAST(node) {
1560
1626
  return new ContinueBranch();
@@ -1564,12 +1630,10 @@ class MEvento extends NodeVisitor {
1564
1630
  if (!Array.isArray(value)) {
1565
1631
  throw Error("Unable to make a tuple from non Array element");
1566
1632
  }
1567
- this._memory[node.first.value] = value[0];
1568
- this._memory[node.second.value] = value[1];
1569
- null;
1633
+ this.changeVariable(node.first.value, value[0]);
1634
+ this.changeVariable(node.second.value, value[1]);
1570
1635
  }
1571
- this._memory[node.value] = value;
1572
- null;
1636
+ this.changeVariable(node.value, value);
1573
1637
  }
1574
1638
  resolveArguments(args) {
1575
1639
  return args.map((it) => {
@@ -1618,12 +1682,12 @@ class MEvento extends NodeVisitor {
1618
1682
  clone() {
1619
1683
  var ret = new MEvento();
1620
1684
  ret._functionsRegistry = Object.assign({}, this._functionsRegistry);
1621
- ret._memory = Object.assign({}, this._memory);
1685
+ ret.rootScope.memory = Object.assign({}, this.rootScope.memory);
1622
1686
  return ret;
1623
1687
  }
1624
1688
  newAsyncInstance() {
1625
1689
  const asyncIns = MEventoAsync.newInstance();
1626
- asyncIns._memory = this._memory;
1690
+ asyncIns.rootScope.memory = this.rootScope.memory;
1627
1691
  /// ????
1628
1692
  asyncIns._functionsRegistry = this._functionsRegistry;
1629
1693
  return asyncIns;
@@ -1650,19 +1714,21 @@ class MEventoAsync extends MEvento {
1650
1714
  return __awaiter(this, void 0, void 0, function* () {
1651
1715
  const list = node.body;
1652
1716
  let last;
1717
+ this.pushScope("Block");
1653
1718
  for (const n of list) {
1654
1719
  last = yield this.visit(n);
1655
1720
  if (last instanceof LoopControl) {
1656
- return last;
1721
+ break;
1657
1722
  }
1658
1723
  }
1724
+ this.popScope();
1659
1725
  return last;
1660
1726
  });
1661
1727
  }
1662
1728
  visitIdentifierAST(node) {
1663
1729
  return __awaiter(this, void 0, void 0, function* () {
1664
1730
  const id = node.value;
1665
- return this._memory[id];
1731
+ return this.resolve(id);
1666
1732
  });
1667
1733
  }
1668
1734
  visitLiteralAST(node) {
@@ -1684,7 +1750,7 @@ class MEventoAsync extends MEvento {
1684
1750
  }
1685
1751
  else if (id instanceof IdentifierAST) {
1686
1752
  value = yield this.visit(init);
1687
- this._memory[id.value] = value;
1753
+ this.changeVariable(id.value, value);
1688
1754
  }
1689
1755
  return value;
1690
1756
  });
@@ -1782,6 +1848,9 @@ class MEventoAsync extends MEvento {
1782
1848
  const arg = node.argument;
1783
1849
  const op = node.operation;
1784
1850
  const argValue = yield this.visit(arg);
1851
+ if (op.type === TokenType.not) {
1852
+ return _boolValue(argValue) === false;
1853
+ }
1785
1854
  if (isNum(argValue)) {
1786
1855
  throw new Error(`Operation ${op.value} not allowed no num value`);
1787
1856
  }
@@ -1865,12 +1934,12 @@ class MEventoAsync extends MEvento {
1865
1934
  return __awaiter(this, void 0, void 0, function* () {
1866
1935
  this.log(`WhileLoopStatement ${node.test} ${node.body}`);
1867
1936
  const retainer = node.retain ? [] : undefined;
1868
- while (_boolValue(this.visit(node.test))) {
1937
+ while (_boolValue(yield this.visit(node.test))) {
1869
1938
  const ret = yield this.visit(node.body);
1870
1939
  if (ret instanceof BreakBranch) {
1871
1940
  break;
1872
1941
  }
1873
- if (ret instanceof ContinueAST) {
1942
+ if (ret instanceof ContinueBranch) {
1874
1943
  continue;
1875
1944
  }
1876
1945
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
@@ -1884,59 +1953,68 @@ class MEventoAsync extends MEvento {
1884
1953
  const loopIdentifier = node.init.identifier;
1885
1954
  if (!(loopIdentifier instanceof IdentifierAST))
1886
1955
  throw new Error("Unexpected identifer found");
1887
- const initialValue = this.visit(node.init.init);
1888
- this._memory[loopIdentifier.value] = initialValue;
1956
+ this.pushScope("ForLoopStatement");
1957
+ const initialValue = yield this.visit(node.init.init);
1958
+ this.changeVariable(loopIdentifier.value, initialValue);
1889
1959
  retainer === null || retainer === void 0 ? void 0 : retainer.push(initialValue);
1890
- const test = () => {
1891
- const ret = this.visit(node.test);
1960
+ const test = () => __awaiter(this, void 0, void 0, function* () {
1961
+ const ret = yield this.visit(node.test);
1892
1962
  if (isNum(ret)) {
1893
- const tmp = this._memory[loopIdentifier.value];
1963
+ const tmp = this.resolve(loopIdentifier.value);
1894
1964
  return node.direction.type === TokenType.up ? ret >= tmp : ret <= tmp;
1895
1965
  }
1896
1966
  return _boolValue(ret);
1897
- };
1898
- while (test()) {
1967
+ });
1968
+ const update = () => __awaiter(this, void 0, void 0, function* () {
1969
+ const updateValue = yield this.visit(node.update);
1970
+ if (isNum(updateValue)) {
1971
+ // auto handle
1972
+ const tmp = this.resolve(loopIdentifier.value);
1973
+ if (!isNum(tmp))
1974
+ throw Error("Cant update value");
1975
+ this.changeVariable(loopIdentifier.value, node.direction.type === TokenType.up ? tmp + updateValue : tmp - updateValue);
1976
+ }
1977
+ else {
1978
+ throw Error("Update value cant be non number");
1979
+ }
1980
+ });
1981
+ while (yield test()) {
1899
1982
  const ret = yield this.visit(node.body);
1900
1983
  if (ret instanceof BreakBranch) {
1901
1984
  break;
1902
1985
  }
1903
- if (ret instanceof ContinueAST) {
1986
+ if (ret instanceof ContinueBranch) {
1987
+ yield update();
1904
1988
  continue;
1905
1989
  }
1906
1990
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1907
1991
  // update
1908
- if (node.update instanceof LiteralAST && isNum(node.update.value)) {
1909
- // auto handle
1910
- const tmp = this._memory[loopIdentifier.value];
1911
- if (!isNum(tmp))
1912
- throw Error("Can update value");
1913
- this._memory[loopIdentifier.value] = node.direction.type === TokenType.up ? tmp + node.update.value : tmp - node.update.value;
1914
- }
1915
- else {
1916
- this.visit(node.update);
1917
- }
1992
+ yield update();
1918
1993
  }
1919
- return retainer;
1994
+ this.popScope();
1995
+ return retainer !== null && retainer !== void 0 ? retainer : null;
1920
1996
  });
1921
1997
  }
1922
1998
  visitForOfStatement(node) {
1923
1999
  return __awaiter(this, void 0, void 0, function* () {
1924
- const collection = this.visit(node.collection);
2000
+ const collection = yield this.visit(node.collection);
1925
2001
  if (!Array.isArray(collection)) {
1926
2002
  throw Error("Can iterate non array object");
1927
2003
  }
1928
2004
  const retainer = node.retain ? [] : undefined;
2005
+ this.pushScope("ForOfStatement");
1929
2006
  for (const it of collection) {
1930
2007
  this._declareForIdentifier(node.identifier, it);
1931
2008
  const ret = yield this.visit(node.body);
1932
2009
  if (ret instanceof BreakBranch) {
1933
2010
  break;
1934
2011
  }
1935
- if (ret instanceof ContinueAST) {
2012
+ if (ret instanceof ContinueBranch) {
1936
2013
  continue;
1937
2014
  }
1938
2015
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1939
2016
  }
2017
+ this.popScope();
1940
2018
  return retainer;
1941
2019
  });
1942
2020
  }
@@ -1972,7 +2050,7 @@ class MEventoAsync extends MEvento {
1972
2050
  clone() {
1973
2051
  var ret = new MEventoAsync();
1974
2052
  ret._functionsRegistry = Object.assign({}, this._functionsRegistry);
1975
- ret._memory = Object.assign({}, this._memory);
2053
+ ret.rootScope.memory = Object.assign({}, this.rootScope.memory);
1976
2054
  return ret;
1977
2055
  }
1978
2056
  }
@@ -1985,5 +2063,5 @@ class MEventoAsync extends MEvento {
1985
2063
  * Generated bundle index. Do not edit.
1986
2064
  */
1987
2065
 
1988
- 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 };
2066
+ 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 };
1989
2067
  //# sourceMappingURL=mevento.mjs.map