embedded-react 0.15.0 → 0.16.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.
Files changed (57) hide show
  1. package/aot/compile.mjs +173 -8272
  2. package/aot/compiler/animations.mjs +1001 -0
  3. package/aot/compiler/assemble.mjs +806 -0
  4. package/aot/compiler/c/animations.mjs +362 -0
  5. package/aot/compiler/c/expressions.mjs +215 -0
  6. package/aot/compiler/c/functions.mjs +268 -0
  7. package/aot/compiler/c/keyboard.mjs +96 -0
  8. package/aot/compiler/c/nodes.mjs +282 -0
  9. package/aot/compiler/c/pan-responder.mjs +136 -0
  10. package/aot/compiler/c/statements.mjs +242 -0
  11. package/aot/compiler/c/text.mjs +83 -0
  12. package/aot/compiler/c/vectors.mjs +381 -0
  13. package/aot/compiler/c-syntax.mjs +127 -0
  14. package/aot/compiler/check.mjs +165 -0
  15. package/aot/compiler/collect.mjs +743 -0
  16. package/aot/compiler/control-flow.mjs +683 -0
  17. package/aot/compiler/diagnostics.mjs +137 -0
  18. package/aot/compiler/elements/activity-indicator.mjs +160 -0
  19. package/aot/compiler/elements/dial.mjs +343 -0
  20. package/aot/compiler/elements/flat-list.mjs +139 -0
  21. package/aot/compiler/elements/image.mjs +151 -0
  22. package/aot/compiler/elements/modal.mjs +158 -0
  23. package/aot/compiler/elements/props.mjs +474 -0
  24. package/aot/compiler/elements/switch.mjs +183 -0
  25. package/aot/compiler/elements/text-input.mjs +171 -0
  26. package/aot/compiler/expressions.mjs +1479 -0
  27. package/aot/compiler/handlers.mjs +1072 -0
  28. package/aot/compiler/ir/animations.mjs +16 -0
  29. package/aot/compiler/ir/app.mjs +16 -0
  30. package/aot/compiler/ir/expressions.mjs +16 -0
  31. package/aot/compiler/ir/functions.mjs +16 -0
  32. package/aot/compiler/ir/keyboard.mjs +16 -0
  33. package/aot/compiler/ir/nodes.mjs +83 -0
  34. package/aot/compiler/ir/pan-responder.mjs +16 -0
  35. package/aot/compiler/ir/source.mjs +16 -0
  36. package/aot/compiler/ir/statements.mjs +16 -0
  37. package/aot/compiler/ir/text.mjs +16 -0
  38. package/aot/compiler/ir/types.mjs +185 -0
  39. package/aot/compiler/ir/vectors.mjs +16 -0
  40. package/aot/compiler/keyboard.mjs +152 -0
  41. package/aot/compiler/lower.mjs +282 -0
  42. package/aot/compiler/nodes.mjs +683 -0
  43. package/aot/compiler/options.mjs +55 -0
  44. package/aot/compiler/out.mjs +94 -0
  45. package/aot/compiler/pan-responder.mjs +354 -0
  46. package/aot/compiler/parse.mjs +300 -0
  47. package/aot/compiler/static-eval.mjs +228 -0
  48. package/aot/compiler/style-text.mjs +648 -0
  49. package/aot/compiler/svg.mjs +1167 -0
  50. package/aot/compiler/types.mjs +16 -0
  51. package/aot/screenshot-smoke.mjs +95 -55
  52. package/aot/style-map.mjs +366 -304
  53. package/cli.mjs +5 -8
  54. package/package.json +14 -6
  55. package/sim/embedded-react.wasm +0 -0
  56. package/src/embedded-react/FlatList.js +4 -4
  57. package/src/props.js +1 -1
