@yannosay/sinth 0.18.0 → 0.19.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 CHANGED
@@ -236,9 +236,9 @@ function Counter(int value) -> ui {
236
236
  }
237
237
 
238
238
  Main {
239
- (Counter(apples))
240
- (Counter(bananas))
241
- (Counter(oranges))
239
+ Counter(apples)
240
+ Counter(bananas)
241
+ Counter(oranges)
242
242
  }
243
243
  ```
244
244
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.18.0",
3
+ "version": "0.19.0",
4
4
  "description": "A declarative & reactive language that compiles to pure HTML, CSS, and JavaScript.",
5
5
  "author": "Yannosay Productions",
6
6
  "license": "AGPL-3.0",
@@ -1282,6 +1282,16 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
1282
1282
  m.key === "domdiffing" &&
1283
1283
  (m.value.kind === "bool" ? m.value.value === true : (m.value.kind === "str" && m.value.value === "true"))
1284
1284
  );
1285
+ const topLevelLogicIfs: string[] = [];
1286
+ file.scripts = file.scripts.filter(s => {
1287
+ const trimmed = s.raw.trim();
1288
+ if (/^if\s*\(/.test(trimmed) || /^\{/.test(trimmed)) {
1289
+ topLevelLogicIfs.push(trimmed);
1290
+ return false;
1291
+ }
1292
+ return true;
1293
+ });
1294
+
1285
1295
  const ctx: CompileCtx = {
1286
1296
  allDefs, functionDefs, customEls, cssLinks, jsLinks,
1287
1297
  scopeHash: hash,
@@ -1289,7 +1299,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
1289
1299
  extraCSS: [],
1290
1300
  mixedBlocks: [],
1291
1301
  mixedCounter: 0,
1292
- logicBlocks: [],
1302
+ logicBlocks: [...topLevelLogicIfs],
1293
1303
  ifIdCounter: 0,
1294
1304
  exprRegistry: [],
1295
1305
  exprMap: new Map(),
@@ -106,7 +106,17 @@ sinthRender();`;
106
106
 
