orvyn-lang 0.2.0 → 0.4.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/GUIDE.md +198 -0
- package/LANGUAGE.md +818 -30
- package/README.md +211 -250
- package/brand/orvyn-sunset-animated.svg +15 -0
- package/package.json +3 -1
- package/src/cli/orvyn.js +207 -16
- package/src/core/signal.js +104 -6
- package/src/devtools/runtime.js +25 -1
- package/src/lang/a11y.js +152 -0
- package/src/lang/auth.js +239 -0
- package/src/lang/bundle-native.js +10 -6
- package/src/lang/codegen.js +615 -58
- package/src/lang/device.js +41 -0
- package/src/lang/doctor.js +60 -0
- package/src/lang/env.js +27 -0
- package/src/lang/errors.js +513 -0
- package/src/lang/form.js +69 -8
- package/src/lang/index.js +2 -1
- package/src/lang/lexer.js +2 -0
- package/src/lang/list.js +44 -0
- package/src/lang/native/android/MainActivity.kt +65 -4
- package/src/lang/native/android/OrvynDevice.kt +163 -0
- package/src/lang/native/android/OrvynEngine.kt +176 -7
- package/src/lang/native/android/OrvynRenderer.kt +970 -50
- package/src/lang/native/android.js +74 -11
- package/src/lang/native/assets.js +39 -0
- package/src/lang/native/build.js +140 -63
- package/src/lang/native/compatibility.js +57 -0
- package/src/lang/native/config.js +108 -0
- package/src/lang/native/ios/OrvynApp.swift +6 -0
- package/src/lang/native/ios/OrvynEngine.swift +291 -4
- package/src/lang/native/ios/OrvynViews.swift +769 -41
- package/src/lang/native/ios.js +81 -2
- package/src/lang/native-runtime.js +541 -55
- package/src/lang/native-size.js +53 -0
- package/src/lang/page-files.js +71 -0
- package/src/lang/page.js +63 -0
- package/src/lang/parser.js +278 -15
- package/src/lang/prerender.js +300 -0
- package/src/lang/read-import.js +12 -0
- package/src/lang/router.js +266 -0
- package/src/lang/runtime-shared.js +796 -10
- package/src/lang/runtime.js +399 -51
- package/src/lang/select.js +13 -0
- package/src/lang/server-code.js +233 -0
- package/src/lang/server-export.js +78 -0
- package/src/lang/server-libs.js +53 -0
- package/src/lang/server.js +409 -0
- package/src/lang/style-check.js +60 -0
- package/src/lang/test-runner.js +134 -0
- package/src/lang/types.js +317 -0
- package/src/lang/vite-plugin.js +108 -6
- package/src/lang/web-overlay.js +578 -0
- package/src/lang/widgets.js +94 -0
- package/src/lang/words.js +38 -0
- package/src/runtime/animations.js +24 -0
- package/src/runtime/appear.js +73 -0
- package/src/runtime/marquee.js +57 -0
- package/src/runtime/mount.js +120 -27
- package/src/runtime/patch.js +90 -11
- package/src/runtime/style.js +50 -9
- package/src/styling/fluid.js +129 -6
- package/src/styling/spec.js +319 -0
package/src/lang/codegen.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { OrvynSyntaxError } from "./lexer.js";
|
|
2
|
+
import { LIST_WORDS } from "./words.js";
|
|
3
|
+
import { parse } from "./parser.js";
|
|
2
4
|
const LIST_METHODS = /* @__PURE__ */ new Set(["add", "remove", "removeAt", "clear", "update"]);
|
|
3
5
|
const BUILTINS = {
|
|
4
6
|
Column: { tag: "div", props: { flex: true, direction: "column" } },
|
|
@@ -10,26 +12,56 @@ const BUILTINS = {
|
|
|
10
12
|
Form: { tag: "form", props: { flex: true, direction: "column", noValidate: true } },
|
|
11
13
|
SubmitButton: { tag: "button", props: { type: "submit" }, arg: "child" },
|
|
12
14
|
Image: { tag: "img", arg: "src" },
|
|
15
|
+
DatePicker: { tag: "input", props: { type: "date" }, arg: "placeholder" },
|
|
16
|
+
TextArea: { tag: "textarea", props: { rows: 4 }, arg: "placeholder" },
|
|
13
17
|
Input: { tag: "input", arg: "placeholder" },
|
|
14
18
|
Link: { tag: "a", arg: "child" }
|
|
15
19
|
};
|
|
20
|
+
const TEST_GLOBALS = /* @__PURE__ */ new Set(["tap", "type", "expect", "text", "mock", "settle"]);
|
|
21
|
+
const TEST_WAITS = /* @__PURE__ */ new Set(["tap", "type", "settle"]);
|
|
16
22
|
const EVENTS = {
|
|
17
23
|
onTap: "onClick",
|
|
18
24
|
onClick: "onClick",
|
|
19
25
|
onChange: "onInput",
|
|
20
26
|
onSubmit: "onSubmit",
|
|
27
|
+
onError: "onError",
|
|
28
|
+
onVisible: "onVisible",
|
|
21
29
|
onHover: "onMouseenter",
|
|
22
30
|
onLeave: "onMouseleave"
|
|
23
31
|
};
|
|
24
32
|
const GLOBALS = {
|
|
25
33
|
print: "console.log",
|
|
26
34
|
fetch: "__o.fetchData",
|
|
35
|
+
get: "__o.get",
|
|
36
|
+
post: "__o.post",
|
|
37
|
+
put: "__o.put",
|
|
38
|
+
patch: "__o.patch",
|
|
39
|
+
load: "__o.load",
|
|
40
|
+
task: "__o.task",
|
|
27
41
|
go: "__o.go",
|
|
42
|
+
back: "__o.back",
|
|
43
|
+
canGoBack: "__o.canGoBack",
|
|
44
|
+
openUrl: "__o.openUrl",
|
|
45
|
+
scrollTo: "__o.scrollTo",
|
|
46
|
+
defineAnimation: "__o.defineAnimation",
|
|
47
|
+
platform: "__o.platform",
|
|
28
48
|
sleep: "__o.sleep",
|
|
29
49
|
random: "__o.random",
|
|
30
50
|
len: "__o.len",
|
|
31
51
|
validate: "__o.validate",
|
|
52
|
+
theme: "__o.theme",
|
|
53
|
+
storage: "__o.storage",
|
|
54
|
+
secure: "__o.secure",
|
|
55
|
+
auth: "__o.auth",
|
|
56
|
+
log: "__o.log",
|
|
57
|
+
pickPhoto: "__o.pickPhoto",
|
|
58
|
+
takePhoto: "__o.takePhoto",
|
|
59
|
+
pickFile: "__o.pickFile",
|
|
60
|
+
getLocation: "__o.getLocation",
|
|
61
|
+
env: "__o.env",
|
|
32
62
|
Math: "Math",
|
|
63
|
+
Array: "Array",
|
|
64
|
+
Object: "Object",
|
|
33
65
|
JSON: "JSON",
|
|
34
66
|
Date: "Date",
|
|
35
67
|
Number: "Number",
|
|
@@ -81,9 +113,12 @@ class Scope {
|
|
|
81
113
|
vars = /* @__PURE__ */ new Map();
|
|
82
114
|
parent;
|
|
83
115
|
awaitAllowed;
|
|
84
|
-
|
|
116
|
+
fnBoundary;
|
|
117
|
+
hasAwait = false;
|
|
118
|
+
constructor(parent = null, awaitAllowed = parent?.awaitAllowed ?? false, fnBoundary = false) {
|
|
85
119
|
this.parent = parent;
|
|
86
120
|
this.awaitAllowed = awaitAllowed;
|
|
121
|
+
this.fnBoundary = fnBoundary;
|
|
87
122
|
}
|
|
88
123
|
declare(name, binding, pos) {
|
|
89
124
|
if (this.vars.has(name)) {
|
|
@@ -94,24 +129,66 @@ class Scope {
|
|
|
94
129
|
lookup(name) {
|
|
95
130
|
return this.vars.get(name) ?? this.parent?.lookup(name);
|
|
96
131
|
}
|
|
132
|
+
names() {
|
|
133
|
+
return [...this.vars.keys(), ...this.parent?.names() ?? []];
|
|
134
|
+
}
|
|
135
|
+
markAwait() {
|
|
136
|
+
let current = this;
|
|
137
|
+
while (current) {
|
|
138
|
+
if (current.fnBoundary) {
|
|
139
|
+
current.hasAwait = true;
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
current = current.parent;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
97
145
|
}
|
|
98
|
-
const INTERNAL_NAMES = /* @__PURE__ */ new Set(["__o", "__signal", "__h", "__start", "__forms", "screens", "mountApp"]);
|
|
146
|
+
const INTERNAL_NAMES = /* @__PURE__ */ new Set(["__o", "__signal", "__h", "__start", "__F", "__e", "__forms", "__local", "screens", "mountApp"]);
|
|
99
147
|
function jsName(name) {
|
|
100
148
|
return JS_RESERVED.has(name) || INTERNAL_NAMES.has(name) || name.startsWith("__orvyn_") ? "__orvyn_" + Array.from(name, (c) => c.codePointAt(0).toString(16)).join("_") : name;
|
|
101
149
|
}
|
|
150
|
+
function suggest(name, candidates) {
|
|
151
|
+
let best = "";
|
|
152
|
+
let bestScore = Infinity;
|
|
153
|
+
const lower = name.toLowerCase();
|
|
154
|
+
for (const c of candidates) {
|
|
155
|
+
if (c.startsWith("__")) continue;
|
|
156
|
+
const d = c.toLowerCase() === lower ? 0 : editDistance(lower, c.toLowerCase());
|
|
157
|
+
if (d < bestScore) {
|
|
158
|
+
bestScore = d;
|
|
159
|
+
best = c;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
const limit = name.length <= 4 ? 1 : 2;
|
|
163
|
+
return best && bestScore <= limit ? ` Did you mean "${best}"?` : "";
|
|
164
|
+
}
|
|
165
|
+
function editDistance(a, b) {
|
|
166
|
+
const row = Array.from({ length: b.length + 1 }, (_, i) => i);
|
|
167
|
+
for (let i = 1; i <= a.length; i++) {
|
|
168
|
+
let prev = row[0];
|
|
169
|
+
row[0] = i;
|
|
170
|
+
for (let j = 1; j <= b.length; j++) {
|
|
171
|
+
const temp = row[j];
|
|
172
|
+
row[j] = a[i - 1] === b[j - 1] ? prev : 1 + Math.min(prev, row[j], row[j - 1]);
|
|
173
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1]) row[j] = Math.min(row[j], temp);
|
|
174
|
+
prev = temp;
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
return row[b.length];
|
|
178
|
+
}
|
|
102
179
|
function fail(message, pos) {
|
|
103
180
|
throw new OrvynSyntaxError(message, pos.line, pos.col);
|
|
104
181
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
if (Array.isArray(node)) return node.some(containsWait);
|
|
108
|
-
const value = node;
|
|
109
|
-
if (value.kind === "Lambda") return false;
|
|
110
|
-
if (value.kind === "Wait") return true;
|
|
111
|
-
return Object.values(value).some(containsWait);
|
|
112
|
-
}
|
|
182
|
+
const ASYNC_GLOBALS = /* @__PURE__ */ new Set(["fetch", "get", "post", "put", "patch", "sleep", "pickPhoto", "takePhoto", "pickFile", "getLocation"]);
|
|
183
|
+
const NEEDS = { takePhoto: "camera", getLocation: "location" };
|
|
113
184
|
class Generator {
|
|
185
|
+
currentView = "";
|
|
186
|
+
fnParams = /* @__PURE__ */ new Map();
|
|
187
|
+
asyncFns = /* @__PURE__ */ new Set();
|
|
188
|
+
importedAsyncFns = /* @__PURE__ */ new Set();
|
|
114
189
|
formId = 0;
|
|
190
|
+
needs = /* @__PURE__ */ new Set();
|
|
191
|
+
inTest = false;
|
|
115
192
|
out = [];
|
|
116
193
|
indent = 0;
|
|
117
194
|
global = new Scope();
|
|
@@ -122,33 +199,209 @@ class Generator {
|
|
|
122
199
|
this.program = program;
|
|
123
200
|
this.options = options;
|
|
124
201
|
}
|
|
202
|
+
analyzeAsyncFns() {
|
|
203
|
+
const cache = /* @__PURE__ */ new Map();
|
|
204
|
+
const analyzeProgram = (prog, resolve) => {
|
|
205
|
+
const importedAsync = /* @__PURE__ */ new Set();
|
|
206
|
+
for (const decl of prog.body) {
|
|
207
|
+
if (decl.kind !== "Use" || !resolve || /\.(m?js|ts)$/.test(decl.path) || !/^[./]/.test(decl.path)) continue;
|
|
208
|
+
const found = resolve(decl.path);
|
|
209
|
+
if (!found) continue;
|
|
210
|
+
const { source, resolve: nextResolve } = typeof found === "string" ? { source: found, resolve } : found;
|
|
211
|
+
let childAsync = cache.get(source);
|
|
212
|
+
if (!childAsync) {
|
|
213
|
+
cache.set(source, /* @__PURE__ */ new Set());
|
|
214
|
+
try {
|
|
215
|
+
childAsync = analyzeProgram(parse(source), nextResolve);
|
|
216
|
+
} catch {
|
|
217
|
+
childAsync = /* @__PURE__ */ new Set();
|
|
218
|
+
}
|
|
219
|
+
cache.set(source, childAsync);
|
|
220
|
+
}
|
|
221
|
+
for (const name of decl.names) {
|
|
222
|
+
if (childAsync.has(name)) importedAsync.add(name);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const fnNames = new Set(prog.body.filter((d) => d.kind === "Fn").map((d) => d.name));
|
|
226
|
+
const topShadowed = new Set(prog.body.flatMap((d) => {
|
|
227
|
+
if (d.kind === "State" || d.kind === "Const" || d.kind === "Screen") return [d.name];
|
|
228
|
+
if (d.kind === "Use") return d.names;
|
|
229
|
+
return [];
|
|
230
|
+
}));
|
|
231
|
+
const asyncSet = /* @__PURE__ */ new Set();
|
|
232
|
+
const edges = /* @__PURE__ */ new Map();
|
|
233
|
+
const walkExpr = (expr, locals, fnName) => {
|
|
234
|
+
switch (expr.kind) {
|
|
235
|
+
case "Wait":
|
|
236
|
+
asyncSet.add(fnName);
|
|
237
|
+
walkExpr(expr.expr, locals, fnName);
|
|
238
|
+
return;
|
|
239
|
+
case "Call":
|
|
240
|
+
if (expr.callee.kind === "Ident") {
|
|
241
|
+
const name = expr.callee.name;
|
|
242
|
+
if (!locals.has(name)) {
|
|
243
|
+
if (fnNames.has(name)) {
|
|
244
|
+
let list = edges.get(fnName);
|
|
245
|
+
if (!list) {
|
|
246
|
+
list = /* @__PURE__ */ new Set();
|
|
247
|
+
edges.set(fnName, list);
|
|
248
|
+
}
|
|
249
|
+
list.add(name);
|
|
250
|
+
} else if (importedAsync.has(name)) {
|
|
251
|
+
asyncSet.add(fnName);
|
|
252
|
+
} else if (ASYNC_GLOBALS.has(name) && !topShadowed.has(name)) {
|
|
253
|
+
asyncSet.add(fnName);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
walkExpr(expr.callee, locals, fnName);
|
|
258
|
+
expr.args.forEach((a) => walkExpr(a, locals, fnName));
|
|
259
|
+
return;
|
|
260
|
+
case "String":
|
|
261
|
+
expr.parts.forEach((p) => {
|
|
262
|
+
if (typeof p !== "string") walkExpr(p, locals, fnName);
|
|
263
|
+
});
|
|
264
|
+
return;
|
|
265
|
+
case "List":
|
|
266
|
+
expr.items.forEach((i) => walkExpr(i, locals, fnName));
|
|
267
|
+
return;
|
|
268
|
+
case "Object":
|
|
269
|
+
expr.entries.forEach((e) => walkExpr(e.value, locals, fnName));
|
|
270
|
+
return;
|
|
271
|
+
case "Member":
|
|
272
|
+
walkExpr(expr.object, locals, fnName);
|
|
273
|
+
return;
|
|
274
|
+
case "Index":
|
|
275
|
+
walkExpr(expr.object, locals, fnName);
|
|
276
|
+
walkExpr(expr.index, locals, fnName);
|
|
277
|
+
return;
|
|
278
|
+
case "New":
|
|
279
|
+
walkExpr(expr.callee, locals, fnName);
|
|
280
|
+
expr.args.forEach((a) => walkExpr(a, locals, fnName));
|
|
281
|
+
return;
|
|
282
|
+
case "Unary":
|
|
283
|
+
walkExpr(expr.operand, locals, fnName);
|
|
284
|
+
return;
|
|
285
|
+
case "Binary":
|
|
286
|
+
walkExpr(expr.left, locals, fnName);
|
|
287
|
+
walkExpr(expr.right, locals, fnName);
|
|
288
|
+
return;
|
|
289
|
+
case "Ternary":
|
|
290
|
+
walkExpr(expr.cond, locals, fnName);
|
|
291
|
+
walkExpr(expr.then, locals, fnName);
|
|
292
|
+
walkExpr(expr.else, locals, fnName);
|
|
293
|
+
return;
|
|
294
|
+
case "Lambda":
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
const walkStmts = (stmts, locals, fnName) => {
|
|
299
|
+
const inner = new Set(locals);
|
|
300
|
+
for (const stmt of stmts) {
|
|
301
|
+
switch (stmt.kind) {
|
|
302
|
+
case "Let":
|
|
303
|
+
walkExpr(stmt.init, inner, fnName);
|
|
304
|
+
inner.add(stmt.name);
|
|
305
|
+
break;
|
|
306
|
+
case "Assign":
|
|
307
|
+
walkExpr(stmt.target, inner, fnName);
|
|
308
|
+
walkExpr(stmt.value, inner, fnName);
|
|
309
|
+
break;
|
|
310
|
+
case "ExprStmt":
|
|
311
|
+
walkExpr(stmt.expr, inner, fnName);
|
|
312
|
+
break;
|
|
313
|
+
case "Return":
|
|
314
|
+
if (stmt.value) walkExpr(stmt.value, inner, fnName);
|
|
315
|
+
break;
|
|
316
|
+
case "If":
|
|
317
|
+
walkExpr(stmt.cond, inner, fnName);
|
|
318
|
+
walkStmts(stmt.then, inner, fnName);
|
|
319
|
+
if (stmt.else) walkStmts(stmt.else, inner, fnName);
|
|
320
|
+
break;
|
|
321
|
+
case "For": {
|
|
322
|
+
walkExpr(stmt.iter, inner, fnName);
|
|
323
|
+
const loopLocals = new Set(inner);
|
|
324
|
+
loopLocals.add(stmt.name);
|
|
325
|
+
walkStmts(stmt.body, loopLocals, fnName);
|
|
326
|
+
break;
|
|
327
|
+
}
|
|
328
|
+
case "Try":
|
|
329
|
+
walkStmts(stmt.body, inner, fnName);
|
|
330
|
+
const catchLocals = new Set(inner);
|
|
331
|
+
if (stmt.catchName) catchLocals.add(stmt.catchName);
|
|
332
|
+
walkStmts(stmt.catchBody, catchLocals, fnName);
|
|
333
|
+
break;
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
for (const decl of prog.body) {
|
|
338
|
+
if (decl.kind !== "Fn") continue;
|
|
339
|
+
walkStmts(decl.body, new Set(decl.params), decl.name);
|
|
340
|
+
}
|
|
341
|
+
let changed = true;
|
|
342
|
+
while (changed) {
|
|
343
|
+
changed = false;
|
|
344
|
+
for (const [caller, callees] of edges) {
|
|
345
|
+
if (asyncSet.has(caller)) continue;
|
|
346
|
+
for (const callee of callees) {
|
|
347
|
+
if (asyncSet.has(callee)) {
|
|
348
|
+
asyncSet.add(caller);
|
|
349
|
+
changed = true;
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
if (prog === this.program) this.importedAsyncFns = importedAsync;
|
|
356
|
+
return asyncSet;
|
|
357
|
+
};
|
|
358
|
+
this.asyncFns = analyzeProgram(this.program, this.options.resolveImport);
|
|
359
|
+
}
|
|
125
360
|
generate() {
|
|
126
361
|
const runtime = this.options.runtime ?? "orvyn";
|
|
362
|
+
this.analyzeAsyncFns();
|
|
127
363
|
const imports = [];
|
|
128
364
|
const starts = [];
|
|
129
365
|
for (const decl of this.program.body) {
|
|
130
366
|
if (decl.kind === "Use") {
|
|
131
|
-
decl.
|
|
132
|
-
|
|
133
|
-
|
|
367
|
+
const library = /\.(m?js|ts)$/.test(decl.path) || !/^[./]/.test(decl.path);
|
|
368
|
+
decl.names.forEach((name) => this.global.declare(name, library ? "const" : "imported", decl));
|
|
369
|
+
const path = library || decl.path.endsWith(".orv") ? decl.path : `${decl.path}.orv`;
|
|
370
|
+
if (library) {
|
|
371
|
+
const all = `__orvyn_lib${imports.length}`;
|
|
372
|
+
const fallback = decl.names.length === 1 ? ` ?? Reflect.get(${all}, "default")` : "";
|
|
373
|
+
imports.push([
|
|
374
|
+
`import * as ${all} from ${JSON.stringify(path)}`,
|
|
375
|
+
...decl.names.map((n) => `const ${jsName(n)} = ${all}[${JSON.stringify(n)}]${fallback}`)
|
|
376
|
+
].join("\n"));
|
|
377
|
+
} else {
|
|
378
|
+
imports.push(`import { ${decl.names.map(jsName).join(", ")} } from ${JSON.stringify(path)}`);
|
|
379
|
+
}
|
|
134
380
|
continue;
|
|
135
381
|
}
|
|
136
382
|
if (decl.kind === "Start") {
|
|
137
383
|
starts.push(decl.body);
|
|
138
384
|
continue;
|
|
139
385
|
}
|
|
386
|
+
if (decl.kind === "Test" || decl.kind === "Type") continue;
|
|
140
387
|
const binding = decl.kind === "State" ? "state" : decl.kind === "Const" ? "const" : decl.kind === "Fn" ? "fn" : decl.type;
|
|
141
388
|
this.global.declare(decl.name, binding, decl);
|
|
142
389
|
if (decl.kind === "Screen") this.components.set(decl.name, decl.params);
|
|
390
|
+
if (decl.kind === "Fn") this.fnParams.set(decl.name, decl.params.length);
|
|
143
391
|
}
|
|
144
392
|
this.line(`import * as __o from "${runtime}/lang/runtime"`);
|
|
145
393
|
imports.forEach((line) => this.line(line));
|
|
146
394
|
this.line("const __signal = __o.signal");
|
|
147
395
|
this.line("const __h = __o.createVNode");
|
|
148
396
|
this.line("const __forms = __o.formCache()");
|
|
397
|
+
this.line(`const __F = ${JSON.stringify(this.options.moduleId || "app")}`);
|
|
398
|
+
if (this.options.sourceText !== void 0) {
|
|
399
|
+
this.line(`__o.registerSource?.(__F, ${JSON.stringify(this.options.sourceText)})`);
|
|
400
|
+
}
|
|
401
|
+
if (this.options.env) this.line(`__o.setEnv(${JSON.stringify(this.options.env)})`);
|
|
149
402
|
this.line("");
|
|
150
403
|
for (const decl of this.program.body) {
|
|
151
|
-
if (decl.kind === "Use" || decl.kind === "Start") continue;
|
|
404
|
+
if (decl.kind === "Use" || decl.kind === "Start" || decl.kind === "Test") continue;
|
|
152
405
|
this.decl(decl);
|
|
153
406
|
this.line("");
|
|
154
407
|
}
|
|
@@ -156,14 +409,43 @@ class Generator {
|
|
|
156
409
|
this.line(this.fnHeader("function __start", [], starts.flat(), this.global));
|
|
157
410
|
this.line("");
|
|
158
411
|
}
|
|
412
|
+
const onErrorFn = this.program.body.find((d) => d.kind === "Fn" && d.name === "onError");
|
|
413
|
+
if (onErrorFn) {
|
|
414
|
+
this.line("__o.onError(onError)");
|
|
415
|
+
this.line("");
|
|
416
|
+
}
|
|
417
|
+
for (const need of this.needs) this.line(`globalThis.__orvyn_needs_${need} = true`);
|
|
418
|
+
const tests = this.program.body.filter((d) => d.kind === "Test");
|
|
419
|
+
if (this.options.tests && tests.length) {
|
|
420
|
+
this.line("export const __tests = [");
|
|
421
|
+
for (const test of tests) {
|
|
422
|
+
this.inTest = true;
|
|
423
|
+
const inner = new Scope(this.global, true, true);
|
|
424
|
+
const body = this.capture(() => this.stmts(test.body, inner));
|
|
425
|
+
this.inTest = false;
|
|
426
|
+
this.line(`{ name: ${JSON.stringify(test.name)}, run: async (__t) => {
|
|
427
|
+
${body.join("\n")}
|
|
428
|
+
} },`);
|
|
429
|
+
}
|
|
430
|
+
this.line("]");
|
|
431
|
+
this.line("");
|
|
432
|
+
}
|
|
159
433
|
const screens = this.program.body.filter(
|
|
160
434
|
(d) => d.kind === "Screen" && d.type === "screen"
|
|
161
435
|
);
|
|
162
|
-
const
|
|
163
|
-
|
|
436
|
+
const importedNames = this.program.body.flatMap((d) => d.kind === "Use" && !/\.(m?js|ts)$/.test(d.path) && /^[./]/.test(d.path) ? d.names.map(jsName) : []);
|
|
437
|
+
const entry = screens.find((s) => s.name === "App") ?? screens.find((s) => s.name !== "NotFound" && s.name !== "ErrorPage") ?? screens[0];
|
|
438
|
+
if (screens.some((s) => s.name === "ErrorPage")) {
|
|
439
|
+
this.line("__o.setErrorPage(ErrorPage)");
|
|
440
|
+
this.line("");
|
|
441
|
+
}
|
|
442
|
+
if (screens.length) this.line(`__o.registerScreens?.({ ${screens.map((s) => s.name).join(", ")} })`);
|
|
443
|
+
const localScreens = screens.map((s) => s.name).join(", ");
|
|
444
|
+
const importedScreens = importedNames.length ? `...__o.registeredScreens(), ...Object.fromEntries([${importedNames.map((n) => `() => ${n}`).join(", ")}].map(read => { try { return read() } catch { return undefined } }).filter(value => value?.__name).map(value => [value.__name, value]))` : "";
|
|
445
|
+
this.line(`export const screens = { ${[localScreens, importedScreens, localScreens && importedScreens ? localScreens : ""].filter(Boolean).join(", ")} }`);
|
|
164
446
|
this.line("");
|
|
165
|
-
this.line("export function mountApp(target, screen) {");
|
|
166
|
-
this.line(` __o.startApp(target, screen ? screens[screen] : ${entry ? entry.name : "null
|
|
447
|
+
this.line("export function mountApp(target, screen, params) {");
|
|
448
|
+
this.line(` return __o.startApp(target, screen ? screens[screen] : ${entry ? entry.name : '(screens.App ?? screens.Home ?? Object.entries(screens).find(([name]) => name !== "NotFound" && name !== "ErrorPage")?.[1] ?? null)'}, ${starts.length > 0 ? `__o.guard(__start, "start", __F, ${starts[0][0]?.line ?? 0}, "start")` : "null"}, screens, params)`);
|
|
167
449
|
this.line("}");
|
|
168
450
|
this.line("");
|
|
169
451
|
this.line(`export default ${entry ? entry.name : "null"}`);
|
|
@@ -177,11 +459,21 @@ class Generator {
|
|
|
177
459
|
decl(decl) {
|
|
178
460
|
const scope = this.global;
|
|
179
461
|
switch (decl.kind) {
|
|
180
|
-
case "State":
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
462
|
+
case "State": {
|
|
463
|
+
if (!decl.saved) {
|
|
464
|
+
this.line(`export const ${jsName(decl.name)} = __signal(${this.expr(decl.init, scope)}, "${decl.name}")`);
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
const opts = [];
|
|
468
|
+
if (this.options.moduleId) opts.push(`module: ${JSON.stringify(this.options.moduleId)}`);
|
|
469
|
+
if (decl.savedVersion && !(decl.savedVersion.kind === "Number" && decl.savedVersion.value === 1)) {
|
|
470
|
+
opts.push(`version: ${this.expr(decl.savedVersion, scope)}`);
|
|
471
|
+
}
|
|
472
|
+
if (decl.savedMigrate) opts.push(`migrate: ${this.expr(decl.savedMigrate, scope)}`);
|
|
473
|
+
const suffix = opts.length > 0 ? `, { ${opts.join(", ")} }` : "";
|
|
474
|
+
this.line(`export const ${jsName(decl.name)} = __o.savedSignal("${decl.name}", ${this.expr(decl.init, scope)}${suffix})`);
|
|
184
475
|
return;
|
|
476
|
+
}
|
|
185
477
|
case "Const":
|
|
186
478
|
this.line(`export const ${jsName(decl.name)} = ${this.expr(decl.init, scope)}`);
|
|
187
479
|
return;
|
|
@@ -193,22 +485,61 @@ class Generator {
|
|
|
193
485
|
decl.params.forEach((p) => inner.declare(p, "local", decl));
|
|
194
486
|
inner.declare("children", "local", decl);
|
|
195
487
|
const params = [...decl.params.map((p) => p === jsName(p) ? p : `${p}: ${jsName(p)}`), "children"];
|
|
488
|
+
this.currentView = decl.name;
|
|
196
489
|
this.line(`export function ${decl.name}({ ${params.join(", ")} } = {}) {`);
|
|
197
490
|
this.indent++;
|
|
491
|
+
this.line(`try {`);
|
|
492
|
+
this.indent++;
|
|
493
|
+
this.line(`__o.at(__F, ${decl.line})`);
|
|
494
|
+
const locals = decl.locals ?? [];
|
|
495
|
+
if (locals.some((local) => local.kind === "State")) {
|
|
496
|
+
this.line("const __local = __o.componentState()");
|
|
497
|
+
}
|
|
498
|
+
let stateSlot = 0;
|
|
499
|
+
for (const local of locals) {
|
|
500
|
+
if (local.kind === "State") {
|
|
501
|
+
const init = this.expr(local.init, inner);
|
|
502
|
+
inner.declare(local.name, "state", local);
|
|
503
|
+
this.line(`const ${jsName(local.name)} = __local(${stateSlot++}, () => ${init}, ${JSON.stringify(local.name)})`);
|
|
504
|
+
} else {
|
|
505
|
+
const init = this.expr(local.init, inner);
|
|
506
|
+
inner.declare(local.name, "const", local);
|
|
507
|
+
this.line(`const ${jsName(local.name)} = ${init}`);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
if (decl.type === "screen" && (decl.page || decl.requiresAuth)) {
|
|
511
|
+
const fields = (decl.page ?? []).map((p) => `${p.key}: ${p.value.kind === "Expr" ? this.expr(p.value.expr, inner) : "null"}`);
|
|
512
|
+
if (decl.requiresAuth) fields.push("noindex: true");
|
|
513
|
+
this.line(`__o.setPage?.({ ${fields.join(", ")} })`);
|
|
514
|
+
}
|
|
198
515
|
this.line(`return ${this.viewList(decl.body, inner)}`);
|
|
199
516
|
this.indent--;
|
|
517
|
+
this.line(`} catch (__e) {`);
|
|
518
|
+
const isScreen = decl.type === "screen" && decl.name !== "ErrorPage";
|
|
519
|
+
this.line(` return __o.renderError(__e, ${JSON.stringify(decl.name)}${isScreen ? ", true" : ""})`);
|
|
520
|
+
this.line(`}`);
|
|
521
|
+
this.indent--;
|
|
200
522
|
this.line("}");
|
|
523
|
+
if (decl.type === "screen") this.line(`${decl.name}.__name = ${JSON.stringify(decl.name)}`);
|
|
524
|
+
if (decl.type === "screen" && decl.params.length > 0) {
|
|
525
|
+
this.line(`${decl.name}.__params = ${JSON.stringify(decl.params)}`);
|
|
526
|
+
}
|
|
527
|
+
if (decl.requiresAuth) this.line(`${decl.name}.__requiresAuth = true`);
|
|
528
|
+
if (decl.prebuild) {
|
|
529
|
+
const asyncScope = new Scope(scope, true, true);
|
|
530
|
+
const values = this.expr(decl.prebuild, asyncScope);
|
|
531
|
+
this.line(`${decl.name}.__prebuild = async () => ${decl.prebuild.kind === "Object" ? `(${values})` : values}`);
|
|
532
|
+
}
|
|
201
533
|
return;
|
|
202
534
|
}
|
|
203
535
|
}
|
|
204
536
|
}
|
|
205
537
|
// `function name(params) { body }`, made async when the body uses wait
|
|
206
538
|
fnHeader(head, params, body, scope, pos = { line: 0, col: 0 }) {
|
|
207
|
-
const inner = new Scope(scope, true);
|
|
539
|
+
const inner = new Scope(scope, true, true);
|
|
208
540
|
params.forEach((p) => inner.declare(p, "local", pos));
|
|
209
541
|
const lines = this.capture(() => this.stmts(body, inner));
|
|
210
|
-
const
|
|
211
|
-
const fullHead = isAsync ? head.replace(/^(export )?/, "$1async ") : head;
|
|
542
|
+
const fullHead = inner.hasAwait ? head.replace(/^(export )?/, "$1async ") : head;
|
|
212
543
|
const code = [`${fullHead}(${params.map(jsName).join(", ")}) {`, ...lines.map((l) => " " + l), "}"];
|
|
213
544
|
return code.join("\n" + " ".repeat(this.indent));
|
|
214
545
|
}
|
|
@@ -228,7 +559,10 @@ class Generator {
|
|
|
228
559
|
}
|
|
229
560
|
// --- statements ----------------------------------------------------------
|
|
230
561
|
stmts(body, scope) {
|
|
231
|
-
for (const stmt of body)
|
|
562
|
+
for (const stmt of body) {
|
|
563
|
+
if (stmt.line) this.line(`__o.at(__F, ${stmt.line}, ${stmt.col ?? 0})`);
|
|
564
|
+
this.stmt(stmt, scope);
|
|
565
|
+
}
|
|
232
566
|
}
|
|
233
567
|
stmt(stmt, scope) {
|
|
234
568
|
switch (stmt.kind) {
|
|
@@ -264,13 +598,35 @@ class Generator {
|
|
|
264
598
|
case "For": {
|
|
265
599
|
const inner = new Scope(scope);
|
|
266
600
|
inner.declare(stmt.name, "local", stmt);
|
|
267
|
-
this.line(`for (const ${jsName(stmt.name)} of ${this.expr(stmt.iter, scope)}) {`);
|
|
601
|
+
this.line(`for (const ${jsName(stmt.name)} of ${this.expr(stmt.iter, scope)} ?? []) {`);
|
|
268
602
|
this.indent++;
|
|
269
603
|
this.stmts(stmt.body, inner);
|
|
270
604
|
this.indent--;
|
|
271
605
|
this.line("}");
|
|
272
606
|
return;
|
|
273
607
|
}
|
|
608
|
+
case "Try": {
|
|
609
|
+
this.line("try {");
|
|
610
|
+
this.indent++;
|
|
611
|
+
this.stmts(stmt.body, new Scope(scope));
|
|
612
|
+
this.indent--;
|
|
613
|
+
if (stmt.catchName) {
|
|
614
|
+
const catchScope = new Scope(scope);
|
|
615
|
+
catchScope.declare(stmt.catchName, "const", stmt);
|
|
616
|
+
this.line("} catch (__orvyn_err) {");
|
|
617
|
+
this.indent++;
|
|
618
|
+
this.line(`const ${jsName(stmt.catchName)} = __o.errorText(__orvyn_err)`);
|
|
619
|
+
this.stmts(stmt.catchBody, catchScope);
|
|
620
|
+
this.indent--;
|
|
621
|
+
} else {
|
|
622
|
+
this.line("} catch {");
|
|
623
|
+
this.indent++;
|
|
624
|
+
this.stmts(stmt.catchBody, new Scope(scope));
|
|
625
|
+
this.indent--;
|
|
626
|
+
}
|
|
627
|
+
this.line("}");
|
|
628
|
+
return;
|
|
629
|
+
}
|
|
274
630
|
}
|
|
275
631
|
}
|
|
276
632
|
assign(stmt, scope) {
|
|
@@ -279,7 +635,8 @@ class Generator {
|
|
|
279
635
|
if (target.kind === "Ident") {
|
|
280
636
|
const binding = scope.lookup(target.name);
|
|
281
637
|
if (binding === void 0) {
|
|
282
|
-
|
|
638
|
+
const hint = suggest(target.name, scope.names());
|
|
639
|
+
fail(hint ? `"${target.name}" is not declared.${hint}` : `"${target.name}" is not declared. Add: state ${target.name} = ...`, target);
|
|
283
640
|
}
|
|
284
641
|
if (binding === "state" || binding === "imported") {
|
|
285
642
|
const name = this.stateRef(target.name, scope);
|
|
@@ -317,11 +674,15 @@ class Generator {
|
|
|
317
674
|
return this.expr(expr, scope);
|
|
318
675
|
}
|
|
319
676
|
// todos.add(x) on state → todos.set(__o.add(todos.get(), x))
|
|
677
|
+
// todos.reset() on saved state → __o.resetState(todos)
|
|
320
678
|
stateListCall(expr, scope) {
|
|
321
679
|
if (expr.kind !== "Call" || expr.callee.kind !== "Member" || expr.callee.object.kind !== "Ident" || !["state", "imported"].includes(scope.lookup(expr.callee.object.name))) {
|
|
322
680
|
return null;
|
|
323
681
|
}
|
|
324
682
|
const method = expr.callee.property;
|
|
683
|
+
if (method === "reset" && expr.args.length === 0) {
|
|
684
|
+
return `__o.resetState(${this.stateRef(expr.callee.object.name, scope)})`;
|
|
685
|
+
}
|
|
325
686
|
if (!LIST_METHODS.has(method)) return null;
|
|
326
687
|
const name = this.stateRef(expr.callee.object.name, scope);
|
|
327
688
|
const args = [`${name}.get()`, ...expr.args.map((a) => this.expr(a, scope))];
|
|
@@ -350,31 +711,72 @@ class Generator {
|
|
|
350
711
|
if (binding === "imported") return `__o.readImported(${jsName(expr.name)})`;
|
|
351
712
|
if (binding !== void 0) return jsName(expr.name);
|
|
352
713
|
if (Object.hasOwn(GLOBALS, expr.name)) return GLOBALS[expr.name];
|
|
714
|
+
if (this.options.projectScreens?.includes(expr.name)) return `__o.registeredScreens()[${JSON.stringify(expr.name)}]`;
|
|
353
715
|
if (bareWords) return JSON.stringify(expr.name);
|
|
354
|
-
|
|
716
|
+
{
|
|
717
|
+
const hint = suggest(expr.name, [...scope.names(), ...Object.keys(GLOBALS)]);
|
|
718
|
+
return fail(`"${expr.name}" is not declared${hint ? "." + hint : ""}`, expr);
|
|
719
|
+
}
|
|
355
720
|
}
|
|
356
721
|
case "List":
|
|
357
722
|
return `[${expr.items.map((item) => this.expr(item, scope)).join(", ")}]`;
|
|
358
723
|
// ?. so user.name is empty (not a crash) while data is loading
|
|
359
724
|
case "Member":
|
|
725
|
+
this.checkEnv(expr, scope);
|
|
360
726
|
return `${this.postfix(expr.object, scope)}?.${expr.property}`;
|
|
361
727
|
case "Index":
|
|
362
728
|
return `${this.postfix(expr.object, scope)}?.[${this.expr(expr.index, scope)}]`;
|
|
363
729
|
case "Object":
|
|
364
|
-
return `{ ${expr.entries.map((e) => `${JSON.stringify(e.key)}: ${this.expr(e.value, scope)}`).join(", ")} }`;
|
|
730
|
+
return `{ ${expr.entries.map((e) => `${JSON.stringify(e.key)}: ${this.expr(e.value, scope, bareWords)}`).join(", ")} }`;
|
|
365
731
|
case "Lambda": {
|
|
366
|
-
const inner = new Scope(scope, true);
|
|
732
|
+
const inner = new Scope(scope, true, true);
|
|
367
733
|
expr.params.forEach((p) => inner.declare(p, "local", expr));
|
|
368
734
|
const body = this.expr(expr.body, inner);
|
|
369
|
-
return `(${
|
|
735
|
+
return `(${inner.hasAwait ? "async " : ""}(${expr.params.map(jsName).join(", ")}) => ${expr.body.kind === "Object" ? `(${body})` : body})`;
|
|
370
736
|
}
|
|
371
737
|
case "Ternary":
|
|
372
738
|
return `(${this.expr(expr.cond, scope)} ? ${this.expr(expr.then, scope, bareWords)} : ${this.expr(expr.else, scope, bareWords)})`;
|
|
373
|
-
case "Wait":
|
|
739
|
+
case "Wait": {
|
|
374
740
|
if (!scope.awaitAllowed) fail("Use wait inside fn, start, an event handler, or a lambda", expr);
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
return
|
|
741
|
+
scope.markAwait();
|
|
742
|
+
const inner = this.expr(expr.expr, scope);
|
|
743
|
+
return inner.startsWith("__o.resume(await ") ? inner : `__o.resume(await __o.track(${inner}))`;
|
|
744
|
+
}
|
|
745
|
+
case "New":
|
|
746
|
+
return `new (${this.expr(expr.callee, scope)})(${expr.args.map((a) => this.expr(a, scope)).join(", ")})`;
|
|
747
|
+
case "Call": {
|
|
748
|
+
if (this.inTest && expr.callee.kind === "Ident" && TEST_GLOBALS.has(expr.callee.name) && scope.lookup(expr.callee.name) === void 0) {
|
|
749
|
+
const name = expr.callee.name;
|
|
750
|
+
const args = expr.args.map((a) => this.expr(a, scope));
|
|
751
|
+
if (name === "expect") args.push(String(expr.line));
|
|
752
|
+
const call2 = `__t.${name}(${args.join(", ")})`;
|
|
753
|
+
return TEST_WAITS.has(name) ? `await ${call2}` : call2;
|
|
754
|
+
}
|
|
755
|
+
this.checkCall(expr, scope);
|
|
756
|
+
if (expr.callee.kind === "Member" && LIST_WORDS.includes(expr.callee.property)) {
|
|
757
|
+
const target = this.expr(expr.callee.object, scope);
|
|
758
|
+
return `__o.words(${target}, ${JSON.stringify(expr.callee.property)}, [${expr.args.map((a) => this.expr(a, scope)).join(", ")}])`;
|
|
759
|
+
}
|
|
760
|
+
const call = `${this.postfix(expr.callee, scope)}(${expr.args.map((a) => this.expr(a, scope)).join(", ")})`;
|
|
761
|
+
const calleeBinding = expr.callee.kind === "Ident" ? scope.lookup(expr.callee.name) : void 0;
|
|
762
|
+
if (expr.callee.kind === "Ident" && scope.awaitAllowed && (calleeBinding === "fn" && this.asyncFns.has(expr.callee.name) || calleeBinding === "imported" && this.importedAsyncFns.has(expr.callee.name))) {
|
|
763
|
+
scope.markAwait();
|
|
764
|
+
return `__o.resume(await __o.track(${call}))`;
|
|
765
|
+
}
|
|
766
|
+
if (expr.callee.kind === "Ident" && !scope.awaitAllowed && (calleeBinding === "fn" && this.asyncFns.has(expr.callee.name) || calleeBinding === "imported" && this.importedAsyncFns.has(expr.callee.name))) {
|
|
767
|
+
fail(`${expr.callee.name}() waits for something (it uses wait, or calls a function that does), so its answer isn't ready here. Call it inside fn, start, or an event handler, and keep the result in a state`, expr);
|
|
768
|
+
}
|
|
769
|
+
if (expr.callee.kind === "Ident" && ASYNC_GLOBALS.has(expr.callee.name) && scope.lookup(expr.callee.name) === void 0) {
|
|
770
|
+
if (NEEDS[expr.callee.name]) this.needs.add(NEEDS[expr.callee.name]);
|
|
771
|
+
if (!scope.awaitAllowed) {
|
|
772
|
+
const name = expr.callee.name;
|
|
773
|
+
fail(/^(pick|take|getLocation)/.test(name) ? `${name}() waits for the person, so use it inside fn or an event handler` : name === "sleep" ? "sleep() pauses, so use it inside fn, start, or an event handler" : `${name}() talks to a server and takes time. To show data, use load: state users = load("/api/users"). To send data, call ${name}() inside fn, start, or an event handler`, expr);
|
|
774
|
+
}
|
|
775
|
+
scope.markAwait();
|
|
776
|
+
return `__o.resume(await __o.track(${call}))`;
|
|
777
|
+
}
|
|
778
|
+
return call;
|
|
779
|
+
}
|
|
378
780
|
case "Unary":
|
|
379
781
|
return `${expr.op}${this.wrap(expr.operand, scope)}`;
|
|
380
782
|
case "Binary": {
|
|
@@ -383,6 +785,40 @@ class Generator {
|
|
|
383
785
|
}
|
|
384
786
|
}
|
|
385
787
|
}
|
|
788
|
+
// a typo like env.API_ULR fails the build instead of shipping an empty value
|
|
789
|
+
checkEnv(expr, scope) {
|
|
790
|
+
const env = this.options.env;
|
|
791
|
+
if (!env || expr.object.kind !== "Ident" || expr.object.name !== "env" || scope.lookup("env") !== void 0) return;
|
|
792
|
+
if (Object.hasOwn(env, expr.property)) return;
|
|
793
|
+
fail(`env.${expr.property} is not set for the "${env.name}" environment.${suggest(expr.property, Object.keys(env)) || " Add it under env in orvyn.config.js"}`, expr);
|
|
794
|
+
}
|
|
795
|
+
// catch wrong argument counts and go(NotAScreen) before the app runs
|
|
796
|
+
checkCall(expr, scope) {
|
|
797
|
+
if (expr.callee.kind !== "Ident") return;
|
|
798
|
+
const name = expr.callee.name;
|
|
799
|
+
const binding = scope.lookup(name);
|
|
800
|
+
if (binding === "fn" && this.fnParams.has(name)) {
|
|
801
|
+
const want = this.fnParams.get(name);
|
|
802
|
+
const got = expr.args.length;
|
|
803
|
+
if (got !== want) {
|
|
804
|
+
const values = (n) => `${n} value${n === 1 ? "" : "s"}`;
|
|
805
|
+
fail(`${name}() takes ${values(want)}, but got ${values(got)}`, expr);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
if (binding === void 0 && (name === "go" || name === "back")) {
|
|
809
|
+
const target = expr.args[0];
|
|
810
|
+
if (name === "go" && !target) fail("go() needs a screen, like go(Home)", expr);
|
|
811
|
+
if (target?.kind === "Ident") {
|
|
812
|
+
const kind = scope.lookup(target.name);
|
|
813
|
+
if (kind === void 0 && !this.options.projectScreens?.includes(target.name)) {
|
|
814
|
+
const screens = this.program.body.flatMap((d) => d.kind === "Screen" && d.type === "screen" ? [d.name] : []);
|
|
815
|
+
fail(`There is no screen called "${target.name}".${suggest(target.name, screens) || (screens.length ? ` Screens: ${screens.join(", ")}` : "")}`, target);
|
|
816
|
+
}
|
|
817
|
+
if (kind === "component") fail(`${target.name} is a component, not a screen. Use "screen ${target.name}" to navigate to it`, target);
|
|
818
|
+
if (kind !== void 0 && kind !== "screen" && kind !== "imported" && kind !== "local") fail(`${name}() needs a screen, but "${target.name}" is a ${kind}`, target);
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
386
822
|
postfix(expr, scope) {
|
|
387
823
|
const code = this.expr(expr, scope);
|
|
388
824
|
return ["Binary", "Unary", "Object"].includes(expr.kind) ? `(${code})` : code;
|
|
@@ -401,6 +837,83 @@ class Generator {
|
|
|
401
837
|
return `\`${body}\``;
|
|
402
838
|
}
|
|
403
839
|
// --- view ----------------------------------------------------------------
|
|
840
|
+
// List rowHeight: 56 { for item in items key item.id { ... } }
|
|
841
|
+
list(node, scope) {
|
|
842
|
+
const loop = node.children[0];
|
|
843
|
+
if (node.arg || node.children.length !== 1 || loop?.kind !== "ViewFor") {
|
|
844
|
+
fail("List needs one loop inside, like: List { for item in items key item.id { Text item.name } }", node);
|
|
845
|
+
}
|
|
846
|
+
const inner = new Scope(scope);
|
|
847
|
+
inner.declare(loop.name, "local", loop);
|
|
848
|
+
const name = jsName(loop.name);
|
|
849
|
+
const props = node.props.map((p) => this.prop(p, "List", scope));
|
|
850
|
+
props.push(`items: ${this.expr(loop.iter, scope)}`);
|
|
851
|
+
props.push(`row: (${name}) => ${this.viewList(loop.body, inner)}`);
|
|
852
|
+
if (loop.key) props.push(`keyOf: (${name}) => ${this.expr(loop.key, inner)}`);
|
|
853
|
+
return `__h(__o.List, { ${props.join(", ")} }, [])`;
|
|
854
|
+
}
|
|
855
|
+
// bind: for Checkbox, Switch and Select: the value and the update handler
|
|
856
|
+
bound(node, scope, helper) {
|
|
857
|
+
const bind = node.props.find((p) => p.key === "bind");
|
|
858
|
+
if (!bind || bind.value.kind !== "Expr") fail(`${node.name} needs bind, like: ${node.name} "...", bind: name`, node);
|
|
859
|
+
const target = bind.value.expr;
|
|
860
|
+
const root = rootIdent(target);
|
|
861
|
+
if (!root || !["state", "imported"].includes(scope.lookup(root.name)) || target.kind !== "Ident" && !((target.kind === "Member" || target.kind === "Index") && target.object.kind === "Ident")) {
|
|
862
|
+
fail("bind needs a state name or one field, like bind: user.name", bind);
|
|
863
|
+
}
|
|
864
|
+
const key = target.kind === "Member" ? JSON.stringify(target.property) : target.kind === "Index" ? this.expr(target.index, scope) : "undefined";
|
|
865
|
+
return {
|
|
866
|
+
value: this.expr(target, scope),
|
|
867
|
+
handler: `__o.${helper}(${this.stateRef(root.name, scope)}, ${key}, ${target.kind === "Index"})`
|
|
868
|
+
};
|
|
869
|
+
}
|
|
870
|
+
// Checkbox "I agree", bind: agreed
|
|
871
|
+
toggle(node, scope) {
|
|
872
|
+
if (!node.arg) fail(`${node.name} needs its words, like: ${node.name} "Send me news", bind: news`, node);
|
|
873
|
+
const { value, handler } = this.bound(node, scope, "bindCheck");
|
|
874
|
+
const said = this.expr(node.arg, scope);
|
|
875
|
+
const input = [`type: "checkbox"`, `checked: !!(${value})`, `onInput: ${handler}`];
|
|
876
|
+
if (node.name === "Switch") input.push(`role: "switch"`);
|
|
877
|
+
const outer = [`flex: true`, `direction: "row"`, `align: "center"`, `gap: 8`, `nativeRole: "toggle"`];
|
|
878
|
+
for (const prop of node.props) {
|
|
879
|
+
if (prop.key === "disabled") input.push(this.prop(prop, node.name, scope));
|
|
880
|
+
else if (prop.key !== "bind") outer.push(this.prop(prop, node.name, scope));
|
|
881
|
+
}
|
|
882
|
+
return `__h("label", { ${outer.join(", ")} }, [__h("input", { ${input.join(", ")} }, []), __h("span", {}, [${said}])])`;
|
|
883
|
+
}
|
|
884
|
+
// Select "Country", bind: country, options: ["India", "Nepal"]
|
|
885
|
+
select(node, scope) {
|
|
886
|
+
const options = node.props.find((p) => p.key === "options");
|
|
887
|
+
if (!options || options.value.kind !== "Expr") fail(`Select needs options, like: Select "Country", bind: country, options: ["India", "Nepal"]`, node);
|
|
888
|
+
const { value, handler } = this.bound(node, scope, "bindInput");
|
|
889
|
+
const placeholder = node.arg ? this.expr(node.arg, scope) : `""`;
|
|
890
|
+
const props = [`value: ${value}`, `onInput: ${handler}`, `placeholder: ${placeholder}`];
|
|
891
|
+
for (const prop of node.props) {
|
|
892
|
+
if (prop.key === "label" && prop.value.kind === "Expr") props.push(`"aria-label": ${this.expr(prop.value.expr, scope)}`);
|
|
893
|
+
else if (prop.key !== "bind" && prop.key !== "options") props.push(this.prop(prop, "Select", scope));
|
|
894
|
+
}
|
|
895
|
+
if (node.arg && !node.props.some((p) => p.key === "label")) props.push(`"aria-label": ${placeholder}`);
|
|
896
|
+
return `__o.createSelect({ ${props.join(", ")} }, ${this.expr(options.value.expr, scope)})`;
|
|
897
|
+
}
|
|
898
|
+
tabs(node, scope) {
|
|
899
|
+
const options = node.props.find((p) => p.key === "options");
|
|
900
|
+
if (!options || options.value.kind !== "Expr") fail(`Tabs needs options, like: Tabs bind: tab, options: ["Home", "Profile"]`, node);
|
|
901
|
+
const { value, handler } = this.bound(node, scope, "bindInput");
|
|
902
|
+
const props = [
|
|
903
|
+
`value: ${value}`,
|
|
904
|
+
`onInput: ${handler}`,
|
|
905
|
+
...node.props.filter((p) => p.key !== "bind" && p.key !== "options").map((p) => this.prop(p, "Tabs", scope))
|
|
906
|
+
];
|
|
907
|
+
return `__o.createTabs({ ${props.join(", ")} }, ${this.expr(options.value.expr, scope)})`;
|
|
908
|
+
}
|
|
909
|
+
modal(node, scope) {
|
|
910
|
+
for (const key of ["open", "onClose"]) {
|
|
911
|
+
if (!node.props.some((p) => p.key === key)) fail(`Modal needs ${key}, like: Modal open: help, onClose: help = false { ... }`, node);
|
|
912
|
+
}
|
|
913
|
+
const props = node.props.map((p) => p.key === "onClose" ? `onClose: ${p.value.kind === "Handler" ? this.handler(p.value.body, scope) : this.exprHandler(p.value.expr, scope)}` : this.prop(p, "Modal", scope));
|
|
914
|
+
if (node.arg) props.push(`title: ${this.expr(node.arg, scope)}`);
|
|
915
|
+
return `__o.createModal({ ${props.join(", ")} }, [${node.children.map((c) => this.view(c, scope)).join(", ")}])`;
|
|
916
|
+
}
|
|
404
917
|
viewList(nodes, scope) {
|
|
405
918
|
return `[${nodes.map((node) => this.view(node, scope)).join(", ")}]`;
|
|
406
919
|
}
|
|
@@ -415,13 +928,40 @@ class Generator {
|
|
|
415
928
|
const inner = new Scope(scope);
|
|
416
929
|
inner.declare(node.name, "local", node);
|
|
417
930
|
const name = jsName(node.name);
|
|
418
|
-
|
|
931
|
+
const body = this.viewList(node.body, inner);
|
|
932
|
+
const keyed = node.key ? `__o.keyed(${this.expr(node.key, inner)}, ${body})` : body;
|
|
933
|
+
return `Array.from(${this.expr(node.iter, scope)} ?? [], (${name}) => ${keyed})`;
|
|
419
934
|
}
|
|
420
935
|
case "Element":
|
|
421
|
-
return this.element(node, scope);
|
|
936
|
+
return node.line ? `(__o.at(__F, ${node.line}, ${node.col ?? 0}), ${this.element(node, scope)})` : this.element(node, scope);
|
|
422
937
|
}
|
|
423
938
|
}
|
|
424
939
|
element(node, scope) {
|
|
940
|
+
if (node.name === "Slot") return "(children ?? [])";
|
|
941
|
+
if (node.name === "List" && !this.components.has("List")) return this.list(node, scope);
|
|
942
|
+
if (node.name === "Question" && !this.components.has("Question")) {
|
|
943
|
+
if (!node.arg || !node.children.length) fail(`Question needs text and an answer, like: Question "Is it free?" { Text "Yes" }`, node);
|
|
944
|
+
const asked = this.expr(node.arg, scope);
|
|
945
|
+
const hasHeadingSize = node.props.some((p) => p.key === "size");
|
|
946
|
+
const headingProps = node.props.filter((p) => ["size", "weight", "fontWeight", "color"].includes(p.key)).map((p) => this.prop(p, "Question", scope));
|
|
947
|
+
const props2 = node.props.filter((p) => !["size", "weight", "fontWeight", "color", "layout"].includes(p.key)).map((p) => this.prop(p, "Question", scope));
|
|
948
|
+
const layout = node.props.find((p) => p.key === "layout");
|
|
949
|
+
const direction = layout?.value.kind === "Expr" ? this.expr(layout.value.expr, scope, true) : '"column"';
|
|
950
|
+
const answer = node.children.map((c) => this.view(c, scope)).join(", ");
|
|
951
|
+
const headingAttrs = [...hasHeadingSize ? [] : ["size: 20"], ...headingProps].join(", ");
|
|
952
|
+
return `__h("section", { ${[`flex: true`, `direction: ${direction}`, `gap: 8`, `"data-orvyn-question": ${asked}`, ...props2].join(", ")} }, [__h("h2", { ${headingAttrs} }, [${asked}]), ${answer}])`;
|
|
953
|
+
}
|
|
954
|
+
if ((node.name === "Checkbox" || node.name === "Switch") && !this.components.has(node.name)) return this.toggle(node, scope);
|
|
955
|
+
if (node.name === "Select" && !this.components.has("Select")) return this.select(node, scope);
|
|
956
|
+
if (node.name === "Tabs" && !this.components.has("Tabs")) return this.tabs(node, scope);
|
|
957
|
+
if (node.name === "Modal" && !this.components.has("Modal")) return this.modal(node, scope);
|
|
958
|
+
if (node.name === "Map" && !this.components.has("Map")) {
|
|
959
|
+
for (const key of ["latitude", "longitude"]) {
|
|
960
|
+
if (!node.props.some((p) => p.key === key)) fail(`Map needs ${key}, like: Map latitude: 28.61, longitude: 77.21`, node);
|
|
961
|
+
}
|
|
962
|
+
const props2 = node.props.map((p) => p.key === "label" && p.value.kind === "Expr" ? `title: ${this.expr(p.value.expr, scope)}` : this.prop(p, "Map", scope));
|
|
963
|
+
return `__o.createMap({ ${props2.join(", ")} })`;
|
|
964
|
+
}
|
|
425
965
|
const builtin = Object.hasOwn(BUILTINS, node.name) ? BUILTINS[node.name] : void 0;
|
|
426
966
|
const imported = scope.lookup(node.name) === "imported";
|
|
427
967
|
const userComponent = this.components.get(node.name) ?? (imported ? null : void 0);
|
|
@@ -449,7 +989,7 @@ class Generator {
|
|
|
449
989
|
}
|
|
450
990
|
const bind = node.props.find((p) => p.key === "bind");
|
|
451
991
|
if (bind) {
|
|
452
|
-
if (node.name !== "Input") fail("Use bind on Input,
|
|
992
|
+
if (node.name !== "Input" && node.name !== "TextArea" && node.name !== "DatePicker") fail("Use bind on Input, TextArea, Checkbox, Switch or Select", bind);
|
|
453
993
|
if (node.props.filter((p) => p.key === "bind").length > 1 || node.props.some((p) => ["value", "onChange", "onInput"].includes(p.key))) {
|
|
454
994
|
fail("bind supplies value and onChange. Remove the duplicate binding or event", bind);
|
|
455
995
|
}
|
|
@@ -467,13 +1007,30 @@ class Generator {
|
|
|
467
1007
|
props.push(`onInput: __o.bindInput(${this.stateRef(root.name, scope)}, ${key}, ${target.kind === "Index"})`);
|
|
468
1008
|
}
|
|
469
1009
|
for (const prop of node.props) {
|
|
470
|
-
if (prop.key
|
|
1010
|
+
if (node.name === "Input" && prop.key === "type" && prop.value.kind === "Expr") {
|
|
1011
|
+
const kind = prop.value.expr;
|
|
1012
|
+
const known = ["text", "email", "password", "number", "phone"];
|
|
1013
|
+
const word = kind.kind === "String" && kind.parts.length === 1 ? String(kind.parts[0]) : kind.kind === "Ident" && (known.includes(kind.name) || scope.lookup(kind.name) === void 0) ? kind.name : null;
|
|
1014
|
+
if (word !== null) {
|
|
1015
|
+
if (!["text", "email", "password", "number", "phone"].includes(word)) {
|
|
1016
|
+
fail(`Input type can be: text, email, password, number or phone`, prop);
|
|
1017
|
+
}
|
|
1018
|
+
props.push(`type: ${JSON.stringify(word === "phone" ? "tel" : word)}`);
|
|
1019
|
+
continue;
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
if (prop.key === "label" && prop.value.kind === "Expr") {
|
|
1023
|
+
const said = this.expr(prop.value.expr, scope);
|
|
1024
|
+
props.push(`"aria-label": ${said}`);
|
|
1025
|
+
if (node.name === "Image") props.push(`alt: ${said}`);
|
|
1026
|
+
} else if (prop.key !== "bind") props.push(this.prop(prop, node.name, scope));
|
|
471
1027
|
}
|
|
1028
|
+
if (node.name === "Image") props.push(`loading: "lazy"`, `decoding: "async"`);
|
|
472
1029
|
} else {
|
|
473
1030
|
if (node.arg) fail(`${node.name} needs named arguments, like ${node.name} title: "..."`, node.arg);
|
|
474
1031
|
const params = userComponent;
|
|
475
1032
|
for (const prop of node.props) {
|
|
476
|
-
if (params && !params.includes(prop.key)) {
|
|
1033
|
+
if (params && !params.includes(prop.key) && prop.key !== "key") {
|
|
477
1034
|
fail(`${node.name} has no parameter "${prop.key}". It takes: ${params.join(", ") || "nothing"}`, prop);
|
|
478
1035
|
}
|
|
479
1036
|
if (prop.value.kind === "Handler") {
|
|
@@ -485,7 +1042,7 @@ class Generator {
|
|
|
485
1042
|
}
|
|
486
1043
|
for (const child of node.children) children.push(this.view(child, scope));
|
|
487
1044
|
if (!builtin) {
|
|
488
|
-
return
|
|
1045
|
+
return `__h(${node.name}, { ${props.join(", ")} }, [${children.join(", ")}])`;
|
|
489
1046
|
}
|
|
490
1047
|
if (node.name === "Form") {
|
|
491
1048
|
return `__o.createForm(__forms, ${this.formId++}, { ${props.join(", ")} }, [${children.join(", ")}])`;
|
|
@@ -502,28 +1059,28 @@ class Generator {
|
|
|
502
1059
|
if (!event) {
|
|
503
1060
|
fail(`Unknown event "${prop.key}". Available: ${Object.keys(EVENTS).join(", ")}`, prop);
|
|
504
1061
|
}
|
|
1062
|
+
const isCallableRef = prop.value.kind === "Expr" && (prop.value.expr.kind === "Ident" || prop.value.expr.kind === "Member" || prop.value.expr.kind === "Lambda");
|
|
505
1063
|
if (prop.key === "onChange") {
|
|
506
|
-
const call = prop.value.kind === "Handler" ? this.handler(prop.value.body, scope, ["value"]) : this.expr(prop.value.expr, scope);
|
|
507
|
-
return `${event}: (e) => (${call})(e.target.value)`;
|
|
508
|
-
}
|
|
509
|
-
let handler;
|
|
510
|
-
if (prop.value.kind === "Handler") {
|
|
511
|
-
handler = this.handler(prop.value.body, scope);
|
|
512
|
-
} else if (prop.value.expr.kind === "Ident" || prop.value.expr.kind === "Member") {
|
|
513
|
-
handler = this.expr(prop.value.expr, scope);
|
|
514
|
-
} else if (this.stateListCall(prop.value.expr, scope)) {
|
|
515
|
-
handler = `() => ${this.stateListCall(prop.value.expr, scope)}`;
|
|
516
|
-
} else {
|
|
517
|
-
handler = `() => ${this.expr(prop.value.expr, scope)}`;
|
|
1064
|
+
const call = prop.value.kind === "Handler" ? this.handler(prop.value.body, scope, ["value"]) : isCallableRef ? this.expr(prop.value.expr, scope) : this.exprHandler(prop.value.expr, scope, ["value"]);
|
|
1065
|
+
return `${event}: __o.guard((e) => (${call})(e.target.value), ${JSON.stringify(`onChange in ${this.currentView || element}`)}, __F, ${prop.line ?? 0})`;
|
|
518
1066
|
}
|
|
519
|
-
|
|
1067
|
+
const handler = prop.value.kind === "Handler" ? this.handler(prop.value.body, scope) : isCallableRef ? this.expr(prop.value.expr, scope) : this.exprHandler(prop.value.expr, scope);
|
|
1068
|
+
if (element === "Form" && prop.key === "onSubmit") return `${event}: __o.submitHandler(${handler})`;
|
|
1069
|
+
return `${event}: __o.guard(${handler}, ${JSON.stringify(`${prop.key} in ${this.currentView || element}`)}, __F, ${prop.line ?? 0})`;
|
|
1070
|
+
}
|
|
1071
|
+
// `onTap: post(...)` or `onTap: sleep(10)` runs in an event-handler scope so
|
|
1072
|
+
// waiting works the same with or without `{ ... }`.
|
|
1073
|
+
exprHandler(expr, scope, params = []) {
|
|
1074
|
+
const inner = new Scope(scope, true, true);
|
|
1075
|
+
params.forEach((p) => inner.declare(p, "local", expr));
|
|
1076
|
+
const body = this.stateListCall(expr, inner) ?? this.expr(expr, inner);
|
|
1077
|
+
return `${inner.hasAwait ? "async " : ""}(${params.join(", ")}) => ${expr.kind === "Object" ? `(${body})` : body}`;
|
|
520
1078
|
}
|
|
521
1079
|
handler(body, scope, params = []) {
|
|
522
|
-
const inner = new Scope(scope, true);
|
|
1080
|
+
const inner = new Scope(scope, true, true);
|
|
523
1081
|
params.forEach((p) => inner.declare(p, "local", body[0] ?? { line: 0, col: 0 }));
|
|
524
1082
|
const lines = this.capture(() => this.stmts(body, inner));
|
|
525
|
-
|
|
526
|
-
return `${isAsync ? "async " : ""}(${params.join(", ")}) => {
|
|
1083
|
+
return `${inner.hasAwait ? "async " : ""}(${params.join(", ")}) => {
|
|
527
1084
|
${lines.join("\n")}
|
|
528
1085
|
}`;
|
|
529
1086
|
}
|