@yannosay/sinth 1.7.0 → 1.8.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
@@ -1,5 +1,5 @@
1
- # Sinth
2
-
1
+ 
2
+ # Sinth <img src="https://sinth.yannosay.com/assets/logosinth.webp" alt="logo" width="48" style="border-radius: 12px; vertical-align: middle; margin-left: 8px;">
3
3
  [![npm version](https://img.shields.io/npm/v/@yannosay/sinth)](https://www.npmjs.com/package/@yannosay/sinth)
4
4
  [![npm downloads](https://img.shields.io/npm/dw/@yannosay/sinth)](https://www.npmjs.com/package/@yannosay/sinth)
5
5
  [![license](https://img.shields.io/npm/l/@yannosay/sinth)](https://github.com/Yannosay/sinth/blob/main/LICENSE)
@@ -12,7 +12,7 @@ The language that makes HTML feel SO easy. Declarative. Reactive. Compiles to pu
12
12
  * Discord: [Join here!](https://discord.gg/SUvcrafTQm)
13
13
  * Documentation: [sinth.yannosay.com/docs](https://sinth.yannosay.com/docs) (HIGHLY WIP, not yet finished at all!)
14
14
 
15
- #### Try out Sinth directly in Web! (Beta)
15
+ #### Try out Sinth directly in Web! (BETA. Uses an older version of Sinth.)
16
16
 
17
17
  ```html
18
18
  <div id="app"></div>
@@ -50,7 +50,7 @@ var int count = 0
50
50
 
51
51
  Main {
52
52
  Heading(level: 1) {count}
53
- Button(onClick: count = count + 1) {"Increment"}
53
+ Button(onClick: count += 1) {"Increment"}
54
54
  }
55
55
  ```
56
56
 
@@ -77,7 +77,7 @@ Button(onClick: "deletePost()") { "Delete" }
77
77
  }
78
78
  ```
79
79
 
80
- No JSX ternaries or v-if. No separate script tags. Mixed logic there where you need it.
80
+ No JSX ternaries or v-if or separate script tags. Mixed logic there where you need it.
81
81
 
82
82
  #### Sinth Language Syntax Reference
83
83
 
@@ -111,7 +111,7 @@ No JSX ternaries or v-if. No separate script tags. Mixed logic there where you n
111
111
  | `component MyComp { ... }` | Defines a reusable component |
112
112
  | `custom el "my-tag" { ... }` | Defines a custom element |
113
113
  | `custom MyCounter(export: <awesome-counter />)` | Exports a custom element tag |
114
-
114
+ | `$myFunction()` (e.g. `$alert("displayed once")`)| Indicator for Memorization - Useful for functions that should only show once |
115
115
 
116
116
 
117
117
  #### Functions - easy as you know it
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "1.7.0",
3
+ "version": "1.8.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",
@@ -432,6 +432,18 @@ export function renderChild(
432
432
  }
433
433
  if (child.expression.kind === "call" && child.expression.callee?.kind === "variable") {
434
434
  const fnName = child.expression.callee.name;
435
+ const isMemo = child.expression.memo === true;
436
+ if (isMemo) {
437
+ if (fnName && fnName.includes(".")) {
438
+ SinthWarning.emit(`'$' on '${fnName}()' has no effect — dotted calls cannot be memoized.`);
439
+ ctx.logicBlocks.push(compileExprToJS(child.expression) + ";");
440
+ return "";
441
+ }
442
+ const js = compileExprToJS(child.expression);
443
+ ctx.logicBlocks.push(`if(!_memo_${fnName}_done){_memo_${fnName}=${js};_memo_${fnName}_done=true;}`);
444
+ const exprId = registerExpr(ctx, { kind: "variable", name: `_memo_${fnName}` });
445
+ return `<span class="sinth-expr" data-expr-id="${exprId}"></span>`;
446
+ }
435
447
  if (fnName && fnName.includes(".")) {
436
448
  ctx.logicBlocks.push(compileExprToJS(child.expression) + ";");
437
449
  return "";
@@ -1124,8 +1136,22 @@ export function buildRuntime(opts: {
1124
1136
  return `let ${v.name} = ${val};`;
1125
1137
  }).join("\n");
1126
1138
 
1139
+ const memoVars = new Set<string>();
1140
+ for (const lb of logicBlocks) {
1141
+ let matches = lb.match(/_memo_(\w+)/g);
1142
+ if (matches) matches.forEach(m => memoVars.add(m));
1143
+ matches = lb.match(/_memo_(\w+)_done/g);
1144
+ if (matches) matches.forEach(m => memoVars.add(m));
1145
+ }
1146
+ for (const e of exprRegistry) {
1147
+ let matches = e.match(/_memo_(\w+)/g);
1148
+ if (matches) matches.forEach(m => memoVars.add(m));
1149
+ }
1150
+ const memoVarDecls = [...memoVars].map(m => `var ${m};`).join("\n");
1151
+
1127
1152
  if (!needsRender) {
1128
- return varLines ? `// Sinth compiled runtime\n${varLines}` : "";
1153
+ const out = [varLines, memoVarDecls].filter(Boolean).join("\n");
1154
+ return out ? `// Sinth compiled runtime\n${out}` : "";
1129
1155
  }
1130
1156
 
1131
1157
 
@@ -1197,6 +1223,7 @@ export function buildRuntime(opts: {
1197
1223
  const pageCode = `// Sinth Page Runtime
1198
1224
  ${forDataJS}
1199
1225
  ${exprArrayJS}
1226
+ ${memoVarDecls}
1200
1227
  ${varLines}
1201
1228
  ${renderFunc}`;
1202
1229
  if (opts.sharedRuntime && helpers.trim()) {
@@ -1209,6 +1236,7 @@ ${helpers}`;
1209
1236
  ${forDataJS}
1210
1237
  ${functionsJS ? functionsJS + "\n" : ""}${helpers}
1211
1238
  ${exprArrayJS}
1239
+ ${memoVarDecls}
1212
1240
  ${varLines}
1213
1241
  ${renderFunc}`;
1214
1242
  }
package/src/core/lexer.ts CHANGED
@@ -271,6 +271,6 @@ export class Lexer {
271
271
 
272
272
  private loc(): Loc { return { file: this.file, line: this.line, col: this.col }; }
273
273
  private isDigit(c: string) { return c >= "0" && c <= "9"; }
274
- private isIdentStart(c: string) { return /[a-zA-Z_\u00C0-\uFFFF]/.test(c); }
275
- private isIdentCont(c: string) { return /[a-zA-Z0-9_\-\u00C0-\uFFFF]/.test(c); }
274
+ private isIdentStart(c: string) { return /[a-zA-Z_$\u00C0-\uFFFF]/.test(c); }
275
+ private isIdentCont(c: string) { return /[a-zA-Z0-9_$\-\u00C0-\uFFFF]/.test(c); }
276
276
  }
@@ -323,7 +323,13 @@ private parseArrayLiteral(): Literal {
323
323
 
324
324
  // var or assignment
325
325
  if (tok.type === TT.IDENT) {
326
- const name = this.consume(TT.IDENT).value;
326
+ let memo = false;
327
+ let rawName = this.consume(TT.IDENT).value;
328
+ if (rawName.startsWith("$")) {
329
+ memo = true;
330
+ rawName = rawName.substring(1);
331
+ }
332
+ const name = rawName;
327
333
  let varExpr: Expression;
328
334
 
329
335
  // dot notation -> e.g. user.age
@@ -380,7 +386,13 @@ private parseArrayLiteral(): Literal {
380
386
  return { kind: "literal", value: lit };
381
387
  }
382
388
  if (tok.type === TT.IDENT) {
383
- const name = this.consume(TT.IDENT).value;
389
+ let memo = false;
390
+ let rawName = this.consume(TT.IDENT).value;
391
+ if (rawName.startsWith("$")) {
392
+ memo = true;
393
+ rawName = rawName.substring(1);
394
+ }
395
+ const name = rawName;
384
396
  let expr: Expression;
385
397
  if (this.check(TT.DOT)) {
386
398
  this.consume(TT.DOT);
@@ -388,6 +400,7 @@ private parseArrayLiteral(): Literal {
388
400
  } else {
389
401
  expr = { kind: "variable", name };
390
402
  }
403
+ if (memo) expr.memo = true;
391
404
  return expr;
392
405
  }
393
406
  if (tok.type === TT.LPAREN) {
@@ -897,7 +910,9 @@ private parseArrayLiteral(): Literal {
897
910
 
898
911
  if (nextType === TT.LPAREN) {
899
912
  const identName = this.peek().value;
900
- if (identName[0] === identName[0].toUpperCase() && identName[0] !== identName[0].toLowerCase()) {
913
+ const isMemo = identName.startsWith("$");
914
+ const realName = isMemo ? identName.substring(1) : identName;
915
+ if (realName[0] === realName[0].toUpperCase() && realName[0] !== realName[0].toLowerCase()) {
901
916
  children.push(this.parseCompUse());
902
917
  } else {
903
918
  this.consume(TT.IDENT);
@@ -911,7 +926,7 @@ private parseArrayLiteral(): Literal {
911
926
  }
912
927
  }
913
928
  this.consume(TT.RPAREN);
914
- const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
929
+ const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: realName }, args, memo: isMemo || undefined };
915
930
  children.push({ kind: "expr", expression: callExpr, loc });
916
931
  }
917
932
  continue;
@@ -1255,9 +1270,11 @@ private parseArrayLiteral(): Literal {
1255
1270
  }
1256
1271
 
1257
1272
  if (nextType === TT.DOT) {
1258
- let fullName = this.consume(TT.IDENT).value;
1273
+ let rawFirst = this.consume(TT.IDENT).value;
1274
+ const isMemo = rawFirst.startsWith("$");
1275
+ const firstPart = isMemo ? rawFirst.slice(1) : rawFirst;
1259
1276
  this.consume(TT.DOT);
1260
- fullName = fullName + "." + this.consume(TT.IDENT).value;
1277
+ const fullName = firstPart + "." + this.consume(TT.IDENT).value;
1261
1278
  const afterDotType = this.tokens[this.pos]?.type;
1262
1279
 
1263
1280
  if (afterDotType === TT.OP_PLUS) {
@@ -1284,11 +1301,27 @@ private parseArrayLiteral(): Literal {
1284
1301
  children.push({ kind: "expr", expression: leftExpr, loc });
1285
1302
  } else if (this.loopVar && fullName.split('.')[0] === this.loopVar) {
1286
1303
  children.push({ kind: "expr", expression: { kind: "variable", name: fullName }, loc });
1287
- } else if (afterDotType === TT.LPAREN || afterDotType === TT.LBRACE || afterDotType === TT.RAW_BLOCK) {
1304
+ } else if (afterDotType === TT.LPAREN) {
1305
+ this.consume(TT.LPAREN);
1306
+ const args: Expression[] = [];
1307
+ if (!this.check(TT.RPAREN)) {
1308
+ args.push(this.parseExpression()!);
1309
+ while (this.check(TT.COMMA)) {
1310
+ this.consume(TT.COMMA);
1311
+ args.push(this.parseExpression()!);
1312
+ }
1313
+ }
1314
+ this.consume(TT.RPAREN);
1315
+ const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: fullName }, args, memo: isMemo || undefined };
1316
+ children.push({ kind: "expr", expression: callExpr, loc });
1317
+ } else if (afterDotType === TT.LBRACE || afterDotType === TT.RAW_BLOCK) {
1288
1318
  throw new SinthError(`Unexpected dot notation before component usage`, loc);
1289
1319
  } else {
1290
- children.push({ kind: "expr", expression: { kind: "variable", name: fullName }, loc });
1320
+ const varExpr: Expression = { kind: "variable", name: fullName };
1321
+ if (isMemo) varExpr.memo = true;
1322
+ children.push({ kind: "expr", expression: varExpr, loc });
1291
1323
  }
1324
+ continue;
1292
1325
  } else if (nextType === TT.OP_PLUS) {
1293
1326
  const name = this.consume(TT.IDENT).value;
1294
1327
  let leftExpr: Expression = { kind: "variable", name };
@@ -1346,7 +1379,9 @@ private parseArrayLiteral(): Literal {
1346
1379
  }
1347
1380
  } else if (nextType === TT.LPAREN) {
1348
1381
  const identName = this.peek().value;
1349
- if (identName[0] === identName[0].toUpperCase() && identName[0] !== identName[0].toLowerCase()) {
1382
+ const isMemo = identName.startsWith("$");
1383
+ const realName = isMemo ? identName.substring(1) : identName;
1384
+ if (realName[0] === realName[0].toUpperCase() && realName[0] !== realName[0].toLowerCase()) {
1350
1385
  children.push(this.parseCompUse());
1351
1386
  continue;
1352
1387
  } else {
@@ -1361,7 +1396,7 @@ private parseArrayLiteral(): Literal {
1361
1396
  }
1362
1397
  }
1363
1398
  this.consume(TT.RPAREN);
1364
- let finalExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
1399
+ let finalExpr: Expression = { kind: "call", callee: { kind: "variable", name: realName }, args, memo: isMemo || undefined };
1365
1400
  while (this.check(TT.OP_PLUS)) {
1366
1401
  this.consume(TT.OP_PLUS);
1367
1402
  let rhs: Expression | null = null;
package/src/core/types.ts CHANGED
@@ -42,6 +42,7 @@ export type PostfixOp = "++" | "--";
42
42
 
43
43
  export interface Expression {
44
44
  kind: "literal" | "variable" | "unary" | "binary" | "assign" | "postfix" | "index" | "call" | "expr_ref";
45
+ memo?: boolean;
45
46
  value?: Literal;
46
47
  name?: string;
47
48
  op?: UnaryOp | BinaryOp | AssignOp | PostfixOp;