@@ -0,0 +1,362 @@
1
+ /*
2
+ * Copyright 2026 Cory Lamming
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /*----------------------------------------------------------------------------------------------------------------------
17
+ - Imports
18
+ ---------------------------------------------------------------------------------------------------------------------*/
19
+ import { floatLit } from '../c-syntax.mjs';
20
+ import { printExpr } from './expressions.mjs';
21
+ /*----------------------------------------------------------------------------------------------------------------------
22
+ - Implementation
23
+ ---------------------------------------------------------------------------------------------------------------------*/
24
+ /**
25
+ * One animation as a scoped ERAnimConfig and the er_anim_value_animate call that starts it. Animation lines
26
+ * carry their own indent, whatever block they sit in.
27
+ *
28
+ * @param animation The animation.
29
+ * @param configName The config's name, unique within the function.
30
+ * @param onComplete The C function to run when it completes, or null.
31
+ *
32
+ * @returns The C block.
33
+ */
34
+ export function printAnimation(animation, configName, onComplete) {
35
+ const set = (field, value) => ` ${configName}.${field} = ${value};`;
36
+ const lines = [
37
+ ' {',
38
+ ` ERAnimConfig ${configName};`,
39
+ ` memset(&${configName}, 0, sizeof(${configName}));`,
40
+ ];
41
+ // The curve's own fields.
42
+ const curve = animation.curve;
43
+ if (curve.kind === 'spring') {
44
+ lines.push(set('type', 'ER_ANIM_SPRING'), set('stiffness', floatLit(curve.stiffness)), set('damping', floatLit(curve.damping)), set('mass', floatLit(curve.mass)));
45
+ }
46
+ else if (curve.kind === 'decay') {
47
+ lines.push(set('type', 'ER_ANIM_DECAY'), set('deceleration', floatLit(curve.deceleration)), set('velocity', floatLit(curve.velocity)));
48
+ }
49
+ else {
50
+ lines.push(set('type', 'ER_ANIM_TIMING'), set('duration_ms', String(curve.durationMs)), set('easing', curve.easing));
51
+ if (curve.bezier) {
52
+ const [x1, y1, x2, y2] = curve.bezier.map(point => floatLit(point));
53
+ lines.push(` ${configName}.bezier_x1 = ${x1}; ${configName}.bezier_y1 = ${y1};`, ` ${configName}.bezier_x2 = ${x2}; ${configName}.bezier_y2 = ${y2};`);
54
+ }
55
+ }
56
+ // The fields every curve shares.
57
+ if (animation.delayMs > 0) {
58
+ lines.push(set('delay_ms', String(animation.delayMs)));
59
+ }
60
+ if (animation.isLooping) {
61
+ lines.push(set('loop', 'true'));
62
+ }
63
+ if (onComplete) {
64
+ lines.push(set('on_complete', onComplete));
65
+ }
66
+ // Decay with no target is given the value it holds now.
67
+ const to = animation.to
68
+ ? printExpr(animation.to)
69
+ : `er_anim_value_get(${animation.handle})`;
70
+ lines.push(` er_anim_value_animate(${animation.handle}, (float)(${to}), &${configName});`, ' }');
71
+ return lines;
72
+ }
73
+ /**
74
+ * Runs a completion as finished, outside the engine's own on_complete.
75
+ *
76
+ * @param completion The completion.
77
+ *
78
+ * @returns The C line.
79
+ */
80
+ export const printCompletionCall = (completion) => ` ${completion.name}(true, NULL);`;
81
+ /**
82
+ * Starts a completion wait for one run of a composition.
83
+ *
84
+ * @param owner The composition.
85
+ * @param delayMs The wait.
86
+ * @param completion The completion it runs.
87
+ *
88
+ * @returns The C line.
89
+ */
90
+ const printWaitStart = (owner, delayMs, completion) => ` app_wait_start(${owner}, ${delayMs}, ${completion.name});`;
91
+ /**
92
+ * The completion-wait table and the helpers that drive it, each helper only when the generated code calls it
93
+ * (an unused static function would warn). Every run of a composition has a wait of its own, so starting one never
94
+ * ends another; a stop ends every run of its composition.
95
+ *
96
+ * @param callers The generated code, to look for calls in.
97
+ *
98
+ * @returns The C block.
99
+ */
100
+ export function printWaitDeclarations(callers) {
101
+ const blocks = [
102
+ `#ifndef ER_AOT_MAX_WAITS
103
+ #define ER_AOT_MAX_WAITS 8
104
+ #endif
105
+
106
+ /* The wait between a run's last animation and its completion: the composition it belongs to (which a stop
107
+ names), the time left and the completion it runs. */
108
+ typedef struct
109
+ {
110
+ int remaining_ms;
111
+ int owner;
112
+ ERAnimCompleteFn done;
113
+ uint8_t state;
114
+ } ErAnimWait;
115
+ enum
116
+ {
117
+ APP_WAIT_FREE,
118
+ APP_WAIT_PENDING,
119
+ APP_WAIT_DUE,
120
+ APP_WAIT_STOPPING
121
+ };
122
+ static ErAnimWait s_waits[ER_AOT_MAX_WAITS];
123
+
124
+ /* Starts a wait for one run; runs never share one. */
125
+ static void app_wait_start(int owner, int ms, ERAnimCompleteFn done)
126
+ {
127
+ for (int i = 0; i < ER_AOT_MAX_WAITS; i++)
128
+ {
129
+ if (s_waits[i].state == APP_WAIT_FREE)
130
+ {
131
+ s_waits[i].remaining_ms = ms;
132
+ s_waits[i].owner = owner;
133
+ s_waits[i].done = done;
134
+ s_waits[i].state = APP_WAIT_PENDING;
135
+ return;
136
+ }
137
+ }
138
+
139
+ /* The table is full (raise ER_AOT_MAX_WAITS): the run cannot wait, so it ends now, as not finished. A run
140
+ that completion starts while the table is still full is dropped instead of recursing. */
141
+ static bool s_reporting_full;
142
+ if (!s_reporting_full)
143
+ {
144
+ s_reporting_full = true;
145
+ done(false, NULL);
146
+ s_reporting_full = false;
147
+ }
148
+ }
149
+
150
+ /* Counts down every wait, then completes the ones that ran out. A wait a completion starts here waits its
151
+ full time. */
152
+ static void app_wait_tick(int dt_ms)
153
+ {
154
+ for (int i = 0; i < ER_AOT_MAX_WAITS; i++)
155
+ {
156
+ if (s_waits[i].state == APP_WAIT_PENDING)
157
+ {
158
+ s_waits[i].remaining_ms -= dt_ms;
159
+ if (s_waits[i].remaining_ms <= 0)
160
+ {
161
+ s_waits[i].state = APP_WAIT_DUE;
162
+ }
163
+ }
164
+ }
165
+ for (int i = 0; i < ER_AOT_MAX_WAITS; i++)
166
+ {
167
+ if (s_waits[i].state == APP_WAIT_DUE)
168
+ {
169
+ s_waits[i].state = APP_WAIT_FREE;
170
+ s_waits[i].done(true, NULL);
171
+ }
172
+ }
173
+ }`,
174
+ ];
175
+ if (callers.includes('app_wait_detach(')) {
176
+ blocks.push(`/* A stop's two halves: detach the composition's waits, then report them as not finished. Anything a
177
+ completion in between starts is a new run, which the stop leaves alone. */
178
+ static void app_wait_detach(int owner)
179
+ {
180
+ for (int i = 0; i < ER_AOT_MAX_WAITS; i++)
181
+ {
182
+ if (s_waits[i].owner == owner &&
183
+ (s_waits[i].state == APP_WAIT_PENDING || s_waits[i].state == APP_WAIT_DUE))
184
+ {
185
+ s_waits[i].state = APP_WAIT_STOPPING;
186
+ }
187
+ }
188
+ }
189
+
190
+ static void app_wait_report(int owner)
191
+ {
192
+ for (int i = 0; i < ER_AOT_MAX_WAITS; i++)
193
+ {
194
+ if (s_waits[i].owner == owner && s_waits[i].state == APP_WAIT_STOPPING)
195
+ {
196
+ s_waits[i].state = APP_WAIT_FREE;
197
+ s_waits[i].done(false, NULL);
198
+ }
199
+ }
200
+ }`);
201
+ }
202
+ return blocks.join('\n\n');
203
+ }
204
+ /**
205
+ * The name of a chain's callback that starts step `index` (step 0's starts a repeat).
206
+ *
207
+ * @param chain The chain.
208
+ * @param index The step.
209
+ *
210
+ * @returns The C function's name.
211
+ */
212
+ const chainCallback = (chain, index) => `er_seqcb_${chain.id}_${index}`;
213
+ /** The slot that counts a counted loop's passes. */
214
+ const passCounter = (chain) => `s_loop${chain.id}_n`;
215
+ /** The slot that keeps where a looped value started. */
216
+ const startValue = (chain) => `s_loop${chain.id}_from`;
217
+ /**
218
+ * The callback the step after `index` is started from, or what runs after the last step: the repeat for a
219
+ * loop, else the completion.
220
+ *
221
+ * @param chain The chain.
222
+ * @param index The step.
223
+ *
224
+ * @returns The C function's name, or null for nothing.
225
+ */
226
+ function afterStep(chain, index) {
227
+ if (index < chain.steps.length - 1) {
228
+ return chainCallback(chain, index + 1);
229
+ }
230
+ if (chain.loop) {
231
+ return chainCallback(chain, 0);
232
+ }
233
+ return chain.completionWait?.callback ?? chain.completion?.name ?? null;
234
+ }
235
+ /**
236
+ * Calls the completion with `finished`, from inside a chain callback.
237
+ *
238
+ * @param completion The completion, or null.
239
+ * @param finished The C value passed as `finished`.
240
+ *
241
+ * @returns The C line, or none.
242
+ */
243
+ const callCompletion = (completion, finished) => completion ? [` ${completion.name}(${finished}, NULL);`] : [];
244
+ /**
245
+ * Ends a chain or composition when the animation before was interrupted, calling the completion as not finished.
246
+ *
247
+ * @param completion The completion, or null.
248
+ *
249
+ * @returns The C lines.
250
+ */
251
+ const endIfInterrupted = (completion) => [
252
+ ' if (!finished)',
253
+ ' {',
254
+ ...callCompletion(completion, 'false'),
255
+ ' return;',
256
+ ' }',
257
+ ];
258
+ /**
259
+ * The on_complete callback that starts a completion's wait once the last animation has finished.
260
+ *
261
+ * @param completion The completion.
262
+ * @param wait The wait.
263
+ *
264
+ * @returns The C function.
265
+ */
266
+ export const printCompletionWait = (completion, wait) => ({
267
+ name: wait.callback,
268
+ body: [
269
+ ...endIfInterrupted(completion),
270
+ printWaitStart(wait.owner, wait.delayMs, completion),
271
+ ],
272
+ });
273
+ /**
274
+ * A chain's file-scope callbacks and loop slots. Each callback first ends the chain if the step before it was
275
+ * interrupted, as JS does.
276
+ *
277
+ * @param chain The chain.
278
+ *
279
+ * @returns The callbacks and declarations.
280
+ */
281
+ export function printChainFunctions(chain) {
282
+ const interrupted = endIfInterrupted(chain.completion);
283
+ // The wait after the last step comes first, then steps n..1, each started from the callback of the step before.
284
+ const callbacks = [];
285
+ if (chain.completion && chain.completionWait) {
286
+ callbacks.push(printCompletionWait(chain.completion, chain.completionWait));
287
+ }
288
+ for (let index = chain.steps.length - 1; index >= 1; index--) {
289
+ callbacks.push({
290
+ name: chainCallback(chain, index),
291
+ body: [
292
+ ...interrupted,
293
+ ...printAnimation(chain.steps[index], `cfg${chain.id}_${index}`, afterStep(chain, index)),
294
+ ],
295
+ });
296
+ }
297
+ const decls = [];
298
+ const loop = chain.loop;
299
+ if (!loop) {
300
+ return { callbacks, decls };
301
+ }
302
+ // The repeat runs when the last step finishes: it counts the pass, resets the value, and starts step 0 again.
303
+ const repeat = [...interrupted];
304
+ if (loop.iterations >= 0) {
305
+ const counter = passCounter(chain);
306
+ decls.push(`static int ${counter};`);
307
+ const finish = loop.finalWait && chain.completion
308
+ ? [
309
+ ' ' +
310
+ printWaitStart(loop.finalWait.owner, loop.finalWait.delayMs, chain.completion),
311
+ ]
312
+ : callCompletion(chain.completion, 'true');
313
+ repeat.push(` if (${counter} >= ${loop.iterations})`, ' {', ...finish, ' return;', ' }', ` ${counter}++;`);
314
+ }
315
+ if (loop.resetHandle) {
316
+ decls.push(`static float ${startValue(chain)};`);
317
+ repeat.push(` er_anim_value_set(${loop.resetHandle}, ${startValue(chain)});`);
318
+ }
319
+ repeat.push(...printAnimation(loop.restart, `cfg${chain.id}_0`, afterStep(chain, 0)));
320
+ callbacks.push({ name: chainCallback(chain, 0), body: repeat });
321
+ return { callbacks, decls };
322
+ }
323
+ /**
324
+ * The C that starts an animation where `.start()` was called. A chain's callbacks and slots are file-scope
325
+ * (see printChainFunctions).
326
+ *
327
+ * @param start The start.
328
+ *
329
+ * @returns The C lines, which carry their own indent.
330
+ */
331
+ export function printAnimationStart(start) {
332
+ switch (start.kind) {
333
+ case 'startAnimations': {
334
+ // A completion nothing can run (an endless loop of delays) is still referenced, so -Werror builds pass.
335
+ if (!start.animations.length) {
336
+ return start.completion ? [` (void)${start.completion.name};`] : [];
337
+ }
338
+ // The completion (or its wait) goes on the last animation, the only one when there is a completion.
339
+ const last = start.animations.length - 1;
340
+ const onComplete = start.completionWait?.callback ?? start.completion?.name ?? null;
341
+ return start.animations.flatMap((animation, index) => printAnimation(animation, `cfg${start.firstConfig + index}`, index === last ? onComplete : null));
342
+ }
343
+ case 'completeAfter':
344
+ return start.owner !== null
345
+ ? [printWaitStart(start.owner, start.delayMs, start.completion)]
346
+ : [printCompletionCall(start.completion)];
347
+ case 'startChain': {
348
+ // A loop first records the pass and where the value starts, then step 0 starts inline.
349
+ const lines = [];
350
+ if (start.loop && start.loop.iterations >= 0) {
351
+ lines.push(` ${passCounter(start)} = 1;`);
352
+ }
353
+ if (start.loop?.resetHandle) {
354
+ lines.push(` ${startValue(start)} = er_anim_value_get(${start.loop.resetHandle});`);
355
+ }
356
+ return [
357
+ ...lines,
358
+ ...printAnimation(start.steps[0], `cfg${start.id}_0`, afterStep(start, 0)),
359
+ ];
360
+ }
361
+ }
362
+ }
@@ -0,0 +1,215 @@
1
+ /*
2
+ * Copyright 2026 Cory Lamming
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+ /*----------------------------------------------------------------------------------------------------------------------
17
+ - Imports
18
+ ---------------------------------------------------------------------------------------------------------------------*/
19
+ import { cstr, floatLit, i64Lit } from '../c-syntax.mjs';
20
+ import { slotOf } from '../ir/types.mjs';
21
+ /*----------------------------------------------------------------------------------------------------------------------
22
+ - Constants
23
+ ---------------------------------------------------------------------------------------------------------------------*/
24
+ /** The C function each float Math function prints as. */
25
+ const FLOAT_MATH_C = {
26
+ sin: 'sinf',
27
+ cos: 'cosf',
28
+ tan: 'tanf',
29
+ sqrt: 'sqrtf',
30
+ abs: 'fabsf',
31
+ round: 'app_roundf',
32
+ floor: 'floorf',
33
+ ceil: 'ceilf',
34
+ min: 'fminf',
35
+ max: 'fmaxf',
36
+ atan2: 'atan2f',
37
+ pow: 'powf',
38
+ mod: 'fmodf',
39
+ };
40
+ /** The field of the engine's ERRect each onLayout field reads. */
41
+ const LAYOUT_RECT_C = {
42
+ x: 'x',
43
+ y: 'y',
44
+ width: 'w',
45
+ height: 'h',
46
+ };
47
+ /** The smallest C int. */
48
+ const INT_MIN = -(2 ** 31);
49
+ /*----------------------------------------------------------------------------------------------------------------------
50
+ - Implementation
51
+ ---------------------------------------------------------------------------------------------------------------------*/
52
+ /**
53
+ * `expr` used as a C condition. JS treats a string as truthy when it is non-empty; a bare char[] in C tests
54
+ * its ADDRESS, which is always true — and real GCC refuses that under -Werror=address.
55
+ *
56
+ * @param expr The lowered expression.
57
+ *
58
+ * @returns C that is true exactly when JS would find `expr` truthy.
59
+ */
60
+ export const asCond = (expr) => expr.cType === 'string' ? `(${expr.code}[0] != '\\0')` : expr.code;
61
+ /**
62
+ * A gestureState field. Travel is rebased onto the grant (RN's anchor); the point and the velocity come straight
63
+ * off the engine payload; the finger count is the engine's own, which is why er_touch_active_count() exists.
64
+ *
65
+ * @param expr The field.
66
+ *
67
+ * @returns The C expression.
68
+ */
69
+ function printGestureField(expr) {
70
+ switch (expr.field) {
71
+ case 'dx':
72
+ case 'dy':
73
+ return `(data->${expr.field} - ${expr.statePrefix}_base_${expr.field})`;
74
+ case 'moveX':
75
+ return 'data->x';
76
+ case 'moveY':
77
+ return 'data->y';
78
+ case 'x0':
79
+ case 'y0':
80
+ return `${expr.statePrefix}_${expr.field}`;
81
+ case 'vx':
82
+ case 'vy':
83
+ return `data->${expr.field}`;
84
+ case 'numberActiveTouches':
85
+ return 'er_touch_active_count()';
86
+ case 'stateID':
87
+ return String(expr.stateID);
88
+ }
89
+ }
90
+ /**
91
+ * A compile-time number as a C constant.
92
+ *
93
+ * @param expr The number.
94
+ *
95
+ * @returns Its C constant.
96
+ */
97
+ function printNumber(expr) {
98
+ if (expr.type.kind === 'float') {
99
+ return floatLit(expr.value);
100
+ }
101
+ if (expr.type.kind === 'int64') {
102
+ return i64Lit(expr.value);
103
+ }
104
+ // `-2147483648` is `-` applied to 2147483648, which does not fit an int.
105
+ return expr.value === INT_MIN ? '(-2147483647 - 1)' : String(expr.value);
106
+ }
107
+ /**
108
+ * An expression as a C condition (see asCond).
109
+ *
110
+ * @param expr The expression.
111
+ *
112
+ * @returns C that is true exactly when JS would find it truthy.
113
+ */
114
+ export const printCondition = (expr) => asCond({ code: printExpr(expr), cType: slotOf(expr.type) });
115
+ /**
116
+ * Prints an expression as C.
117
+ *
118
+ * @param expr The expression.
119
+ *
120
+ * @returns Its C.
121
+ */
122
+ export function printExpr(expr) {
123
+ switch (expr.kind) {
124
+ case 'number':
125
+ return printNumber(expr);
126
+ case 'string':
127
+ return cstr(expr.value);
128
+ case 'boolean':
129
+ return expr.value ? '1' : '0';
130
+ case 'state':
131
+ return expr.state.cMember;
132
+ case 'local':
133
+ return expr.local.code;
134
+ case 'ref':
135
+ return expr.ref.cVar;
136
+ case 'listLength':
137
+ return expr.list.countMember;
138
+ case 'itemField':
139
+ return `${expr.item.code}.${expr.field.key}`;
140
+ case 'event':
141
+ return `data->${expr.field}`;
142
+ case 'layout':
143
+ return `data->layout_rect.${LAYOUT_RECT_C[expr.field]}`;
144
+ case 'clock':
145
+ return expr.clock === 'Date' ? 'app_date_now()' : 'app_perf_now()';
146
+ case 'pi':
147
+ return '(float)M_PI';
148
+ case 'gesture':
149
+ return printGestureField(expr);
150
+ case 'toFloat':
151
+ return `((float)${printExpr(expr.operand)})`;
152
+ case 'unary': {
153
+ // The operand is parenthesized so `-` on a negative operand prints `(-(-x))`, not `(--x)` (a decrement).
154
+ const operand = expr.op === '!'
155
+ ? printCondition(expr.operand)
156
+ : printExpr(expr.operand);
157
+ return `(${expr.op}(${operand}))`;
158
+ }
159
+ case 'helper':
160
+ return `app_${expr.helper}${expr.bits === 64 ? '64' : ''}(${expr.args.map(printExpr).join(', ')})`;
161
+ case 'arithmetic':
162
+ return `(${printExpr(expr.left)} ${expr.op} ${printExpr(expr.right)})`;
163
+ case 'floatDivide':
164
+ return `((float)(${printExpr(expr.left)}) / (float)(${printExpr(expr.right)}))`;
165
+ case 'narrowRemainder':
166
+ return `((int)(${printExpr(expr.left)} % ${printExpr(expr.right)}))`;
167
+ case 'floatMath': {
168
+ const args = expr.args.map(arg => `(float)(${printExpr(arg)})`);
169
+ return `${FLOAT_MATH_C[expr.fn]}(${args.join(', ')})`;
170
+ }
171
+ case 'wholeRound':
172
+ return printExpr(expr.operand);
173
+ case 'trunc':
174
+ return `app_f2i((float)(${printExpr(expr.operand)}))`;
175
+ case 'toInt':
176
+ return `app_f2i(${printExpr(expr.operand)})`;
177
+ case 'compare':
178
+ return `(${printExpr(expr.left)} ${expr.op} ${printExpr(expr.right)})`;
179
+ case 'stringEquals':
180
+ return `(strcmp(${printExpr(expr.left)}, ${printExpr(expr.right)}) ${expr.op} 0)`;
181
+ case 'logical':
182
+ return `(${printCondition(expr.left)} ${expr.op} ${printCondition(expr.right)})`;
183
+ case 'pick': {
184
+ // Reading `left` twice is safe: expressions have no side effects, and the clock only moves in er_tick().
185
+ const left = printExpr(expr.left);
186
+ const right = printExpr(expr.right);
187
+ return expr.op === '||'
188
+ ? `(${left} ? ${left} : ${right})`
189
+ : `(${left} ? ${right} : ${left})`;
190
+ }
191
+ case 'conditional':
192
+ return `(${printCondition(expr.test)} ? ${printExpr(expr.consequent)} : ${printExpr(expr.alternate)})`;
193
+ }
194
+ }
195
+ /**
196
+ * An expression as the C expression the emitters build with: its C, and what is known about its value.
197
+ *
198
+ * @param expr The expression.
199
+ *
200
+ * @returns The C expression.
201
+ */
202
+ export function toCExpr(expr) {
203
+ // A bare local is the Local itself, so a list row keeps its struct (extractProps copies it into a prop).
204
+ if (expr.kind === 'local') {
205
+ return expr.local;
206
+ }
207
+ const cExpr = { code: printExpr(expr), cType: slotOf(expr.type) };
208
+ if (expr.type.kind === 'bool') {
209
+ cExpr.isBool = true;
210
+ }
211
+ else if (expr.type.kind === 'int64' && expr.type.source !== 'time') {
212
+ cExpr[expr.type.source === 'number' ? 'lit' : 'wide'] = true;
213
+ }
214
+ return cExpr;
215
+ }