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.
@@ -1,6 +1,7 @@
1
- import { THandler } from "../types";
1
+ import { IValue, THandler } from "../types";
2
2
  export declare const ObjectExpression: THandler;
3
3
  export declare const ArrayExpression: THandler;
4
4
  export declare const MemberExpression: THandler;
5
5
  export declare const ArrayPattern: THandler;
6
6
  export declare const ObjectPattern: THandler;
7
+ export declare const OptionalMemberExpression: THandler<IValue>;
@@ -6,3 +6,4 @@ export declare const AssignmentExpression: THandler;
6
6
  export declare const UnaryExpression: THandler;
7
7
  export declare const UpdateExpression: THandler;
8
8
  export declare const ConditionalExpression: THandler;
9
+ export declare const SequenceExpression: THandler;
@@ -3,4 +3,5 @@ export declare const ExpressionStatement: THandler<IValue | null>;
3
3
  export declare const BreakStatement: THandler<null>;
4
4
  export declare const ContinueStatement: THandler<null>;
5
5
  export declare const ReturnStatement: THandler<IValue | null>;
6
+ export declare const EmptyStatement: THandler<null>;
6
7
  export declare const LabeledStatement: THandler<null>;
package/dist/index.cjs CHANGED
@@ -283,6 +283,11 @@ function camelToDashCase(name) {
283
283
  // eslint-disable-next-line @typescript-eslint/ban-types
284
284
  function assign(obj, props) {
285
285
  return Object.assign(obj, props);
286
+ }
287
+ /** Lazily computes and caches an expression */
288
+ function lazy(fn) {
289
+ let value;
290
+ return () => (value ??= fn());
286
291
  }
287
292
 
288
293
  function appendSourceLocations(valueInst, node) {
@@ -545,12 +550,16 @@ class BaseValue extends VoidValue {
545
550
  ],
546
551
  ];
547
552
  }
553
+ "!=="(scope, other, out) {
554
+ const [equal, equalInst] = this["==="](scope, other);
555
+ const [result, resultInst] = equal["!"](scope, out);
556
+ return [result, [...equalInst, ...resultInst]];
557
+ }
548
558
  }
549
559
  const operatorMap$1 = {
550
560
  "==": "equal",
551
561
  "===": "strictEqual",
552
562
  "!=": "notEqual",
553
- "!==": "notEqual",
554
563
  "<": "lessThan",
555
564
  ">": "greaterThan",
556
565
  "<=": "lessThanEq",
@@ -760,12 +769,40 @@ class StoreValue extends BaseValue {
760
769
  const [evalValue, evalInst] = value.eval(scope, this);
761
770
  return [this, [...evalInst, new SetInstruction(this, evalValue)]];
762
771
  }
772
+ "=="(scope, value, out) {
773
+ if (compareStores(this, value))
774
+ return [new LiteralValue(1), []];
775
+ return super["=="](scope, value, out);
776
+ }
777
+ "==="(scope, value, out) {
778
+ if (compareStores(this, value))
779
+ return [new LiteralValue(1), []];
780
+ return super["==="](scope, value, out);
781
+ }
782
+ "!="(scope, value, out) {
783
+ if (compareStores(this, value))
784
+ return [new LiteralValue(0), []];
785
+ return super["!="](scope, value, out);
786
+ }
787
+ "!=="(scope, value, out) {
788
+ if (compareStores(this, value))
789
+ return [new LiteralValue(0), []];
790
+ return super["!=="](scope, value, out);
791
+ }
763
792
  eval(_scope) {
764
793
  return [this, []];
765
794
  }
766
795
  toString() {
767
796
  return this.name;
768
797
  }
798
+ }
799
+ function compareStores(left, right) {
800
+ return (left === right ||
801
+ // prevents a store from being equal to a senseable value
802
+ (right instanceof left.constructor &&
803
+ right.mutability === EMutability.constant &&
804
+ left.mutability === EMutability.constant &&
805
+ right.name === left.name));
769
806
  }
770
807
 
771
808
  class ObjectValue extends VoidValue {
@@ -1249,6 +1286,17 @@ const ConditionalExpression = (c, scope, node, out) => {
1249
1286
  new AddressResolver(endExpressionAdress),
1250
1287
  ],
1251
1288
  ];
1289
+ };
1290
+ const SequenceExpression = (c, scope, node, out) => {
1291
+ const { expressions } = node;
1292
+ const inst = [];
1293
+ // compute every expression except the last one
1294
+ for (let i = 0; i < expressions.length - 1; i++) {
1295
+ const [, exprInst] = c.handleEval(scope, expressions[i], discardedName);
1296
+ inst.push(...exprInst);
1297
+ }
1298
+ const [value, valueInst] = c.handleEval(scope, expressions[expressions.length - 1], out);
1299
+ return [value, [...inst, ...valueInst]];
1252
1300
  };
