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.
@@ -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 ||
@@ -1269,14 +1271,61 @@ class NodeVisitor extends ANodeVisitor {
1269
1271
  else { }
1270
1272
  }
1271
1273
  }
1274
+ class MEventScope {
1275
+ constructor(name, memory, parent) {
1276
+ this.memory = {};
1277
+ this.name = name;
1278
+ this.memory = memory;
1279
+ this.parent = parent;
1280
+ }
1281
+ resolve(key) {
1282
+ return Object.keys(this.memory).includes(key) ? this.memory[key] : (this.parent ? this.parent.resolve(key) : null);
1283
+ }
1284
+ change(key, value, declare = true) {
1285
+ if (Object.keys(this.memory).includes(key)) {
1286
+ // change here
1287
+ this.memory[key] = value;
1288
+ return true;
1289
+ }
1290
+ if (this.parent && this.parent.change(key, value, false)) {
1291
+ return true;
1292
+ }
1293
+ if (!declare) {
1294
+ return false;
1295
+ }
1296
+ this.memory[key] = value;
1297
+ return true;
1298
+ }
1299
+ }
1272
1300
  class MEvento extends NodeVisitor {
1273
1301
  constructor() {
1274
1302
  super();
1275
- this._memory = {};
1303
+ this.rootScope = new MEventScope("Program", {});
1304
+ this.currentScrope = this.rootScope;
1276
1305
  this.debug = false;
1277
1306
  this._functionsRegistry = {};
1278
1307
  this._functionsRegistry = Object.assign({}, MEvento._globalFunctionsRegistry);
1279
1308
  }
1309
+ resolve(name) {
1310
+ var _a, _b;
1311
+ return (_b = (_a = this.currentScrope) === null || _a === void 0 ? void 0 : _a.resolve(name)) !== null && _b !== void 0 ? _b : null;
1312
+ }
1313
+ changeVariable(name, value) {
1314
+ var _a;
1315
+ if ((_a = this.currentScrope) === null || _a === void 0 ? void 0 : _a.change(name, value)) {
1316
+ return value;
1317
+ }
1318
+ else
1319
+ return null;
1320
+ }
1321
+ pushScope(name) {
1322
+ const scope = new MEventScope(name, {}, this.currentScrope);
1323
+ this.currentScrope = scope;
1324
+ }
1325
+ popScope() {
1326
+ var _a;
1327
+ this.currentScrope = (_a = this.currentScrope) === null || _a === void 0 ? void 0 : _a.parent;
1328
+ }
1280
1329
  log(message) {
1281
1330
  if (this.debug) {
1282
1331
  console.log(message);
@@ -1293,17 +1342,20 @@ class MEvento extends NodeVisitor {
1293
1342
  visitBlockStatementAST(node) {
1294
1343
  const list = node.body;
1295
1344
  let last;
1345
+ this.pushScope("Block");
1296
1346
  for (const n of list) {
1297
1347
  last = this.visit(n);
1298
1348
  if (last instanceof LoopControl) {
1299
- return last;
1349
+ break;
1300
1350
  }
1301
1351
  }
1352
+ this.popScope();
1302
1353
  return last;
1303
1354
  }
1304
1355
  visitIdentifierAST(node) {
1356
+ var _a, _b;
1305
1357
  const id = node.value;
1306
- return this._memory[id];
1358
+ return (_b = (_a = this.currentScrope) === null || _a === void 0 ? void 0 : _a.resolve(id)) !== null && _b !== void 0 ? _b : null;
1307
1359
  }
1308
1360
  visitLiteralAST(node) {
1309
1361
  const value = node.value;
@@ -1321,7 +1373,7 @@ class MEvento extends NodeVisitor {
1321
1373
  }
1322
1374
  else if (id instanceof IdentifierAST) {
1323
1375
  value = this.visit(init);
1324
- this._memory[id.value] = value;
1376
+ this.changeVariable(id.value, value);
1325
1377
  }
1326
1378
  return value;
1327
1379
  }
@@ -1491,51 +1543,59 @@ class MEvento extends NodeVisitor {
1491
1543
  if (ret instanceof BreakBranch) {
1492
1544
  break;
1493
1545
  }
1494
- if (ret instanceof ContinueAST) {
1546
+ if (ret instanceof ContinueBranch) {
1495
1547
  continue;
1496
1548
  }
1497
1549
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1498
1550
  }
1499
- return retainer || null;
1551
+ return retainer !== null && retainer !== void 0 ? retainer : null;
1500
1552
  }
1501
1553
  visitForLoopStatement(node) {
1502
1554
  const retainer = node.retain ? [] : undefined;
1503
1555
  const loopIdentifier = node.init.identifier;
1504
1556
  if (!(loopIdentifier instanceof IdentifierAST))
1505
1557
  throw new Error("Unexpected identifer found");
1558
+ this.pushScope("ForLoopStatement");
1506
1559
  const initialValue = this.visit(node.init.init);
1507
- this._memory[loopIdentifier.value] = initialValue;
1560
+ this.changeVariable(loopIdentifier.value, initialValue);
1508
1561
  retainer === null || retainer === void 0 ? void 0 : retainer.push(initialValue);
1509
1562
  const test = () => {
1510
1563
  const ret = this.visit(node.test);
1511
1564
  if (isNum(ret)) {
1512
- const tmp = this._memory[loopIdentifier.value];
1565
+ const tmp = this.resolve(loopIdentifier.value);
1513
1566
  return node.direction.type === TokenType.up ? ret >= tmp : ret <= tmp;
1514
1567
  }
1515
1568
  return _boolValue(ret);
1516
1569
  };
1570
+ const update = () => {
1571
+ const updateValue = this.visit(node.update);
1572
+ if (isNum(updateValue)) {
1573
+ // auto handle
1574
+ const tmp = this.resolve(loopIdentifier.value);
1575
+ if (!isNum(tmp))
1576
+ throw Error("Cant update value");
1577
+ this.changeVariable(loopIdentifier.value, node.direction.type === TokenType.up ? tmp + updateValue : tmp - updateValue);
1578
+ // console.log('Update loop from ' + tmp + ' to ' + this.resolve(loopIdentifier.value))
1579
+ }
1580
+ else {
1581
+ throw Error("Update value cant be non number");
1582
+ }
1583
+ };
1517
1584
  while (test()) {
1518
1585
  const ret = this.visit(node.body);
1519
1586
  if (ret instanceof BreakBranch) {
1520
1587
  break;
1521
1588
  }
1522
- if (ret instanceof ContinueAST) {
1589
+ if (ret instanceof ContinueBranch) {
1590
+ update();
1523
1591
  continue;
1524
1592
  }
1525
1593
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1526
1594
  // 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
- }
1595
+ update();
1537
1596
  }
1538
- return retainer || null;
1597
+ this.popScope();
1598
+ return retainer !== null && retainer !== void 0 ? retainer : null;
1539
1599
  }
1540
1600
  visitForOfStatement(node) {
1541
1601
  const collection = this.visit(node.collection);
@@ -1543,18 +1603,20 @@ class MEvento extends NodeVisitor {
1543
1603
  throw Error("Can iterate non array object");
1544
1604
  }
1545
1605
  const retainer = node.retain ? [] : undefined;
1606
+ this.pushScope("ForOfStatement");
1546
1607
  for (const it of collection) {
1547
1608
  this._declareForIdentifier(node.identifier, it);
1548
1609
  const ret = this.visit(node.body);
1549
1610
  if (ret instanceof BreakBranch) {
1550
1611
  break;
1551
1612
  }
1552
- if (ret instanceof ContinueAST) {
1613
+ if (ret instanceof ContinueBranch) {
1553
1614
  continue;
1554
1615
  }
1555
1616
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1556
1617
  }
1557
- return retainer || null;
1618
+ this.popScope();
1619
+ return retainer !== null && retainer !== void 0 ? retainer : null;
1558
1620
  }
1559
1621
  visitContinueAST(node) {
1560
1622
  return new ContinueBranch();
@@ -1564,12 +1626,10 @@ class MEvento extends NodeVisitor {
1564
1626
  if (!Array.isArray(value)) {
1565
1627
  throw Error("Unable to make a tuple from non Array element");
1566
1628
  }
1567
- this._memory[node.first.value] = value[0];
1568
- this._memory[node.second.value] = value[1];
1569
- null;
1629
+ this.changeVariable(node.first.value, value[0]);
1630
+ this.changeVariable(node.second.value, value[1]);
1570
1631
  }
1571
- this._memory[node.value] = value;
1572
- null;
1632
+ this.changeVariable(node.value, value);
1573
1633
  }
1574
1634
  resolveArguments(args) {
1575
1635
  return args.map((it) => {
@@ -1618,12 +1678,12 @@ class MEvento extends NodeVisitor {
1618
1678
  clone() {
1619
1679
  var ret = new MEvento();
1620
1680
  ret._functionsRegistry = Object.assign({}, this._functionsRegistry);
1621
- ret._memory = Object.assign({}, this._memory);
1681
+ ret.rootScope.memory = Object.assign({}, this.rootScope.memory);
1622
1682
  return ret;
1623
1683
  }
1624
1684
  newAsyncInstance() {
1625
1685
  const asyncIns = MEventoAsync.newInstance();
1626
- asyncIns._memory = this._memory;
1686
+ asyncIns.rootScope.memory = this.rootScope.memory;
1627
1687
  /// ????
1628
1688
  asyncIns._functionsRegistry = this._functionsRegistry;
1629
1689
  return asyncIns;
@@ -1650,19 +1710,21 @@ class MEventoAsync extends MEvento {
1650
1710
  return __awaiter(this, void 0, void 0, function* () {
1651
1711
  const list = node.body;
1652
1712
  let last;
1713
+ this.pushScope("Block");
1653
1714
  for (const n of list) {
1654
1715
  last = yield this.visit(n);
1655
1716
  if (last instanceof LoopControl) {
1656
- return last;
1717
+ break;
1657
1718
  }
1658
1719
  }
1720
+ this.popScope();
1659
1721
  return last;
1660
1722
  });
1661
1723
  }
1662
1724
  visitIdentifierAST(node) {
1663
1725
  return __awaiter(this, void 0, void 0, function* () {
1664
1726
  const id = node.value;
1665
- return this._memory[id];
1727
+ return this.resolve(id);
1666
1728
  });
1667
1729
  }
1668
1730
  visitLiteralAST(node) {
@@ -1684,7 +1746,7 @@ class MEventoAsync extends MEvento {
1684
1746
  }
1685
1747
  else if (id instanceof IdentifierAST) {
1686
1748
  value = yield this.visit(init);
1687
- this._memory[id.value] = value;
1749
+ this.changeVariable(id.value, value);
1688
1750
  }
1689
1751
  return value;
1690
1752
  });
@@ -1782,6 +1844,9 @@ class MEventoAsync extends MEvento {
1782
1844
  const arg = node.argument;
1783
1845
  const op = node.operation;
1784
1846
  const argValue = yield this.visit(arg);
1847
+ if (op.type === TokenType.not) {
1848
+ return _boolValue(argValue) === false;
1849
+ }
1785
1850
  if (isNum(argValue)) {
1786
1851
  throw new Error(`Operation ${op.value} not allowed no num value`);
1787
1852
  }
@@ -1865,12 +1930,12 @@ class MEventoAsync extends MEvento {
1865
1930
  return __awaiter(this, void 0, void 0, function* () {
1866
1931
  this.log(`WhileLoopStatement ${node.test} ${node.body}`);
1867
1932
  const retainer = node.retain ? [] : undefined;
1868
- while (_boolValue(this.visit(node.test))) {
1933
+ while (_boolValue(yield this.visit(node.test))) {
1869
1934
  const ret = yield this.visit(node.body);
1870
1935
  if (ret instanceof BreakBranch) {
1871
1936
  break;
1872
1937
  }
1873
- if (ret instanceof ContinueAST) {
1938
+ if (ret instanceof ContinueBranch) {
1874
1939
  continue;
1875
1940
  }
1876
1941
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
@@ -1884,59 +1949,68 @@ class MEventoAsync extends MEvento {
1884
1949
  const loopIdentifier = node.init.identifier;
1885
1950
  if (!(loopIdentifier instanceof IdentifierAST))
1886
1951
  throw new Error("Unexpected identifer found");
1887
- const initialValue = this.visit(node.init.init);
1888
- this._memory[loopIdentifier.value] = initialValue;
1952
+ this.pushScope("ForLoopStatement");
1953
+ const initialValue = yield this.visit(node.init.init);
1954
+ this.changeVariable(loopIdentifier.value, initialValue);
1889
1955
  retainer === null || retainer === void 0 ? void 0 : retainer.push(initialValue);
1890
- const test = () => {
1891
- const ret = this.visit(node.test);
1956
+ const test = () => __awaiter(this, void 0, void 0, function* () {
1957
+ const ret = yield this.visit(node.test);
1892
1958
  if (isNum(ret)) {
1893
- const tmp = this._memory[loopIdentifier.value];
1959
+ const tmp = this.resolve(loopIdentifier.value);
1894
1960
  return node.direction.type === TokenType.up ? ret >= tmp : ret <= tmp;
1895
1961
  }
1896
1962
  return _boolValue(ret);
1897
- };
1898
- while (test()) {
1963
+ });
1964
+ const update = () => __awaiter(this, void 0, void 0, function* () {
1965
+ const updateValue = yield this.visit(node.update);
1966
+ if (isNum(updateValue)) {
1967
+ // auto handle
1968
+ const tmp = this.resolve(loopIdentifier.value);
1969
+ if (!isNum(tmp))
1970
+ throw Error("Cant update value");
1971
+ this.changeVariable(loopIdentifier.value, node.direction.type === TokenType.up ? tmp + updateValue : tmp - updateValue);
1972
+ }
1973
+ else {
1974
+ throw Error("Update value cant be non number");
1975
+ }
1976
+ });
1977
+ while (yield test()) {
1899
1978
  const ret = yield this.visit(node.body);
1900
1979
  if (ret instanceof BreakBranch) {
1901
1980
  break;
1902
1981
  }
1903
- if (ret instanceof ContinueAST) {
1982
+ if (ret instanceof ContinueBranch) {
1983
+ yield update();
1904
1984
  continue;
1905
1985
  }
1906
1986
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1907
1987
  // 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
- }
1988
+ yield update();
1918
1989
  }
1919
- return retainer;
1990
+ this.popScope();
1991
+ return retainer !== null && retainer !== void 0 ? retainer : null;
1920
1992
  });
1921
1993
  }
1922
1994
  visitForOfStatement(node) {
1923
1995
  return __awaiter(this, void 0, void 0, function* () {
1924
- const collection = this.visit(node.collection);
1996
+ const collection = yield this.visit(node.collection);
1925
1997
  if (!Array.isArray(collection)) {
1926
1998
  throw Error("Can iterate non array object");
1927
1999
  }
1928
2000
  const retainer = node.retain ? [] : undefined;
2001
+ this.pushScope("ForOfStatement");
1929
2002
  for (const it of collection) {
1930
2003
  this._declareForIdentifier(node.identifier, it);
1931
2004
  const ret = yield this.visit(node.body);
1932
2005
  if (ret instanceof BreakBranch) {
1933
2006
  break;
1934
2007
  }
1935
- if (ret instanceof ContinueAST) {
2008
+ if (ret instanceof ContinueBranch) {
1936
2009
  continue;
1937
2010
  }
1938
2011
  retainer === null || retainer === void 0 ? void 0 : retainer.push(ret);
1939
2012
  }
2013
+ this.popScope();
1940
2014
  return retainer;
1941
2015
  });
1942
2016
  }
@@ -1972,7 +2046,7 @@ class MEventoAsync extends MEvento {
1972
2046
  clone() {
1973
2047
  var ret = new MEventoAsync();
1974
2048
  ret._functionsRegistry = Object.assign({}, this._functionsRegistry);
1975
- ret._memory = Object.assign({}, this._memory);
2049
+ ret.rootScope.memory = Object.assign({}, this.rootScope.memory);
1976
2050
  return ret;
1977
2051
  }
1978
2052
  }
@@ -1985,5 +2059,5 @@ class MEventoAsync extends MEvento {
1985
2059
  * Generated bundle index. Do not edit.
1986
2060
  */
1987
2061
 
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 };
2062
+ 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
2063
  //# sourceMappingURL=mevento.mjs.map