@yannosay/sinth 0.17.0 → 0.18.1

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
@@ -48,7 +48,7 @@ page
48
48
 
49
49
  title = "Functions"
50
50
 
51
- var str userName = "Sinths User"
51
+ var str userName = "Sinth User"
52
52
 
53
53
  function greet(str name) -> str {
54
54
  "Hello, " + name
@@ -61,11 +61,11 @@ function renderHeading(str label) -> ui {
61
61
  }
62
62
 
63
63
  Div {
64
- (greet(userName)) + " — welcome back!"
64
+ greet(userName) + " - welcome back!"
65
65
  }
66
66
 
67
- (renderHeading(userName))
68
- (renderHeading("This renders!"))
67
+ renderHeading(userName)
68
+ renderHeading("This renders!")
69
69
  ```
70
70
  or
71
71
 
@@ -87,10 +87,10 @@ function getStatus() -> str {
87
87
  return "OFF"
88
88
  }
89
89
 
90
- Checkbox(checked: isAdmin, onChange: toggleAdmin(), label: "Admin mode?")
90
+ Checkbox(checked: isAdmin label: "Admin mode?")
91
91
 
92
92
  Paragraph {
93
- "Admin mode is " + (getStatus()) + "!"
93
+ "Admin mode is " + getStatus() + "!"
94
94
  }
95
95
  ```
96
96
  #### NEW (0.13.5): Fullscreen activation + if-else inside Component
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.17.0",
3
+ "version": "0.18.1",
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",
@@ -1185,7 +1185,99 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
1185
1185
  );
1186
1186
  }
1187
1187
  }
1188
+ for (const child of fn.body) {
1189
+ checkTypeInChild(child, allVarDecls, functionDefs);
1190
+ }
1191
+ }
1192
+ for (const use of file.uses) {
1193
+ checkTypeInChild(use, allVarDecls, functionDefs);
1194
+ }
1195
+
1196
+ function inferType(expr: Expression, varDecls: VarDeclaration[], functionDefs: FunctionDef[]): string | null {
1197
+ if (expr.kind === "literal" && expr.value) {
1198
+ if (expr.value.kind === "str") return "str";
1199
+ if (expr.value.kind === "num") return "int";
1200
+ if (expr.value.kind === "bool") return "bool";
1201
+ return null;
1202
+ }
1203
+ if (expr.kind === "variable" && expr.name) {
1204
+ const vd = varDecls.find(v => v.name === expr.name);
1205
+ if (vd) return vd.varType === "str[]" ? "str[]" : vd.varType;
1206
+ return null;
1207
+ }
1208
+ if (expr.kind === "call" && expr.callee?.kind === "variable" && expr.callee.name) {
1209
+ const fd = functionDefs.find(f => f.name === expr.callee!.name);
1210
+ if (fd?.returnType) return fd.returnType;
1211
+ return null;
1212
+ }
1213
+ if (expr.kind === "binary" && expr.op === "+") return "str";
1214
+ if (expr.kind === "unary" && expr.op === "not") return "bool";
1215
+ return null;
1216
+ }
1217
+
1218
+ function checkCallArgs(fnDef: FunctionDef, args: Expression[], loc: Loc, varDecls: VarDeclaration[], functionDefs: FunctionDef[]): void {
1219
+ for (let i = 0; i < fnDef.params.length && i < args.length; i++) {
1220
+ const param = fnDef.params[i];
1221
+ const arg = args[i];
1222
+ if (!param.paramType) continue;
1223
+ const argType = inferType(arg, varDecls, functionDefs);
1224
+ if (argType && argType !== param.paramType) {
1225
+ if (param.paramType === "int" && argType === "str" && arg.kind === "literal" && arg.value?.kind === "str") {
1226
+ if (!isNaN(Number(arg.value.value))) continue;
1227
+ }
1228
+ throw new SinthError(
1229
+ `Function '${fnDef.name}' parameter '${param.name}' expects '${param.paramType}' but got '${argType}'`,
1230
+ loc
1231
+ );
1232
+ }
1233
+ }
1234
+ }
1235
+
1236
+ function checkTypeInExpr(expr: Expression | undefined, varDecls: VarDeclaration[], functionDefs: FunctionDef[]): void {
1237
+ if (!expr) return;
1238
+ if (expr.kind === "call" && expr.callee?.kind === "variable" && expr.callee.name) {
1239
+ const fnDef = functionDefs.find(f => f.name === expr.callee!.name);
1240
+ if (fnDef) checkCallArgs(fnDef, expr.args ?? [], fnDef.loc, varDecls, functionDefs);
1241
+ }
1242
+ if (expr.left) checkTypeInExpr(expr.left, varDecls, functionDefs);
1243
+ if (expr.right) checkTypeInExpr(expr.right, varDecls, functionDefs);
1244
+ if (expr.operand) checkTypeInExpr(expr.operand, varDecls, functionDefs);
1245
+ if (expr.args) expr.args.forEach(a => checkTypeInExpr(a, varDecls, functionDefs));
1188
1246
  }
