@yannosay/sinth 3.1.0 → 4.1.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/README.md +1 -1
- package/dist/browser/compile.js +3 -2
- package/dist/cli.js +62 -59
- package/dist/core/cli-compiler.js +178 -28
- package/dist/core/compiler.js +163 -68
- package/dist/core/expr.js +9 -2
- package/dist/core/lexer.js +3 -7
- package/dist/core/parser.js +191 -215
- package/dist/core/runtime/render.js +1 -1
- package/dist/server.js +9 -3
- package/dist/update-check.js +6 -2
- package/dist/utils.js +15 -8
- package/package.json +10 -3
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# 
|
|
2
2
|
|
|
3
|
-
[](https://www.npmjs.com/package/@yannosay/sinth) [](https://www.npmjs.com/package/@yannosay/sinth) [](https://github.com/yannosay/sinth/blob/main/LICENSE)[](https://github.com/yannosay/sinth/commits) [](https://github.com/yannosay/sinth/pulls)
|
|
3
|
+
[](https://www.npmjs.com/package/@yannosay/sinth) [](https://www.npmjs.com/package/@yannosay/sinth) [](https://github.com/yannosay/sinth/blob/main/LICENSE) [](https://github.com/yannosay/sinth/commits) [](https://github.com/yannosay/sinth/pulls)
|
|
4
4
|
|
|
5
5
|
## Sinth Compiler
|
|
6
6
|
|
package/dist/browser/compile.js
CHANGED
|
@@ -37,13 +37,14 @@ function compileSinth(source) {
|
|
|
37
37
|
};
|
|
38
38
|
const bodyHTML = file.uses.map(u => (0, compiler_1.renderCompUse)(u, ctx, new Map(), 0)).join("\n");
|
|
39
39
|
const pageCSS = file.styles.map(s => (0, style_processor_1.processStyleBlock)(s, hash, new Map())).join("\n");
|
|
40
|
+
const allVarDecls = [...file.varDecls, ...(ctx.varDecls?.filter(v => !file.varDecls.some(fv => fv.name === v.name)) ?? [])];
|
|
40
41
|
const assignedVars = new Set();
|
|
41
|
-
for (const v of
|
|
42
|
+
for (const v of allVarDecls) {
|
|
42
43
|
if (v.value)
|
|
43
44
|
assignedVars.add(v.name);
|
|
44
45
|
}
|
|
45
46
|
const runtime = (0, compiler_1.buildRuntime)({
|
|
46
|
-
varDecls:
|
|
47
|
+
varDecls: allVarDecls,
|
|
47
48
|
bodyHTML,
|
|
48
49
|
logicBlocks: ctx.logicBlocks,
|
|
49
50
|
mixedBlocks: ctx.mixedBlocks,
|
package/dist/cli.js
CHANGED
|
@@ -35,6 +35,8 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
const fs = __importStar(require("fs"));
|
|
37
37
|
const path = __importStar(require("path"));
|
|
38
|
+
const https = __importStar(require("https"));
|
|
39
|
+
const child_process_1 = require("child_process");
|
|
38
40
|
const types_1 = require("./core/types");
|
|
39
41
|
const cli_compiler_1 = require("./core/cli-compiler");
|
|
40
42
|
const server_1 = require("./server");
|
|
@@ -46,8 +48,9 @@ function loadConfig(root) {
|
|
|
46
48
|
try {
|
|
47
49
|
return JSON.parse(fs.readFileSync(cfgPath, "utf-8"));
|
|
48
50
|
}
|
|
49
|
-
catch {
|
|
51
|
+
catch (e) {
|
|
50
52
|
types_1.SinthWarning.emit("Could not parse sinth.config.json");
|
|
53
|
+
console.error(e);
|
|
51
54
|
}
|
|
52
55
|
}
|
|
53
56
|
return {};
|
|
@@ -80,7 +83,7 @@ async function main() {
|
|
|
80
83
|
(0, update_check_1.checkForUpdate)(pkg4.version);
|
|
81
84
|
}
|
|
82
85
|
if (nonSinth.length > 0)
|
|
83
|
-
process.stdout.write(`\
|
|
86
|
+
process.stdout.write(`\u001b[33mSkipping non-.sinth files:\u001b[0m ${nonSinth.join(", ")}\n`);
|
|
84
87
|
const fileArgs = cleanArgs.filter(a => a.endsWith(".sinth"));
|
|
85
88
|
const pages = fileArgs.length > 0
|
|
86
89
|
? fileArgs.map(f => path.resolve(cwd, f)).filter(f => fs.existsSync(f))
|
|
@@ -104,23 +107,23 @@ async function main() {
|
|
|
104
107
|
const out = path.join(outDir, rel);
|
|
105
108
|
fs.mkdirSync(path.dirname(out), { recursive: true });
|
|
106
109
|
fs.writeFileSync(out, html);
|
|
107
|
-
process.stdout.write(` \
|
|
110
|
+
process.stdout.write(` \u001b[32m✓\u001b[0m ${rel}\n`);
|
|
108
111
|
built++;
|
|
109
112
|
}
|
|
110
113
|
catch (e) {
|
|
111
|
-
process.stderr.write(` \
|
|
114
|
+
process.stderr.write(` \u001b[31m✗\u001b[0m ${path.relative(cwd, p)}\n${e.message}\n`);
|
|
112
115
|
hadError = true;
|
|
113
116
|
}
|
|
114
117
|
}
|
|
115
118
|
if (sharedRuntimes.length > 0) {
|
|
116
119
|
const combined = sharedRuntimes.join("\n");
|
|
117
120
|
fs.writeFileSync(path.join(outDir, "sinth-runtime.js"), combined);
|
|
118
|
-
process.stdout.write(` \
|
|
121
|
+
process.stdout.write(` \u001b[32m✓\u001b[0m sinth-runtime.js (shared)\n`);
|
|
119
122
|
}
|
|
120
123
|
const assetsIn = path.join(cwd, "assets"), assetsOut = path.join(outDir, "assets");
|
|
121
124
|
if (fs.existsSync(assetsIn)) {
|
|
122
125
|
(0, cli_compiler_1.copyDir)(assetsIn, assetsOut);
|
|
123
|
-
process.stdout.write(` \
|
|
126
|
+
process.stdout.write(` \u001b[32m✓\u001b[0m assets/ → ${path.relative(cwd, assetsOut)}/\n`);
|
|
124
127
|
}
|
|
125
128
|
const libIn = path.join(cwd, "libraries"), libOut = path.join(outDir, "libraries");
|
|
126
129
|
if (fs.existsSync(libIn)) {
|
|
@@ -131,15 +134,16 @@ async function main() {
|
|
|
131
134
|
try {
|
|
132
135
|
fs.unlinkSync(path.join(libOut, f));
|
|
133
136
|
}
|
|
134
|
-
catch {
|
|
137
|
+
catch (e) {
|
|
138
|
+
console.warn(`Failed to delete ${f}:`, e);
|
|
139
|
+
}
|
|
135
140
|
}
|
|
136
141
|
}
|
|
137
|
-
process.stdout.write(` \
|
|
142
|
+
process.stdout.write(` \u001b[32m✓\u001b[0m libraries/ → ${path.relative(cwd, libOut)}/\n`);
|
|
138
143
|
}
|
|
139
144
|
const buildTime = ((Date.now() - buildStart) / 1000).toFixed(2);
|
|
140
|
-
process.stdout.write(`\n\
|
|
141
|
-
process.exit(hadError ? 1 : 0);
|
|
142
|
-
break;
|
|
145
|
+
process.stdout.write(`\n\u001b[1mBuilt ${built} page(s)\u001b[0m${hadError ? " with errors" : ""} \u001b[2min ${buildTime}s\u001b[0m\n`);
|
|
146
|
+
return process.exit(hadError ? 1 : 0);
|
|
143
147
|
}
|
|
144
148
|
case "dev": {
|
|
145
149
|
const pkgPath3 = path.join(__dirname, "..", "package.json");
|
|
@@ -152,7 +156,7 @@ async function main() {
|
|
|
152
156
|
? fileArgs.map(f => path.resolve(cwd, f)).filter(f => fs.existsSync(f))
|
|
153
157
|
: undefined;
|
|
154
158
|
await (0, server_1.startDevServer)({ ...opts, port, files });
|
|
155
|
-
|
|
159
|
+
return;
|
|
156
160
|
}
|
|
157
161
|
case "check": {
|
|
158
162
|
opts.checkOnly = true;
|
|
@@ -161,41 +165,39 @@ async function main() {
|
|
|
161
165
|
for (const p of pages) {
|
|
162
166
|
try {
|
|
163
167
|
(0, cli_compiler_1.compileFile)(p, opts);
|
|
164
|
-
process.stdout.write(` \
|
|
168
|
+
process.stdout.write(` \u001b[32m✓\u001b[0m ${path.relative(cwd, p)}\n`);
|
|
165
169
|
}
|
|
166
170
|
catch (e) {
|
|
167
|
-
process.stderr.write(` \
|
|
171
|
+
process.stderr.write(` \u001b[31m✗\u001b[0m ${path.relative(cwd, p)}\n${e.message}\n`);
|
|
168
172
|
hadError = true;
|
|
169
173
|
}
|
|
170
174
|
}
|
|
171
|
-
process.exit(hadError ? 1 : 0);
|
|
175
|
+
return process.exit(hadError ? 1 : 0);
|
|
172
176
|
}
|
|
173
177
|
case "update": {
|
|
174
178
|
const pkgPath2 = path.join(__dirname, "..", "package.json");
|
|
175
179
|
const current = fs.existsSync(pkgPath2) ? JSON.parse(fs.readFileSync(pkgPath2, "utf-8")).version : null;
|
|
176
|
-
const https = require("https");
|
|
177
180
|
https.get("https://registry.npmjs.org/@yannosay/sinth/latest", { timeout: 5000 }, (res) => {
|
|
178
181
|
let data = "";
|
|
179
|
-
res.on("data", (chunk) => data += chunk);
|
|
182
|
+
res.on("data", (chunk) => data += chunk.toString());
|
|
180
183
|
res.on("end", () => {
|
|
181
184
|
try {
|
|
182
185
|
const latest = JSON.parse(data).version;
|
|
183
186
|
if (latest === current) {
|
|
184
|
-
process.stdout.write(`\
|
|
187
|
+
process.stdout.write(`\u001b[32mAlready on latest version: ${current} ✓\u001b[0m\n`);
|
|
185
188
|
}
|
|
186
189
|
else if (latest) {
|
|
187
|
-
process.stdout.write(`\
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
process.stdout.write(`\x1b[32m✓ Sinth updated to ${latest}!\x1b[0m\n`);
|
|
190
|
+
process.stdout.write(`\u001b[36mUpdating Sinth ${current} → ${latest}...\u001b[0m\n`);
|
|
191
|
+
(0, child_process_1.execSync)("npm install -g @yannosay/sinth@latest", { stdio: "inherit" });
|
|
192
|
+
process.stdout.write(`\u001b[32m✓ Sinth updated to ${latest}!\u001b[0m\n`);
|
|
191
193
|
}
|
|
192
194
|
}
|
|
193
195
|
catch {
|
|
194
|
-
process.stderr.write("\
|
|
196
|
+
process.stderr.write("\u001b[31mCould not check for updates.\u001b[0m\n");
|
|
195
197
|
}
|
|
196
198
|
});
|
|
197
|
-
}).on("error", () => process.stderr.write("\
|
|
198
|
-
|
|
199
|
+
}).on("error", () => process.stderr.write("\u001b[31mCould not reach npm registry.\u001b[0m\n"));
|
|
200
|
+
return;
|
|
199
201
|
}
|
|
200
202
|
case "version":
|
|
201
203
|
case "--version":
|
|
@@ -209,11 +211,12 @@ async function main() {
|
|
|
209
211
|
else {
|
|
210
212
|
process.stdout.write("Sinth Compiler v1.0.0\n");
|
|
211
213
|
}
|
|
212
|
-
|
|
214
|
+
return;
|
|
213
215
|
}
|
|
214
216
|
case "init": {
|
|
215
217
|
await interactiveInit(cwd);
|
|
216
|
-
|
|
218
|
+
process.stdin.destroy();
|
|
219
|
+
return process.exit(0);
|
|
217
220
|
}
|
|
218
221
|
default: {
|
|
219
222
|
let version = "1.0.0";
|
|
@@ -230,20 +233,22 @@ async function main() {
|
|
|
230
233
|
version = pkg.version;
|
|
231
234
|
break;
|
|
232
235
|
}
|
|
233
|
-
catch {
|
|
236
|
+
catch {
|
|
237
|
+
// Ignore - use default version
|
|
238
|
+
}
|
|
234
239
|
}
|
|
235
240
|
}
|
|
236
241
|
process.stdout.write(`
|
|
237
|
-
\
|
|
242
|
+
\u001b[1mSinth Compiler v${version}\u001b[0m
|
|
238
243
|
|
|
239
|
-
\
|
|
244
|
+
\u001b[1mCommands:\u001b[0m
|
|
240
245
|
sinth build [files] [--out ./dist] [--prod] [--shared-runtime] Compile .sinth pages
|
|
241
246
|
sinth dev [files] [--port 3000] Live-reload dev server
|
|
242
247
|
sinth check Lint without emitting
|
|
243
248
|
sinth init [name] Scaffold a new project
|
|
244
249
|
sinth version Print version
|
|
245
250
|
`);
|
|
246
|
-
|
|
251
|
+
return;
|
|
247
252
|
}
|
|
248
253
|
}
|
|
249
254
|
}
|
|
@@ -259,31 +264,29 @@ async function interactiveInit(cwd) {
|
|
|
259
264
|
terminal: true,
|
|
260
265
|
escapeCodeTimeout: 50,
|
|
261
266
|
});
|
|
262
|
-
process.stdout.write("\
|
|
263
|
-
const rawName = await question(rl, "\
|
|
267
|
+
process.stdout.write("\u001b[s\u001b[1m\n✨ Welcome to Sinth project setup!\n\n\u001b[0m");
|
|
268
|
+
const rawName = await question(rl, "\u001b[45m\u001b[30m Project name: \u001b[0m ", "my-sinth-project");
|
|
269
|
+
rl.close();
|
|
270
|
+
process.stdin.pause();
|
|
271
|
+
process.stdin.removeAllListeners();
|
|
272
|
+
process.stdin.read();
|
|
264
273
|
process.stdout.write("\n");
|
|
265
274
|
const projectName = rawName.toLowerCase().replace(/\s+/g, "-").replace(/[^a-z0-9-]/g, "");
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
input: process.stdin,
|
|
269
|
-
output: process.stdout,
|
|
270
|
-
terminal: true,
|
|
271
|
-
escapeCodeTimeout: 50,
|
|
272
|
-
});
|
|
273
|
-
const preset = await select(rl2);
|
|
274
|
-
rl2.close();
|
|
275
|
+
const preset = await select();
|
|
276
|
+
process.stdout.write("\u001b[u\u001b[0J");
|
|
275
277
|
const root = path.resolve(cwd, projectName);
|
|
276
|
-
process.stdout.write("\n");
|
|
277
278
|
const start = Date.now();
|
|
278
279
|
scaffoldByPreset(root, projectName, preset);
|
|
279
280
|
const elapsed = ((Date.now() - start) / 1000).toFixed(2);
|
|
280
281
|
process.stdout.write(`
|
|
281
|
-
\
|
|
282
|
+
\u001b[1m✨ Success!\u001b[0m
|
|
283
|
+
|
|
284
|
+
\u001b[45m\u001b[30m Project name: \u001b[0m ${projectName}
|
|
285
|
+
\u001b[43m\u001b[30m Preset: \u001b[0m ${preset}
|
|
286
|
+
\u001b[32m✓ ${projectName} scaffolded at \u001b[4m${projectName}/\u001b[0m\u001b[32m in ${elapsed}s\u001b[0m
|
|
282
287
|
|
|
283
|
-
\
|
|
284
|
-
|
|
285
|
-
sinth dev
|
|
286
|
-
sinth build
|
|
288
|
+
\u001b[47m\u001b[30m Get started: \u001b[0m
|
|
289
|
+
\u001b[100m\u001b[37m sinth dev \u001b[0m to start dev server.
|
|
287
290
|
`);
|
|
288
291
|
}
|
|
289
292
|
function question(rl, prompt, defaultVal) {
|
|
@@ -293,30 +296,30 @@ function question(rl, prompt, defaultVal) {
|
|
|
293
296
|
});
|
|
294
297
|
});
|
|
295
298
|
}
|
|
296
|
-
function select(
|
|
299
|
+
function select() {
|
|
297
300
|
return new Promise((resolve) => {
|
|
298
301
|
const items = PRESETS;
|
|
299
302
|
let selected = 1;
|
|
300
303
|
const prefix = " ";
|
|
301
|
-
const cursor = "\
|
|
304
|
+
const cursor = "\u001b[36m❯\u001b[0m";
|
|
302
305
|
const empty = " ";
|
|
303
306
|
function render(first = false) {
|
|
304
307
|
if (!first) {
|
|
305
|
-
process.stdout.write(`\
|
|
308
|
+
process.stdout.write(`\u001b[${items.length + 1}A`);
|
|
306
309
|
}
|
|
307
|
-
process.stdout.write(`\
|
|
310
|
+
process.stdout.write(`\u001b[43m\u001b[30m Select preset: \u001b[0m\n`);
|
|
308
311
|
for (let i = 0; i < items.length; i++) {
|
|
309
312
|
const pointer = i === selected ? cursor : empty;
|
|
310
313
|
const isFull = items[i].value === "full";
|
|
311
314
|
let label;
|
|
312
315
|
if (i === selected) {
|
|
313
|
-
label = isFull ? `\
|
|
316
|
+
label = isFull ? `\u001b[1m\u001b[35m${items[i].label}\u001b[0m` : `\u001b[1m${items[i].label}\u001b[0m`;
|
|
314
317
|
}
|
|
315
318
|
else {
|
|
316
319
|
label = items[i].label;
|
|
317
320
|
}
|
|
318
|
-
const desc = `\
|
|
319
|
-
process.stdout.write(`${prefix}${pointer} ${label} ${desc}\
|
|
321
|
+
const desc = `\u001b[2m- ${items[i].description}\u001b[0m`;
|
|
322
|
+
process.stdout.write(`${prefix}${pointer} ${label} ${desc}\u001b[0K\n`);
|
|
320
323
|
}
|
|
321
324
|
}
|
|
322
325
|
process.stdin.setRawMode(true);
|
|
@@ -334,9 +337,9 @@ function select(rl) {
|
|
|
334
337
|
}
|
|
335
338
|
else if (str === "\r" || str === "\n") {
|
|
336
339
|
process.stdin.setRawMode(false);
|
|
337
|
-
process.stdin.
|
|
338
|
-
process.stdin.
|
|
339
|
-
process.stdout.write(`\
|
|
340
|
+
process.stdin.removeAllListeners("data");
|
|
341
|
+
process.stdin.resume();
|
|
342
|
+
process.stdout.write(`\u001b[${items.length + 1}A\u001b[0J`);
|
|
340
343
|
resolve(items[selected].value);
|
|
341
344
|
}
|
|
342
345
|
else if (str === "\u0003") {
|
|
@@ -501,6 +504,6 @@ component Card(title, color = "blue") {
|
|
|
501
504
|
`);
|
|
502
505
|
}
|
|
503
506
|
main().catch(e => {
|
|
504
|
-
process.stderr.write(`\
|
|
507
|
+
process.stderr.write(`\u001b[31m${e.message}\u001b[0m\n`);
|
|
505
508
|
process.exit(1);
|
|
506
509
|
});
|
|
@@ -80,10 +80,14 @@ function compileFile(filePath, opts) {
|
|
|
80
80
|
function collectExprVars(expr, vars) {
|
|
81
81
|
if (!expr)
|
|
82
82
|
return;
|
|
83
|
-
if (expr.kind === "variable" && expr.name && !declaredFuncs.has(expr.name))
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
83
|
+
if (expr.kind === "variable" && expr.name && !declaredFuncs.has(expr.name)) {
|
|
84
|
+
const rootVar = expr.name.split('.')[0];
|
|
85
|
+
vars.add(rootVar);
|
|
86
|
+
}
|
|
87
|
+
if (expr.kind === "assign" && expr.target) {
|
|
88
|
+
const rootVar = expr.target.split('.')[0];
|
|
89
|
+
vars.add(rootVar);
|
|
90
|
+
}
|
|
87
91
|
if (expr.left)
|
|
88
92
|
collectExprVars(expr.left, vars);
|
|
89
93
|
if (expr.right)
|
|
@@ -122,7 +126,9 @@ function compileFile(filePath, opts) {
|
|
|
122
126
|
: [JSON.parse(v.substring(8))];
|
|
123
127
|
exprs.forEach(e => collectExprVars(e, vars));
|
|
124
128
|
}
|
|
125
|
-
catch {
|
|
129
|
+
catch {
|
|
130
|
+
void 0;
|
|
131
|
+
}
|
|
126
132
|
}
|
|
127
133
|
}
|
|
128
134
|
});
|
|
@@ -155,7 +161,7 @@ function compileFile(filePath, opts) {
|
|
|
155
161
|
for (const v of pageVars) {
|
|
156
162
|
if (pageLoopVars.has(v))
|
|
157
163
|
continue;
|
|
158
|
-
const rootVar = v
|
|
164
|
+
const rootVar = v;
|
|
159
165
|
if (declaredVars.has(rootVar) || fnLocalVarNames.has(rootVar))
|
|
160
166
|
continue;
|
|
161
167
|
let foundInFn = false;
|
|
@@ -239,7 +245,7 @@ function compileFile(filePath, opts) {
|
|
|
239
245
|
for (const v of fnBodyVars) {
|
|
240
246
|
if (fnLoopVars.has(v))
|
|
241
247
|
continue;
|
|
242
|
-
const rootVar = v
|
|
248
|
+
const rootVar = v;
|
|
243
249
|
if (!fnParamNames.has(rootVar) && !declaredVars.has(rootVar) && !declaredFuncs.has(rootVar) && !fnLocalDeclared.has(rootVar)) {
|
|
244
250
|
throw new types_1.SinthError(`Variable '${rootVar}' used in function '${fn.name}' is not declared. It must be a parameter or a page-level variable.`);
|
|
245
251
|
}
|
|
@@ -248,6 +254,9 @@ function compileFile(filePath, opts) {
|
|
|
248
254
|
checkTypeInChild(child, allVarDecls, functionDefs);
|
|
249
255
|
}
|
|
250
256
|
}
|
|
257
|
+
for (const vd of pageVarDecls) {
|
|
258
|
+
checkVarInitType(vd, allVarDecls, functionDefs);
|
|
259
|
+
}
|
|
251
260
|
for (const use of file.uses) {
|
|
252
261
|
checkTypeInChild(use, allVarDecls, functionDefs);
|
|
253
262
|
}
|
|
@@ -256,7 +265,7 @@ function compileFile(filePath, opts) {
|
|
|
256
265
|
if (expr.value.kind === "str")
|
|
257
266
|
return "str";
|
|
258
267
|
if (expr.value.kind === "num")
|
|
259
|
-
return "
|
|
268
|
+
return "num";
|
|
260
269
|
if (expr.value.kind === "bool")
|
|
261
270
|
return "bool";
|
|
262
271
|
return null;
|
|
@@ -273,12 +282,78 @@ function compileFile(filePath, opts) {
|
|
|
273
282
|
return fd.returnType;
|
|
274
283
|
return null;
|
|
275
284
|
}
|
|
276
|
-
if (expr.kind === "binary"
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
285
|
+
if (expr.kind === "binary") {
|
|
286
|
+
const leftType = inferType(expr.left, varDecls, functionDefs);
|
|
287
|
+
const rightType = inferType(expr.right, varDecls, functionDefs);
|
|
288
|
+
const isNum = (t) => t === "int" || t === "num";
|
|
289
|
+
if (leftType && rightType && isNum(leftType) && isNum(rightType)) {
|
|
290
|
+
return "num";
|
|
291
|
+
}
|
|
292
|
+
if (expr.op === "+") {
|
|
293
|
+
return "str";
|
|
294
|
+
}
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
if (expr.kind === "unary") {
|
|
298
|
+
if (expr.op === "not")
|
|
299
|
+
return "bool";
|
|
300
|
+
if (expr.op === "-") {
|
|
301
|
+
const operandType = inferType(expr.operand, varDecls, functionDefs);
|
|
302
|
+
if (operandType === "int" || operandType === "num")
|
|
303
|
+
return "num";
|
|
304
|
+
return null;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
280
307
|
return null;
|
|
281
308
|
}
|
|
309
|
+
function checkVarInitType(vd, varDecls, functionDefs) {
|
|
310
|
+
if (!vd.value)
|
|
311
|
+
return;
|
|
312
|
+
if (vd.value.kind === "num") {
|
|
313
|
+
if (vd.varType !== "int" && vd.varType !== "num")
|
|
314
|
+
throw new types_1.SinthError(`Cannot assign number to variable '${vd.name}' of type '${vd.varType}'`, vd.loc);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
if (vd.value.kind === "bool") {
|
|
318
|
+
if (vd.varType !== "bool")
|
|
319
|
+
throw new types_1.SinthError(`Cannot assign boolean to variable '${vd.name}' of type '${vd.varType}'`, vd.loc);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
if (vd.value.kind !== "str")
|
|
323
|
+
return;
|
|
324
|
+
const raw = vd.value.value;
|
|
325
|
+
if (raw.startsWith("__EXPR__")) {
|
|
326
|
+
let expr;
|
|
327
|
+
try {
|
|
328
|
+
expr = JSON.parse(raw.substring(8));
|
|
329
|
+
}
|
|
330
|
+
catch {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
const exprType = inferType(expr, varDecls, functionDefs);
|
|
334
|
+
if (exprType && exprType !== vd.varType) {
|
|
335
|
+
throw new types_1.SinthError(`Cannot assign expression of type '${exprType}' to variable '${vd.name}' of type '${vd.varType}'`, vd.loc);
|
|
336
|
+
}
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
if (raw.startsWith("__VAR__")) {
|
|
340
|
+
const varName = raw.substring(7);
|
|
341
|
+
const exprType = inferType({ kind: "variable", name: varName }, varDecls, functionDefs);
|
|
342
|
+
if (exprType && exprType !== vd.varType) {
|
|
343
|
+
throw new types_1.SinthError(`Cannot assign variable '${varName}' of type '${exprType}' to variable '${vd.name}' of type '${vd.varType}'`, vd.loc);
|
|
344
|
+
}
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (raw.startsWith("__ARR__")) {
|
|
348
|
+
if (vd.varType !== "str[]") {
|
|
349
|
+
throw new types_1.SinthError(`Cannot assign array literal to variable '${vd.name}' of type '${vd.varType}'`, vd.loc);
|
|
350
|
+
}
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
if (vd.varType !== "str" && vd.varType !== "str[]") {
|
|
354
|
+
throw new types_1.SinthError(`Cannot assign string value to variable '${vd.name}' of type '${vd.varType}'`, vd.loc);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
282
357
|
function checkCallArgs(fnDef, args, loc, varDecls, functionDefs) {
|
|
283
358
|
for (let i = 0; i < fnDef.params.length && i < args.length; i++) {
|
|
284
359
|
const param = fnDef.params[i];
|
|
@@ -286,8 +361,8 @@ function compileFile(filePath, opts) {
|
|
|
286
361
|
if (!param.paramType)
|
|
287
362
|
continue;
|
|
288
363
|
const argType = inferType(arg, varDecls, functionDefs);
|
|
289
|
-
if (argType && argType !== param.paramType) {
|
|
290
|
-
if (param.paramType === "int" && argType === "str" && arg.kind === "literal" && arg.value?.kind === "str") {
|
|
364
|
+
if (argType && argType !== param.paramType && !(param.paramType === "num" && argType === "int") && !(param.paramType === "int" && argType === "num")) {
|
|
365
|
+
if ((param.paramType === "int" || param.paramType === "num") && argType === "str" && arg.kind === "literal" && arg.value?.kind === "str") {
|
|
291
366
|
if (!isNaN(Number(arg.value.value)))
|
|
292
367
|
continue;
|
|
293
368
|
}
|
|
@@ -295,36 +370,94 @@ function compileFile(filePath, opts) {
|
|
|
295
370
|
}
|
|
296
371
|
}
|
|
297
372
|
}
|
|
298
|
-
function checkTypeInExpr(expr, varDecls, functionDefs) {
|
|
373
|
+
function checkTypeInExpr(expr, varDecls, functionDefs, loc) {
|
|
299
374
|
if (!expr)
|
|
300
375
|
return;
|
|
376
|
+
if (expr.kind === "assign" && expr.target) {
|
|
377
|
+
const parts = expr.target.split('.');
|
|
378
|
+
const rootVar = parts[0];
|
|
379
|
+
const vd = varDecls.find(v => v.name === rootVar);
|
|
380
|
+
if (vd) {
|
|
381
|
+
let expected = vd.varType;
|
|
382
|
+
if (parts.length > 1) {
|
|
383
|
+
if (expected === "obj")
|
|
384
|
+
expected = "str";
|
|
385
|
+
else if (expected === "str[]" && parts[1] === "length")
|
|
386
|
+
expected = "num";
|
|
387
|
+
}
|
|
388
|
+
const rhsType = inferType(expr.right, varDecls, functionDefs);
|
|
389
|
+
if (rhsType && expected !== rhsType) {
|
|
390
|
+
if (expr.op === "+=" && expected === "str") { /* allow string += any */ }
|
|
391
|
+
else if (expr.op === "-=") {
|
|
392
|
+
if (expected !== "int" && expected !== "num") {
|
|
393
|
+
throw new types_1.SinthError(`Cannot use '-=' on non‑numeric variable '${expr.target}'`, loc || { file: "", line: 0, col: 0 });
|
|
394
|
+
}
|
|
395
|
+
else if (rhsType !== "int" && rhsType !== "num") {
|
|
396
|
+
throw new types_1.SinthError(`Right operand of '-=' must be int or num, got '${rhsType}'`, loc || { file: "", line: 0, col: 0 });
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
else {
|
|
400
|
+
throw new types_1.SinthError(`Cannot assign value of type '${rhsType}' to variable '${expr.target}' of type '${expected}'`, loc || { file: "", line: 0, col: 0 });
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
if (expr.kind === "binary") {
|
|
406
|
+
const leftType = inferType(expr.left, varDecls, functionDefs);
|
|
407
|
+
const rightType = inferType(expr.right, varDecls, functionDefs);
|
|
408
|
+
if (leftType && rightType) {
|
|
409
|
+
const op = expr.op;
|
|
410
|
+
if (["==", "!=", "<", ">", "<=", ">="].includes(op)) {
|
|
411
|
+
if (leftType !== rightType) {
|
|
412
|
+
throw new types_1.SinthError(`Cannot compare values of type '${leftType}' and '${rightType}' with '${op}'`, loc || { file: "", line: 0, col: 0 });
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
else if (["and", "or"].includes(op)) {
|
|
416
|
+
if (leftType !== "bool")
|
|
417
|
+
throw new types_1.SinthError(`Left side of '${op}' must be bool, got '${leftType}'`, loc || { file: "", line: 0, col: 0 });
|
|
418
|
+
if (rightType !== "bool")
|
|
419
|
+
throw new types_1.SinthError(`Right side of '${op}' must be bool, got '${rightType}'`, loc || { file: "", line: 0, col: 0 });
|
|
420
|
+
}
|
|
421
|
+
else if (["-", "*", "/", "%"].includes(op)) {
|
|
422
|
+
if ((leftType !== "int" && leftType !== "num") || (rightType !== "int" && rightType !== "num")) {
|
|
423
|
+
throw new types_1.SinthError(`Arithmetic operator '${op}' requires int or num operands, got '${leftType}' and '${rightType}'`, loc || { file: "", line: 0, col: 0 });
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
}
|
|
428
|
+
if (expr.kind === "unary" && expr.op === "not") {
|
|
429
|
+
const operandType = inferType(expr.operand, varDecls, functionDefs);
|
|
430
|
+
if (operandType && operandType !== "bool") {
|
|
431
|
+
throw new types_1.SinthError(`'not' operator requires bool operand, got '${operandType}'`, loc || { file: "", line: 0, col: 0 });
|
|
432
|
+
}
|
|
433
|
+
}
|
|
301
434
|
if (expr.kind === "call" && expr.callee?.kind === "variable" && expr.callee.name) {
|
|
302
435
|
const fnDef = functionDefs.find(f => f.name === expr.callee.name);
|
|
303
436
|
if (fnDef)
|
|
304
|
-
checkCallArgs(fnDef, expr.args ?? [], fnDef.loc, varDecls, functionDefs);
|
|
437
|
+
checkCallArgs(fnDef, expr.args ?? [], loc || fnDef.loc, varDecls, functionDefs);
|
|
305
438
|
}
|
|
306
439
|
if (expr.left)
|
|
307
|
-
checkTypeInExpr(expr.left, varDecls, functionDefs);
|
|
440
|
+
checkTypeInExpr(expr.left, varDecls, functionDefs, loc);
|
|
308
441
|
if (expr.right)
|
|
309
|
-
checkTypeInExpr(expr.right, varDecls, functionDefs);
|
|
442
|
+
checkTypeInExpr(expr.right, varDecls, functionDefs, loc);
|
|
310
443
|
if (expr.operand)
|
|
311
|
-
checkTypeInExpr(expr.operand, varDecls, functionDefs);
|
|
444
|
+
checkTypeInExpr(expr.operand, varDecls, functionDefs, loc);
|
|
312
445
|
if (expr.args)
|
|
313
|
-
expr.args.forEach(a => checkTypeInExpr(a, varDecls, functionDefs));
|
|
446
|
+
expr.args.forEach(a => checkTypeInExpr(a, varDecls, functionDefs, loc));
|
|
314
447
|
}
|
|
315
448
|
function checkTypeInChild(child, varDecls, functionDefs) {
|
|
316
449
|
if (child.kind === "expr" && child.expression) {
|
|
317
|
-
checkTypeInExpr(child.expression, varDecls, functionDefs);
|
|
450
|
+
checkTypeInExpr(child.expression, varDecls, functionDefs, child.loc);
|
|
318
451
|
}
|
|
319
452
|
if (child.kind === "assign_stmt" && child.expression) {
|
|
320
|
-
checkTypeInExpr(child.expression, varDecls, functionDefs);
|
|
453
|
+
checkTypeInExpr(child.expression, varDecls, functionDefs, child.loc);
|
|
321
454
|
}
|
|
322
455
|
if (child.kind === "return" && child.expression) {
|
|
323
456
|
checkTypeInExpr(child.expression, varDecls, functionDefs);
|
|
324
457
|
}
|
|
325
458
|
if (child.kind === "if") {
|
|
326
459
|
const ib = child;
|
|
327
|
-
checkTypeInExpr(ib.condition, varDecls, functionDefs);
|
|
460
|
+
checkTypeInExpr(ib.condition, varDecls, functionDefs, ib.loc);
|
|
328
461
|
ib.body.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
|
|
329
462
|
ib.elseBody?.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
|
|
330
463
|
}
|
|
@@ -337,9 +470,11 @@ function compileFile(filePath, opts) {
|
|
|
337
470
|
if (a.value?.kind === "str" && a.value.value.startsWith("__EXPR__")) {
|
|
338
471
|
try {
|
|
339
472
|
const expr = JSON.parse(a.value.value.substring(8));
|
|
340
|
-
checkTypeInExpr(expr, varDecls, functionDefs);
|
|
473
|
+
checkTypeInExpr(expr, varDecls, functionDefs, a.loc || u.loc);
|
|
474
|
+
}
|
|
475
|
+
catch {
|
|
476
|
+
void 0;
|
|
341
477
|
}
|
|
342
|
-
catch { }
|
|
343
478
|
}
|
|
344
479
|
});
|
|
345
480
|
u.children.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
|
|
@@ -347,15 +482,19 @@ function compileFile(filePath, opts) {
|
|
|
347
482
|
}
|
|
348
483
|
const enableDiffing = file.meta.some(m => m.key === "domdiffing" &&
|
|
349
484
|
(m.value.kind === "bool" ? m.value.value === true : (m.value.kind === "str" && m.value.value === "true")));
|
|
485
|
+
const REACTIVE = /^if\b|^\{|^[a-zA-Z_$][a-zA-Z0-9_$]*\s*(?:[+\-*/%]?=|<<=|>>=|>>>=|&&=|\|\|=|\?\?=)/;
|
|
350
486
|
const topLevelLogicIfs = [];
|
|
351
487
|
file.scripts = file.scripts.filter(s => {
|
|
352
488
|
const trimmed = s.raw.trim();
|
|
353
|
-
if (
|
|
489
|
+
if (REACTIVE.test(trimmed)) {
|
|
354
490
|
topLevelLogicIfs.push(trimmed);
|
|
355
491
|
return false;
|
|
356
492
|
}
|
|
357
493
|
return true;
|
|
358
494
|
});
|
|
495
|
+
if (file.initLogic) {
|
|
496
|
+
file.initLogic = file.initLogic.filter(s => !REACTIVE.test(s.trim()));
|
|
497
|
+
}
|
|
359
498
|
const ctx = {
|
|
360
499
|
allDefs, functionDefs, customEls, cssLinks, jsLinks,
|
|
361
500
|
scopeHash: hash,
|
|
@@ -381,14 +520,24 @@ function compileFile(filePath, opts) {
|
|
|
381
520
|
return { html: h };
|
|
382
521
|
}
|
|
383
522
|
const headData = (0, head_builder_1.buildHeadData)(file.meta);
|
|
384
|
-
const
|
|
523
|
+
const flatUses = [];
|
|
524
|
+
function flattenUses(child) {
|
|
525
|
+
if (child.kind === "use" && child.name === "__IF_ROOT__") {
|
|
526
|
+
child.children.forEach(flattenUses);
|
|
527
|
+
}
|
|
528
|
+
else {
|
|
529
|
+
flatUses.push(child);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
file.uses.forEach(flattenUses);
|
|
533
|
+
const bodyHTML = flatUses.map(u => (0, compiler_1.renderChild)(u, ctx, new Map(), 0)).join("\n");
|
|
385
534
|
const pageCSS = file.styles.map(s => (0, style_processor_1.processStyleBlock)(s, hash, new Map())).join("\n");
|
|
386
535
|
const scopedCSS = [pageCSS, ...ctx.extraCSS].filter(c => c.trim()).join("\n");
|
|
387
536
|
const { componentScripts, pageScripts } = (0, compiler_1.collectScripts)(file, allDefs);
|
|
388
537
|
// collect all assigned variables for default-value warnings
|
|
389
538
|
const assignedVars = new Set();
|
|
390
539
|
for (const s of file.scripts) {
|
|
391
|
-
for (const m of s.raw.matchAll(/\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?:[
|
|
540
|
+
for (const m of s.raw.matchAll(/\b([a-zA-Z_][a-zA-Z0-9_]*)\s*(?:[+-]?=)/g))
|
|
392
541
|
assignedVars.add(m[1]);
|
|
393
542
|
}
|
|
394
543
|
for (const v of file.varDecls) {
|
|
@@ -424,6 +573,7 @@ function compileFile(filePath, opts) {
|
|
|
424
573
|
varDecls: pageVarDecls,
|
|
425
574
|
bodyHTML,
|
|
426
575
|
logicBlocks: ctx.logicBlocks,
|
|
576
|
+
initBlocks: ctx.initBlocks,
|
|
427
577
|
mixedBlocks: ctx.mixedBlocks,
|
|
428
578
|
assignedVars,
|
|
429
579
|
exprRegistry: ctx.exprRegistry,
|