mlogjs 0.4.8 → 0.4.9

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.
package/dist/index.mjs CHANGED
@@ -262,6 +262,11 @@ function camelToDashCase(name) {
262
262
  // eslint-disable-next-line @typescript-eslint/ban-types
263
263
  function assign(obj, props) {
264
264
  return Object.assign(obj, props);
265
+ }
266
+ /** Lazily computes and caches an expression */
267
+ function lazy(fn) {
268
+ let value;
269
+ return () => (value ??= fn());
265
270
  }
266
271
 
267
272
  function appendSourceLocations(valueInst, node) {
@@ -524,12 +529,16 @@ class BaseValue extends VoidValue {
524
529
  ],
525
530
  ];
526
531
  }
532
+ "!=="(scope, other, out) {
533
+ const [equal, equalInst] = this["==="](scope, other);
534
+ const [result, resultInst] = equal["!"](scope, out);
535
+ return [result, [...equalInst, ...resultInst]];
536
+ }
527
537
  }
528
538
  const operatorMap$1 = {
529
539
  "==": "equal",
530
540
  "===": "strictEqual",
531
541
  "!=": "notEqual",
532
- "!==": "notEqual",
533
542
  "<": "lessThan",
534
543
  ">": "greaterThan",
535
544
  "<=": "lessThanEq",
@@ -739,12 +748,40 @@ class StoreValue extends BaseValue {
739
748
  const [evalValue, evalInst] = value.eval(scope, this);
740
749
  return [this, [...evalInst, new SetInstruction(this, evalValue)]];
741
750
  }
751
+ "=="(scope, value, out) {
752
+ if (compareStores(this, value))
753
+ return [new LiteralValue(1), []];
754
+ return super["=="](scope, value, out);
755
+ }
756
+ "==="(scope, value, out) {
757
+ if (compareStores(this, value))
758
+ return [new LiteralValue(1), []];
759
+ return super["==="](scope, value, out);
760
+ }
761
+ "!="(scope, value, out) {
762
+ if (compareStores(this, value))
763
+ return [new LiteralValue(0), []];
764
+ return super["!="](scope, value, out);
765
+ }
766
+ "!=="(scope, value, out) {
767
+ if (compareStores(this, value))
768
+ return [new LiteralValue(0), []];
769
+ return super["!=="](scope, value, out);
770
+ }
742
771
  eval(_scope) {
743
772
  return [this, []];
744
773
  }
745
774
  toString() {
746
775
  return this.name;
747
776
  }
777
+ }
778
+ function compareStores(left, right) {
779
+ return (left === right ||
780
+ // prevents a store from being equal to a senseable value
781
+ (right instanceof left.constructor &&
782
+ right.mutability === EMutability.constant &&
783
+ left.mutability === EMutability.constant &&
784
+ right.name === left.name));
748
785
  }
749
786
 
750
787
  class ObjectValue extends VoidValue {
@@ -1228,6 +1265,17 @@ const ConditionalExpression = (c, scope, node, out) => {
1228
1265
  new AddressResolver(endExpressionAdress),
1229
1266
  ],
1230
1267
  ];
1268
+ };
1269
+ const SequenceExpression = (c, scope, node, out) => {
1270
+ const { expressions } = node;
1271
+ const inst = [];
1272
+ // compute every expression except the last one
1273
+ for (let i = 0; i < expressions.length - 1; i++) {
1274
+ const [, exprInst] = c.handleEval(scope, expressions[i], discardedName);
1275
+ inst.push(...exprInst);
1276
+ }
1277
+ const [value, valueInst] = c.handleEval(scope, expressions[expressions.length - 1], out);
1278
+ return [value, [...inst, ...valueInst]];
1231
1279
  };
1232
1280
 