1247
+
1248
+ function checkTypeInChild(child: Child, varDecls: VarDeclaration[], functionDefs: FunctionDef[]): void {
1249
+ if (child.kind === "expr" && child.expression) {
1250
+ checkTypeInExpr(child.expression, varDecls, functionDefs);
1251
+ }
1252
+ if (child.kind === "assign_stmt" && child.expression) {
1253
+ checkTypeInExpr(child.expression, varDecls, functionDefs);
1254
+ }
1255
+ if (child.kind === "return" && (child as ReturnStmt).expression) {
1256
+ checkTypeInExpr((child as ReturnStmt).expression, varDecls, functionDefs);
1257
+ }
1258
+ if (child.kind === "if") {
1259
+ const ib = child as IfBlock;
1260
+ checkTypeInExpr(ib.condition, varDecls, functionDefs);
1261
+ ib.body.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
1262
+ ib.elseBody?.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
1263
+ }
1264
+ if (child.kind === "for") {
1265
+ (child as ForLoop).body.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
1266
+ }
1267
+ if (child.kind === "use") {
1268
+ const u = child as CompUse;
1269
+ u.attrs.forEach(a => {
1270
+ if (a.value?.kind === "str" && a.value.value.startsWith("__EXPR__")) {
1271
+ try {
1272
+ const expr: Expression = JSON.parse(a.value.value.substring(8));
1273
+ checkTypeInExpr(expr, varDecls, functionDefs);
1274
+ } catch {}
1275
+ }
1276
+ });
1277
+ u.children.forEach(c => checkTypeInChild(c, varDecls, functionDefs));
1278
+ }
1279
+ }
1280
+
1189
1281
  const enableDiffing = file.meta.some(m =>
1190
1282
  m.key === "domdiffing" &&
1191
1283
  (m.value.kind === "bool" ? m.value.value === true : (m.value.kind === "str" && m.value.value === "true"))
@@ -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;
@@ -483,7 +493,7 @@ private parseArrayLiteral(): Literal {
483
493
  }
484
494
  if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after function signature", this.peek().loc);
485
495
  this.consume(TT.LBRACE);
486
- const body = this.parseChildList();
496
+ const body = this.parseUnifiedBody();
487
497
  this.consume(TT.RBRACE);
488
498
  return { name, params, returnType, body, loc };
489
499
  }
@@ -523,9 +533,10 @@ private parseArrayLiteral(): Literal {
523
533
  while (!this.check(TT.RPAREN)) {
524
534
  const ploc = this.peek().loc;
525
535
  let pname: string;
536
+ let paramType: VarType | undefined;
526
537
  if (this.check(TT.IDENT) && ["int","str","bool","str[]","obj"].includes(this.peek().value) &&
527
538
  this.tokens[this.pos + 1]?.type === TT.IDENT) {
528
- this.consume(TT.IDENT); // consume type
539
+ paramType = this.consume(TT.IDENT).value as VarType;
529
540
  pname = this.consume(TT.IDENT).value;
530
541
  } else {
531
542
  const nameTok = this.check(TT.IDENT) ? this.consume(TT.IDENT) : this.consume(this.peek().type);
@@ -533,7 +544,7 @@ private parseArrayLiteral(): Literal {
533
544
  }
534
545
  let defaultVal: Literal | undefined;
535
546
  if (this.check(TT.EQUALS)) { this.consume(TT.EQUALS); defaultVal = this.parseLiteral(); }
536
- params.push({ name: pname, defaultVal, loc: ploc });
547
+ params.push({ name: pname, paramType, defaultVal, loc: ploc });
537
548
  if (!this.check(TT.RPAREN)) this.consume(TT.COMMA);
538
549
  }
539
550
  this.consume(TT.RPAREN);
@@ -1079,7 +1090,19 @@ private parseArrayLiteral(): Literal {
1079
1090
  rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
1080
1091
  } else if (this.check(TT.IDENT)) {
1081
1092
  const rhsName = this.consume(TT.IDENT).value;
1082
- if (this.check(TT.DOT)) {
1093
+ if (this.check(TT.LPAREN)) {
1094
+ this.consume(TT.LPAREN);
1095
+ const args: Expression[] = [];
1096
+ if (!this.check(TT.RPAREN)) {
1097
+ args.push(this.parseExpression()!);
1098
+ while (this.check(TT.COMMA)) {
1099
+ this.consume(TT.COMMA);
1100
+ args.push(this.parseExpression()!);
1101
+ }
1102
+ }
1103
+ this.consume(TT.RPAREN);
1104
+ rhs = { kind: "call", callee: { kind: "variable", name: rhsName }, args };
1105
+ } else if (this.check(TT.DOT)) {
1083
1106
  this.consume(TT.DOT);
1084
1107
  rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
1085
1108
  } else if (this.check(TT.LBRACKET)) {
@@ -1226,19 +1249,24 @@ private parseArrayLiteral(): Literal {
1226
1249
  children.push(this.parseCompUse());
1227
1250
  continue;
1228
1251
  } else {
1229
- this.consume(TT.IDENT);
1230
- this.consume(TT.LPAREN);
1231
- const args: Expression[] = [];
1232
- if (!this.check(TT.RPAREN)) {
1233
- args.push(this.parseExpression()!);
1234
- while (this.check(TT.COMMA)) {
1235
- this.consume(TT.COMMA);
1236
- args.push(this.parseExpression()!);
1252
+ const expr = this.parseExpression();
1253
+ if (expr) {
1254
+ let finalExpr = expr;
1255
+ while (this.check(TT.OP_PLUS)) {
1256
+ this.consume(TT.OP_PLUS);
1257
+ let rhs: Expression | null = null;
1258
+ if (this.check(TT.STRING)) {
1259
+ rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
1260
+ } else if (this.check(TT.IDENT)) {
1261
+ rhs = this.parseExpression();
1262
+ } else {
1263
+ rhs = this.parseExpression(true);
1264
+ }
1265
+ if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
1266
+ finalExpr = { kind: "binary", left: finalExpr, op: "+", right: rhs };
1237
1267
  }
1268
+ children.push({ kind: "expr", expression: finalExpr, loc });
1238
1269
  }
1239
- this.consume(TT.RPAREN);
1240
- const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
1241
- children.push({ kind: "expr", expression: callExpr, loc });
1242
1270
  continue;
1243
1271
  }
1244
1272
  } else if (nextType === TT.LBRACE || nextType === TT.RAW_BLOCK) {