@yannosay/sinth 0.13.6 → 0.14.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
@@ -153,7 +153,7 @@ if done == true {
153
153
  remove "myElement"
154
154
  }
155
155
  ```
156
- Delete elements for good. No virtual DOM diffing.
156
+ Delete elements. No virtual DOM diffing!
157
157
 
158
158
  Compiles to pure HTML.
159
159
 
@@ -176,6 +176,53 @@ Main {
176
176
  ```
177
177
 
178
178
 
179
+
180
+ ```ts
181
+ page
182
+ title = "Counter Demo"
183
+
184
+ var int count = 0
185
+ var int addVar = 1
186
+ var int stepSize = 1
187
+
188
+ Main {
189
+ Heading(level: 1) {count}
190
+
191
+ Paragraph {"Increment by:"}
192
+ Input(bind: addVar, step: stepSize)
193
+
194
+ Br()
195
+ Button(onClick: count = count + addVar) {"Increment"}
196
+
197
+ Br()
198
+ Br()
199
+ Paragraph {"Step size for arrows:"}
200
+ Input(bind: stepSize, step: 1)
201
+ }
202
+ ```
203
+ ##### NEW! (0.14.0):
204
+ Sinth now supports Reactivity in Functions!
205
+ ```ts
206
+ page
207
+ title = "Reactive Counter"
208
+
209
+ var int apples = 1
210
+ var int bananas = 2
211
+ var int oranges = -6
212
+
213
+ function Counter(int value) -> ui {
214
+ Heading(level: 1) {"Count is at: " + (value) + "!"}
215
+ Button(onClick: value = value + 1) {"Increment"}
216
+ }
217
+
218
+ Main {
219
+ (Counter(apples))
220
+ (Counter(bananas))
221
+ (Counter(oranges))
222
+ }
223
+ ```
224
+
225
+
179
226
  # Install
180
227
  npm install -g @yannosay/sinth
181
228
 
@@ -198,11 +245,10 @@ Open your browser. Edit your .sinth file. Watch it update live.
198
245
  page
199
246
  title = "My Sinth Project"
200
247
 
201
- var str name = "World"
202
248
 
203
249
  Main {
204
- var str name = "User"
205
- Heading(level: 1) { "Hello, " + name }
250
+ var str nameVar = "User"
251
+ Heading(level: 1) { "Hello, " + nameVar }
206
252
  Paragraph { "Welcome to Sinth." }
207
253
  }