1233
1281
  const Identifier = (c, scope, node) => {
@@ -1259,6 +1307,7 @@ const ReturnStatement = (c, scope, node, out) => {
1259
1307
  const [ret, retInst] = scope.function.return(scope, arg, out);
1260
1308
  return [ret, [...argInst, ...retInst]];
1261
1309
  };
1310
+ const EmptyStatement = () => [null, []];
1262
1311
  const LabeledStatement = (c, scope, node) => {
1263
1312
  const inner = scope.createScope();
1264
1313
  inner.label = node.label.name;
@@ -1308,7 +1357,7 @@ const SwitchStatement = (c, scope, node) => {
1308
1357
  let constantCase = false;
1309
1358
  let onlyConstantTests = true;
1310
1359
  for (const scase of node.cases) {
1311
- const [, bodyInst] = c.handleMany(innerScope, scase.consequent);
1360
+ const bodyInst = lazy(() => c.handleMany(innerScope, scase.consequent)[1]);
1312
1361
  const bodyAdress = new LiteralValue(null);
1313
1362
  const bodyLine = new AddressResolver(bodyAdress);
1314
1363
  const canFallInto = !endsWithoutFalltrough(inst, endLine);
@@ -1322,7 +1371,7 @@ const SwitchStatement = (c, scope, node) => {
1322
1371
  ])[1];
1323
1372
  }
1324
1373
  if (includeBody)
1325
- inst.push(bodyLine, ...bodyInst);
1374
+ inst.push(bodyLine, ...bodyInst());
1326
1375
  continue;
1327
1376
  }
1328
1377
  const [test, testInst] = c.handleEval(innerScope, scase.test);
@@ -1346,8 +1395,8 @@ const SwitchStatement = (c, scope, node) => {
1346
1395
  // and all the other cases will never run
1347
1396
  if (noFallInto &&
1348
1397
  onlyConstantTests &&
1349
- endsWithoutFalltrough(bodyInst, endLine)) {
1350
- return [null, [...bodyInst, endLine]];
1398
+ endsWithoutFalltrough(bodyInst(), endLine)) {
1399
+ return [null, [...bodyInst(), endLine]];
1351
1400
  }
1352
1401
  jump = c.handle(innerScope, scase, () => [
1353
1402
  null,
@@ -1364,7 +1413,7 @@ const SwitchStatement = (c, scope, node) => {
1364
1413
  if (includeJump)
1365
1414
  caseJumps.push(...testInst, ...jump);
1366
1415
  if (includeBody)
1367
- inst.push(bodyLine, ...bodyInst);
1416
+ inst.push(bodyLine, ...bodyInst());
1368
1417
  }
1369
1418
  // ensures that the processor exits
1370
1419
  // the switch if no cases match
@@ -1842,7 +1891,9 @@ const ObjectPattern = (c, scope, node) => {
1842
1891
  });
1843
1892
  }
1844
1893
  return [new DestructuringValue(members), inst];
1845
- };
1894
+ };
1895
+ // the mlog runtime already does this check for us
1896
+ const OptionalMemberExpression = MemberExpression;
1846
1897
 
1847
1898
  const TemplateLiteral = () => {
1848
1899
  throw new CompilerError("Template strings are currently unavailable. To inline mlog code use the asm function.");
@@ -1919,11 +1970,13 @@ var handlers = /*#__PURE__*/Object.freeze({
1919
1970
  UnaryExpression: UnaryExpression,
1920
1971
  UpdateExpression: UpdateExpression,
1921
1972
  ConditionalExpression: ConditionalExpression,
1973
+ SequenceExpression: SequenceExpression,
1922
1974
  Identifier: Identifier,
1923
1975
  ExpressionStatement: ExpressionStatement,
1924
1976
  BreakStatement: BreakStatement,
1925
1977
  ContinueStatement: ContinueStatement,
1926
1978
  ReturnStatement: ReturnStatement,
1979
+ EmptyStatement: EmptyStatement,
1927
1980
  LabeledStatement: LabeledStatement,
1928
1981
  IfStatement: IfStatement,
1929
1982
  SwitchStatement: SwitchStatement,
@@ -1942,6 +1995,7 @@ var handlers = /*#__PURE__*/Object.freeze({
1942
1995
  MemberExpression: MemberExpression,
1943
1996
  ArrayPattern: ArrayPattern,
1944
1997
  ObjectPattern: ObjectPattern,
1998
+ OptionalMemberExpression: OptionalMemberExpression,
1945
1999
  TemplateLiteral: TemplateLiteral,
1946
2000
  BlockStatement: BlockStatement,
1947
2001
  File: File,