diff --git a/src/cse-machine/__tests__/__snapshots__/cse-machine-heap.ts.snap b/src/cse-machine/__tests__/__snapshots__/cse-machine-heap.ts.snap index d571e7912..6b2085f71 100644 --- a/src/cse-machine/__tests__/__snapshots__/cse-machine-heap.ts.snap +++ b/src/cse-machine/__tests__/__snapshots__/cse-machine-heap.ts.snap @@ -162,6 +162,7 @@ EnvTree { ], "callee": Node { "end": 90, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -177,6 +178,7 @@ EnvTree { "type": "Identifier", }, "end": 101, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 12, @@ -783,6 +785,7 @@ EnvTree { ], "callee": Node { "end": 90, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -798,6 +801,7 @@ EnvTree { "type": "Identifier", }, "end": 101, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 12, @@ -1428,6 +1432,7 @@ EnvTree { ], "callee": Node { "end": 90, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -1443,6 +1448,7 @@ EnvTree { "type": "Identifier", }, "end": 101, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 12, @@ -2094,6 +2100,7 @@ EnvTree { ], "callee": Node { "end": 90, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -2109,6 +2116,7 @@ EnvTree { "type": "Identifier", }, "end": 101, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 12, @@ -2485,6 +2493,7 @@ EnvTree { ], "callee": Node { "end": 90, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -2500,6 +2509,7 @@ EnvTree { "type": "Identifier", }, "end": 101, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 12, @@ -2663,6 +2673,7 @@ EnvTree { ], "callee": Node { "end": 90, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -2678,6 +2689,7 @@ EnvTree { "type": "Identifier", }, "end": 101, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 12, @@ -5600,6 +5612,7 @@ EnvTree { ], "callee": Node { "end": 24, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -5615,6 +5628,7 @@ EnvTree { "type": "Identifier", }, "end": 33, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 10, @@ -6058,6 +6072,7 @@ EnvTree { ], "callee": Node { "end": 24, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -6073,6 +6088,7 @@ EnvTree { "type": "Identifier", }, "end": 33, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 10, @@ -6525,6 +6541,7 @@ EnvTree { ], "callee": Node { "end": 24, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -6540,6 +6557,7 @@ EnvTree { "type": "Identifier", }, "end": 33, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 10, @@ -6891,6 +6909,7 @@ EnvTree { ], "callee": Node { "end": 24, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -6906,6 +6925,7 @@ EnvTree { "type": "Identifier", }, "end": 33, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 10, @@ -7060,6 +7080,7 @@ EnvTree { ], "callee": Node { "end": 24, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -7075,6 +7096,7 @@ EnvTree { "type": "Identifier", }, "end": 33, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 10, @@ -7431,6 +7453,7 @@ EnvTree { }, ], "callee": Object { + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 1, @@ -7444,6 +7467,7 @@ EnvTree { "type": "Literal", "value": [Function], }, + "isEnvDependent": true, "loc": undefined, "optional": false, "type": "CallExpression", @@ -8275,6 +8299,7 @@ EnvTree { }, ], "callee": Object { + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 1, @@ -8288,6 +8313,7 @@ EnvTree { "type": "Literal", "value": [Function], }, + "isEnvDependent": true, "loc": undefined, "optional": false, "type": "CallExpression", @@ -9218,6 +9244,7 @@ EnvTree { }, ], "callee": Object { + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 1, @@ -9231,6 +9258,7 @@ EnvTree { "type": "Literal", "value": [Function], }, + "isEnvDependent": true, "loc": undefined, "optional": false, "type": "CallExpression", @@ -10180,6 +10208,7 @@ EnvTree { }, ], "callee": Object { + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 1, @@ -10193,6 +10222,7 @@ EnvTree { "type": "Literal", "value": [Function], }, + "isEnvDependent": true, "loc": undefined, "optional": false, "type": "CallExpression", @@ -10932,6 +10962,7 @@ EnvTree { }, ], "callee": Object { + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 1, @@ -10945,6 +10976,7 @@ EnvTree { "type": "Literal", "value": [Function], }, + "isEnvDependent": true, "loc": undefined, "optional": false, "type": "CallExpression", @@ -11183,6 +11215,7 @@ EnvTree { }, ], "callee": Object { + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 1, @@ -11196,6 +11229,7 @@ EnvTree { "type": "Literal", "value": [Function], }, + "isEnvDependent": true, "loc": undefined, "optional": false, "type": "CallExpression", diff --git a/src/cse-machine/__tests__/__snapshots__/cse-machine-runtime-context.ts.snap b/src/cse-machine/__tests__/__snapshots__/cse-machine-runtime-context.ts.snap index c66e99a3a..f6c07808f 100644 --- a/src/cse-machine/__tests__/__snapshots__/cse-machine-runtime-context.ts.snap +++ b/src/cse-machine/__tests__/__snapshots__/cse-machine-runtime-context.ts.snap @@ -2,17 +2,22 @@ exports[`Avoid unnescessary environment instruction 1 1`] = ` Control { + "numEnvDependentItems": 0, "storage": Array [ Object { "instrType": "BinaryOperation", + "isEnvDependent": false, "srcNode": Node { "end": 53, + "isEnvDependent": true, "left": Node { "arguments": Array [ Node { "end": 48, + "isEnvDependent": true, "left": Node { "end": 46, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 7, @@ -40,6 +45,7 @@ Control { "operator": "-", "right": Node { "end": 48, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 9, @@ -61,6 +67,7 @@ Control { ], "callee": Node { "end": 44, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 5, @@ -76,6 +83,7 @@ Control { "type": "Identifier", }, "end": 49, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 10, @@ -102,6 +110,7 @@ Control { "operator": "*", "right": Node { "end": 53, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 14, @@ -124,6 +133,7 @@ Control { }, Node { "end": 53, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 14, @@ -141,14 +151,18 @@ Control { }, Object { "instrType": "BinaryOperation", + "isEnvDependent": false, "srcNode": Node { "end": 53, + "isEnvDependent": true, "left": Node { "arguments": Array [ Node { "end": 48, + "isEnvDependent": true, "left": Node { "end": 46, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 7, @@ -176,6 +190,7 @@ Control { "operator": "-", "right": Node { "end": 48, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 9, @@ -197,6 +212,7 @@ Control { ], "callee": Node { "end": 44, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 5, @@ -212,6 +228,7 @@ Control { "type": "Identifier", }, "end": 49, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 10, @@ -238,6 +255,7 @@ Control { "operator": "*", "right": Node { "end": 53, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 14, @@ -260,6 +278,7 @@ Control { }, Node { "end": 53, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 14, @@ -277,14 +296,18 @@ Control { }, Object { "instrType": "BinaryOperation", + "isEnvDependent": false, "srcNode": Node { "end": 53, + "isEnvDependent": true, "left": Node { "arguments": Array [ Node { "end": 48, + "isEnvDependent": true, "left": Node { "end": 46, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 7, @@ -312,6 +335,7 @@ Control { "operator": "-", "right": Node { "end": 48, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 9, @@ -333,6 +357,7 @@ Control { ], "callee": Node { "end": 44, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 5, @@ -348,6 +373,7 @@ Control { "type": "Identifier", }, "end": 49, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 10, @@ -374,6 +400,7 @@ Control { "operator": "*", "right": Node { "end": 53, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 14, @@ -400,13 +427,17 @@ Control { exports[`Avoid unnescessary environment instruction 2 1`] = ` Control { + "numEnvDependentItems": 0, "storage": Array [ Object { "instrType": "BinaryOperation", + "isEnvDependent": false, "srcNode": Node { "end": 53, + "isEnvDependent": true, "left": Node { "end": 44, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 5, @@ -436,8 +467,10 @@ Control { "arguments": Array [ Node { "end": 52, + "isEnvDependent": true, "left": Node { "end": 50, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -465,6 +498,7 @@ Control { "operator": "-", "right": Node { "end": 52, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 13, @@ -486,6 +520,7 @@ Control { ], "callee": Node { "end": 48, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 9, @@ -501,6 +536,7 @@ Control { "type": "Identifier", }, "end": 53, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 14, @@ -521,10 +557,13 @@ Control { }, Object { "instrType": "BinaryOperation", + "isEnvDependent": false, "srcNode": Node { "end": 53, + "isEnvDependent": true, "left": Node { "end": 44, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 5, @@ -554,8 +593,10 @@ Control { "arguments": Array [ Node { "end": 52, + "isEnvDependent": true, "left": Node { "end": 50, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -583,6 +624,7 @@ Control { "operator": "-", "right": Node { "end": 52, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 13, @@ -604,6 +646,7 @@ Control { ], "callee": Node { "end": 48, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 9, @@ -619,6 +662,7 @@ Control { "type": "Identifier", }, "end": 53, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 14, @@ -639,10 +683,13 @@ Control { }, Object { "instrType": "BinaryOperation", + "isEnvDependent": false, "srcNode": Node { "end": 53, + "isEnvDependent": true, "left": Node { "end": 44, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 5, @@ -672,8 +719,10 @@ Control { "arguments": Array [ Node { "end": 52, + "isEnvDependent": true, "left": Node { "end": 50, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -701,6 +750,7 @@ Control { "operator": "-", "right": Node { "end": 52, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 13, @@ -722,6 +772,7 @@ Control { ], "callee": Node { "end": 48, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 9, @@ -737,6 +788,7 @@ Control { "type": "Identifier", }, "end": 53, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 14, @@ -761,11 +813,13 @@ Control { exports[`Avoid unnescessary environment instruction 3 1`] = ` Control { + "numEnvDependentItems": 5, "storage": Array [ Node { "end": 86, "expression": Node { "end": 85, + "isEnvDependent": true, "left": Node { "end": 81, "loc": SourceLocation { @@ -813,6 +867,7 @@ Control { "start": 80, "type": "AssignmentExpression", }, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 6, @@ -828,10 +883,12 @@ Control { }, Object { "instrType": "Pop", + "isEnvDependent": false, "srcNode": Node { "end": 86, "expression": Node { "end": 85, + "isEnvDependent": true, "left": Node { "end": 81, "loc": SourceLocation { @@ -879,6 +936,7 @@ Control { "start": 80, "type": "AssignmentExpression", }, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 6, @@ -1098,10 +1156,12 @@ Control { }, }, "instrType": "Environment", + "isEnvDependent": true, "srcNode": Node { "arguments": Array [ Node { "end": 77, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 3, @@ -1120,6 +1180,7 @@ Control { ], "callee": Node { "end": 75, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -1135,6 +1196,7 @@ Control { "type": "Identifier", }, "end": 78, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 4, @@ -1151,10 +1213,13 @@ Control { }, Object { "instrType": "BinaryOperation", + "isEnvDependent": false, "srcNode": Node { "end": 70, + "isEnvDependent": true, "left": Node { "end": 61, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 7, @@ -1184,8 +1249,10 @@ Control { "arguments": Array [ Node { "end": 69, + "isEnvDependent": true, "left": Node { "end": 67, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 13, @@ -1213,6 +1280,7 @@ Control { "operator": "-", "right": Node { "end": 69, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 15, @@ -1234,6 +1302,7 @@ Control { ], "callee": Node { "end": 65, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -1249,6 +1318,7 @@ Control { "type": "Identifier", }, "end": 70, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -1279,6 +1349,7 @@ Control { ], "callee": Node { "end": 75, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -1294,6 +1365,7 @@ Control { "type": "Identifier", }, "end": 78, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 4, @@ -1520,12 +1592,15 @@ Control { }, }, "instrType": "Environment", + "isEnvDependent": true, "srcNode": Node { "arguments": Array [ Node { "end": 69, + "isEnvDependent": true, "left": Node { "end": 67, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 13, @@ -1553,6 +1628,7 @@ Control { "operator": "-", "right": Node { "end": 69, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 15, @@ -1574,6 +1650,7 @@ Control { ], "callee": Node { "end": 65, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -1589,6 +1666,7 @@ Control { "type": "Identifier", }, "end": 70, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -1605,10 +1683,13 @@ Control { }, Object { "instrType": "BinaryOperation", + "isEnvDependent": false, "srcNode": Node { "end": 70, + "isEnvDependent": true, "left": Node { "end": 61, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 7, @@ -1638,8 +1719,10 @@ Control { "arguments": Array [ Node { "end": 69, + "isEnvDependent": true, "left": Node { "end": 67, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 13, @@ -1667,6 +1750,7 @@ Control { "operator": "-", "right": Node { "end": 69, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 15, @@ -1688,6 +1772,7 @@ Control { ], "callee": Node { "end": 65, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -1703,6 +1788,7 @@ Control { "type": "Identifier", }, "end": 70, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -1733,6 +1819,7 @@ Control { ], "callee": Node { "end": 65, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -1748,6 +1835,7 @@ Control { "type": "Identifier", }, "end": 70, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -1974,12 +2062,15 @@ Control { }, }, "instrType": "Environment", + "isEnvDependent": true, "srcNode": Node { "arguments": Array [ Node { "end": 69, + "isEnvDependent": true, "left": Node { "end": 67, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 13, @@ -2007,6 +2098,7 @@ Control { "operator": "-", "right": Node { "end": 69, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 15, @@ -2028,6 +2120,7 @@ Control { ], "callee": Node { "end": 65, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -2043,6 +2136,7 @@ Control { "type": "Identifier", }, "end": 70, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -2059,10 +2153,13 @@ Control { }, Object { "instrType": "BinaryOperation", + "isEnvDependent": false, "srcNode": Node { "end": 70, + "isEnvDependent": true, "left": Node { "end": 61, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 7, @@ -2092,8 +2189,10 @@ Control { "arguments": Array [ Node { "end": 69, + "isEnvDependent": true, "left": Node { "end": 67, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 13, @@ -2121,6 +2220,7 @@ Control { "operator": "-", "right": Node { "end": 69, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 15, @@ -2142,6 +2242,7 @@ Control { ], "callee": Node { "end": 65, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -2157,6 +2258,7 @@ Control { "type": "Identifier", }, "end": 70, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -2187,6 +2289,7 @@ Control { ], "callee": Node { "end": 65, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -2202,6 +2305,7 @@ Control { "type": "Identifier", }, "end": 70, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -2428,12 +2532,15 @@ Control { }, }, "instrType": "Environment", + "isEnvDependent": true, "srcNode": Node { "arguments": Array [ Node { "end": 69, + "isEnvDependent": true, "left": Node { "end": 67, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 13, @@ -2461,6 +2568,7 @@ Control { "operator": "-", "right": Node { "end": 69, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 15, @@ -2482,6 +2590,7 @@ Control { ], "callee": Node { "end": 65, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 11, @@ -2497,6 +2606,7 @@ Control { "type": "Identifier", }, "end": 70, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -2513,6 +2623,7 @@ Control { }, Node { "end": 53, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 7, @@ -2534,6 +2645,7 @@ Control { exports[`Avoid unnescessary environment instruction 4 1`] = ` Control { + "numEnvDependentItems": 0, "storage": Array [ Object { "body": Array [ @@ -2541,8 +2653,10 @@ Control { "end": 43, "expression": Node { "end": 42, + "isEnvDependent": false, "left": Node { "end": 38, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 5, @@ -2571,6 +2685,7 @@ Control { "operator": "+", "right": Node { "end": 42, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 9, @@ -2589,6 +2704,7 @@ Control { "start": 37, "type": "BinaryExpression", }, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 10, @@ -2606,6 +2722,7 @@ Control { "end": 50, "expression": Node { "end": 49, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 5, @@ -2621,6 +2738,7 @@ Control { "type": "Literal", "value": 3, }, + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 6, @@ -2635,6 +2753,7 @@ Control { "type": "ExpressionStatement", }, ], + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 1, @@ -2701,6 +2820,7 @@ Control { }, ], "end": 14, + "isEnvDependent": true, "kind": "let", "loc": SourceLocation { "end": Position { @@ -2784,6 +2904,7 @@ Control { "type": "VariableDeclaration", }, ], + "isEnvDependent": false, "loc": SourceLocation { "end": Position { "column": 1, diff --git a/src/cse-machine/__tests__/__snapshots__/cse-machine-unique-id.ts.snap b/src/cse-machine/__tests__/__snapshots__/cse-machine-unique-id.ts.snap index 10fff7ec6..8ccb1ec40 100644 --- a/src/cse-machine/__tests__/__snapshots__/cse-machine-unique-id.ts.snap +++ b/src/cse-machine/__tests__/__snapshots__/cse-machine-unique-id.ts.snap @@ -30,6 +30,7 @@ EnvTree { ], "callee": Node { "end": 63, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -45,6 +46,7 @@ EnvTree { "type": "Identifier", }, "end": 78, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -1225,6 +1227,7 @@ EnvTree { ], "callee": Node { "end": 63, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -1240,6 +1243,7 @@ EnvTree { "type": "Identifier", }, "end": 78, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -2519,6 +2523,7 @@ EnvTree { ], "callee": Node { "end": 63, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -2534,6 +2539,7 @@ EnvTree { "type": "Identifier", }, "end": 78, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -3863,6 +3869,7 @@ EnvTree { ], "callee": Node { "end": 63, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -3878,6 +3885,7 @@ EnvTree { "type": "Identifier", }, "end": 78, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -4966,6 +4974,7 @@ EnvTree { ], "callee": Node { "end": 63, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -4981,6 +4990,7 @@ EnvTree { "type": "Identifier", }, "end": 78, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -5273,6 +5283,7 @@ EnvTree { ], "callee": Node { "end": 63, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -5288,6 +5299,7 @@ EnvTree { "type": "Identifier", }, "end": 78, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, @@ -6903,6 +6915,7 @@ EnvTree { ], "callee": Node { "end": 63, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 1, @@ -6918,6 +6931,7 @@ EnvTree { "type": "Identifier", }, "end": 78, + "isEnvDependent": true, "loc": SourceLocation { "end": Position { "column": 16, diff --git a/src/cse-machine/interpreter.ts b/src/cse-machine/interpreter.ts index 57ab05b21..40057b6d0 100644 --- a/src/cse-machine/interpreter.ts +++ b/src/cse-machine/interpreter.ts @@ -14,13 +14,14 @@ import { UNKNOWN_LOCATION } from '../constants' import * as errors from '../errors/errors' import { RuntimeSourceError } from '../errors/runtimeSourceError' import { checkEditorBreakpoints } from '../stdlib/inspector' -import { Context, ContiguousArrayElements, Result, type StatementSequence, Value } from '../types' +import { Context, ContiguousArrayElements, Result, Value, type StatementSequence } from '../types' import * as ast from '../utils/ast/astCreator' import { filterImportDeclarations } from '../utils/ast/helpers' import { evaluateBinaryExpression, evaluateUnaryExpression } from '../utils/operators' import * as rttc from '../utils/rttc' -import { checkProgramForUndefinedVariables } from '../validator/validator' import * as seq from '../utils/statementSeqTransform' +import { checkProgramForUndefinedVariables } from '../validator/validator' +import Closure from './closure' import { Continuation, getContinuationControl, @@ -39,8 +40,8 @@ import { AssmtInstr, BinOpInstr, BranchInstr, - ControlItem, CSEBreak, + ControlItem, CseError, EnvInstr, ForInstr, @@ -72,10 +73,10 @@ import { hasImportDeclarations, isBlockStatement, isEnvArray, + isEnvDependent, isInstr, isNode, isSimpleFunction, - canAvoidEnvInstr, isStreamFn, popEnvironment, pushEnvironment, @@ -83,7 +84,6 @@ import { setVariable, valueProducing } from './utils' -import Closure from './closure' type CmdEvaluator = ( command: ControlItem, @@ -98,15 +98,33 @@ type CmdEvaluator = ( * It contains syntax tree nodes or instructions. */ export class Control extends Stack { + private numEnvDependentItems: number public constructor(program?: es.Program | StatementSequence) { super() - + this.numEnvDependentItems = 0 // Load program into control stack program ? this.push(program) : null } + public canAvoidEnvInstr(): boolean { + return this.numEnvDependentItems === 0 + } + + public pop(): ControlItem | undefined { + const item = super.pop() + if (item !== undefined && isEnvDependent(item)) { + this.numEnvDependentItems-- + } + return item + } + public push(...items: ControlItem[]): void { const itemsNew: ControlItem[] = Control.simplifyBlocksWithoutDeclarations(...items) + itemsNew.forEach((item: ControlItem) => { + if (isEnvDependent(item)) { + this.numEnvDependentItems++ + } + }) super.push(...itemsNew) } @@ -445,7 +463,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = { if ( next && !(isInstr(next) && next.instrType === InstrType.ENVIRONMENT) && - !canAvoidEnvInstr(control) + !control.canAvoidEnvInstr() ) { control.push(instr.envInstr(currentEnvironment(context), command)) } @@ -961,7 +979,7 @@ const cmdEvaluators: { [type: string]: CmdEvaluator } = { if ( next && !(isInstr(next) && next.instrType === InstrType.ENVIRONMENT) && - !canAvoidEnvInstr(control) + !control.canAvoidEnvInstr() ) { control.push(instr.envInstr(currentEnvironment(context), command.srcNode)) } diff --git a/src/cse-machine/types.ts b/src/cse-machine/types.ts index e613cdd26..2726ef6e7 100644 --- a/src/cse-machine/types.ts +++ b/src/cse-machine/types.ts @@ -93,7 +93,9 @@ export type Instr = | GenContInstr | ResumeContInstr -export type ControlItem = Node | Instr +export type ControlItem = (Node | Instr) & { + isEnvDependent?: boolean +} // Every array also has the properties `id` and `environment` for use in the frontend CSE Machine export type EnvArray = any[] & { diff --git a/src/cse-machine/utils.ts b/src/cse-machine/utils.ts index 94679e424..a24610907 100644 --- a/src/cse-machine/utils.ts +++ b/src/cse-machine/utils.ts @@ -672,9 +672,16 @@ export const hasContinueStatement = (block: es.BlockStatement | StatementSequenc * control in SIMPLE CASES, so it might not be exhaustive */ export const isEnvDependent = (command: ControlItem): boolean => { + // If the result is already calculated, return it + if (command.isEnvDependent != undefined) { + return command.isEnvDependent + } + + // Otherwise, calculate and store the result + let isDependent = true if (isInstr(command)) { const type = command.instrType - return !( + isDependent = !( type === InstrType.UNARY_OP || type === InstrType.BINARY_OP || type === InstrType.POP || @@ -688,25 +695,28 @@ export const isEnvDependent = (command: ControlItem): boolean => { const type = command.type switch (type) { case 'StatementSequence': - let isDependent = false - command.body.forEach(function (statement: es.Statement) { - isDependent = isEnvDependent(statement) || isDependent - }) - return isDependent + isDependent = command.body.some((statement: es.Statement) => isEnvDependent(statement)) case 'Literal': - return false + isDependent = false + break case 'BinaryExpression': - return isEnvDependent(command.left) || isEnvDependent(command.right) + isDependent = isEnvDependent(command.left) || isEnvDependent(command.right) + break case 'LogicalExpression': - return isEnvDependent(command.left) || isEnvDependent(command.right) + isDependent = isEnvDependent(command.left) || isEnvDependent(command.right) + break case 'UnaryExpression': - return isEnvDependent(command.argument) + isDependent = isEnvDependent(command.argument) + break case 'ExpressionStatement': - return isEnvDependent(command.expression) + isDependent = isEnvDependent(command.expression) + break default: - return true + break } } + command.isEnvDependent = isDependent + return isDependent } /** @@ -717,9 +727,5 @@ export const isEnvDependent = (command: ControlItem): boolean => { * control in SIMPLE CASES, so it might not be exhaustive */ export const canAvoidEnvInstr = (control: Control): boolean => { - let canAvoid = true - control.getStack().forEach(function (command: ControlItem) { - canAvoid = canAvoid && !isEnvDependent(command) - }) - return canAvoid + return !control.getStack().some((command: ControlItem) => isEnvDependent(command)) } diff --git a/src/types.ts b/src/types.ts index 5002820a3..e4911a649 100644 --- a/src/types.ts +++ b/src/types.ts @@ -325,7 +325,6 @@ export type Node = | StatementSequence | es.MaybeNamedClassDeclaration | es.MaybeNamedFunctionDeclaration - /* Although the ESTree specifications supposedly provide a Directive interface, the index file does not seem to export it. As such this interface was created here to fulfil the same purpose.