embedded-react 0.15.0 → 0.17.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.
- package/aot/compile.mjs +173 -8272
- package/aot/compiler/animations.mjs +1001 -0
- package/aot/compiler/assemble.mjs +922 -0
- package/aot/compiler/boot.mjs +441 -0
- package/aot/compiler/branch-merge.mjs +319 -0
- package/aot/compiler/c/animations.mjs +362 -0
- package/aot/compiler/c/boot.mjs +127 -0
- package/aot/compiler/c/expressions.mjs +270 -0
- package/aot/compiler/c/functions.mjs +268 -0
- package/aot/compiler/c/keyboard.mjs +96 -0
- package/aot/compiler/c/nodes.mjs +294 -0
- package/aot/compiler/c/pan-responder.mjs +136 -0
- package/aot/compiler/c/statements.mjs +271 -0
- package/aot/compiler/c/text.mjs +83 -0
- package/aot/compiler/c/vectors.mjs +381 -0
- package/aot/compiler/c-syntax.mjs +127 -0
- package/aot/compiler/check.mjs +165 -0
- package/aot/compiler/collect.mjs +785 -0
- package/aot/compiler/control-flow.mjs +696 -0
- package/aot/compiler/diagnostics.mjs +137 -0
- package/aot/compiler/elements/activity-indicator.mjs +160 -0
- package/aot/compiler/elements/dial.mjs +346 -0
- package/aot/compiler/elements/flat-list.mjs +139 -0
- package/aot/compiler/elements/image.mjs +151 -0
- package/aot/compiler/elements/modal.mjs +158 -0
- package/aot/compiler/elements/props.mjs +474 -0
- package/aot/compiler/elements/switch.mjs +183 -0
- package/aot/compiler/elements/text-input.mjs +171 -0
- package/aot/compiler/expressions.mjs +1579 -0
- package/aot/compiler/handlers.mjs +1072 -0
- package/aot/compiler/ir/animations.mjs +16 -0
- package/aot/compiler/ir/app.mjs +16 -0
- package/aot/compiler/ir/boot.mjs +16 -0
- package/aot/compiler/ir/expressions.mjs +16 -0
- package/aot/compiler/ir/functions.mjs +16 -0
- package/aot/compiler/ir/keyboard.mjs +16 -0
- package/aot/compiler/ir/nodes.mjs +107 -0
- package/aot/compiler/ir/pan-responder.mjs +16 -0
- package/aot/compiler/ir/reads.mjs +351 -0
- package/aot/compiler/ir/source.mjs +16 -0
- package/aot/compiler/ir/statements.mjs +16 -0
- package/aot/compiler/ir/text.mjs +16 -0
- package/aot/compiler/ir/types.mjs +185 -0
- package/aot/compiler/ir/vectors.mjs +16 -0
- package/aot/compiler/keyboard.mjs +152 -0
- package/aot/compiler/lower.mjs +311 -0
- package/aot/compiler/nodes.mjs +688 -0
- package/aot/compiler/options.mjs +55 -0
- package/aot/compiler/out.mjs +94 -0
- package/aot/compiler/pan-responder.mjs +354 -0
- package/aot/compiler/parse.mjs +300 -0
- package/aot/compiler/static-eval.mjs +454 -0
- package/aot/compiler/style-text.mjs +807 -0
- package/aot/compiler/svg.mjs +1167 -0
- package/aot/compiler/types.mjs +16 -0
- package/aot/screenshot-smoke.mjs +95 -55
- package/aot/style-map.mjs +366 -304
- package/cli.mjs +5 -8
- package/package.json +14 -6
- package/sim/embedded-react.wasm +0 -0
- package/src/embedded-react/FlatList.js +4 -4
- package/src/props.js +1 -1
|
@@ -0,0 +1,127 @@
|
|
|
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
|
+
import { doubleLit, printExpr, printInDoubles } from './expressions.mjs';
|
|
21
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
22
|
+
- Constants
|
|
23
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
24
|
+
/**
|
|
25
|
+
* How far a fraction a guard reads may stray from the build's, relative to its size. Boot works in doubles as JS
|
|
26
|
+
* does, so only a libm function's last digit can differ; any other panel's value is far outside this.
|
|
27
|
+
*/
|
|
28
|
+
const FLOAT_TOLERANCE = 1e-9;
|
|
29
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
30
|
+
- Implementation
|
|
31
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
32
|
+
/**
|
|
33
|
+
* Whether an app works anything out at boot or assumes anything of its panel, so it has an app_boot().
|
|
34
|
+
*
|
|
35
|
+
* @param boot The app's boot values.
|
|
36
|
+
*
|
|
37
|
+
* @returns True when it does.
|
|
38
|
+
*/
|
|
39
|
+
export const hasBoot = (boot) => boot.slots.length > 0 || boot.assumptions.length > 0;
|
|
40
|
+
/**
|
|
41
|
+
* A slot's C declaration in the ErAppBoot struct. A fraction is a double, as JS holds it.
|
|
42
|
+
*
|
|
43
|
+
* @param slot The slot.
|
|
44
|
+
*
|
|
45
|
+
* @returns The C line.
|
|
46
|
+
*/
|
|
47
|
+
function printSlotDecl(slot) {
|
|
48
|
+
const cType = slot.type.kind === 'float'
|
|
49
|
+
? 'double'
|
|
50
|
+
: slot.type.kind === 'string'
|
|
51
|
+
? 'const char*'
|
|
52
|
+
: 'int';
|
|
53
|
+
return ` ${cType} ${slot.field};`;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* The file-scope struct of the values er_app_build() works out at boot.
|
|
57
|
+
*
|
|
58
|
+
* @param boot The app's boot values.
|
|
59
|
+
*
|
|
60
|
+
* @returns The C, or '' when there are none.
|
|
61
|
+
*/
|
|
62
|
+
export const printBootDecls = (boot) => boot.slots.length
|
|
63
|
+
? `/* The values that depend on the panel size, worked out once by er_app_build(). */\n` +
|
|
64
|
+
`typedef struct\n{\n${boot.slots.map(printSlotDecl).join('\n')}\n} ErAppBoot;\n\n` +
|
|
65
|
+
`static ErAppBoot s_boot;\n`
|
|
66
|
+
: '';
|
|
67
|
+
/**
|
|
68
|
+
* The C test that a panel meets one assumption.
|
|
69
|
+
*
|
|
70
|
+
* @param assumption The assumption.
|
|
71
|
+
* @param screen The panel the build was compiled for.
|
|
72
|
+
*
|
|
73
|
+
* @returns The C condition.
|
|
74
|
+
*/
|
|
75
|
+
function printAssumption(assumption, screen) {
|
|
76
|
+
if (assumption.kind === 'panel') {
|
|
77
|
+
return `(screen_w == ${screen.width} && screen_h == ${screen.height})`;
|
|
78
|
+
}
|
|
79
|
+
const read = `s_boot.${assumption.slot.field}`;
|
|
80
|
+
const { value } = assumption;
|
|
81
|
+
if (typeof value === 'string')
|
|
82
|
+
return `(strcmp(${read}, ${cstr(value)}) == 0)`;
|
|
83
|
+
if (typeof value === 'boolean')
|
|
84
|
+
return value ? read : `!${read}`;
|
|
85
|
+
if (assumption.slot.type.kind !== 'float') {
|
|
86
|
+
return `(${read} == ${value})`;
|
|
87
|
+
}
|
|
88
|
+
// A fraction within a small margin: boot and the compiler need not round its last digit alike.
|
|
89
|
+
const margin = doubleLit(FLOAT_TOLERANCE * (1 + Math.abs(value)));
|
|
90
|
+
const expected = doubleLit(value);
|
|
91
|
+
return `(${read} - ${expected} <= ${margin} && ${expected} - ${read} <= ${margin})`;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* app_boot(): sets each boot value from the panel size, then reports whether the panel meets everything the
|
|
95
|
+
* build assumed of it.
|
|
96
|
+
*
|
|
97
|
+
* @param boot The app's boot values.
|
|
98
|
+
* @param screen The panel the build was compiled for.
|
|
99
|
+
*
|
|
100
|
+
* @returns The C function, or '' when the app has no boot values.
|
|
101
|
+
*/
|
|
102
|
+
export function printBootFunction(boot, screen) {
|
|
103
|
+
if (!hasBoot(boot))
|
|
104
|
+
return '';
|
|
105
|
+
const lines = boot.slots.map(({ field, init }) => {
|
|
106
|
+
const value = init.kind === 'expr'
|
|
107
|
+
? printInDoubles(() => printExpr(init.expr))
|
|
108
|
+
: init.dimension === 'width'
|
|
109
|
+
? 'screen_w'
|
|
110
|
+
: 'screen_h';
|
|
111
|
+
return ` s_boot.${field} = ${value};`;
|
|
112
|
+
});
|
|
113
|
+
const body = lines.join('\n');
|
|
114
|
+
const reads = (name) => new RegExp(`\\b${name}\\b`).test(body);
|
|
115
|
+
const guards = boot.assumptions.map(assumption => printAssumption(assumption, screen));
|
|
116
|
+
const unused = ['screen_w', 'screen_h']
|
|
117
|
+
.filter(name => !reads(name) && !guards.some(guard => guard.includes(name)))
|
|
118
|
+
.map(name => ` (void)${name};`);
|
|
119
|
+
const verdict = guards.length
|
|
120
|
+
? ` return ${guards.join('\n && ')};`
|
|
121
|
+
: ' return true;';
|
|
122
|
+
return ('/* Works out the values that depend on the panel size, and whether this panel takes the layout the app\n' +
|
|
123
|
+
' was compiled for: what the compiler folded at its panel has to come out the same here. */\n' +
|
|
124
|
+
'static bool app_boot(int screen_w, int screen_h)\n{\n' +
|
|
125
|
+
[...unused, ...lines, verdict].join('\n') +
|
|
126
|
+
'\n}');
|
|
127
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
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 C function each float Math function prints as when a boot value is worked out in double precision. */
|
|
41
|
+
const DOUBLE_MATH_C = {
|
|
42
|
+
sin: 'sin',
|
|
43
|
+
cos: 'cos',
|
|
44
|
+
tan: 'tan',
|
|
45
|
+
sqrt: 'sqrt',
|
|
46
|
+
abs: 'fabs',
|
|
47
|
+
round: 'app_round',
|
|
48
|
+
floor: 'floor',
|
|
49
|
+
ceil: 'ceil',
|
|
50
|
+
min: 'fmin',
|
|
51
|
+
max: 'fmax',
|
|
52
|
+
atan2: 'atan2',
|
|
53
|
+
pow: 'pow',
|
|
54
|
+
mod: 'fmod',
|
|
55
|
+
};
|
|
56
|
+
/** The field of the engine's ERRect each onLayout field reads. */
|
|
57
|
+
const LAYOUT_RECT_C = {
|
|
58
|
+
x: 'x',
|
|
59
|
+
y: 'y',
|
|
60
|
+
width: 'w',
|
|
61
|
+
height: 'h',
|
|
62
|
+
};
|
|
63
|
+
/** The smallest C int. */
|
|
64
|
+
const INT_MIN = -(2 ** 31);
|
|
65
|
+
/** Whether a fraction prints as a double, as JS computes it, rather than a float (see printInDoubles). */
|
|
66
|
+
let inDoubles = false;
|
|
67
|
+
/*----------------------------------------------------------------------------------------------------------------------
|
|
68
|
+
- Implementation
|
|
69
|
+
---------------------------------------------------------------------------------------------------------------------*/
|
|
70
|
+
/**
|
|
71
|
+
* `expr` used as a C condition. JS treats a string as truthy when it is non-empty; a bare char[] in C tests
|
|
72
|
+
* its ADDRESS, which is always true — and real GCC refuses that under -Werror=address.
|
|
73
|
+
*
|
|
74
|
+
* @param expr The lowered expression.
|
|
75
|
+
*
|
|
76
|
+
* @returns C that is true exactly when JS would find `expr` truthy.
|
|
77
|
+
*/
|
|
78
|
+
export const asCond = (expr) => expr.cType === 'string' ? `(${expr.code}[0] != '\\0')` : expr.code;
|
|
79
|
+
/**
|
|
80
|
+
* A gestureState field. Travel is rebased onto the grant (RN's anchor); the point and the velocity come straight
|
|
81
|
+
* off the engine payload; the finger count is the engine's own, which is why er_touch_active_count() exists.
|
|
82
|
+
*
|
|
83
|
+
* @param expr The field.
|
|
84
|
+
*
|
|
85
|
+
* @returns The C expression.
|
|
86
|
+
*/
|
|
87
|
+
function printGestureField(expr) {
|
|
88
|
+
switch (expr.field) {
|
|
89
|
+
case 'dx':
|
|
90
|
+
case 'dy':
|
|
91
|
+
return `(data->${expr.field} - ${expr.statePrefix}_base_${expr.field})`;
|
|
92
|
+
case 'moveX':
|
|
93
|
+
return 'data->x';
|
|
94
|
+
case 'moveY':
|
|
95
|
+
return 'data->y';
|
|
96
|
+
case 'x0':
|
|
97
|
+
case 'y0':
|
|
98
|
+
return `${expr.statePrefix}_${expr.field}`;
|
|
99
|
+
case 'vx':
|
|
100
|
+
case 'vy':
|
|
101
|
+
return `data->${expr.field}`;
|
|
102
|
+
case 'numberActiveTouches':
|
|
103
|
+
return 'er_touch_active_count()';
|
|
104
|
+
case 'stateID':
|
|
105
|
+
return String(expr.stateID);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* A compile-time number as a C constant.
|
|
110
|
+
*
|
|
111
|
+
* @param expr The number.
|
|
112
|
+
*
|
|
113
|
+
* @returns Its C constant.
|
|
114
|
+
*/
|
|
115
|
+
function printNumber(expr) {
|
|
116
|
+
if (expr.type.kind === 'float') {
|
|
117
|
+
return inDoubles ? doubleLit(expr.value) : floatLit(expr.value);
|
|
118
|
+
}
|
|
119
|
+
if (expr.type.kind === 'int64') {
|
|
120
|
+
return i64Lit(expr.value);
|
|
121
|
+
}
|
|
122
|
+
// `-2147483648` is `-` applied to 2147483648, which does not fit an int.
|
|
123
|
+
return expr.value === INT_MIN ? '(-2147483647 - 1)' : String(expr.value);
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* A number as a C double constant.
|
|
127
|
+
*
|
|
128
|
+
* @param value The number.
|
|
129
|
+
*
|
|
130
|
+
* @returns Its C constant.
|
|
131
|
+
*/
|
|
132
|
+
export const doubleLit = (value) => floatLit(value).replace(/f$/, '');
|
|
133
|
+
/**
|
|
134
|
+
* Prints with every fraction worked out in double precision, as JS works it out: a value set once at boot then
|
|
135
|
+
* rounds exactly as the compiler's own fold would have.
|
|
136
|
+
*
|
|
137
|
+
* @param print The printing.
|
|
138
|
+
*
|
|
139
|
+
* @returns What it prints.
|
|
140
|
+
*/
|
|
141
|
+
export function printInDoubles(print) {
|
|
142
|
+
const outer = inDoubles;
|
|
143
|
+
inDoubles = true;
|
|
144
|
+
try {
|
|
145
|
+
return print();
|
|
146
|
+
}
|
|
147
|
+
finally {
|
|
148
|
+
inDoubles = outer;
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* An expression as a C condition (see asCond).
|
|
153
|
+
*
|
|
154
|
+
* @param expr The expression.
|
|
155
|
+
*
|
|
156
|
+
* @returns C that is true exactly when JS would find it truthy.
|
|
157
|
+
*/
|
|
158
|
+
export const printCondition = (expr) => asCond({ code: printExpr(expr), cType: slotOf(expr.type) });
|
|
159
|
+
/**
|
|
160
|
+
* The C type a fraction is worked out in.
|
|
161
|
+
*
|
|
162
|
+
* @returns `double` while printing in doubles, else `float`.
|
|
163
|
+
*/
|
|
164
|
+
const fraction = () => (inDoubles ? 'double' : 'float');
|
|
165
|
+
/**
|
|
166
|
+
* Prints an expression as C.
|
|
167
|
+
*
|
|
168
|
+
* @param expr The expression.
|
|
169
|
+
*
|
|
170
|
+
* @returns Its C.
|
|
171
|
+
*/
|
|
172
|
+
export function printExpr(expr) {
|
|
173
|
+
switch (expr.kind) {
|
|
174
|
+
case 'number':
|
|
175
|
+
return printNumber(expr);
|
|
176
|
+
case 'string':
|
|
177
|
+
return cstr(expr.value);
|
|
178
|
+
case 'boolean':
|
|
179
|
+
return expr.value ? '1' : '0';
|
|
180
|
+
case 'state':
|
|
181
|
+
return expr.state.cMember;
|
|
182
|
+
case 'boot':
|
|
183
|
+
return `s_boot.${expr.slot.field}`;
|
|
184
|
+
case 'local':
|
|
185
|
+
return expr.local.code;
|
|
186
|
+
case 'ref':
|
|
187
|
+
return expr.ref.cVar;
|
|
188
|
+
case 'listLength':
|
|
189
|
+
return expr.list.countMember;
|
|
190
|
+
case 'itemField':
|
|
191
|
+
return `${expr.item.code}.${expr.field.key}`;
|
|
192
|
+
case 'event':
|
|
193
|
+
return `data->${expr.field}`;
|
|
194
|
+
case 'layout':
|
|
195
|
+
return `data->layout_rect.${LAYOUT_RECT_C[expr.field]}`;
|
|
196
|
+
case 'clock':
|
|
197
|
+
return expr.clock === 'Date' ? 'app_date_now()' : 'app_perf_now()';
|
|
198
|
+
case 'pi':
|
|
199
|
+
return inDoubles ? 'M_PI' : '(float)M_PI';
|
|
200
|
+
case 'gesture':
|
|
201
|
+
return printGestureField(expr);
|
|
202
|
+
case 'toFloat':
|
|
203
|
+
return `((${fraction()})${printExpr(expr.operand)})`;
|
|
204
|
+
case 'unary': {
|
|
205
|
+
// The operand is parenthesized so `-` on a negative operand prints `(-(-x))`, not `(--x)` (a decrement).
|
|
206
|
+
const operand = expr.op === '!'
|
|
207
|
+
? printCondition(expr.operand)
|
|
208
|
+
: printExpr(expr.operand);
|
|
209
|
+
return `(${expr.op}(${operand}))`;
|
|
210
|
+
}
|
|
211
|
+
case 'helper':
|
|
212
|
+
return `app_${expr.helper}${expr.bits === 64 ? '64' : ''}(${expr.args.map(printExpr).join(', ')})`;
|
|
213
|
+
case 'arithmetic':
|
|
214
|
+
return `(${printExpr(expr.left)} ${expr.op} ${printExpr(expr.right)})`;
|
|
215
|
+
case 'floatDivide':
|
|
216
|
+
return `((${fraction()})(${printExpr(expr.left)}) / (${fraction()})(${printExpr(expr.right)}))`;
|
|
217
|
+
case 'narrowRemainder':
|
|
218
|
+
return `((int)(${printExpr(expr.left)} % ${printExpr(expr.right)}))`;
|
|
219
|
+
case 'floatMath': {
|
|
220
|
+
const args = expr.args.map(arg => `(${fraction()})(${printExpr(arg)})`);
|
|
221
|
+
const fn = (inDoubles ? DOUBLE_MATH_C : FLOAT_MATH_C)[expr.fn];
|
|
222
|
+
return `${fn}(${args.join(', ')})`;
|
|
223
|
+
}
|
|
224
|
+
case 'wholeRound':
|
|
225
|
+
return printExpr(expr.operand);
|
|
226
|
+
case 'trunc':
|
|
227
|
+
return inDoubles
|
|
228
|
+
? `app_d2i((double)(${printExpr(expr.operand)}))`
|
|
229
|
+
: `app_f2i((float)(${printExpr(expr.operand)}))`;
|
|
230
|
+
case 'toInt':
|
|
231
|
+
return `${inDoubles ? 'app_d2i' : 'app_f2i'}(${printExpr(expr.operand)})`;
|
|
232
|
+
case 'compare':
|
|
233
|
+
return `(${printExpr(expr.left)} ${expr.op} ${printExpr(expr.right)})`;
|
|
234
|
+
case 'stringEquals':
|
|
235
|
+
return `(strcmp(${printExpr(expr.left)}, ${printExpr(expr.right)}) ${expr.op} 0)`;
|
|
236
|
+
case 'logical':
|
|
237
|
+
return `(${printCondition(expr.left)} ${expr.op} ${printCondition(expr.right)})`;
|
|
238
|
+
case 'pick': {
|
|
239
|
+
// Reading `left` twice is safe: expressions have no side effects, and the clock only moves in er_tick().
|
|
240
|
+
const left = printExpr(expr.left);
|
|
241
|
+
const right = printExpr(expr.right);
|
|
242
|
+
return expr.op === '||'
|
|
243
|
+
? `(${left} ? ${left} : ${right})`
|
|
244
|
+
: `(${left} ? ${right} : ${left})`;
|
|
245
|
+
}
|
|
246
|
+
case 'conditional':
|
|
247
|
+
return `(${printCondition(expr.test)} ? ${printExpr(expr.consequent)} : ${printExpr(expr.alternate)})`;
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
/**
|
|
251
|
+
* An expression as the C expression the emitters build with: its C, and what is known about its value.
|
|
252
|
+
*
|
|
253
|
+
* @param expr The expression.
|
|
254
|
+
*
|
|
255
|
+
* @returns The C expression.
|
|
256
|
+
*/
|
|
257
|
+
export function toCExpr(expr) {
|
|
258
|
+
// A bare local is the Local itself, so a list row keeps its struct (extractProps copies it into a prop).
|
|
259
|
+
if (expr.kind === 'local') {
|
|
260
|
+
return expr.local;
|
|
261
|
+
}
|
|
262
|
+
const cExpr = { code: printExpr(expr), cType: slotOf(expr.type) };
|
|
263
|
+
if (expr.type.kind === 'bool') {
|
|
264
|
+
cExpr.isBool = true;
|
|
265
|
+
}
|
|
266
|
+
else if (expr.type.kind === 'int64' && expr.type.source !== 'time') {
|
|
267
|
+
cExpr[expr.type.source === 'number' ? 'lit' : 'wide'] = true;
|
|
268
|
+
}
|
|
269
|
+
return cExpr;
|
|
270
|
+
}
|
|
@@ -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
|
+
}
|