[hir] Parse UnaryExpression
Sathya Gunasekaran committed
Jan 25, 2023 at 11:11 UTC
f83ff2fedaae4effaa8cf9376f8b32443b50134f
4 files changed
+141
-1
compiler/forget/src/HIR/BuildHIR.ts
+9
@@ -1415,6 +1415,15 @@ function lowerExpression(
1415
loc: exprLoc,
1416
};
1417
}
1418
+ case "UnaryExpression": {
1419
+ let expr = exprPath as NodePath<t.UnaryExpression>;
1420
+ return {
1421
+ kind: "UnaryExpression",
1422
+ operator: expr.node.operator,
1423
+ value: lowerExpressionToPlace(builder, expr.get("argument")),
1424
+ loc: exprLoc,
1425
+ };
1426
+ }
1427
default: {
1428
builder.pushError({
1429
reason: `(BuildHIR::lowerExpression) Handle ${exprPath.type} expressions`,
compiler/forget/src/Inference/InferReferenceEffects.ts
-1
@@ -559,7 +559,6 @@ function inferBlock(env: Environment, block: BasicBlock) {
559
break;
560
}
561
case "UnaryExpression": {
562
- // TODO check that value must be a primitive, or make conditional based on the operator
562
valueKind = ValueKind.Immutable;
563
effectKind = Effect.Read;
564
break;
compiler/forget/src/__tests__/fixtures/hir/unary-expr.expect.md
new
+121
@@ -0,0 +1,121 @@
1
+
2
+## Input
3
+
4
+```javascript
5
+function component(a) {
6
+ let t = { t: a };
7
+ let z = +t.t;
8
+ let q = -t.t;
9
+ let p = void t.t;
10
+ let n = delete t.t;
11
+ let m = !t.t;
12
+ let e = ~t.t;
13
+ let f = typeof t.t;
14
+ return { z, p, q, n, m, e, f };
15
+}
16
+
17
+```
18
+
19
+## Code
20
+
21
+```javascript
22
+function component(a) {
23
+ const $ = React.useMemoCache();
24
+ const c_0 = $[0] !== a;
25
+ let t;
26
+ if (c_0) {
27
+ t = { t: a };
28
+ $[0] = a;
29
+ $[1] = t;
30
+ } else {
31
+ t = $[1];
32
+ }
33
+ const c_2 = $[2] !== t.t;
34
+ let z;
35
+ if (c_2) {
36
+ z = +t.t;
37
+ $[2] = t.t;
38
+ $[3] = z;
39
+ } else {
40
+ z = $[3];
41
+ }
42
+ const c_4 = $[4] !== t.t;
43
+ let q;
44
+ if (c_4) {
45
+ q = -t.t;
46
+ $[4] = t.t;
47
+ $[5] = q;
48
+ } else {
49
+ q = $[5];
50
+ }
51
+ const c_6 = $[6] !== t.t;
52
+ let p;
53
+ if (c_6) {
54
+ p = void t.t;
55
+ $[6] = t.t;
56
+ $[7] = p;
57
+ } else {
58
+ p = $[7];
59
+ }
60
+ const c_8 = $[8] !== t.t;
61
+ let n;
62
+ if (c_8) {
63
+ n = delete t.t;
64
+ $[8] = t.t;
65
+ $[9] = n;
66
+ } else {
67
+ n = $[9];
68
+ }
69
+ const c_10 = $[10] !== t.t;
70
+ let m;
71
+ if (c_10) {
72
+ m = !t.t;
73
+ $[10] = t.t;
74
+ $[11] = m;
75
+ } else {
76
+ m = $[11];
77
+ }
78
+ const c_12 = $[12] !== t.t;
79
+ let e;
80
+ if (c_12) {
81
+ e = ~t.t;
82
+ $[12] = t.t;
83
+ $[13] = e;
84
+ } else {
85
+ e = $[13];
86
+ }
87
+ const c_14 = $[14] !== t.t;
88
+ let f;
89
+ if (c_14) {
90
+ f = typeof t.t;
91
+ $[14] = t.t;
92
+ $[15] = f;
93
+ } else {
94
+ f = $[15];
95
+ }
96
+ const c_16 = $[16] !== z;
97
+ const c_17 = $[17] !== p;
98
+ const c_18 = $[18] !== q;
99
+ const c_19 = $[19] !== n;
100
+ const c_20 = $[20] !== m;
101
+ const c_21 = $[21] !== e;
102
+ const c_22 = $[22] !== f;
103
+ let t23;
104
+ if (c_16 || c_17 || c_18 || c_19 || c_20 || c_21 || c_22) {
105
+ t23 = { z: z, p: p, q: q, n: n, m: m, e: e, f: f };
106
+ $[16] = z;
107
+ $[17] = p;
108
+ $[18] = q;
109
+ $[19] = n;
110
+ $[20] = m;
111
+ $[21] = e;
112
+ $[22] = f;
113
+ $[23] = t23;
114
+ } else {
115
+ t23 = $[23];
116
+ }
117
+ return t23;
118
+}
119
+
120
+```
121
+
\ No newline at end of file
compiler/forget/src/__tests__/fixtures/hir/unary-expr.js
new
+11
@@ -0,0 +1,11 @@
1
+function component(a) {
2
+ let t = { t: a };
3
+ let z = +t.t;
4
+ let q = -t.t;
5
+ let p = void t.t;
6
+ let n = delete t.t;
7
+ let m = !t.t;
8
+ let e = ~t.t;
9
+ let f = typeof t.t;
10
+ return { z, p, q, n, m, e, f };
11
+}