208
254
  ```
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.13.6",
3
+ "version": "0.14.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",
@@ -73,7 +73,7 @@ export function registerExpr(ctx: CompileCtx, expr: Expression): number {
73
73
  }
74
74
 
75
75
 
76
- export function renderAttr(attr: Attr, paramMap: Map<string, string>, ctx?: CompileCtx): string {
76
+ export function renderAttr(attr: Attr, paramMap: Map<string, string>, ctx: CompileCtx): string {
77
77
  const { name, value } = attr;
78
78
  if (value === null) return name;
79
79
  if (value.kind === "null") return "";
@@ -81,7 +81,23 @@ export function renderAttr(attr: Attr, paramMap: Map<string, string>, ctx?: Comp
81
81
 
82
82
  if (name === "model" && value?.kind === "str") {
83
83
  const vName = interpolateAttr(value.value, paramMap);
84
- return `oninput="(function(e){ ${vName} = e.target.value; sinthRender(); })(event)" value="${escAttr(vName)}"`;
84
+ const varDecl = ctx?.varDecls?.find(v => v.name === vName);
85
+ const rhs = (varDecl && varDecl.varType === "int")
86
+ ? `Number(e.target.value) || 0`
87
+ : `e.target.value`;
88
+ return `oninput="(function(e){ ${vName} = ${rhs}; sinthRender(); })(event)" value="${escAttr(vName)}"`;
89
+ }
90
+
91
+ if (name === "step" && value?.kind === "str") {
92
+ const raw = value.value;
93
+ if (raw.startsWith("__EXPR__")) {
94
+ try {
95
+ const expr: Expression = JSON.parse(raw.substring(8));
96
+ if (expr.kind === "variable" && expr.name) {
97
+ return `data-sinth-step="${escAttr(expr.name)}"`;
98
+ }
99
+ } catch {}
100
+ }
85
101
  }
86
102
 
87
103
 
@@ -271,19 +287,35 @@ export function renderChild(
271
287
  localParams.set(fnDef.params[i].name, "__LIT__" + arg.value.value);
272
288
  } else if (arg.kind === "variable" && arg.name) {
273
289
  localParams.set(fnDef.params[i].name, "__VAR__" + arg.name);
290
+ } else {
291
+ const exprId = registerExpr(ctx, arg);
292
+ localParams.set(fnDef.params[i].name, "__EXPRID__" + exprId);
274
293
  }
275
294
  }
276
295
  const substituteExpr = (expr: Expression, pm: Map<string, string>): Expression => {
277
296
  if (expr.kind === "variable" && pm.has(expr.name!)) {
278
297
  const pv = pm.get(expr.name!)!;
279
298
  if (pv.startsWith("__LIT__")) return { kind: "literal", value: { kind: "str", value: pv.slice(7) } };
299
+ if (pv.startsWith("__EXPRID__")) {
300
+ const id = parseInt(pv.slice(10), 10);
301
+ return { kind: "expr_ref", exprId: id };
302
+ }
280
303
  return { kind: "variable", name: pv.slice(7) };
281
304
  }
282
305
  if (expr.kind === "binary") return { ...expr, left: substituteExpr(expr.left!, pm), right: substituteExpr(expr.right!, pm) };
283
306
  if (expr.kind === "unary") return { ...expr, operand: substituteExpr(expr.operand!, pm) };
284
307
  if (expr.kind === "call") return { ...expr, callee: substituteExpr(expr.callee!, pm), args: expr.args?.map(a => substituteExpr(a, pm)) };
285
308
  if (expr.kind === "index") return { ...expr, object: substituteExpr(expr.object!, pm), key: substituteExpr(expr.key!, pm) };
286
- if (expr.kind === "assign") return { ...expr, right: expr.right ? substituteExpr(expr.right, pm) : undefined };
309
+ if (expr.kind === "assign") {
310
+ const target = expr.target;
311
+ if (target && pm.has(target)) {
312
+ const pv = pm.get(target)!;
313
+ if (pv.startsWith("__VAR__")) {
314
+ return { ...expr, target: pv.slice(7), right: expr.right ? substituteExpr(expr.right, pm) : undefined };
315
+ }
316
+ }
317
+ return { ...expr, right: expr.right ? substituteExpr(expr.right, pm) : undefined };
318
+ }
287
319
  return expr;
288
320
  };
289
321
  const substituteChild = (c: Child, pm: Map<string, string>): Child => {
@@ -303,6 +335,20 @@ export function renderChild(
303
335
  const sa: Attr[] = u.attrs.map(a => {
304
336
  if (a.value?.kind === "str") {
305
337
  let raw = a.value.value;
338
+ if (raw.startsWith("__EXPR__")) {
339
+ try {
340
+ const expr: Expression = JSON.parse(raw.substring(8));
341
+ const substituted = substituteExpr(expr, pm);
342
+ return { ...a, value: { kind: "str" as const, value: "__EXPR__" + JSON.stringify(substituted) } };
343
+ } catch {}
344
+ }
345
+ if (raw.startsWith("__MULTI_EXPR__")) {
346
+ try {
347
+ const exprs: Expression[] = JSON.parse(raw.substring("__MULTI_EXPR__".length));
348
+ const substituted = exprs.map(e => substituteExpr(e, pm));
349
+ return { ...a, value: { kind: "str" as const, value: "__MULTI_EXPR__" + JSON.stringify(substituted) } };
350
+ } catch {}
351
+ }
306
352
  for (const [pn, pv] of pm) {
307
353
  if (pv.startsWith("__LIT__")) raw = raw.replace(new RegExp(`\\b${pn}\\b`, 'g'), pv.slice(7));
308
354
  if (pv.startsWith("__VAR__")) raw = raw.replace(new RegExp(`\\b${pn}\\b`, 'g'), pv.slice(7));
@@ -318,6 +364,9 @@ export function renderChild(
318
364
  return fnDef.body.map(c => renderChild(substituteChild(c, localParams), ctx, localParams, depth + 1)).join("");
319
365
  }
320
366
  }
367
+ if (child.expression.kind === "expr_ref" && child.expression.exprId !== undefined) {
368
+ return `<span class="sinth-expr" data-expr-id="${child.expression.exprId}"></span>`;
369
+ }
321
370
  if (child.expression.kind === "literal" && child.expression.value) {
322
371
  return esc(litToString(child.expression.value));
323
372
  }
@@ -580,8 +629,21 @@ export function renderCompUse(
580
629
  if (expr.kind === "variable" && expr.name) vName = expr.name;
581
630
  } catch {}
582
631
  }
583
- if (!use.attrs.some(a => a.name === "type")) attrParts.push(`type="text"`);
584
- attrParts.push(`oninput="(function(e){ ${vName} = e.target.value; sinthRender(); })(event)"`);
632
+ const varDecl = ctx.varDecls?.find(v => v.name === vName);
633
+ if (!use.attrs.some(a => a.name === "type")) {
634
+ if (varDecl && varDecl.varType === "int") {
635
+ attrParts.push(`type="number"`);
636
+ } else {
637
+ attrParts.push(`type="text"`);
638
+ }
639
+ }
640
+ if (varDecl && varDecl.varType === "int" && !use.attrs.some(a => a.name === "step")) {
641
+ attrParts.push(`step="1"`);
642
+ }
643
+ const rhs = (varDecl && varDecl.varType === "int")
644
+ ? `Number(e.target.value) || 0`
645
+ : `e.target.value`;
646
+ attrParts.push(`oninput="(function(e){ ${vName} = ${rhs}; sinthRender(); })(event)"`);
585
647
  attrParts.push(`data-sinth-value="${escAttr(vName)}"`);
586
648
  }
587
649
  }
@@ -851,7 +913,6 @@ ${exprArrayJS}
851
913
  ${renderFunc}`;
