@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/dist/core/parser.js
CHANGED
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Parser = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
|
-
const lexer_1 = require("./lexer");
|
|
6
5
|
const expr_1 = require("./expr");
|
|
6
|
+
const lexer_1 = require("./lexer");
|
|
7
7
|
class Parser {
|
|
8
8
|
constructor(tokens, file) {
|
|
9
9
|
this.tokens = tokens;
|
|
@@ -126,7 +126,9 @@ class Parser {
|
|
|
126
126
|
}
|
|
127
127
|
}
|
|
128
128
|
this.consume(types_1.TT.RPAREN);
|
|
129
|
-
const
|
|
129
|
+
const isMemo = identName.startsWith("$");
|
|
130
|
+
const realName = isMemo ? identName.substring(1) : identName;
|
|
131
|
+
const expr = { kind: "call", callee: { kind: "variable", name: realName }, args, memo: isMemo || undefined };
|
|
130
132
|
uses.push({ kind: "use", name: "__IF_ROOT__", attrs: [], children: [{ kind: "expr", expression: expr, loc: this.peek().loc }], loc: this.peek().loc });
|
|
131
133
|
}
|
|
132
134
|
else {
|
|
@@ -140,12 +142,11 @@ class Parser {
|
|
|
140
142
|
this._visited.clear();
|
|
141
143
|
return { filePath: this.file, isPage, imports, meta, defs, functions, uses, styles, scripts, customEls, varDecls, initLogic };
|
|
142
144
|
}
|
|
143
|
-
flattenIfToUses(ifNode,
|
|
145
|
+
flattenIfToUses(ifNode, _scripts, _initLogic) {
|
|
144
146
|
const bodyHasComp = ifNode.body.some(c => c.kind === "use");
|
|
145
147
|
const elseHasComp = (ifNode.elseBody ?? []).some(c => c.kind === "use");
|
|
146
148
|
if (!bodyHasComp && !elseHasComp) {
|
|
147
|
-
|
|
148
|
-
return [];
|
|
149
|
+
return [{ kind: "use", name: "__IF_ROOT__", attrs: [], children: [ifNode], loc: ifNode.loc }];
|
|
149
150
|
}
|
|
150
151
|
const syntheticUse = {
|
|
151
152
|
kind: "use",
|
|
@@ -200,8 +201,14 @@ class Parser {
|
|
|
200
201
|
this.consume(types_1.TT.RBRACKET);
|
|
201
202
|
typeStr = "str[]";
|
|
202
203
|
}
|
|
203
|
-
if (!["int", "str", "bool", "str[]", "obj", "ui"].includes(typeStr)) {
|
|
204
|
-
throw new types_1.SinthError(`Unknown type '${typeStr}'. Expected: int, str, bool, str[], obj, component`, typeTok.loc);
|
|
204
|
+
if (!["int", "num", "str", "bool", "str[]", "obj", "ui"].includes(typeStr)) {
|
|
205
|
+
throw new types_1.SinthError(`Unknown type '${typeStr}'. Expected: int, num, str, bool, str[], obj, component`, typeTok.loc);
|
|
206
|
+
}
|
|
207
|
+
if (typeStr === "int") {
|
|
208
|
+
types_1.SinthWarning.emit(`\x1b[33mvar int\x1b[0m \x1b[36m${this.peek().value}\x1b[0m is deprecated, use \x1b[32mvar num\x1b[0m \x1b[36m${this.peek().value}\x1b[0m instead. '\x1b[33mvar int\x1b[0m' won't work in the next major version. Highly consider renaming.`, typeTok.loc);
|
|
209
|
+
}
|
|
210
|
+
if (typeStr === "int") {
|
|
211
|
+
types_1.SinthWarning.emit(`\x1b[33mvar int\x1b[0m \x1b[36m${this.peek().value}\x1b[0m is deprecated, use \x1b[32mvar num\x1b[0m \x1b[36m${this.peek().value}\x1b[0m instead. '\x1b[33mvar int\x1b[0m' won't work in the next major version. Highly consider renaming.`, typeTok.loc);
|
|
205
212
|
}
|
|
206
213
|
const nameTok = this.consume(types_1.TT.IDENT);
|
|
207
214
|
let val = null;
|
|
@@ -210,23 +217,25 @@ class Parser {
|
|
|
210
217
|
if (this.check(types_1.TT.LBRACKET)) {
|
|
211
218
|
val = this.parseArrayLiteral();
|
|
212
219
|
}
|
|
213
|
-
else
|
|
220
|
+
else {
|
|
214
221
|
const savedPos = this.pos;
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
222
|
+
if (this.check(types_1.TT.IDENT) || this.check(types_1.TT.LPAREN) || this.check(types_1.TT.OP_MINUS) || this.check(types_1.TT.OP_NOT)) {
|
|
223
|
+
const expr = this.parseExpression();
|
|
224
|
+
if (expr && (expr.kind !== "literal" || (expr.kind === "literal" && expr.value?.kind === "str" && expr.value.value.startsWith("__VAR__")))) {
|
|
225
|
+
val = { kind: "str", value: `__EXPR__${JSON.stringify(expr)}` };
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
this.pos = savedPos;
|
|
229
|
+
val = this.parseLiteral();
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
else if (this.check(types_1.TT.NUMBER)) {
|
|
233
|
+
val = this.parseLiteral();
|
|
218
234
|
}
|
|
219
235
|
else {
|
|
220
|
-
this.pos = savedPos;
|
|
221
236
|
val = this.parseLiteral();
|
|
222
237
|
}
|
|
223
238
|
}
|
|
224
|
-
else if (this.check(types_1.TT.NUMBER)) {
|
|
225
|
-
val = this.parseLiteral();
|
|
226
|
-
}
|
|
227
|
-
else {
|
|
228
|
-
val = this.parseLiteral();
|
|
229
|
-
}
|
|
230
239
|
}
|
|
231
240
|
return { kind: "var", name: nameTok.value, varType: typeStr, value: val, loc };
|
|
232
241
|
}
|
|
@@ -303,19 +312,43 @@ class Parser {
|
|
|
303
312
|
if (this._visited.has(this.pos))
|
|
304
313
|
throw new types_1.SinthError("Circular expression reference", this.peek().loc);
|
|
305
314
|
this._visited.add(this.pos);
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
if (tok.type === types_1.TT.IDENT)
|
|
310
|
-
this.consume(types_1.TT.IDENT);
|
|
311
|
-
else
|
|
312
|
-
this.consume(types_1.TT.OP_NOT);
|
|
313
|
-
const operand = this.parseExpression();
|
|
314
|
-
if (!operand)
|
|
315
|
-
throw new types_1.SinthError("Expected expression after 'not'", tok.loc);
|
|
316
|
-
const unaryExpr = { kind: "unary", op: "not", operand };
|
|
317
|
-
return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(unaryExpr)));
|
|
315
|
+
if (this.peek().type === types_1.TT.KW_REMOVE) {
|
|
316
|
+
const name = this.consume(types_1.TT.KW_REMOVE).value;
|
|
317
|
+
return this.parseBinaryRHS(this.parseCallExpr({ kind: "variable", name }));
|
|
318
318
|
}
|
|
319
|
+
if (this.check(types_1.TT.IDENT)) {
|
|
320
|
+
const savedPos = this.pos;
|
|
321
|
+
const primaryExpr = this.parsePrimary();
|
|
322
|
+
if (this.check(types_1.TT.EQUALS) ||
|
|
323
|
+
(this.check(types_1.TT.OP_PLUS) && this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS) ||
|
|
324
|
+
(this.check(types_1.TT.OP_MINUS) && this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS)) {
|
|
325
|
+
let op = "=";
|
|
326
|
+
if (this.check(types_1.TT.OP_PLUS)) {
|
|
327
|
+
this.consume(types_1.TT.OP_PLUS);
|
|
328
|
+
op = "+=";
|
|
329
|
+
}
|
|
330
|
+
else if (this.check(types_1.TT.OP_MINUS)) {
|
|
331
|
+
this.consume(types_1.TT.OP_MINUS);
|
|
332
|
+
op = "-=";
|
|
333
|
+
}
|
|
334
|
+
this.consume(types_1.TT.EQUALS);
|
|
335
|
+
const rhs = this.parseExpression();
|
|
336
|
+
if (!rhs)
|
|
337
|
+
throw new types_1.SinthError("Expected expression after assignment", this.peek().loc);
|
|
338
|
+
if (primaryExpr.kind !== "variable" || !primaryExpr.name) {
|
|
339
|
+
throw new types_1.SinthError("Invalid assignment target", this.peek().loc);
|
|
340
|
+
}
|
|
341
|
+
return { kind: "assign", target: primaryExpr.name, op, right: rhs };
|
|
342
|
+
}
|
|
343
|
+
this.pos = savedPos;
|
|
344
|
+
}
|
|
345
|
+
const result = this.parseUnary();
|
|
346
|
+
if (skipBinary)
|
|
347
|
+
return result;
|
|
348
|
+
return this.parseBinaryRHS(result);
|
|
349
|
+
}
|
|
350
|
+
parsePrimary() {
|
|
351
|
+
const tok = this.peek();
|
|
319
352
|
if (this.check(types_1.TT.LPAREN)) {
|
|
320
353
|
this.consume(types_1.TT.LPAREN);
|
|
321
354
|
const inner = this.parseExpression();
|
|
@@ -324,195 +357,147 @@ class Parser {
|
|
|
324
357
|
if (!this.check(types_1.TT.RPAREN))
|
|
325
358
|
throw new types_1.SinthError("Expected closing ')'", this.peek().loc);
|
|
326
359
|
this.consume(types_1.TT.RPAREN);
|
|
327
|
-
|
|
328
|
-
return skipBinary ? result : this.parseBinaryRHS(result);
|
|
360
|
+
return inner;
|
|
329
361
|
}
|
|
330
|
-
if (this.check(types_1.TT.
|
|
362
|
+
if (this.check(types_1.TT.NUMBER) || this.check(types_1.TT.STRING) || this.check(types_1.TT.BOOL_TRUE) ||
|
|
331
363
|
this.check(types_1.TT.BOOL_FALSE) || this.check(types_1.TT.NULL_LIT) || this.check(types_1.TT.LBRACE) || this.check(types_1.TT.LBRACKET)) {
|
|
332
364
|
const lit = this.parseLiteral();
|
|
333
|
-
|
|
334
|
-
return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(base)));
|
|
335
|
-
}
|
|
336
|
-
// remove as function
|
|
337
|
-
if (tok.type === types_1.TT.KW_REMOVE) {
|
|
338
|
-
const name = this.consume(types_1.TT.KW_REMOVE).value;
|
|
339
|
-
return this.parseBinaryRHS(this.parseCallExpr({ kind: "variable", name }));
|
|
365
|
+
return { kind: "literal", value: lit };
|
|
340
366
|
}
|
|
341
|
-
// var or assignment
|
|
342
367
|
if (tok.type === types_1.TT.IDENT) {
|
|
343
|
-
let memo = false;
|
|
344
368
|
let rawName = this.consume(types_1.TT.IDENT).value;
|
|
369
|
+
let memo = false;
|
|
345
370
|
if (rawName.startsWith("$")) {
|
|
346
371
|
memo = true;
|
|
347
372
|
rawName = rawName.substring(1);
|
|
348
373
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
// dot notation -> e.g. user.age
|
|
352
|
-
if (this.check(types_1.TT.DOT)) {
|
|
374
|
+
let name = rawName;
|
|
375
|
+
while (this.check(types_1.TT.DOT)) {
|
|
353
376
|
this.consume(types_1.TT.DOT);
|
|
354
|
-
|
|
355
|
-
const fullName = `${name}.${propName}`;
|
|
356
|
-
varExpr = { kind: "variable", name: fullName };
|
|
357
|
-
}
|
|
358
|
-
else {
|
|
359
|
-
varExpr = { kind: "variable", name };
|
|
360
|
-
}
|
|
361
|
-
// apply bracket postfixes (e.g. data[key], data["x"])
|
|
362
|
-
const indexedExpr = this.parsePostfix(varExpr);
|
|
363
|
-
// if indexing was applied, we don't allow assignment, just binary RHS (after possible call)
|
|
364
|
-
if (indexedExpr !== varExpr) {
|
|
365
|
-
return this.parseBinaryRHS(this.parseCallExpr(indexedExpr));
|
|
366
|
-
}
|
|
367
|
-
// no indexing – check for assignment operators
|
|
368
|
-
const nextType = this.tokens[this.pos]?.type;
|
|
369
|
-
if (nextType === types_1.TT.OP_PLUS && this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS) {
|
|
370
|
-
this.consume(types_1.TT.OP_PLUS);
|
|
371
|
-
this.consume(types_1.TT.EQUALS);
|
|
372
|
-
const rhs = this.parseExpression();
|
|
373
|
-
if (!rhs)
|
|
374
|
-
throw new types_1.SinthError("Expected expression after +=", this.peek().loc);
|
|
375
|
-
return { kind: "assign", target: varExpr.name, op: "+=", right: rhs };
|
|
376
|
-
}
|
|
377
|
-
if (nextType === types_1.TT.OP_MINUS && this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS) {
|
|
378
|
-
this.consume(types_1.TT.OP_MINUS);
|
|
379
|
-
this.consume(types_1.TT.EQUALS);
|
|
380
|
-
const rhs = this.parseExpression();
|
|
381
|
-
if (!rhs)
|
|
382
|
-
throw new types_1.SinthError("Expected expression after -=", this.peek().loc);
|
|
383
|
-
return { kind: "assign", target: varExpr.name, op: "-=", right: rhs };
|
|
377
|
+
name += "." + this.consume(types_1.TT.IDENT).value;
|
|
384
378
|
}
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
throw new types_1.SinthError("Expected expression after =", this.peek().loc);
|
|
390
|
-
return { kind: "assign", target: varExpr.name, op: "=", right: rhs };
|
|
391
|
-
}
|
|
392
|
-
return this.parseBinaryRHS(this.parseCallExpr(varExpr));
|
|
379
|
+
const expr = { kind: "variable", name };
|
|
380
|
+
if (memo)
|
|
381
|
+
expr.memo = true;
|
|
382
|
+
return expr;
|
|
393
383
|
}
|
|
394
|
-
|
|
384
|
+
throw new types_1.SinthError(`Expected expression`, this.peek().loc);
|
|
395
385
|
}
|
|
396
|
-
|
|
386
|
+
parseUnary() {
|
|
397
387
|
const tok = this.peek();
|
|
398
|
-
if (tok.type === types_1.TT.
|
|
399
|
-
const
|
|
400
|
-
|
|
388
|
+
if ((tok.type === types_1.TT.IDENT && tok.value === "not") || tok.type === types_1.TT.OP_NOT || tok.type === types_1.TT.OP_MINUS) {
|
|
389
|
+
const op = (tok.type === types_1.TT.IDENT && tok.value === "not") ? "not" : tok.type === types_1.TT.OP_MINUS ? "-" : "not";
|
|
390
|
+
if (tok.type === types_1.TT.IDENT)
|
|
391
|
+
this.consume(types_1.TT.IDENT);
|
|
392
|
+
else if (tok.type === types_1.TT.OP_NOT)
|
|
393
|
+
this.consume(types_1.TT.OP_NOT);
|
|
394
|
+
else
|
|
395
|
+
this.consume(types_1.TT.OP_MINUS);
|
|
396
|
+
const operand = this.parseUnary();
|
|
397
|
+
return { kind: "unary", op, operand };
|
|
401
398
|
}
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
if (
|
|
412
|
-
|
|
413
|
-
|
|
399
|
+
let primary = this.parsePrimary();
|
|
400
|
+
primary = this.parsePostfix(primary);
|
|
401
|
+
primary = this.parseCallExpr(primary);
|
|
402
|
+
return primary;
|
|
403
|
+
}
|
|
404
|
+
parseTerm() {
|
|
405
|
+
let left = this.parseUnary();
|
|
406
|
+
while (true) {
|
|
407
|
+
const t = this.peek().type;
|
|
408
|
+
if (t === types_1.TT.OP_STAR || t === types_1.TT.OP_SLASH || t === types_1.TT.OP_MOD) {
|
|
409
|
+
const op = t === types_1.TT.OP_STAR ? "*" : t === types_1.TT.OP_SLASH ? "/" : "%";
|
|
410
|
+
this.pos++;
|
|
411
|
+
const right = this.parseUnary();
|
|
412
|
+
left = { kind: "binary", left, op, right };
|
|
414
413
|
}
|
|
415
414
|
else {
|
|
416
|
-
|
|
415
|
+
break;
|
|
417
416
|
}
|
|
418
|
-
if (memo)
|
|
419
|
-
expr.memo = true;
|
|
420
|
-
return expr;
|
|
421
|
-
}
|
|
422
|
-
if (tok.type === types_1.TT.LPAREN) {
|
|
423
|
-
this.consume(types_1.TT.LPAREN);
|
|
424
|
-
const inner = this.parseExpression();
|
|
425
|
-
this.consume(types_1.TT.RPAREN);
|
|
426
|
-
return inner;
|
|
427
417
|
}
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
418
|
+
return left;
|
|
419
|
+
}
|
|
420
|
+
parseAddition(left) {
|
|
421
|
+
while (true) {
|
|
422
|
+
const t = this.peek().type;
|
|
423
|
+
if (t === types_1.TT.OP_PLUS || t === types_1.TT.OP_MINUS) {
|
|
424
|
+
const op = t === types_1.TT.OP_PLUS ? "+" : "-";
|
|
425
|
+
this.pos++;
|
|
426
|
+
const right = this.parseTerm();
|
|
427
|
+
left = { kind: "binary", left, op, right };
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
break;
|
|
431
|
+
}
|
|
431
432
|
}
|
|
432
|
-
|
|
433
|
+
return left;
|
|
434
|
+
}
|
|
435
|
+
parseAdditionExpression() {
|
|
436
|
+
return this.parseAddition(this.parseTerm());
|
|
437
|
+
}
|
|
438
|
+
parseComparisonExpression() {
|
|
439
|
+
return this.parseComparison(this.parseAdditionExpression());
|
|
433
440
|
}
|
|
434
441
|
parseComparison(left) {
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
442
|
+
while (true) {
|
|
443
|
+
const savedPos = this.pos;
|
|
444
|
+
const t = this.peek().type;
|
|
445
|
+
let op = null;
|
|
446
|
+
if (t === types_1.TT.OP_EQEQ)
|
|
447
|
+
op = "==";
|
|
448
|
+
else if (t === types_1.TT.OP_NEQ)
|
|
449
|
+
op = "!=";
|
|
450
|
+
else if (t === types_1.TT.OP_LT)
|
|
451
|
+
op = "<";
|
|
452
|
+
else if (t === types_1.TT.OP_GT)
|
|
453
|
+
op = ">";
|
|
454
|
+
else if (t === types_1.TT.OP_LTEQ)
|
|
455
|
+
op = "<=";
|
|
456
|
+
else if (t === types_1.TT.OP_GTEQ)
|
|
457
|
+
op = ">=";
|
|
458
|
+
if (!op)
|
|
459
|
+
return left;
|
|
460
|
+
this.pos++;
|
|
461
|
+
const right = this.parseAdditionExpression();
|
|
462
|
+
if (!right) {
|
|
463
|
+
this.pos = savedPos;
|
|
464
|
+
return left;
|
|
465
|
+
}
|
|
466
|
+
left = { kind: "binary", left, op, right };
|
|
460
467
|
}
|
|
461
|
-
if (this.pos === savedPos)
|
|
462
|
-
return { kind: "binary", left, op, right };
|
|
463
|
-
return { kind: "binary", left, op, right };
|
|
464
468
|
}
|
|
465
469
|
parseLogical(left) {
|
|
466
|
-
const savedPos = this.pos;
|
|
467
|
-
const t = this.peek().type;
|
|
468
|
-
let op = null;
|
|
469
|
-
if (t === types_1.TT.IDENT && this.peek().value === "and")
|
|
470
|
-
op = "and";
|
|
471
|
-
else if (t === types_1.TT.IDENT && this.peek().value === "or")
|
|
472
|
-
op = "or";
|
|
473
|
-
if (!op)
|
|
474
|
-
return left;
|
|
475
|
-
this.pos++;
|
|
476
|
-
const right = this.parseExpression();
|
|
477
|
-
if (!right) {
|
|
478
|
-
this.pos = savedPos;
|
|
479
|
-
return left;
|
|
480
|
-
}
|
|
481
|
-
return this.parseLogical({ kind: "binary", left, op, right });
|
|
482
|
-
}
|
|
483
|
-
parseArithmetic(left) {
|
|
484
470
|
while (true) {
|
|
471
|
+
const savedPos = this.pos;
|
|
485
472
|
const t = this.peek().type;
|
|
486
473
|
let op = null;
|
|
487
|
-
if (t === types_1.TT.
|
|
488
|
-
op = "
|
|
489
|
-
else if (t === types_1.TT.
|
|
490
|
-
op = "
|
|
491
|
-
else if (t === types_1.TT.OP_STAR)
|
|
492
|
-
op = "*";
|
|
493
|
-
else if (t === types_1.TT.OP_SLASH)
|
|
494
|
-
op = "/";
|
|
495
|
-
else if (t === types_1.TT.OP_MOD)
|
|
496
|
-
op = "%";
|
|
474
|
+
if (t === types_1.TT.IDENT && this.peek().value === "and")
|
|
475
|
+
op = "and";
|
|
476
|
+
else if (t === types_1.TT.IDENT && this.peek().value === "or")
|
|
477
|
+
op = "or";
|
|
497
478
|
if (!op)
|
|
498
|
-
|
|
479
|
+
return left;
|
|
499
480
|
this.pos++;
|
|
500
|
-
const
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
throw new types_1.SinthError(`Expected expression after '${op}'`, this.peek().loc);
|
|
481
|
+
const right = this.parseComparisonExpression();
|
|
482
|
+
if (!right) {
|
|
483
|
+
this.pos = savedPos;
|
|
484
|
+
return left;
|
|
485
|
+
}
|
|
506
486
|
left = { kind: "binary", left, op, right };
|
|
507
487
|
}
|
|
508
|
-
return left;
|
|
509
488
|
}
|
|
510
489
|
parseBinaryRHS(left) {
|
|
511
|
-
left = this.
|
|
490
|
+
left = this.parseAddition(left);
|
|
512
491
|
left = this.parseComparison(left);
|
|
513
492
|
left = this.parseLogical(left);
|
|
514
493
|
return left;
|
|
515
494
|
}
|
|
495
|
+
parseConcatRHS() {
|
|
496
|
+
const rhs = this.parseExpression(true);
|
|
497
|
+
if (!rhs)
|
|
498
|
+
throw new types_1.SinthError("Expected expression after +", this.peek().loc);
|
|
499
|
+
return rhs;
|
|
500
|
+
}
|
|
516
501
|
parsePostfix(base) {
|
|
517
502
|
let expr = base;
|
|
518
503
|
while (this.check(types_1.TT.LBRACKET)) {
|
|
@@ -555,8 +540,8 @@ class Parser {
|
|
|
555
540
|
if (this.check(types_1.TT.OP_ARROW)) {
|
|
556
541
|
this.consume(types_1.TT.OP_ARROW);
|
|
557
542
|
const typeTok = this.consume(types_1.TT.IDENT);
|
|
558
|
-
if (!["int", "str", "bool", "str[]", "obj", "ui"].includes(typeTok.value)) {
|
|
559
|
-
throw new types_1.SinthError(`Unknown return type '${typeTok.value}'. Expected:
|
|
543
|
+
if (!["int", "num", "str", "bool", "str[]", "obj", "ui"].includes(typeTok.value)) {
|
|
544
|
+
throw new types_1.SinthError(`Unknown return type '${typeTok.value}'. Expected: num, str, bool, str[], obj, ui`, typeTok.loc);
|
|
560
545
|
}
|
|
561
546
|
returnType = typeTok.value;
|
|
562
547
|
}
|
|
@@ -665,9 +650,13 @@ class Parser {
|
|
|
665
650
|
const ploc = this.peek().loc;
|
|
666
651
|
let pname;
|
|
667
652
|
let paramType;
|
|
668
|
-
if (this.check(types_1.TT.IDENT) && ["int", "str", "bool", "str[]", "obj"].includes(this.peek().value) &&
|
|
653
|
+
if (this.check(types_1.TT.IDENT) && ["int", "num", "str", "bool", "str[]", "obj"].includes(this.peek().value) &&
|
|
669
654
|
this.tokens[this.pos + 1]?.type === types_1.TT.IDENT) {
|
|
670
|
-
|
|
655
|
+
const pTypeTok = this.consume(types_1.TT.IDENT);
|
|
656
|
+
paramType = pTypeTok.value;
|
|
657
|
+
if (paramType === "int") {
|
|
658
|
+
types_1.SinthWarning.emit(`\x1b[33mvar int\x1b[0m \x1b[36m${this.peek().value}\x1b[0m is deprecated, use \x1b[32mvar num\x1b[0m \x1b[36m${this.peek().value}\x1b[0m instead. '\x1b[33mvar int\x1b[0m' won't work in the next major version. Highly consider renaming.`, pTypeTok.loc);
|
|
659
|
+
}
|
|
671
660
|
pname = this.consume(types_1.TT.IDENT).value;
|
|
672
661
|
}
|
|
673
662
|
else {
|
|
@@ -895,7 +884,7 @@ class Parser {
|
|
|
895
884
|
let leftExpr = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
|
|
896
885
|
while (this.check(types_1.TT.OP_PLUS)) {
|
|
897
886
|
this.consume(types_1.TT.OP_PLUS);
|
|
898
|
-
let rhs
|
|
887
|
+
let rhs;
|
|
899
888
|
if (this.check(types_1.TT.STRING)) {
|
|
900
889
|
rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
|
|
901
890
|
}
|
|
@@ -904,10 +893,8 @@ class Parser {
|
|
|
904
893
|
rhs = { kind: "variable", name: rhsName };
|
|
905
894
|
}
|
|
906
895
|
else {
|
|
907
|
-
rhs = this.
|
|
896
|
+
rhs = this.parseConcatRHS();
|
|
908
897
|
}
|
|
909
|
-
if (!rhs)
|
|
910
|
-
throw new types_1.SinthError("Expected expression after +", this.peek().loc);
|
|
911
898
|
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
912
899
|
}
|
|
913
900
|
children.push({ kind: "expr", expression: leftExpr, loc });
|
|
@@ -1182,7 +1169,8 @@ class Parser {
|
|
|
1182
1169
|
}
|
|
1183
1170
|
const hasIf = actions.some(a => a.kind === "if");
|
|
1184
1171
|
if (actions.length === 1 && !hasIf) {
|
|
1185
|
-
const
|
|
1172
|
+
const firstAction = actions[0];
|
|
1173
|
+
const single = firstAction.kind === "expr" ? firstAction.expr : { kind: "literal", value: { kind: "str", value: firstAction.js } };
|
|
1186
1174
|
value = { kind: "str", value: "__EXPR__" + JSON.stringify(single) };
|
|
1187
1175
|
}
|
|
1188
1176
|
else {
|
|
@@ -1272,7 +1260,7 @@ class Parser {
|
|
|
1272
1260
|
this.consume(types_1.TT.RPAREN);
|
|
1273
1261
|
while (this.check(types_1.TT.OP_PLUS)) {
|
|
1274
1262
|
this.consume(types_1.TT.OP_PLUS);
|
|
1275
|
-
let rhs
|
|
1263
|
+
let rhs;
|
|
1276
1264
|
if (this.check(types_1.TT.STRING)) {
|
|
1277
1265
|
rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
|
|
1278
1266
|
}
|
|
@@ -1281,10 +1269,8 @@ class Parser {
|
|
|
1281
1269
|
rhs = { kind: "variable", name: rhsName };
|
|
1282
1270
|
}
|
|
1283
1271
|
else {
|
|
1284
|
-
rhs = this.
|
|
1272
|
+
rhs = this.parseConcatRHS();
|
|
1285
1273
|
}
|
|
1286
|
-
if (!rhs)
|
|
1287
|
-
throw new types_1.SinthError("Expected expression after +", this.peek().loc);
|
|
1288
1274
|
expr = { kind: "binary", left: expr, op: "+", right: rhs };
|
|
1289
1275
|
}
|
|
1290
1276
|
children.push({ kind: "expr", expression: expr, loc });
|
|
@@ -1295,7 +1281,7 @@ class Parser {
|
|
|
1295
1281
|
let leftExpr = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
|
|
1296
1282
|
while (this.check(types_1.TT.OP_PLUS)) {
|
|
1297
1283
|
this.consume(types_1.TT.OP_PLUS);
|
|
1298
|
-
let rhs
|
|
1284
|
+
let rhs;
|
|
1299
1285
|
if (this.check(types_1.TT.STRING)) {
|
|
1300
1286
|
rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
|
|
1301
1287
|
}
|
|
@@ -1323,10 +1309,8 @@ class Parser {
|
|
|
1323
1309
|
}
|
|
1324
1310
|
}
|
|
1325
1311
|
else {
|
|
1326
|
-
rhs = this.
|
|
1312
|
+
rhs = this.parseConcatRHS();
|
|
1327
1313
|
}
|
|
1328
|
-
if (!rhs)
|
|
1329
|
-
throw new types_1.SinthError("Expected expression after +", this.peek().loc);
|
|
1330
1314
|
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
1331
1315
|
}
|
|
1332
1316
|
children.push({ kind: "expr", expression: leftExpr, loc });
|
|
@@ -1374,7 +1358,7 @@ class Parser {
|
|
|
1374
1358
|
continue;
|
|
1375
1359
|
}
|
|
1376
1360
|
if (nextType === types_1.TT.DOT) {
|
|
1377
|
-
|
|
1361
|
+
const rawFirst = this.consume(types_1.TT.IDENT).value;
|
|
1378
1362
|
const isMemo = rawFirst.startsWith("$");
|
|
1379
1363
|
const firstPart = isMemo ? rawFirst.slice(1) : rawFirst;
|
|
1380
1364
|
this.consume(types_1.TT.DOT);
|
|
@@ -1384,7 +1368,7 @@ class Parser {
|
|
|
1384
1368
|
let leftExpr = { kind: "variable", name: fullName };
|
|
1385
1369
|
while (this.check(types_1.TT.OP_PLUS)) {
|
|
1386
1370
|
this.consume(types_1.TT.OP_PLUS);
|
|
1387
|
-
let rhs
|
|
1371
|
+
let rhs;
|
|
1388
1372
|
if (this.check(types_1.TT.STRING)) {
|
|
1389
1373
|
rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
|
|
1390
1374
|
}
|
|
@@ -1399,10 +1383,8 @@ class Parser {
|
|
|
1399
1383
|
}
|
|
1400
1384
|
}
|
|
1401
1385
|
else {
|
|
1402
|
-
rhs = this.
|
|
1386
|
+
rhs = this.parseConcatRHS();
|
|
1403
1387
|
}
|
|
1404
|
-
if (!rhs)
|
|
1405
|
-
throw new types_1.SinthError("Expected expression after +", this.peek().loc);
|
|
1406
1388
|
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
1407
1389
|
}
|
|
1408
1390
|
children.push({ kind: "expr", expression: leftExpr, loc });
|
|
@@ -1440,7 +1422,7 @@ class Parser {
|
|
|
1440
1422
|
let leftExpr = { kind: "variable", name };
|
|
1441
1423
|
while (this.check(types_1.TT.OP_PLUS)) {
|
|
1442
1424
|
this.consume(types_1.TT.OP_PLUS);
|
|
1443
|
-
let rhs
|
|
1425
|
+
let rhs;
|
|
1444
1426
|
if (this.check(types_1.TT.STRING)) {
|
|
1445
1427
|
rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
|
|
1446
1428
|
}
|
|
@@ -1455,10 +1437,8 @@ class Parser {
|
|
|
1455
1437
|
}
|
|
1456
1438
|
}
|
|
1457
1439
|
else {
|
|
1458
|
-
rhs = this.
|
|
1440
|
+
rhs = this.parseConcatRHS();
|
|
1459
1441
|
}
|
|
1460
|
-
if (!rhs)
|
|
1461
|
-
throw new types_1.SinthError("Expected expression after +", this.peek().loc);
|
|
1462
1442
|
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
1463
1443
|
}
|
|
1464
1444
|
children.push({ kind: "expr", expression: leftExpr, loc });
|
|
@@ -1470,12 +1450,12 @@ class Parser {
|
|
|
1470
1450
|
else if (nextType === types_1.TT.LBRACKET) {
|
|
1471
1451
|
const name = this.consume(types_1.TT.IDENT).value;
|
|
1472
1452
|
const varExpr = { kind: "variable", name };
|
|
1473
|
-
|
|
1453
|
+
const indexedExpr = this.parsePostfix(varExpr);
|
|
1474
1454
|
if (this.check(types_1.TT.OP_PLUS)) {
|
|
1475
1455
|
let leftExpr = indexedExpr;
|
|
1476
1456
|
while (this.check(types_1.TT.OP_PLUS)) {
|
|
1477
1457
|
this.consume(types_1.TT.OP_PLUS);
|
|
1478
|
-
let rhs
|
|
1458
|
+
let rhs;
|
|
1479
1459
|
if (this.check(types_1.TT.STRING)) {
|
|
1480
1460
|
rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
|
|
1481
1461
|
}
|
|
@@ -1490,10 +1470,8 @@ class Parser {
|
|
|
1490
1470
|
}
|
|
1491
1471
|
}
|
|
1492
1472
|
else {
|
|
1493
|
-
rhs = this.
|
|
1473
|
+
rhs = this.parseConcatRHS();
|
|
1494
1474
|
}
|
|
1495
|
-
if (!rhs)
|
|
1496
|
-
throw new types_1.SinthError("Expected expression after +", this.peek().loc);
|
|
1497
1475
|
leftExpr = { kind: "binary", left: leftExpr, op: "+", right: rhs };
|
|
1498
1476
|
}
|
|
1499
1477
|
children.push({ kind: "expr", expression: leftExpr, loc });
|
|
@@ -1525,7 +1503,7 @@ class Parser {
|
|
|
1525
1503
|
let finalExpr = { kind: "call", callee: { kind: "variable", name: realName }, args, memo: isMemo || undefined };
|
|
1526
1504
|
while (this.check(types_1.TT.OP_PLUS)) {
|
|
1527
1505
|
this.consume(types_1.TT.OP_PLUS);
|
|
1528
|
-
let rhs
|
|
1506
|
+
let rhs;
|
|
1529
1507
|
if (this.check(types_1.TT.STRING)) {
|
|
1530
1508
|
rhs = { kind: "literal", value: { kind: "str", value: this.consume(types_1.TT.STRING).value } };
|
|
1531
1509
|
}
|
|
@@ -1533,10 +1511,8 @@ class Parser {
|
|
|
1533
1511
|
rhs = this.parseExpression();
|
|
1534
1512
|
}
|
|
1535
1513
|
else {
|
|
1536
|
-
rhs = this.
|
|
1514
|
+
rhs = this.parseConcatRHS();
|
|
1537
1515
|
}
|
|
1538
|
-
if (!rhs)
|
|
1539
|
-
throw new types_1.SinthError("Expected expression after +", this.peek().loc);
|
|
1540
1516
|
finalExpr = { kind: "binary", left: finalExpr, op: "+", right: rhs };
|
|
1541
1517
|
}
|
|
1542
1518
|
children.push({ kind: "expr", expression: finalExpr, loc });
|
|
@@ -44,7 +44,7 @@ function buildRenderBody(opts) {
|
|
|
44
44
|
renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(function(t) { var p = t.parentElement; while(p && p.tagName !== 'TEMPLATE') p = p.parentElement; if (!p || (!p.hasAttribute('data-sinth-for') && !p.hasAttribute('data-sinth-for-expr'))) sinthIfBlock(t); });\n`;
|
|
45
45
|
}
|
|
46
46
|
renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
|
|
47
|
-
try {
|
|
47
|
+
try { let v=el.dataset.sinthValue; el.value = ${X}[v] ? ${X}[v]({}) : ''; } catch(e) {}
|
|
48
48
|
});\n`;
|
|
49
49
|
renderBody += ` document.querySelectorAll('[data-sinth-step]').forEach(function(el) {
|
|
50
50
|
try { let s=el.dataset.sinthStep; el.step = ${X}[s] ? ${X}[s]({}) : 1; } catch(e) {}
|