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,268 @@
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 { cScalarType, floatLit } from '../c-syntax.mjs';
20
+ import { printChainFunctions, printCompletionWait } from './animations.mjs';
21
+ import { toCExpr } from './expressions.mjs';
22
+ import { printPanEvent } from './pan-responder.mjs';
23
+ import { APP_UPDATE_CALL, printCallback, printStmts } from './statements.mjs';
24
+ /*----------------------------------------------------------------------------------------------------------------------
25
+ - Constants
26
+ ---------------------------------------------------------------------------------------------------------------------*/
27
+ const INDENT = ' ';
28
+ /*----------------------------------------------------------------------------------------------------------------------
29
+ - Implementation
30
+ ---------------------------------------------------------------------------------------------------------------------*/
31
+ /**
32
+ * The function a dep-driven effect runs in.
33
+ *
34
+ * @param effect The effect.
35
+ *
36
+ * @returns Its name.
37
+ */
38
+ export const effectName = (effect) => `er_effect_${effect.id}`;
39
+ /**
40
+ * The flag that says an effect's last run reached its cleanup.
41
+ *
42
+ * @param effectId The effect's id.
43
+ *
44
+ * @returns The flag's name.
45
+ */
46
+ export const armedFlag = (effectId) => `s_eff${effectId}_armed`;
47
+ /**
48
+ * The copy an effect keeps of one dep from its last run.
49
+ *
50
+ * @param effect The effect.
51
+ * @param depIndex The dep's position in the list.
52
+ *
53
+ * @returns The copy's name.
54
+ */
55
+ const depCopy = (effect, depIndex) => `s_eff${effect.id}_d${depIndex}`;
56
+ /**
57
+ * A press fade as a scoped timing animation of the node's opacity value.
58
+ *
59
+ * @param fade The fade.
60
+ *
61
+ * @returns The C lines.
62
+ */
63
+ const printPressFade = (fade) => [
64
+ ' {',
65
+ ' ERAnimConfig cfg;',
66
+ ' memset(&cfg, 0, sizeof(cfg));',
67
+ ' cfg.type = ER_ANIM_TIMING;',
68
+ ` cfg.duration_ms = ${fade.durationMs};`,
69
+ ' cfg.easing = ER_EASE_QUAD_IN_OUT;',
70
+ ` er_anim_value_animate(${fade.value}, ${floatLit(fade.opacity)}, &cfg);`,
71
+ ' }',
72
+ ];
73
+ /**
74
+ * An event handler as a C function.
75
+ *
76
+ * @param handler The handler.
77
+ *
78
+ * @returns The function.
79
+ */
80
+ export function printHandler(handler) {
81
+ switch (handler.kind) {
82
+ case 'callback':
83
+ return { name: handler.name, body: printCallback(handler.callback) };
84
+ case 'pressFade':
85
+ return {
86
+ name: handler.name,
87
+ body: [
88
+ ...printPressFade(handler.fade),
89
+ ...(handler.then
90
+ ? [` ${handler.then}(node, data, user_data);`]
91
+ : []),
92
+ ],
93
+ };
94
+ case 'panEvent':
95
+ return printPanEvent(handler.pan, handler.event);
96
+ }
97
+ }
98
+ /**
99
+ * A timer callback as a C function.
100
+ *
101
+ * @param timer The timer callback.
102
+ *
103
+ * @returns The function.
104
+ */
105
+ export const printTimerFunction = (timer) => ({
106
+ name: timer.name,
107
+ body: printCallback(timer.callback),
108
+ });
109
+ /**
110
+ * An animation's on_complete callbacks as C functions.
111
+ *
112
+ * @param callback The callbacks.
113
+ *
114
+ * @returns The functions.
115
+ */
116
+ export function printAnimationCallback(callback) {
117
+ switch (callback.kind) {
118
+ case 'completion':
119
+ return [
120
+ {
121
+ name: callback.completion.name,
122
+ body: printCallback(callback.completion),
123
+ },
124
+ ];
125
+ case 'completionWait':
126
+ return [printCompletionWait(callback.completion, callback.wait)];
127
+ case 'chain':
128
+ return printChainFunctions(callback.chain).callbacks;
129
+ }
130
+ }
131
+ /**
132
+ * A function an effect runs in. A dep-driven effect with a cleanup first runs the last run's cleanup, if that
133
+ * run armed it.
134
+ *
135
+ * @param effectFunction The function.
136
+ *
137
+ * @returns The C function.
138
+ */
139
+ export function printEffectFunction(effectFunction) {
140
+ switch (effectFunction.kind) {
141
+ case 'effect': {
142
+ const { effect } = effectFunction;
143
+ const name = effectName(effect);
144
+ const body = printStmts(effect.body, INDENT);
145
+ if (!effect.hasCleanup) {
146
+ return { name, body };
147
+ }
148
+ const armed = armedFlag(effect.id);
149
+ return {
150
+ name,
151
+ body: [
152
+ ` if (${armed})`,
153
+ ' {',
154
+ ` ${armed} = 0;`,
155
+ ` ${name}_cleanup();`,
156
+ ' }',
157
+ ...body,
158
+ ],
159
+ };
160
+ }
161
+ case 'cleanup':
162
+ return {
163
+ name: `${effectName(effectFunction.effect)}_cleanup`,
164
+ body: printStmts(effectFunction.effect.cleanup, INDENT),
165
+ };
166
+ case 'mount':
167
+ return {
168
+ name: effectFunction.name,
169
+ body: printStmts(effectFunction.body, INDENT),
170
+ };
171
+ }
172
+ }
173
+ /**
174
+ * The statement that copies one dep of an effect, so the next app_update() can tell whether it changed.
175
+ *
176
+ * @param effect The effect.
177
+ * @param depIndex The dep's position in the list.
178
+ *
179
+ * @returns The C statement, without its `;`.
180
+ */
181
+ function printDepSnapshot(effect, depIndex) {
182
+ const dep = toCExpr(effect.deps[depIndex]);
183
+ const copy = depCopy(effect, depIndex);
184
+ return dep.cType === 'string'
185
+ ? `snprintf(${copy}, sizeof(${copy}), "%s", ${dep.code})`
186
+ : `${copy} = ${dep.code}`;
187
+ }
188
+ /**
189
+ * What er_app_build() runs for one mount effect.
190
+ *
191
+ * @param mountEffect The mount effect.
192
+ *
193
+ * @returns The C lines.
194
+ */
195
+ export function printMountEffect(mountEffect) {
196
+ switch (mountEffect.kind) {
197
+ case 'inline':
198
+ return printCallback(mountEffect.callback);
199
+ case 'call':
200
+ return [
201
+ ` ${mountEffect.name}();`,
202
+ ...(mountEffect.updatesState ? [APP_UPDATE_CALL] : []),
203
+ ];
204
+ case 'depEffect': {
205
+ const { effect } = mountEffect;
206
+ return [
207
+ ` ${effectName(effect)}();`,
208
+ ...effect.deps.map((_, depIndex) => ` ${printDepSnapshot(effect, depIndex)};`),
209
+ ];
210
+ }
211
+ }
212
+ }
213
+ /**
214
+ * The app_update() block that copies each dep of an effect that changed, and runs the effect once if any did.
215
+ *
216
+ * @param effect The effect.
217
+ *
218
+ * @returns The C block.
219
+ */
220
+ export function printDepEffectCheck(effect) {
221
+ const lines = [' {', ' int er_changed = 0;'];
222
+ effect.deps.forEach((depExpr, depIndex) => {
223
+ const dep = toCExpr(depExpr);
224
+ const copy = depCopy(effect, depIndex);
225
+ if (dep.cType === 'string') {
226
+ lines.push(` if (strcmp(${copy}, ${dep.code}) != 0) { ` +
227
+ `${printDepSnapshot(effect, depIndex)}; er_changed = 1; }`);
228
+ }
229
+ else {
230
+ const cType = cScalarType(dep.cType);
231
+ lines.push(` ${cType} er_d${depIndex} = ${dep.code}; if (er_d${depIndex} != ` +
232
+ `${copy}) { ${copy} = er_d${depIndex}; ` +
233
+ `er_changed = 1; }`);
234
+ }
235
+ });
236
+ lines.push(` if (er_changed) ${effectName(effect)}();`, ' }');
237
+ return lines.join('\n');
238
+ }
239
+ /**
240
+ * A file-scope variable that effects and animation chains keep between calls.
241
+ *
242
+ * @param decl The variable.
243
+ * @param stringCap The size of a string buffer.
244
+ *
245
+ * @returns The C declarations.
246
+ */
247
+ export function printStaticDecl(decl, stringCap) {
248
+ switch (decl.kind) {
249
+ case 'local':
250
+ return [
251
+ decl.cType === 'string'
252
+ ? `static char ${decl.name}[${stringCap}];`
253
+ : `static ${cScalarType(decl.cType)} ${decl.name};`,
254
+ ];
255
+ case 'armed':
256
+ return [`static int ${armedFlag(decl.effect.id)};`];
257
+ case 'deps':
258
+ return decl.effect.deps.map((depExpr, depIndex) => {
259
+ const copy = depCopy(decl.effect, depIndex);
260
+ const { cType } = toCExpr(depExpr);
261
+ return cType === 'string'
262
+ ? `static char ${copy}[${stringCap}];`
263
+ : `static ${cScalarType(cType)} ${copy};`;
264
+ });
265
+ case 'chain':
266
+ return printChainFunctions(decl.chain).decls;
267
+ }
268
+ }
@@ -0,0 +1,96 @@
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 } from '../c-syntax.mjs';
20
+ /*----------------------------------------------------------------------------------------------------------------------
21
+ - Constants
22
+ ---------------------------------------------------------------------------------------------------------------------*/
23
+ /** Key kind → its ERKeyboardKey type. */
24
+ const KEY_TYPES = {
25
+ char: 'ER_KBD_KEY_CHAR',
26
+ layer: 'ER_KBD_KEY_LAYER',
27
+ backspace: 'ER_KBD_KEY_BACKSPACE',
28
+ done: 'ER_KBD_KEY_DONE',
29
+ };
30
+ /** The highlight layer that means "never highlighted". */
31
+ const NEVER_HIGHLIGHTED = 255;
32
+ /*----------------------------------------------------------------------------------------------------------------------
33
+ - Implementation
34
+ ---------------------------------------------------------------------------------------------------------------------*/
35
+ /**
36
+ * An ARGB color as a C literal, or the engine's "use default" 0.
37
+ *
38
+ * @param color The color, or null.
39
+ *
40
+ * @returns The C.
41
+ */
42
+ const colorOrDefault = (color) => color == null
43
+ ? '0'
44
+ : '0x' + color.toString(16).padStart(8, '0').toUpperCase() + 'u';
45
+ /**
46
+ * A size, or the engine's "use default" 0.
47
+ *
48
+ * @param size The size, or null.
49
+ *
50
+ * @returns The C.
51
+ */
52
+ const sizeOrDefault = (size) => size == null ? '0' : String(size);
53
+ /**
54
+ * One key as an ERKeyboardKey initializer.
55
+ *
56
+ * @param key The key.
57
+ *
58
+ * @returns The C.
59
+ */
60
+ const printKey = (key) => `{ ${key.label == null ? 'NULL' : cstr(key.label)}, ${key.text == null ? 'NULL' : cstr(key.text)}, ` +
61
+ `${KEY_TYPES[key.kind]}, ${key.layer}, ${key.span}, ${key.highlightLayer ?? NEVER_HIGHLIGHTED} }`;
62
+ /**
63
+ * A keyboard config as static C tables (a key table per row, a row table per layer, the layer table, and the
64
+ * ERKeyboardConfig) and the er_app_build() line that installs it.
65
+ *
66
+ * @param keyboard The config.
67
+ *
68
+ * @returns The tables and the setup line.
69
+ */
70
+ export function printKeyboard(keyboard) {
71
+ const tables = [];
72
+ let layersExpr = 'NULL';
73
+ if (keyboard.layers) {
74
+ const layerInits = keyboard.layers.map((layer, layerIndex) => {
75
+ const rowInits = layer.map((row, rowIndex) => {
76
+ const name = `kbd_l${layerIndex}r${rowIndex}`;
77
+ tables.push(`static const ERKeyboardKey ${name}[] = { ${row.map(printKey).join(', ')} };`);
78
+ return `{ ${name}, ${row.length} }`;
79
+ });
80
+ tables.push(`static const ERKeyboardRow kbd_l${layerIndex}rows[] = { ${rowInits.join(', ')} };`);
81
+ return `{ kbd_l${layerIndex}rows, ${layer.length} }`;
82
+ });
83
+ tables.push(`static const ERKeyboardLayer kbd_layers[] = { ${layerInits.join(', ')} };`);
84
+ layersExpr = 'kbd_layers';
85
+ }
86
+ tables.push(`static const ERKeyboardConfig kbd_cfg = { ${layersExpr}, ${keyboard.layers?.length ?? 0}, ` +
87
+ `${sizeOrDefault(keyboard.gridCols)}, ${sizeOrDefault(keyboard.rowHeight)}, ` +
88
+ `${sizeOrDefault(keyboard.keyGap)}, ${sizeOrDefault(keyboard.keyRadius)}, ` +
89
+ `${sizeOrDefault(keyboard.fontSize)}, ${colorOrDefault(keyboard.panelColor)}, ` +
90
+ `${colorOrDefault(keyboard.keyColor)}, ` +
91
+ `${colorOrDefault(keyboard.keyActiveColor)}, ${colorOrDefault(keyboard.labelColor)} };`);
92
+ return {
93
+ data: tables.join('\n'),
94
+ setup: ' er_keyboard_set_config(&kbd_cfg);',
95
+ };
96
+ }
@@ -0,0 +1,282 @@
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 } from '../c-syntax.mjs';
20
+ import { printCondition, printExpr } from './expressions.mjs';
21
+ import { panEventName, panQueryName } from './pan-responder.mjs';
22
+ import { printText } from './text.mjs';
23
+ import { printStateDrivenVectorWiring, printVectorOps } from './vectors.mjs';
24
+ import { branchElements, isStateDriven } from '../ir/nodes.mjs';
25
+ /*----------------------------------------------------------------------------------------------------------------------
26
+ - Implementation
27
+ ---------------------------------------------------------------------------------------------------------------------*/
28
+ /**
29
+ * The C condition a conditional's node shows under.
30
+ *
31
+ * @param show The condition.
32
+ *
33
+ * @returns The C.
34
+ */
35
+ export function printShow(show) {
36
+ if (show.kind === 'rowInUse') {
37
+ return `(${show.row} < ${show.list.countMember})`;
38
+ }
39
+ const test = printCondition(show.test);
40
+ const shown = show.negate ? `!(${test})` : test;
41
+ return show.within ? `((${printShow(show.within)}) && (${shown}))` : shown;
42
+ }
43
+ /**
44
+ * A state-driven prop value as the C written to its ERProps field.
45
+ *
46
+ * @param value The value.
47
+ *
48
+ * @returns The C.
49
+ */
50
+ export function printPropValue(value) {
51
+ switch (value.kind) {
52
+ case 'constant':
53
+ return value.code;
54
+ case 'select':
55
+ return (`((${printCondition(value.test)}) ? ${printPropValue(value.whenTrue)} : ` +
56
+ `${printPropValue(value.whenFalse)})`);
57
+ case 'opacity':
58
+ return `app_opacity(${printExpr(value.value)})`;
59
+ case 'dimension':
60
+ return `app_round_dim(${printExpr(value.value)})`;
61
+ case 'visible':
62
+ return `((${printExpr(value.value)}) ? ER_DISPLAY_FLEX : ER_DISPLAY_NONE)`;
63
+ case 'shown':
64
+ return `((${printShow(value.show)}) ? ER_DISPLAY_FLEX : ER_DISPLAY_NONE)`;
65
+ case 'whileShown':
66
+ return `((${printShow(value.show)}) ? ${printPropValue(value.value)} : 0)`;
67
+ case 'flag': {
68
+ const test = value.truthiness === 'condition'
69
+ ? printCondition(value.test)
70
+ : printExpr(value.test);
71
+ return `(uint8_t)((${value.negate ? `!(${test})` : test}) ? ${value.whenTrue} : 0)`;
72
+ }
73
+ case 'byte':
74
+ return `(uint8_t)(${printPropValue(value.value)})`;
75
+ case 'float':
76
+ return `(float)(${printExpr(value.value)})`;
77
+ }
78
+ }
79
+ /**
80
+ * An animated value's bind onto a node; an interpolated one first declares its table. The Dial lays its table
81
+ * out one field per line.
82
+ *
83
+ * @param node The node.
84
+ * @param bind The bind.
85
+ * @param index Numbers the table within the node.
86
+ *
87
+ * @returns The C lines.
88
+ */
89
+ function printBind(node, bind, index) {
90
+ if (!bind.interp) {
91
+ return [` er_anim_value_bind(${bind.cVar}, ${node.id}, ${bind.prop});`];
92
+ }
93
+ const interp = bind.interp;
94
+ const name = `interp_${node.id}_${index}`;
95
+ const input = interp.input.map(floatLit).join(', ');
96
+ const output = interp.output.map(floatLit).join(', ');
97
+ const table = node.type === 'ER_NODE_ARC'
98
+ ? [
99
+ ` static const ERInterpolation ${name} = {`,
100
+ ` { ${input} },`,
101
+ ` { ${output} },`,
102
+ ` ${interp.input.length},`,
103
+ ` ${interp.exLeft},`,
104
+ ` ${interp.exRight}`,
105
+ ` };`,
106
+ ]
107
+ : [
108
+ ` static const ERInterpolation ${name} = { { ${input} }, { ${output} }, ` +
109
+ `${interp.input.length}, ${interp.exLeft}, ${interp.exRight} };`,
110
+ ];
111
+ return [
112
+ ` {`,
113
+ ...table,
114
+ ` er_anim_value_bind_interpolated(${bind.cVar}, ${node.id}, ${bind.prop}, &${name});`,
115
+ ` }`,
116
+ ];
117
+ }
118
+ /**
119
+ * Wires a PanResponder's handlers and queries onto a node.
120
+ *
121
+ * @param pan The responder.
122
+ * @param nodeId The node's variable.
123
+ *
124
+ * @returns The C lines.
125
+ */
126
+ export function printPanWiring(pan, nodeId) {
127
+ return [
128
+ ...pan.events.map(event => ` er_event_set(${nodeId}, ${event.event}, ${panEventName(pan, event)}, NULL);`),
129
+ ...pan.queries.map(query => ` er_responder_query_set(${nodeId}, ${query.query}, ${panQueryName(pan, query)}, NULL);`),
130
+ ];
131
+ }
132
+ /**
133
+ * A node as C. A node with no state-driven content sets its props once; otherwise it keeps a handle and
134
+ * app_update() sets them all again. Spans and the node's wiring are set up once.
135
+ *
136
+ * @param node The node.
137
+ *
138
+ * @returns Its build lines and app_update() entry.
139
+ */
140
+ export function printNode(node) {
141
+ const { id } = node;
142
+ const text = node.text ? printText(node.text) : null;
143
+ const imageName = node.image?.kind === 'value'
144
+ ? printExpr(node.image.value)
145
+ : node.image?.resetOnUpdate
146
+ ? cstr(node.image.name)
147
+ : null;
148
+ const build = [` ${id} = er_node_create(${node.type});`];
149
+ let update = null;
150
+ if (isStateDriven(node)) {
151
+ build.push(` s_${id} = ${id};`);
152
+ update = {
153
+ nodeId: id,
154
+ styleAssigns: node.constantProps,
155
+ text,
156
+ dynAssigns: node.stateProps,
157
+ imageName,
158
+ placeholder: node.placeholder,
159
+ };
160
+ }
161
+ else {
162
+ build.push(` er_props_default(&p);`);
163
+ for (const prop of node.constantProps) {
164
+ build.push(` p.${prop.field} = ${prop.expr};`);
165
+ }
166
+ if (node.placeholder != null) {
167
+ build.push(` snprintf(p.placeholder, sizeof(p.placeholder), "%s", ${cstr(node.placeholder)});`);
168
+ }
169
+ if (text) {
170
+ build.push(` snprintf(p.text, sizeof(p.text), "%s", ${cstr(text.format.replace(/%%/g, '%'))});`);
171
+ }
172
+ if (node.image?.kind === 'static') {
173
+ build.push(` snprintf(p.image_name, sizeof(p.image_name), "%s", ${cstr(node.image.name)});`);
174
+ }
175
+ build.push(` er_node_set_props(${id}, &p);`);
176
+ }
177
+ // Inline text spans are set once; each inherits the node's base style unless it overrides it.
178
+ if (node.spans) {
179
+ build.push(` {`, ` static const ERTextSpan spans_${id}[] = {`);
180
+ for (const span of node.spans) {
181
+ build.push(` { ${span.text}, ${span.color}, ${span.font_size}, ${span.font_weight}, ` +
182
+ `${span.font_style}, ${span.text_decoration}, ${span.letter_spacing} },`);
183
+ }
184
+ build.push(` };`, ` er_node_set_text_spans(${id}, spans_${id}, ${node.spans.length});`, ` }`);
185
+ }
186
+ // Binds, refs, events and PanResponders, in the order the element wires them.
187
+ for (const wire of node.wiring) {
188
+ switch (wire.kind) {
189
+ case 'bind':
190
+ build.push(...printBind(node, wire.bind, wire.index));
191
+ break;
192
+ case 'ref':
193
+ build.push(` ${wire.slot} = ${id};`);
194
+ break;
195
+ case 'event':
196
+ build.push(` er_event_set(${id}, ${wire.event}, ${wire.handler}, NULL);`);
197
+ break;
198
+ case 'responder':
199
+ build.push(...printPanWiring(wire.pan, id));
200
+ break;
201
+ case 'vectorTables':
202
+ build.push(...printVectorOps(id, wire.tables));
203
+ break;
204
+ case 'stateDrivenVector':
205
+ build.push(...printStateDrivenVectorWiring(id, wire.vector));
206
+ break;
207
+ }
208
+ }
209
+ return { build, update };
210
+ }
211
+ /**
212
+ * Appends a built element to its container.
213
+ *
214
+ * @param parentId The container's variable.
215
+ * @param child The element.
216
+ *
217
+ * @returns The C line.
218
+ */
219
+ export const printAppend = (parentId, child) => ` er_tree_append_child(${parentId}, ${child.id});`;
220
+ /**
221
+ * An element and everything under it as er_app_build() lines: each node, then each child's subtree followed by
222
+ * the line that appends it.
223
+ *
224
+ * @param element The element.
225
+ *
226
+ * @returns The C lines.
227
+ */
228
+ export function printElementTree(element) {
229
+ if (element.kind === 'component') {
230
+ return printElementTree(element.root);
231
+ }
232
+ const lines = printNode(element.node).build;
233
+ const appendChild = (child) => {
234
+ if (child) {
235
+ lines.push(...printElementTree(child), printAppend(element.id, child));
236
+ }
237
+ };
238
+ for (const child of element.node.children) {
239
+ if (child.kind === 'element') {
240
+ appendChild(child.element);
241
+ }
242
+ else if (child.kind === 'conditional') {
243
+ branchElements(child).forEach(appendChild);
244
+ }
245
+ else {
246
+ child.rows.forEach(appendChild);
247
+ }
248
+ }
249
+ return lines;
250
+ }
251
+ /**
252
+ * A state-driven node's app_update() entry: its props built again, then set.
253
+ *
254
+ * @param update The node's entry.
255
+ *
256
+ * @returns The C lines.
257
+ */
258
+ export function printNodeUpdate(update) {
259
+ const lines = [` er_props_default(&p);`];
260
+ for (const assign of update.styleAssigns) {
261
+ lines.push(` p.${assign.field} = ${assign.expr};`);
262
+ }
263
+ for (const assign of update.dynAssigns) {
264
+ lines.push(` p.${assign.field} = ${printPropValue(assign.value)};`);
265
+ }
266
+ if (update.placeholder != null) {
267
+ lines.push(` snprintf(p.placeholder, sizeof(p.placeholder), "%s", ${cstr(update.placeholder)});`);
268
+ }
269
+ if (update.imageName != null) {
270
+ lines.push(` snprintf(p.image_name, sizeof(p.image_name), "%s", ${update.imageName});`);
271
+ }
272
+ if (update.text) {
273
+ if (update.text.args.length) {
274
+ lines.push(` snprintf(p.text, sizeof(p.text), ${cstr(update.text.format)}, ${update.text.args.join(', ')});`);
275
+ }
276
+ else {
277
+ lines.push(` snprintf(p.text, sizeof(p.text), "%s", ${cstr(update.text.format.replace(/%%/g, '%'))});`);
278
+ }
279
+ }
280
+ lines.push(` er_node_set_props(s_${update.nodeId}, &p);`);
281
+ return lines;
282
+ }