1253
1301
 
1254
1302
  const Identifier = (c, scope, node) => {
@@ -1280,6 +1328,7 @@ const ReturnStatement = (c, scope, node, out) => {
1280
1328
  const [ret, retInst] = scope.function.return(scope, arg, out);
1281
1329
  return [ret, [...argInst, ...retInst]];
1282
1330
  };
1331
+ const EmptyStatement = () => [null, []];
1283
1332
  const LabeledStatement = (c, scope, node) => {
1284
1333
  const inner = scope.createScope();
1285
1334
  inner.label = node.label.name;
@@ -1329,7 +1378,7 @@ const SwitchStatement = (c, scope, node) => {
1329
1378
  let constantCase = false;
1330
1379
  let onlyConstantTests = true;
1331
1380
  for (const scase of node.cases) {
1332
- const [, bodyInst] = c.handleMany(innerScope, scase.consequent);
1381
+ const bodyInst = lazy(() => c.handleMany(innerScope, scase.consequent)[1]);
1333
1382
  const bodyAdress = new LiteralValue(null);
1334
1383
  const bodyLine = new AddressResolver(bodyAdress);
1335
1384
  const canFallInto = !endsWithoutFalltrough(inst, endLine);
@@ -1343,7 +1392,7 @@ const SwitchStatement = (c, scope, node) => {
1343
1392
  ])[1];
1344
1393
  }
1345
1394
  if (includeBody)
1346
- inst.push(bodyLine, ...bodyInst);
1395
+ inst.push(bodyLine, ...bodyInst());
1347
1396
  continue;
1348
1397
  }
1349
1398
  const [test, testInst] = c.handleEval(innerScope, scase.test);
@@ -1367,8 +1416,8 @@ const SwitchStatement = (c, scope, node) => {
1367
1416
  // and all the other cases will never run
1368
1417
  if (noFallInto &&
1369
1418
  onlyConstantTests &&
1370
- endsWithoutFalltrough(bodyInst, endLine)) {
1371
- return [null, [...bodyInst, endLine]];
1419
+ endsWithoutFalltrough(bodyInst(), endLine)) {
1420
+ return [null, [...bodyInst(), endLine]];
1372
1421
  }
1373
1422
  jump = c.handle(innerScope, scase, () => [
1374
1423
  null,
@@ -1385,7 +1434,7 @@ const SwitchStatement = (c, scope, node) => {
1385
1434
  if (includeJump)
1386
1435
  caseJumps.push(...testInst, ...jump);
1387
1436
  if (includeBody)
1388
- inst.push(bodyLine, ...bodyInst);
1437
+ inst.push(bodyLine, ...bodyInst());
1389
1438
  }
1390
1439
  // ensures that the processor exits
1391
1440
  // the switch if no cases match
@@ -1863,7 +1912,9 @@ const ObjectPattern = (c, scope, node) => {
1863
1912
  });
1864
1913
  }
1865
1914
  return [new DestructuringValue(members), inst];
1866
- };
1915
+ };
1916
+ // the mlog runtime already does this check for us
1917
+ const OptionalMemberExpression = MemberExpression;
1867
1918
 
1868
1919
  const TemplateLiteral = () => {
1869
1920
  throw new CompilerError("Template strings are currently unavailable. To inline mlog code use the asm function.");
@@ -1940,11 +1991,13 @@ var handlers = /*#__PURE__*/Object.freeze({
1940
1991
  UnaryExpression: UnaryExpression,
1941
1992
  UpdateExpression: UpdateExpression,
1942
1993
  ConditionalExpression: ConditionalExpression,
1994
+ SequenceExpression: SequenceExpression,
1943
1995
  Identifier: Identifier,
1944
1996
  ExpressionStatement: ExpressionStatement,
1945
1997
  BreakStatement: BreakStatement,
1946
1998
  ContinueStatement: ContinueStatement,
1947
1999
  ReturnStatement: ReturnStatement,
2000
+ EmptyStatement: EmptyStatement,
1948
2001
  LabeledStatement: LabeledStatement,
1949
2002
  IfStatement: IfStatement,
1950
2003
  SwitchStatement: SwitchStatement,
@@ -1963,6 +2016,7 @@ var handlers = /*#__PURE__*/Object.freeze({
1963
2016
  MemberExpression: MemberExpression,
1964
2017
  ArrayPattern: ArrayPattern,
1965
2018
  ObjectPattern: ObjectPattern,
2019
+ OptionalMemberExpression: OptionalMemberExpression,
1966
2020
  TemplateLiteral: TemplateLiteral,
1967
2021
  BlockStatement: BlockStatement,
1968
2022
  File: File,