107
107
  if (nextType === TT.LPAREN && identName[0] === identName[0].toLowerCase()) {
108
108
  this.consume(TT.IDENT);
109
- const expr = this.parseCallExpr({ kind: "variable", name: identName });
109
+ this.consume(TT.LPAREN);
110
+ const args: Expression[] = [];
111
+ if (!this.check(TT.RPAREN)) {
112
+ args.push(this.parseExpression()!);
113
+ while (this.check(TT.COMMA)) {
114
+ this.consume(TT.COMMA);
115
+ args.push(this.parseExpression()!);
116
+ }
117
+ }
118
+ this.consume(TT.RPAREN);
119
+ const expr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
110
120
  uses.push({ kind: "use", name: "__IF_ROOT__", attrs: [], children: [{ kind: "expr", expression: expr, loc: this.peek().loc }], loc: this.peek().loc });
111
121
  } else {
112
122
  this.pos = savedPos;
@@ -471,6 +481,9 @@ private parseArrayLiteral(): Literal {
471
481
  private parseFunctionDef(): FunctionDef {
472
482
  const loc = this.consume(TT.KW_FUNCTION).loc;
473
483
  const name = this.consume(TT.IDENT).value;
484
+ if (name[0] === name[0].toUpperCase() && name[0] !== name[0].toLowerCase()) {
485
+ throw new SinthError(`Function '${name}' must start with lowercase. Use 'component' for uppercase names.`, loc);
486
+ }
474
487
  const params = this.parseParamDecls();
475
488
  let returnType: VarType | undefined;
476
489
  if (this.check(TT.OP_ARROW)) {
@@ -593,45 +606,53 @@ private parseArrayLiteral(): Literal {
593
606
  const body = this.parseUnifiedBody();
594
607
  this.consume(TT.RBRACE);
595
608
 
596
- let elseBody: Child[] | undefined;
597
- if (this.check(TT.KW_ELSE)) {
598
- const elseTok = this.consume(TT.KW_ELSE);
599
- if (this.check(TT.KW_IF) || elseTok.value === "elif") {
600
- if (elseTok.value === "elif") {
601
- const cond = this.parseExpression();
602
- if (!cond) throw new SinthError("Expected condition after 'elif'", this.peek().loc);
603
- if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after elif condition", this.peek().loc);
604
- this.consume(TT.LBRACE);
605
- const elifBody = this.parseUnifiedBody();
606
- this.consume(TT.RBRACE);
607
- const elifBlock: IfBlock = { kind: "if", condition: cond, body: elifBody, loc: elseTok.loc };
608
- if (this.check(TT.KW_ELSE)) {
609
- const nextElse = this.consume(TT.KW_ELSE);
610
- if (this.check(TT.KW_IF) || nextElse.value === "elif") {
611
- this.pos--;
612
- elifBlock.elseBody = [this.parseIfBlock()];
613
- } else {
614
- if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
615
- this.consume(TT.LBRACE);
616
- elifBlock.elseBody = this.parseUnifiedBody();
617
- this.consume(TT.RBRACE);
618
- }
619
- }
620
- elseBody = [elifBlock];
621
- } else {
622
- elseBody = [this.parseIfBlock()];
623
- }
624
- } else {
625
- if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after else", this.peek().loc);
626
- this.consume(TT.LBRACE);
627
- elseBody = this.parseUnifiedBody();
628
- this.consume(TT.RBRACE);
629
- }
630
- }
609
+ const elseBody = this.parseElseChain();
631
610
 
632
611
  return { kind: "if", condition, body, elseBody, persist, loc };
633
612
  }
634
613
 
614
+ private parseElseChain(): Child[] | undefined {
615
+ if (!this.check(TT.KW_ELSE)) return undefined;
616
+
617
+ const elseTok = this.consume(TT.KW_ELSE);
618
+
619
+ if (this.check(TT.LBRACE)) {
620
+ this.consume(TT.LBRACE);
621
+ const body = this.parseUnifiedBody();
622
+ this.consume(TT.RBRACE);
623
+ return body;
624
+ }
625
+
626
+ if (this.check(TT.KW_IF) || elseTok.value === "elif") {
627
+ const ifBlock = this.parseIfContinue(elseTok);
628
+ return [ifBlock];
629
+ }
630
+
631
+ throw new SinthError("Expected '{', 'if', or 'elif' after 'else'", this.peek().loc);
632
+ }
633
+
634
+ private parseIfContinue(elseTok: Token): IfBlock {
635
+ let condition: Expression;
636
+
637
+ if (elseTok.value === "elif") {
638
+ condition = this.parseExpression()!;
639
+ if (!condition) throw new SinthError("Expected condition after 'elif'", this.peek().loc);
640
+ } else {
641
+ this.consume(TT.KW_IF);
642
+ condition = this.parseExpression()!;
643
+ if (!condition) throw new SinthError("Expected condition after 'if'", this.peek().loc);
644
+ }
645
+
646
+ if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after condition", this.peek().loc);
647
+ this.consume(TT.LBRACE);
648
+ const body = this.parseUnifiedBody();
649
+ this.consume(TT.RBRACE);
650
+
651
+ const elseBody = this.parseElseChain();
652
+
653
+ return { kind: "if", condition, body, elseBody, loc: elseTok.loc };
654
+ }
655
+
635
656
  private parseRawIfStatement(): string {
636
657
  this.consume(TT.KW_IF);
637
658
  const cond = this.parseExpression();
@@ -815,10 +836,19 @@ private parseArrayLiteral(): Literal {
815
836
  if (identName[0] === identName[0].toUpperCase() && identName[0] !== identName[0].toLowerCase()) {
816
837
  children.push(this.parseCompUse());
817
838
  } else {
818
- const expr = this.parseExpression();
819
- if (expr) {
820
- children.push({ kind: "expr", expression: expr, loc });
839
+ this.consume(TT.IDENT);
840
+ this.consume(TT.LPAREN);
841
+ const args: Expression[] = [];
842
+ if (!this.check(TT.RPAREN)) {
843
+ args.push(this.parseExpression()!);
844
+ while (this.check(TT.COMMA)) {
845
+ this.consume(TT.COMMA);
846
+ args.push(this.parseExpression()!);
847
+ }
821
848
  }
849
+ this.consume(TT.RPAREN);
850
+ const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
851
+ children.push({ kind: "expr", expression: callExpr, loc });
822
852
  }
823
853
  continue;
824
854
  }
@@ -1239,24 +1269,32 @@ private parseArrayLiteral(): Literal {
1239
1269
  children.push(this.parseCompUse());
1240
1270
  continue;
1241
1271
  } else {
1242
- const expr = this.parseExpression();
1243
- if (expr) {
1244
- let finalExpr = expr;
1245
- while (this.check(TT.OP_PLUS)) {
1246
- this.consume(TT.OP_PLUS);
1247
- let rhs: Expression | null = null;
1248
- if (this.check(TT.STRING)) {
1249
- rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
1250
- } else if (this.check(TT.IDENT)) {
1251
- rhs = this.parseExpression();
1252
- } else {
1253
- rhs = this.parseExpression(true);
1254
- }
1255
- if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
1256
- finalExpr = { kind: "binary", left: finalExpr, op: "+", right: rhs };
1272
+ this.consume(TT.IDENT);
1273
+ this.consume(TT.LPAREN);
1274
+ const args: Expression[] = [];
1275
+ if (!this.check(TT.RPAREN)) {
1276
+ args.push(this.parseExpression()!);
1277
+ while (this.check(TT.COMMA)) {
1278
+ this.consume(TT.COMMA);
1279
+ args.push(this.parseExpression()!);
1257
1280
  }
1258
- children.push({ kind: "expr", expression: finalExpr, loc });
1259
1281
  }
1282
+ this.consume(TT.RPAREN);
1283
+ let finalExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
1284
+ while (this.check(TT.OP_PLUS)) {
1285
+ this.consume(TT.OP_PLUS);
1286
+ let rhs: Expression | null = null;
1287
+ if (this.check(TT.STRING)) {
1288
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
1289
+ } else if (this.check(TT.IDENT)) {
1290
+ rhs = this.parseExpression();
1291
+ } else {
1292
+ rhs = this.parseExpression(true);
1293
+ }
1294
+ if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
1295
+ finalExpr = { kind: "binary", left: finalExpr, op: "+", right: rhs };
1296
+ }
1297
+ children.push({ kind: "expr", expression: finalExpr, loc });
1260
1298
  continue;
1261
1299
  }
1262
1300
  } else if (nextType === TT.LBRACE || nextType === TT.RAW_BLOCK) {
package/src/core/types.ts CHANGED
@@ -70,7 +70,7 @@ export interface ComponentExpr { kind: "component_expr"; children: Child[]; loc:
70
70
 
71
71
  export type Child = TextNode | CompUse | ExprNode | AssignStmt | IfBlock | ForLoop | RemoveStmt | ReturnStmt | ComponentExpr;
72
72
 
73
- export interface ParamDecl { name: string; defaultVal?: Literal; loc: Loc }
73
+ export interface ParamDecl { name: string; paramType?: VarType; defaultVal?: Literal; loc: Loc }
74
74
 
75
75
  export interface StyleBlock {
76
76
  global: boolean;