852
914
  }
853
915
 
854
- // main compilation pipeline
855
916
 
856
917
  export interface CompileOptions {
857
918
  projectRoot: string;
@@ -865,14 +926,77 @@ export interface CompileOptions {
865
926
  export function compileFile(filePath: string, opts: CompileOptions): { html: string; shared?: string } | null {
866
927
  const absPath = path.resolve(filePath);
867
928
  const file = parseFile(absPath);
868
-
869
929
  const cfg: ResolverConfig = { projectRoot: opts.projectRoot, libraryPaths: opts.libraryPaths };
870
930
  const { allDefs, customEls, cssLinks, jsLinks } = resolveImports(file, cfg);
871
931
  const hash = fnv1a(absPath);
872
-
873
932
  const allVarDecls: VarDeclaration[] = file.varDecls;
874
933
  const functionDefs: FunctionDef[] = file.functions;
934
+
935
+ const declaredVars = new Set(allVarDecls.map(v => v.name));
936
+ const declaredFuncs = new Set(file.functions.map(f => f.name));
937
+
938
+ function collectExprVars(expr: Expression | undefined, vars: Set<string>): void {
939
+ if (!expr) return;
940
+ if (expr.kind === "variable" && expr.name && !declaredFuncs.has(expr.name)) vars.add(expr.name);
941
+ if (expr.kind === "assign" && expr.target) vars.add(expr.target);
942
+ if (expr.left) collectExprVars(expr.left, vars);
943
+ if (expr.right) collectExprVars(expr.right, vars);
944
+ if (expr.operand) collectExprVars(expr.operand, vars);
945
+ if (expr.object) collectExprVars(expr.object, vars);
946
+ if (expr.key) collectExprVars(expr.key, vars);
947
+ if (expr.args) expr.args.forEach(a => collectExprVars(a, vars));
948
+ }
875
949
 
950
+ function collectChildVars(child: Child, vars: Set<string>): void {
951
+ if (child.kind === "expr" || child.kind === "assign_stmt" || child.kind === "return") {
952
+ collectExprVars(child.expression, vars);
953
+ }
954
+ if (child.kind === "if") {
955
+ collectExprVars((child as IfBlock).condition, vars);
956
+ (child as IfBlock).body.forEach(c => collectChildVars(c, vars));
957
+ (child as IfBlock).elseBody?.forEach(c => collectChildVars(c, vars));
958
+ }
959
+ if (child.kind === "for") {
960
+ (child as ForLoop).body.forEach(c => collectChildVars(c, vars));
961
+ }
962
+ if (child.kind === "use") {
963
+ (child as CompUse).attrs.forEach(a => {
964
+ if (a.value?.kind === "str") {
965
+ const v = a.value.value;
966
+ if (v.startsWith("__EXPR__") || v.startsWith("__MULTI_EXPR__")) {
967
+ try {
968
+ const exprs: Expression[] = v.startsWith("__MULTI_EXPR__")
969
+ ? JSON.parse(v.substring("__MULTI_EXPR__".length))
970
+ : [JSON.parse(v.substring(8))];
971
+ exprs.forEach(e => collectExprVars(e, vars));
972
+ } catch {}
973
+ }
974
+ }
975
+ });
976
+ (child as CompUse).children.forEach(c => collectChildVars(c, vars));
977
+ }
978
+ }
979
+ const pageVars = new Set<string>();
980
+ file.uses.forEach(u => collectChildVars(u, pageVars));
981
+ for (const v of pageVars) {
982
+ if (!declaredVars.has(v)) {
983
+ throw new SinthError(
984
+ `Variable '${v}' is used but never declared. Add 'var str ${v}' or 'var int ${v}' before using it.`
985
+ );
986
+ }
987
+ }
988
+ for (const fn of file.functions) {
989
+ const fnParamNames = new Set(fn.params.map(p => p.name));
990
+ const fnBodyVars = new Set<string>();
991
+ fn.body.forEach(c => collectChildVars(c, fnBodyVars));
992
+ for (const v of fnBodyVars) {
993
+ if (!fnParamNames.has(v) && !declaredVars.has(v) && !declaredFuncs.has(v)) {
994
+ throw new SinthError(
995
+ `Variable '${v}' used in function '${fn.name}' is not declared. It must be a parameter or a page-level variable.`
996
+ );
997
+ }
998
+ }
999
+ }
876
1000
  const ctx: CompileCtx = {
877
1001
  allDefs, functionDefs, customEls, cssLinks, jsLinks,
878
1002
  scopeHash: hash,
@@ -884,6 +1008,7 @@ export function compileFile(filePath: string, opts: CompileOptions): { html: str
884
1008
  ifIdCounter: 0,
885
1009
  exprRegistry: [],
886
1010
  exprMap: new Map(),
1011
+ varDecls: allVarDecls,
887
1012
  };
888
1013
 
889
1014
  if (!file.isPage) {
@@ -43,19 +43,21 @@ export function buildRenderBody(opts: {
43
43
  }
44
44
  }
45
45
 
46
+ if (needsIf) {
47
+ renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);\n`;
48
+ }
46
49
  renderBody += ` document.querySelectorAll('[data-sinth-remove]').forEach(function(el) {
47
50
  var target = document.getElementById(el.dataset.sinthRemove);
48
51
  if (target) target.remove();
49
52
  });\n`;
50
-
51
- if (needsIf) {
52
- renderBody += ` document.querySelectorAll('template[data-sinth-if-expr]').forEach(sinthIfBlock);\n`;
53
- }
54
53
  if (needsFor) {
55
54
  renderBody += ` document.querySelectorAll('template[data-sinth-for]').forEach(sinthForBlock);\n`;
56
55
  }
57
56
  renderBody += ` document.querySelectorAll('[data-sinth-value]').forEach(function(el) {
58
- try { el.value = window[el.dataset.sinthValue] || ''; } catch(e) {}
57
+ try { if (document.activeElement !== el) el.value = window[el.dataset.sinthValue] || ''; } catch(e) {}
58
+ });\n`;
59
+ renderBody += ` document.querySelectorAll('[data-sinth-step]').forEach(function(el) {
60
+ try { el.step = window[el.dataset.sinthStep] || 1; } catch(e) {}
59
61
  });\n`;
60
62
  renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
61
63
  try { el.checked = !!window[el.dataset.sinthChecked]; } catch(e) {}
package/src/core/types.ts CHANGED
@@ -41,7 +41,7 @@ export type AssignOp = "=" | "+=" | "-=";
41
41
  export type PostfixOp = "++" | "--";
42
42
 
43
43
  export interface Expression {
44
- kind: "literal" | "variable" | "unary" | "binary" | "assign" | "postfix" | "index" | "call";
44
+ kind: "literal" | "variable" | "unary" | "binary" | "assign" | "postfix" | "index" | "call" | "expr_ref";
45
45
  value?: Literal;
46
46
  name?: string;
47
47
  op?: UnaryOp | BinaryOp | AssignOp | PostfixOp;
@@ -53,6 +53,7 @@ export interface Expression {
53
53
  key?: Expression;
54
54
  callee?: Expression;
55
55
  args?: Expression[];
56
+ exprId?: number; // for "expr_ref" kind
56
57
  }
57
58
 
58
59
  export interface Attr { name: string; value: Literal | null; loc: Loc }
@@ -142,6 +143,7 @@ export interface CompileCtx {
142
143
  ifIdCounter: number;
143
144
  exprRegistry: string[];
144
145
  exprMap: Map<string, number>;
146
+ varDecls?: VarDeclaration[];
145
147
  loopVars?: Set<string>;
146
148
  }
147
149
 
package/tsconfig.json CHANGED
@@ -5,11 +5,11 @@
5
5
  "lib": ["ES2020"],
6
6
  "outDir": "./dist",
7
7
  "rootDir": "./src",
8
- "strict": false,
8
+ "strict": true,
9
9
  "esModuleInterop": true,
10
10
  "skipLibCheck": true,
11
- "noImplicitAny": false,
12
- "strictNullChecks": false,
11
+ "noImplicitAny": true,
12
+ "strictNullChecks": true,
13
13
  "allowImportingTsExtensions": true,
14
14
  "noEmit": true,
15
15
  "types": ["node"],