@yannosay/sinth 1.6.2 → 1.8.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 +4 -4
- package/package.json +1 -1
- package/src/core/compiler.ts +55 -5
- package/src/core/lexer.ts +2 -2
- package/src/core/parser.ts +57 -14
- package/src/core/runtime/render.ts +5 -1
- package/src/core/types.ts +1 -0
package/README.md
CHANGED
|
@@ -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! (
|
|
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
|
|
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
|
|
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
package/src/core/compiler.ts
CHANGED
|
@@ -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 "";
|
|
@@ -1104,6 +1116,15 @@ export function buildRuntime(opts: {
|
|
|
1104
1116
|
const val = litToString(v.value);
|
|
1105
1117
|
if (val.startsWith("__VAR__")) return `let ${v.name} = ${val.slice(7)};`;
|
|
1106
1118
|
if (val.startsWith("__ARR__")) return `let ${v.name} = ${val.slice(7)};`;
|
|
1119
|
+
if (val.startsWith("__EXPR__")) {
|
|
1120
|
+
try {
|
|
1121
|
+
const expr: Expression = JSON.parse(val.substring(8));
|
|
1122
|
+
const js = compileExprToJS(expr);
|
|
1123
|
+
const id = exprRegistry.length;
|
|
1124
|
+
exprRegistry.push(js);
|
|
1125
|
+
return `let ${v.name} = __X[${id}]({});`;
|
|
1126
|
+
} catch { return `let ${v.name} = ${val};`; }
|
|
1127
|
+
}
|
|
1107
1128
|
if (v.varType === "obj") return `let ${v.name} = ${val};`;
|
|
1108
1129
|
if (v.varType === "str") return `let ${v.name} = ${JSON.stringify(val)};`;
|
|
1109
1130
|
if (v.varType === "str[]" && typeof val === 'string' && val.startsWith("__ARR__")) {
|
|
@@ -1115,16 +1136,44 @@ export function buildRuntime(opts: {
|
|
|
1115
1136
|
return `let ${v.name} = ${val};`;
|
|
1116
1137
|
}).join("\n");
|
|
1117
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
|
+
|
|
1118
1152
|
if (!needsRender) {
|
|
1119
|
-
|
|
1153
|
+
const out = [varLines, memoVarDecls].filter(Boolean).join("\n");
|
|
1154
|
+
return out ? `// Sinth compiled runtime\n${out}` : "";
|
|
1120
1155
|
}
|
|
1121
1156
|
|
|
1122
1157
|
|
|
1123
1158
|
const helpers = generateHelpers({ needsExpr, needsIf, needsFor, needsDelay, needsMixed });
|
|
1124
1159
|
|
|
1160
|
+
const exprVarUpdates = varDecls
|
|
1161
|
+
.filter(v => v.value && litToString(v.value).startsWith("__EXPR__"))
|
|
1162
|
+
.map(v => {
|
|
1163
|
+
try {
|
|
1164
|
+
const expr: Expression = JSON.parse(litToString(v.value!).substring(8));
|
|
1165
|
+
const js = compileExprToJS(expr);
|
|
1166
|
+
const idx = exprRegistry.indexOf(js);
|
|
1167
|
+
return `${v.name} = __X[${idx}]({});`;
|
|
1168
|
+
} catch { return ""; }
|
|
1169
|
+
})
|
|
1170
|
+
.filter(Boolean)
|
|
1171
|
+
.join("\n ");
|
|
1172
|
+
|
|
1125
1173
|
const renderBody = buildRenderBody({
|
|
1126
1174
|
bodyHTML, logicBlocks, mixedBlocks,
|
|
1127
|
-
needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay
|
|
1175
|
+
needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay,
|
|
1176
|
+
exprVarUpdates: exprVarUpdates || undefined
|
|
1128
1177
|
});
|
|
1129
1178
|
|
|
1130
1179
|
let forDataJS = "";
|
|
@@ -1172,11 +1221,11 @@ export function buildRuntime(opts: {
|
|
|
1172
1221
|
}
|
|
1173
1222
|
|
|
1174
1223
|
const pageCode = `// Sinth Page Runtime
|
|
1175
|
-
${varLines}
|
|
1176
1224
|
${forDataJS}
|
|
1177
1225
|
${exprArrayJS}
|
|
1226
|
+
${memoVarDecls}
|
|
1227
|
+
${varLines}
|
|
1178
1228
|
${renderFunc}`;
|
|
1179
|
-
|
|
1180
1229
|
if (opts.sharedRuntime && helpers.trim()) {
|
|
1181
1230
|
const sharedCode = `// Sinth shared runtime
|
|
1182
1231
|
${helpers}`;
|
|
@@ -1184,10 +1233,11 @@ ${helpers}`;
|
|
|
1184
1233
|
}
|
|
1185
1234
|
|
|
1186
1235
|
return `// Sinth compiled runtime
|
|
1187
|
-
${varLines}
|
|
1188
1236
|
${forDataJS}
|
|
1189
1237
|
${functionsJS ? functionsJS + "\n" : ""}${helpers}
|
|
1190
1238
|
${exprArrayJS}
|
|
1239
|
+
${memoVarDecls}
|
|
1240
|
+
${varLines}
|
|
1191
1241
|
${renderFunc}`;
|
|
1192
1242
|
}
|
|
1193
1243
|
|
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_
|
|
275
|
-
private isIdentCont(c: string) { return /[a-zA-Z0-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); }
|
|
276
276
|
}
|
package/src/core/parser.ts
CHANGED
|
@@ -188,7 +188,7 @@ if (nextType === TT.LPAREN && identName[0] === identName[0].toLowerCase()) {
|
|
|
188
188
|
|
|
189
189
|
private parseVarDeclaration(): VarDeclaration | null {
|
|
190
190
|
const loc = this.peek().loc;
|
|
191
|
-
this.consume(TT.IDENT);
|
|
191
|
+
this.consume(TT.IDENT);
|
|
192
192
|
|
|
193
193
|
const typeTok = this.consume(TT.IDENT);
|
|
194
194
|
let typeStr = typeTok.value;
|
|
@@ -207,9 +207,17 @@ if (nextType === TT.LPAREN && identName[0] === identName[0].toLowerCase()) {
|
|
|
207
207
|
this.consume(TT.EQUALS);
|
|
208
208
|
if (this.check(TT.LBRACKET)) {
|
|
209
209
|
val = this.parseArrayLiteral();
|
|
210
|
-
} else if (this.check(TT.IDENT)) {
|
|
211
|
-
const
|
|
212
|
-
|
|
210
|
+
} else if (this.check(TT.IDENT) || this.check(TT.LPAREN)) {
|
|
211
|
+
const savedPos = this.pos;
|
|
212
|
+
const expr = this.parseExpression();
|
|
213
|
+
if (expr && (expr.kind !== "literal" || (expr.kind === "literal" && expr.value?.kind === "str" && expr.value.value.startsWith("__VAR__")))) {
|
|
214
|
+
val = { kind: "str", value: `__EXPR__${JSON.stringify(expr)}` };
|
|
215
|
+
} else {
|
|
216
|
+
this.pos = savedPos;
|
|
217
|
+
val = this.parseLiteral();
|
|
218
|
+
}
|
|
219
|
+
} else if (this.check(TT.NUMBER)) {
|
|
220
|
+
val = this.parseLiteral();
|
|
213
221
|
} else {
|
|
214
222
|
val = this.parseLiteral();
|
|
215
223
|
}
|
|
@@ -315,7 +323,13 @@ private parseArrayLiteral(): Literal {
|
|
|
315
323
|
|
|
316
324
|
// var or assignment
|
|
317
325
|
if (tok.type === TT.IDENT) {
|
|
318
|
-
|
|
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;
|
|
319
333
|
let varExpr: Expression;
|
|
320
334
|
|
|
321
335
|
// dot notation -> e.g. user.age
|
|
@@ -372,7 +386,13 @@ private parseArrayLiteral(): Literal {
|
|
|
372
386
|
return { kind: "literal", value: lit };
|
|
373
387
|
}
|
|
374
388
|
if (tok.type === TT.IDENT) {
|
|
375
|
-
|
|
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;
|
|
376
396
|
let expr: Expression;
|
|
377
397
|
if (this.check(TT.DOT)) {
|
|
378
398
|
this.consume(TT.DOT);
|
|
@@ -380,6 +400,7 @@ private parseArrayLiteral(): Literal {
|
|
|
380
400
|
} else {
|
|
381
401
|
expr = { kind: "variable", name };
|
|
382
402
|
}
|
|
403
|
+
if (memo) expr.memo = true;
|
|
383
404
|
return expr;
|
|
384
405
|
}
|
|
385
406
|
if (tok.type === TT.LPAREN) {
|
|
@@ -889,7 +910,9 @@ private parseArrayLiteral(): Literal {
|
|
|
889
910
|
|
|
890
911
|
if (nextType === TT.LPAREN) {
|
|
891
912
|
const identName = this.peek().value;
|
|
892
|
-
|
|
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()) {
|
|
893
916
|
children.push(this.parseCompUse());
|
|
894
917
|
} else {
|
|
895
918
|
this.consume(TT.IDENT);
|
|
@@ -903,7 +926,7 @@ private parseArrayLiteral(): Literal {
|
|
|
903
926
|
}
|
|
904
927
|
}
|
|
905
928
|
this.consume(TT.RPAREN);
|
|
906
|
-
const callExpr: Expression = { kind: "call", callee: { kind: "variable", name:
|
|
929
|
+
const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: realName }, args, memo: isMemo || undefined };
|
|
907
930
|
children.push({ kind: "expr", expression: callExpr, loc });
|
|
908
931
|
}
|
|
909
932
|
continue;
|
|
@@ -1247,9 +1270,11 @@ private parseArrayLiteral(): Literal {
|
|
|
1247
1270
|
}
|
|
1248
1271
|
|
|
1249
1272
|
if (nextType === TT.DOT) {
|
|
1250
|
-
let
|
|
1273
|
+
let rawFirst = this.consume(TT.IDENT).value;
|
|
1274
|
+
const isMemo = rawFirst.startsWith("$");
|
|
1275
|
+
const firstPart = isMemo ? rawFirst.slice(1) : rawFirst;
|
|
1251
1276
|
this.consume(TT.DOT);
|
|
1252
|
-
fullName =
|
|
1277
|
+
const fullName = firstPart + "." + this.consume(TT.IDENT).value;
|
|
1253
1278
|
const afterDotType = this.tokens[this.pos]?.type;
|
|
1254
1279
|
|
|
1255
1280
|
if (afterDotType === TT.OP_PLUS) {
|
|
@@ -1276,11 +1301,27 @@ private parseArrayLiteral(): Literal {
|
|
|
1276
1301
|
children.push({ kind: "expr", expression: leftExpr, loc });
|
|
1277
1302
|
} else if (this.loopVar && fullName.split('.')[0] === this.loopVar) {
|
|
1278
1303
|
children.push({ kind: "expr", expression: { kind: "variable", name: fullName }, loc });
|
|
1279
|
-
} else if (afterDotType === TT.LPAREN
|
|
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) {
|
|
1280
1318
|
throw new SinthError(`Unexpected dot notation before component usage`, loc);
|
|
1281
1319
|
} else {
|
|
1282
|
-
|
|
1320
|
+
const varExpr: Expression = { kind: "variable", name: fullName };
|
|
1321
|
+
if (isMemo) varExpr.memo = true;
|
|
1322
|
+
children.push({ kind: "expr", expression: varExpr, loc });
|
|
1283
1323
|
}
|
|
1324
|
+
continue;
|
|
1284
1325
|
} else if (nextType === TT.OP_PLUS) {
|
|
1285
1326
|
const name = this.consume(TT.IDENT).value;
|
|
1286
1327
|
let leftExpr: Expression = { kind: "variable", name };
|
|
@@ -1338,7 +1379,9 @@ private parseArrayLiteral(): Literal {
|
|
|
1338
1379
|
}
|
|
1339
1380
|
} else if (nextType === TT.LPAREN) {
|
|
1340
1381
|
const identName = this.peek().value;
|
|
1341
|
-
|
|
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()) {
|
|
1342
1385
|
children.push(this.parseCompUse());
|
|
1343
1386
|
continue;
|
|
1344
1387
|
} else {
|
|
@@ -1353,7 +1396,7 @@ private parseArrayLiteral(): Literal {
|
|
|
1353
1396
|
}
|
|
1354
1397
|
}
|
|
1355
1398
|
this.consume(TT.RPAREN);
|
|
1356
|
-
let finalExpr: Expression = { kind: "call", callee: { kind: "variable", name:
|
|
1399
|
+
let finalExpr: Expression = { kind: "call", callee: { kind: "variable", name: realName }, args, memo: isMemo || undefined };
|
|
1357
1400
|
while (this.check(TT.OP_PLUS)) {
|
|
1358
1401
|
this.consume(TT.OP_PLUS);
|
|
1359
1402
|
let rhs: Expression | null = null;
|
|
@@ -10,8 +10,9 @@ export function buildRenderBody(opts: {
|
|
|
10
10
|
needsFor: boolean;
|
|
11
11
|
needsExpr: boolean;
|
|
12
12
|
needsDelay: boolean;
|
|
13
|
+
exprVarUpdates?: string;
|
|
13
14
|
}): string {
|
|
14
|
-
const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay } = opts;
|
|
15
|
+
const { bodyHTML, logicBlocks, mixedBlocks, needsLogic, needsMixed, needsIf, needsFor, needsExpr, needsDelay, exprVarUpdates } = opts;
|
|
15
16
|
let renderBody = "";
|
|
16
17
|
renderBody += ` let _sx = window.scrollX, _sy = window.scrollY;\n`;
|
|
17
18
|
|
|
@@ -63,6 +64,9 @@ export function buildRenderBody(opts: {
|
|
|
63
64
|
renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
|
|
64
65
|
try { let c=el.dataset.sinthChecked; el.checked = __X[c] ? !!__X[c]({}) : false; } catch(e) {}
|
|
65
66
|
});\n`;
|
|
67
|
+
if (exprVarUpdates) {
|
|
68
|
+
renderBody += ` ${exprVarUpdates}\n`;
|
|
69
|
+
}
|
|
66
70
|
if (bodyHTML.includes("data-sinth-checked-expr")) {
|
|
67
71
|
renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
|
|
68
72
|
try {
|
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;
|