@yannosay/sinth 4.0.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.
@@ -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 file.varDecls) {
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: file.varDecls,
47
+ varDecls: allVarDecls,
47
48
  bodyHTML,
48
49
  logicBlocks: ctx.logicBlocks,
49
50
  mixedBlocks: ctx.mixedBlocks,
@@ -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
- vars.add(expr.name);
85
- if (expr.kind === "assign" && expr.target)
86
- vars.add(expr.target);
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)
@@ -157,7 +161,7 @@ function compileFile(filePath, opts) {
157
161
  for (const v of pageVars) {
158
162
  if (pageLoopVars.has(v))
159
163
  continue;
160
- const rootVar = v.split('.')[0];
164
+ const rootVar = v;
161
165
  if (declaredVars.has(rootVar) || fnLocalVarNames.has(rootVar))
162
166
  continue;
163
167
  let foundInFn = false;
@@ -241,7 +245,7 @@ function compileFile(filePath, opts) {
241
245
  for (const v of fnBodyVars) {
242
246
  if (fnLoopVars.has(v))
243
247
  continue;
244
- const rootVar = v.split('.')[0];
248
+ const rootVar = v;
245
249
  if (!fnParamNames.has(rootVar) && !declaredVars.has(rootVar) && !declaredFuncs.has(rootVar) && !fnLocalDeclared.has(rootVar)) {
246
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.`);
247
251
  }
@@ -261,7 +265,7 @@ function compileFile(filePath, opts) {
261
265
  if (expr.value.kind === "str")
262
266
  return "str";
263
267
  if (expr.value.kind === "num")
264
- return "int";
268
+ return "num";
265
269
  if (expr.value.kind === "bool")
266
270
  return "bool";
267
271
  return null;
@@ -278,17 +282,35 @@ function compileFile(filePath, opts) {
278
282
  return fd.returnType;
279
283
  return null;
280
284
  }
281
- if (expr.kind === "binary" && expr.op === "+")
282
- return "str";
283
- if (expr.kind === "unary" && expr.op === "not")
284
- return "bool";
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
+ }
285
307
  return null;
286
308
  }
287
309
  function checkVarInitType(vd, varDecls, functionDefs) {
288
310
  if (!vd.value)
289
311
  return;
290
312
  if (vd.value.kind === "num") {
291
- if (vd.varType !== "int")
313
+ if (vd.varType !== "int" && vd.varType !== "num")
292
314
  throw new types_1.SinthError(`Cannot assign number to variable '${vd.name}' of type '${vd.varType}'`, vd.loc);
293
315
  return;
294
316
  }
@@ -339,8 +361,8 @@ function compileFile(filePath, opts) {
339
361
  if (!param.paramType)
340
362
  continue;
341
363
  const argType = inferType(arg, varDecls, functionDefs);
342
- if (argType && argType !== param.paramType) {
343
- 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") {
344
366
  if (!isNaN(Number(arg.value.value)))
345
367
  continue;
346
368
  }
@@ -361,17 +383,17 @@ function compileFile(filePath, opts) {
361
383
  if (expected === "obj")
362
384
  expected = "str";
363
385
  else if (expected === "str[]" && parts[1] === "length")
364
- expected = "int";
386
+ expected = "num";
365
387
  }
366
388
  const rhsType = inferType(expr.right, varDecls, functionDefs);
367
389
  if (rhsType && expected !== rhsType) {
368
390
  if (expr.op === "+=" && expected === "str") { /* allow string += any */ }
369
391
  else if (expr.op === "-=") {
370
- if (expected !== "int") {
371
- throw new types_1.SinthError(`Cannot use '-=' on non‑int variable '${expr.target}'`, loc || { file: "", line: 0, col: 0 });
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 });
372
394
  }
373
- else if (rhsType !== "int") {
374
- throw new types_1.SinthError(`Right operand of '-=' must be int, got '${rhsType}'`, loc || { file: "", line: 0, col: 0 });
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 });
375
397
  }
376
398
  }
377
399
  else {
@@ -397,8 +419,8 @@ function compileFile(filePath, opts) {
397
419
  throw new types_1.SinthError(`Right side of '${op}' must be bool, got '${rightType}'`, loc || { file: "", line: 0, col: 0 });
398
420
  }
399
421
  else if (["-", "*", "/", "%"].includes(op)) {
400
- if (leftType !== "int" || rightType !== "int") {
401
- throw new types_1.SinthError(`Arithmetic operator '${op}' requires int operands, got '${leftType}' and '${rightType}'`, loc || { file: "", line: 0, col: 0 });
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 });
402
424
  }
403
425
  }
404
426
  }
@@ -187,7 +187,7 @@ function inlineFunctionBody(fnDef, args, ctx) {
187
187
  }
188
188
  }
189
189
  else {
190
- const defaults = { str: '""', int: "0", bool: "false", "str[]": "[]", obj: "{}" };
190
+ const defaults = { str: '""', int: "0", num: "0", bool: "false", "str[]": "[]", obj: "{}" };
191
191
  initJS = defaults[vd.varType] ?? "undefined";
192
192
  }
193
193
  statements.push(`let ${vd.name} = ${initJS};`);
@@ -231,6 +231,18 @@ function emitCEAction(ctx, eventName, handlerBody) {
231
231
  ctx.ceActionHandlers.push(`function(e, host) { ${handlerBody} }`);
232
232
  return `data-sinth-ce-${eventName}="${idx}"`;
233
233
  }
234
+ function resolveExprWithParams(expr, ctx, paramMap) {
235
+ const paramNames = new Set(paramMap.keys());
236
+ const combinedVars = new Set();
237
+ if (ctx.declaredVars) {
238
+ for (const v of ctx.declaredVars)
239
+ combinedVars.add(v);
240
+ }
241
+ for (const p of paramNames) {
242
+ combinedVars.add(p);
243
+ }
244
+ return (0, expr_1.compileExprToJS)(expr, ctx.loopVars, ctx.namespace, combinedVars);
245
+ }
234
246
  function renderAttr(attr, paramMap, ctx) {
235
247
  const { name, value } = attr;
236
248
  const renderFn = ctx.namespace ? "sinthRender_" + ctx.namespace : "sinthRender";
@@ -248,7 +260,7 @@ function renderAttr(attr, paramMap, ctx) {
248
260
  if (ctx.namespace)
249
261
  vName = ctx.namespace + "_" + vName;
250
262
  const varDecl = ctx?.varDecls?.find(v => v.name === vName);
251
- const rhs = (varDecl && varDecl.varType === "int")
263
+ const rhs = (varDecl && (varDecl.varType === "int" || varDecl.varType === "num"))
252
264
  ? `Number(e.target.value) || 0`
253
265
  : `e.target.value`;
254
266
  const renderCall = ctx.scopeVar ? `${ctx.scopeVar}._render()` : renderFn + "()";
@@ -403,7 +415,10 @@ function renderAttr(attr, paramMap, ctx) {
403
415
  if (raw.startsWith("__EXPR__")) {
404
416
  const exprJson = raw.substring("__EXPR__".length);
405
417
  try {
406
- const expr = JSON.parse(exprJson);
418
+ let expr = JSON.parse(exprJson);
419
+ if (paramMap.size > 0) {
420
+ expr = substituteParamsInExpr(expr, paramMap);
421
+ }
407
422
  const ev = eventAttrName(name);
408
423
  if (ev && expr.kind === "call" && expr.callee?.kind === "variable") {
409
424
  const fnName = expr.callee.name;
@@ -416,9 +431,17 @@ function renderAttr(attr, paramMap, ctx) {
416
431
  }
417
432
  }
418
433
  }
419
- let jsExpr = (0, expr_1.compileExprToJS)(expr, ctx?.loopVars, ctx.namespace, ctx.declaredVars);
420
- if (ctx.namespace && ctx.declaredVars && !ctx.scopePrefix) {
421
- for (const v of ctx.declaredVars) {
434
+ let jsExpr = resolveExprWithParams(expr, ctx, paramMap);
435
+ if (ctx.namespace && !ctx.scopePrefix) {
436
+ const combinedVars = new Set();
437
+ if (ctx.declaredVars) {
438
+ for (const v of ctx.declaredVars)
439
+ combinedVars.add(v);
440
+ }
441
+ for (const p of paramMap.keys()) {
442
+ combinedVars.add(p);
443
+ }
444
+ for (const v of combinedVars) {
422
445
  const regex = new RegExp(`\\b${v}\\b`, 'g');
423
446
  jsExpr = jsExpr.replace(regex, ctx.namespace + '_' + v);
424
447
  }
@@ -526,6 +549,9 @@ function renderChild(child, ctx, params, depth) {
526
549
  case "expr": {
527
550
  if (!child.expression)
528
551
  return "";
552
+ if (params.size > 0) {
553
+ child = { ...child, expression: substituteParamsInExpr(child.expression, params) };
554
+ }
529
555
  if (child.expression.kind === "variable" && child.expression.name && params.has(child.expression.name)) {
530
556
  const val = params.get(child.expression.name);
531
557
  if (val.startsWith("__LIT__")) {
@@ -586,16 +612,49 @@ function renderChild(child, ctx, params, depth) {
586
612
  const callArgs = child.expression.args ?? [];
587
613
  const localParams = new Map();
588
614
  for (let i = 0; i < fnDef.params.length && i < callArgs.length; i++) {
589
- const arg = callArgs[i];
590
- if (arg.kind === "literal" && arg.value?.kind === "str") {
591
- localParams.set(fnDef.params[i].name, "__LIT__" + arg.value.value);
592
- }
593
- else if (arg.kind === "variable" && arg.name) {
594
- localParams.set(fnDef.params[i].name, "__VAR__" + arg.name);
615
+ localParams.set(fnDef.params[i].name, "__EXPR__" + JSON.stringify(callArgs[i]));
616
+ }
617
+ if (!ctx.fnCallCounters)
618
+ ctx.fnCallCounters = new Map();
619
+ const fnCallIdx = (ctx.fnCallCounters.get(fnName) ?? 0) + 1;
620
+ ctx.fnCallCounters.set(fnName, fnCallIdx);
621
+ const scopePrefix = `_fn_${fnName}_${fnCallIdx}_`;
622
+ for (const bodyChild of fnDef.body) {
623
+ if (bodyChild.kind === "var") {
624
+ const vd = bodyChild;
625
+ const scopedName = scopePrefix + vd.name;
626
+ localParams.set(vd.name, "__VAR__" + scopedName);
627
+ if (!ctx.varDecls)
628
+ ctx.varDecls = [];
629
+ ctx.varDecls.push({
630
+ kind: "var",
631
+ name: scopedName,
632
+ varType: vd.varType,
633
+ value: vd.value,
634
+ loc: vd.loc,
635
+ });
636
+ if (ctx.declaredVars)
637
+ ctx.declaredVars.add(scopedName);
595
638
  }
596
- else {
597
- const exprId = registerExpr(ctx, arg);
598
- localParams.set(fnDef.params[i].name, "__EXPRID__" + exprId);
639
+ }
640
+ for (let i = 0; i < fnDef.params.length && i < callArgs.length; i++) {
641
+ const param = fnDef.params[i];
642
+ const argExpr = callArgs[i];
643
+ const isSimpleVar = argExpr && argExpr.kind === "variable" && argExpr.name && !argExpr.name.includes(".");
644
+ if (!isSimpleVar && argExpr) {
645
+ const scopedName = scopePrefix + param.name;
646
+ localParams.set(param.name, "__VAR__" + scopedName);
647
+ if (!ctx.varDecls)
648
+ ctx.varDecls = [];
649
+ ctx.varDecls.push({
650
+ kind: "var",
651
+ name: scopedName,
652
+ varType: param.paramType || "num",
653
+ value: { kind: "str", value: "__EXPR__" + JSON.stringify(argExpr) },
654
+ loc: param.loc,
655
+ });
656
+ if (ctx.declaredVars)
657
+ ctx.declaredVars.add(scopedName);
599
658
  }
600
659
  }
601
660
  const substituteExpr = (expr, pm) => {
@@ -607,6 +666,14 @@ function renderChild(child, ctx, params, depth) {
607
666
  const id = parseInt(pv.slice(10), 10);
608
667
  return { kind: "expr_ref", exprId: id };
609
668
  }
669
+ if (pv.startsWith("__EXPR__")) {
670
+ try {
671
+ return JSON.parse(pv.substring(8));
672
+ }
673
+ catch {
674
+ return expr;
675
+ }
676
+ }
610
677
  return { kind: "variable", name: pv.slice(7) };
611
678
  }
612
679
  if (expr.kind === "binary")
@@ -624,6 +691,17 @@ function renderChild(child, ctx, params, depth) {
624
691
  if (pv.startsWith("__VAR__")) {
625
692
  return { ...expr, target: pv.slice(7), right: expr.right ? substituteExpr(expr.right, pm) : undefined };
626
693
  }
694
+ if (pv.startsWith("__EXPR__")) {
695
+ try {
696
+ const argExpr = JSON.parse(pv.substring(8));
697
+ if (argExpr.kind === "variable" && argExpr.name) {
698
+ return { ...expr, target: argExpr.name, right: expr.right ? substituteExpr(expr.right, pm) : undefined };
699
+ }
700
+ }
701
+ catch {
702
+ void 0;
703
+ }
704
+ }
627
705
  }
628
706
  return { ...expr, right: expr.right ? substituteExpr(expr.right, pm) : undefined };
629
707
  }
@@ -1065,17 +1143,17 @@ function renderCompUse(use, ctx, params, depth) {
1065
1143
  }
1066
1144
  const varDecl = ctx.varDecls?.find(v => v.name === vName);
1067
1145
  if (!use.attrs.some(a => a.name === "type")) {
1068
- if (varDecl && varDecl.varType === "int") {
1146
+ if (varDecl && (varDecl.varType === "int" || varDecl.varType === "num")) {
1069
1147
  attrParts.push(`type="number"`);
1070
1148
  }
1071
1149
  else {
1072
1150
  attrParts.push(`type="text"`);
1073
1151
  }
1074
1152
  }
1075
- if (varDecl && varDecl.varType === "int" && !use.attrs.some(a => a.name === "step")) {
1153
+ if (varDecl && (varDecl.varType === "int" || varDecl.varType === "num") && !use.attrs.some(a => a.name === "step")) {
1076
1154
  attrParts.push(`step="1"`);
1077
1155
  }
1078
- const rhs = (varDecl && varDecl.varType === "int")
1156
+ const rhs = (varDecl && (varDecl.varType === "int" || varDecl.varType === "num"))
1079
1157
  ? `Number(e.target.value) || 0`
1080
1158
  : `e.target.value`;
1081
1159
  const initialVal = varDecl?.value ? (0, utils_1.litToString)(varDecl.value).replace(/"/g, '&quot;') : "";
@@ -1259,10 +1337,10 @@ function buildRuntime(opts) {
1259
1337
  const vn = ns ? ns.slice(1) + "_" + v.name : v.name;
1260
1338
  if (!v.value) {
1261
1339
  if (!assignedVars.has(v.name)) {
1262
- const defaults = { str: '""', int: "0", bool: "false", "str[]": "[]" };
1340
+ const defaults = { str: '""', int: "0", num: "0", bool: "false", "str[]": "[]" };
1263
1341
  types_1.SinthWarning.emit(`Variable '${v.name}' is declared but never assigned. Defaulting to ${defaults[v.varType] ?? "undefined"}.`, v.loc);
1264
1342
  }
1265
- const defaults = { str: '""', int: "0", bool: "false", "str[]": "[]" };
1343
+ const defaults = { str: '""', int: "0", num: "0", bool: "false", "str[]": "[]" };
1266
1344
  return `var ${vn} = ${defaults[v.varType] ?? "undefined"};`;
1267
1345
  }
1268
1346
  const val = (0, utils_1.litToString)(v.value);
@@ -1325,26 +1403,9 @@ function buildRuntime(opts) {
1325
1403
  return out ? `// Sinth Compiled Runtime\n${out}` : "";
1326
1404
  }
1327
1405
  const helpers = (0, helpers_1.generateHelpers)({ needsExpr, needsIf, needsFor, needsDelay, needsMixed, ns });
1328
- const exprVarUpdates = varDecls
1329
- .filter(v => v.value && (0, utils_1.litToString)(v.value).startsWith("__EXPR__"))
1330
- .map(v => {
1331
- try {
1332
- const expr = JSON.parse((0, utils_1.litToString)(v.value).substring(8));
1333
- const js = (0, expr_1.compileExprToJS)(expr, undefined, opts.namespace, opts.declaredVars);
1334
- const idx = exprRegistry.indexOf(js);
1335
- const vn2 = ns ? ns.slice(1) + "_" + v.name : v.name;
1336
- return `${vn2} = __X${ns}[${idx}]({});`;
1337
- }
1338
- catch {
1339
- return "";
1340
- }
1341
- })
1342
- .filter(Boolean)
1343
- .join("\n ");
1344
1406
  const renderBody = (0, render_1.buildRenderBody)({
1345
1407
  bodyHTML, logicBlocks: [], mixedBlocks,
1346
1408
  needsLogic: false, needsMixed, needsIf, needsFor, needsExpr, needsDelay,
1347
- exprVarUpdates: exprVarUpdates || undefined,
1348
1409
  ns
1349
1410
  });
1350
1411
  let forDataJS = "";
package/dist/core/expr.js CHANGED
@@ -46,7 +46,11 @@ function compileExprToJS(expr, loopVars, namespace, declaredVars) {
46
46
  }
47
47
  case "unary": {
48
48
  const o = expr.op === "not" ? "!" : expr.op;
49
- return `${o}(${compileExprToJS(expr.operand, loopVars, namespace, declaredVars)})`;
49
+ const operand = expr.operand;
50
+ if (operand.kind === "binary" && (operand.op === "+" || operand.op === "-" || operand.op === "*" || operand.op === "/" || operand.op === "%")) {
51
+ return `${o}(${compileExprToJS(operand, loopVars, namespace, declaredVars)})`;
52
+ }
53
+ return `${o}${compileExprToJS(operand, loopVars, namespace, declaredVars)}`;
50
54
  }
51
55
  case "assign": {
52
56
  const v = expr.right ? compileExprToJS(expr.right, loopVars, namespace, declaredVars) : "null";
@@ -143,7 +143,7 @@ class Lexer {
143
143
  tokens.push({ type: types_1.TT.OP_ARROW, value: "->", loc });
144
144
  continue;
145
145
  }
146
- if (ch === "-" && !this.isDigit(this.src[this.pos + 1] ?? "")) {
146
+ if (ch === "-") {
147
147
  tokens.push(this.single(types_1.TT.OP_MINUS, loc));
148
148
  continue;
149
149
  }
@@ -161,7 +161,7 @@ class Lexer {
161
161
  tokens.push(this.readString(loc));
162
162
  continue;
163
163
  }
164
- if (this.isDigit(ch) || (ch === "-" && this.isDigit(this.src[this.pos + 1] ?? ""))) {
164
+ if (this.isDigit(ch)) {
165
165
  tokens.push(this.readNumber(loc));
166
166
  continue;
167
167
  }
@@ -365,10 +365,6 @@ class Lexer {
365
365
  }
366
366
  readNumber(loc) {
367
367
  let s = "";
368
- if (this.src[this.pos] === "-") {
369
- s += "-";
370
- this.adv();
371
- }
372
368
  while (this.isDigit(this.src[this.pos] ?? "")) {
373
369
  s += this.src[this.pos];
374
370
  this.adv();
@@ -409,6 +405,6 @@ class Lexer {
409
405
  loc() { return { file: this.file, line: this.line, col: this.col }; }
410
406
  isDigit(c) { return c >= "0" && c <= "9"; }
411
407
  isIdentStart(c) { return /[a-zA-Z_$\u00C0-\uFFFF]/.test(c); }
412
- isIdentCont(c) { return /[a-zA-Z0-9_$\-\u00C0-\uFFFF]/.test(c); }
408
+ isIdentCont(c) { return /[a-zA-Z0-9_$\u00C0-\uFFFF]/.test(c); }
413
409
  }
414
410
  exports.Lexer = Lexer;
@@ -201,8 +201,14 @@ class Parser {
201
201
  this.consume(types_1.TT.RBRACKET);
202
202
  typeStr = "str[]";
203
203
  }
204
- if (!["int", "str", "bool", "str[]", "obj", "ui"].includes(typeStr)) {
205
- 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);
206
212
  }
207
213
  const nameTok = this.consume(types_1.TT.IDENT);
208
214
  let val = null;
@@ -211,23 +217,25 @@ class Parser {
211
217
  if (this.check(types_1.TT.LBRACKET)) {
212
218
  val = this.parseArrayLiteral();
213
219
  }
214
- else if (this.check(types_1.TT.IDENT) || this.check(types_1.TT.LPAREN)) {
220
+ else {
215
221
  const savedPos = this.pos;
216
- const expr = this.parseExpression();
217
- if (expr && (expr.kind !== "literal" || (expr.kind === "literal" && expr.value?.kind === "str" && expr.value.value.startsWith("__VAR__")))) {
218
- val = { kind: "str", value: `__EXPR__${JSON.stringify(expr)}` };
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();
219
234
  }
220
235
  else {
221
- this.pos = savedPos;
222
236
  val = this.parseLiteral();
223
237
  }
224
238
  }
225
- else if (this.check(types_1.TT.NUMBER)) {
226
- val = this.parseLiteral();
227
- }
228
- else {
229
- val = this.parseLiteral();
230
- }
231
239
  }
232
240
  return { kind: "var", name: nameTok.value, varType: typeStr, value: val, loc };
233
241
  }
@@ -304,19 +312,43 @@ class Parser {
304
312
  if (this._visited.has(this.pos))
305
313
  throw new types_1.SinthError("Circular expression reference", this.peek().loc);
306
314
  this._visited.add(this.pos);
307
- const tok = this.peek();
308
- // unary 'not'
309
- if ((tok.type === types_1.TT.IDENT && tok.value === "not") || tok.type === types_1.TT.OP_NOT) {
310
- if (tok.type === types_1.TT.IDENT)
311
- this.consume(types_1.TT.IDENT);
312
- else
313
- this.consume(types_1.TT.OP_NOT);
314
- const operand = this.parseExpression();
315
- if (!operand)
316
- throw new types_1.SinthError("Expected expression after 'not'", tok.loc);
317
- const unaryExpr = { kind: "unary", op: "not", operand };
318
- 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
+ }
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;
319
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();
320
352
  if (this.check(types_1.TT.LPAREN)) {
321
353
  this.consume(types_1.TT.LPAREN);
322
354
  const inner = this.parseExpression();
@@ -325,21 +357,13 @@ class Parser {
325
357
  if (!this.check(types_1.TT.RPAREN))
326
358
  throw new types_1.SinthError("Expected closing ')'", this.peek().loc);
327
359
  this.consume(types_1.TT.RPAREN);
328
- const result = this.parseCallExpr(this.parsePostfix(inner));
329
- return skipBinary ? result : this.parseBinaryRHS(result);
360
+ return inner;
330
361
  }
331
- if (this.check(types_1.TT.STRING) || this.check(types_1.TT.NUMBER) || this.check(types_1.TT.BOOL_TRUE) ||
362
+ if (this.check(types_1.TT.NUMBER) || this.check(types_1.TT.STRING) || this.check(types_1.TT.BOOL_TRUE) ||
332
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)) {
333
364
  const lit = this.parseLiteral();
334
- const base = { kind: "literal", value: lit };
335
- return this.parseBinaryRHS(this.parseCallExpr(this.parsePostfix(base)));
336
- }
337
- // remove as function
338
- if (tok.type === types_1.TT.KW_REMOVE) {
339
- const name = this.consume(types_1.TT.KW_REMOVE).value;
340
- return this.parseBinaryRHS(this.parseCallExpr({ kind: "variable", name }));
365
+ return { kind: "literal", value: lit };
341
366
  }
342
- // var or assignment
343
367
  if (tok.type === types_1.TT.IDENT) {
344
368
  let rawName = this.consume(types_1.TT.IDENT).value;
345
369
  let memo = false;
@@ -347,170 +371,123 @@ class Parser {
347
371
  memo = true;
348
372
  rawName = rawName.substring(1);
349
373
  }
350
- const name = rawName;
351
- let varExpr;
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
- const propName = this.consume(types_1.TT.IDENT).value;
355
- const fullName = `${name}.${propName}`;
356
- varExpr = { kind: "variable", name: fullName };
357
- }
358
- else {
359
- varExpr = { kind: "variable", name };
377
+ name += "." + this.consume(types_1.TT.IDENT).value;
360
378
  }
379
+ const expr = { kind: "variable", name };
361
380
  if (memo)
362
- varExpr.memo = true;
363
- // apply bracket postfixes (e.g. data[key], data["x"])
364
- const indexedExpr = this.parsePostfix(varExpr);
365
- // if indexing was applied, we don't allow assignment, just binary RHS (after possible call)
366
- if (indexedExpr !== varExpr) {
367
- return this.parseBinaryRHS(this.parseCallExpr(indexedExpr));
368
- }
369
- // no indexing – check for assignment operators
370
- const nextType = this.tokens[this.pos]?.type;
371
- if (nextType === types_1.TT.OP_PLUS && this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS) {
372
- this.consume(types_1.TT.OP_PLUS);
373
- this.consume(types_1.TT.EQUALS);
374
- const rhs = this.parseExpression();
375
- if (!rhs)
376
- throw new types_1.SinthError("Expected expression after +=", this.peek().loc);
377
- return { kind: "assign", target: varExpr.name, op: "+=", right: rhs };
378
- }
379
- if (nextType === types_1.TT.OP_MINUS && this.tokens[this.pos + 1]?.type === types_1.TT.EQUALS) {
380
- this.consume(types_1.TT.OP_MINUS);
381
- this.consume(types_1.TT.EQUALS);
382
- const rhs = this.parseExpression();
383
- if (!rhs)
384
- throw new types_1.SinthError("Expected expression after -=", this.peek().loc);
385
- return { kind: "assign", target: varExpr.name, op: "-=", right: rhs };
386
- }
387
- if (nextType === types_1.TT.EQUALS) {
388
- this.consume(types_1.TT.EQUALS);
389
- const rhs = this.parseExpression();
390
- if (!rhs)
391
- throw new types_1.SinthError("Expected expression after =", this.peek().loc);
392
- return { kind: "assign", target: varExpr.name, op: "=", right: rhs };
393
- }
394
- return this.parseBinaryRHS(this.parseCallExpr(varExpr));
381
+ expr.memo = true;
382
+ return expr;
395
383
  }
396
- return null;
384
+ throw new types_1.SinthError(`Expected expression`, this.peek().loc);
397
385
  }
398
- parsePrimary() {
386
+ parseUnary() {
399
387
  const tok = this.peek();
400
- if (tok.type === types_1.TT.NUMBER || tok.type === types_1.TT.STRING || tok.type === types_1.TT.BOOL_TRUE || tok.type === types_1.TT.BOOL_FALSE || tok.type === types_1.TT.NULL_LIT) {
401
- const lit = this.parseLiteral();
402
- return { kind: "literal", value: lit };
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 };
403
398
  }
404
- if (tok.type === types_1.TT.IDENT) {
405
- let memo = false;
406
- let rawName = this.consume(types_1.TT.IDENT).value;
407
- if (rawName.startsWith("$")) {
408
- memo = true;
409
- rawName = rawName.substring(1);
410
- }
411
- const name = rawName;
412
- let expr;
413
- if (this.check(types_1.TT.DOT)) {
414
- this.consume(types_1.TT.DOT);
415
- expr = { kind: "variable", name: name + "." + this.consume(types_1.TT.IDENT).value };
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 };
416
413
  }
417
414
  else {
418
- expr = { kind: "variable", name };
415
+ break;
419
416
  }
420
- if (memo)
421
- expr.memo = true;
422
- return expr;
423
417
  }
424
- if (tok.type === types_1.TT.LPAREN) {
425
- this.consume(types_1.TT.LPAREN);
426
- const inner = this.parseExpression();
427
- this.consume(types_1.TT.RPAREN);
428
- return inner;
429
- }
430
- if (tok.type === types_1.TT.LBRACE) {
431
- const obj = this.parseObjectLiteral();
432
- return { kind: "literal", value: { kind: "str", value: JSON.stringify(obj) } };
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
+ }
433
432
  }
434
- throw new types_1.SinthError(`Expected expression`, this.peek().loc);
433
+ return left;
434
+ }
435
+ parseAdditionExpression() {
436
+ return this.parseAddition(this.parseTerm());
437
+ }
438
+ parseComparisonExpression() {
439
+ return this.parseComparison(this.parseAdditionExpression());
435
440
  }
436
441
  parseComparison(left) {
437
- const savedPos = this.pos;
438
- const t = this.peek().type;
439
- let op = null;
440
- if (t === types_1.TT.OP_EQEQ)
441
- op = "==";
442
- else if (t === types_1.TT.OP_NEQ)
443
- op = "!=";
444
- else if (t === types_1.TT.OP_LT)
445
- op = "<";
446
- else if (t === types_1.TT.OP_GT)
447
- op = ">";
448
- else if (t === types_1.TT.OP_LTEQ)
449
- op = "<=";
450
- else if (t === types_1.TT.OP_GTEQ)
451
- op = ">=";
452
- if (!op)
453
- return left;
454
- this.pos++;
455
- const rightPrimary = this.parsePrimary();
456
- const rightWithPostfix = this.parsePostfix(rightPrimary);
457
- const rightWithCall = this.parseCallExpr(rightWithPostfix);
458
- const right = this.parseArithmetic(rightWithCall);
459
- if (!right) {
460
- this.pos = savedPos;
461
- return left;
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 };
462
467
  }
463
- if (this.pos === savedPos)
464
- return { kind: "binary", left, op, right };
465
- return { kind: "binary", left, op, right };
466
468
  }
467
469
  parseLogical(left) {
468
- const savedPos = this.pos;
469
- const t = this.peek().type;
470
- let op = null;
471
- if (t === types_1.TT.IDENT && this.peek().value === "and")
472
- op = "and";
473
- else if (t === types_1.TT.IDENT && this.peek().value === "or")
474
- op = "or";
475
- if (!op)
476
- return left;
477
- this.pos++;
478
- const right = this.parseExpression();
479
- if (!right) {
480
- this.pos = savedPos;
481
- return left;
482
- }
483
- return this.parseLogical({ kind: "binary", left, op, right });
484
- }
485
- parseArithmetic(left) {
486
470
  while (true) {
471
+ const savedPos = this.pos;
487
472
  const t = this.peek().type;
488
473
  let op = null;
489
- if (t === types_1.TT.OP_PLUS)
490
- op = "+";
491
- else if (t === types_1.TT.OP_MINUS)
492
- op = "-";
493
- else if (t === types_1.TT.OP_STAR)
494
- op = "*";
495
- else if (t === types_1.TT.OP_SLASH)
496
- op = "/";
497
- else if (t === types_1.TT.OP_MOD)
498
- 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";
499
478
  if (!op)
500
- break;
479
+ return left;
501
480
  this.pos++;
502
- const rightPrimary = this.parsePrimary();
503
- const rightWithPostfix = this.parsePostfix(rightPrimary);
504
- const rightWithCall = this.parseCallExpr(rightWithPostfix);
505
- const right = rightWithCall;
506
- if (!right)
507
- 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
+ }
508
486
  left = { kind: "binary", left, op, right };
509
487
  }
510
- return left;
511
488
  }
512
489
  parseBinaryRHS(left) {
513
- left = this.parseArithmetic(left);
490
+ left = this.parseAddition(left);
514
491
  left = this.parseComparison(left);
515
492
  left = this.parseLogical(left);
516
493
  return left;
@@ -563,8 +540,8 @@ class Parser {
563
540
  if (this.check(types_1.TT.OP_ARROW)) {
564
541
  this.consume(types_1.TT.OP_ARROW);
565
542
  const typeTok = this.consume(types_1.TT.IDENT);
566
- if (!["int", "str", "bool", "str[]", "obj", "ui"].includes(typeTok.value)) {
567
- throw new types_1.SinthError(`Unknown return type '${typeTok.value}'. Expected: int, str, bool, str[], obj, ui`, typeTok.loc);
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);
568
545
  }
569
546
  returnType = typeTok.value;
570
547
  }
@@ -673,9 +650,13 @@ class Parser {
673
650
  const ploc = this.peek().loc;
674
651
  let pname;
675
652
  let paramType;
676
- 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) &&
677
654
  this.tokens[this.pos + 1]?.type === types_1.TT.IDENT) {
678
- paramType = this.consume(types_1.TT.IDENT).value;
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
+ }
679
660
  pname = this.consume(types_1.TT.IDENT).value;
680
661
  }
681
662
  else {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "4.0.0",
3
+ "version": "4.1.0",
4
4
  "description": "An easy, powerful, declarative & reactive language that compiles to pure HTML, CSS, and JavaScript.",
5
5
  "author": "Yannosay Productions",
6
6
  "license": "AGPL-3.0",