@yannosay/sinth 0.11.0 → 0.12.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
@@ -1,7 +1,9 @@
1
1
  # Sinth
2
2
 
3
3
  The language that fixes HTML. Declarative. Reactive. Compiles to pure HTML.
4
+
4
5
  Find out more: https://www.youtube.com/watch?v=W0tOMTiIF0Q
6
+ Discord: [Join here!](https://discord.gg/SUvcrafTQm)
5
7
 
6
8
 
7
9
  [![npm version](https://img.shields.io/npm/v/@yannosay/sinth)](https://www.npmjs.com/package/@yannosay/sinth)
@@ -52,7 +54,32 @@ Div {
52
54
  (renderHeading(userName))
53
55
  (renderHeading("This renders!"))
54
56
  ```
57
+ or
58
+
59
+ ```ts
60
+ page
61
+
62
+ title = "Checkbox Test"
63
+
64
+ var bool isAdmin = false
65
+
66
+ function toggleAdmin() {
67
+ isAdmin = not isAdmin
68
+ }
69
+
70
+ function getStatus() -> str {
71
+ if (isAdmin) {
72
+ return "ON"
73
+ }
74
+ return "OFF"
75
+ }
55
76
 
77
+ Checkbox(checked: isAdmin, onChange: toggleAdmin(), label: "Admin mode?")
78
+
79
+ Paragraph {
80
+ "Admin mode is " + (getStatus()) + "! :D"
81
+ }
82
+ ```
56
83
 
57
84
 
58
85
  #### Animations that make sense.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yannosay/sinth",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "A declarative language that compiles to pure HTML, CSS, and JavaScript.",
5
5
  "author": "Yannosay Productions",
6
6
  "license": "AGPL-3.0",
@@ -666,7 +666,12 @@ export function renderChild(
666
666
 
667
667
  case "remove": {
668
668
  return `<span data-sinth-remove="${esc((child as RemoveStmt).target)}"></span>`;
669
- }
669
+ }
670
+
671
+ case "return":
672
+ return "";
673
+
674
+
670
675
 
671
676
  case "if":
672
677
  return renderIfBlock(child, ctx, params, depth);
@@ -847,6 +852,7 @@ export function renderCompUse(
847
852
  if (attr.value?.kind === "str") userClass = interpolateAttr(attr.value.value, params);
848
853
  continue;
849
854
  }
855
+ if (use.name === "Checkbox" && (attr.name === "checked" || attr.name === "onChange" || attr.name === "label")) continue;
850
856
  const rendered = renderAttr(attr, params, ctx);
851
857
  if (rendered) attrParts.push(rendered);
852
858
  }
@@ -874,17 +880,61 @@ export function renderCompUse(
874
880
  }
875
881
  if (use.name === "Checkbox") {
876
882
  if (!use.attrs.some(a => a.name === "type")) attrParts.push(`type="checkbox"`);
877
- const bindAttr = use.attrs.find(a => a.name === "bind");
878
- if (bindAttr?.value?.kind === "str") {
879
- let vName = bindAttr.value.value;
880
- if (vName.startsWith("__EXPR__")) {
883
+
884
+ const checkedAttr = use.attrs.find(a => a.name === "checked");
885
+ if (checkedAttr && checkedAttr.value) {
886
+ if (checkedAttr.value.kind === "bool") {
887
+ if (checkedAttr.value.value) attrParts.push(`checked`);
888
+ } else if (checkedAttr.value.kind === "str") {
889
+ let raw = checkedAttr.value.value;
890
+ if (raw.startsWith("__EXPR__")) {
891
+ try {
892
+ const expr: Expression = JSON.parse(raw.substring(8));
893
+ if (expr.kind === "variable" && expr.name) {
894
+ attrParts.push(`data-sinth-checked="${escAttr(expr.name)}"`);
895
+ } else {
896
+ const exprId = registerExpr(ctx, expr);
897
+ attrParts.push(`data-sinth-checked-expr="${exprId}"`);
898
+ }
899
+ } catch {}
900
+ } else {
901
+ attrParts.push(`data-sinth-checked="${escAttr(raw)}"`);
902
+ }
903
+ }
904
+ }
905
+
906
+ const onChangeAttr = use.attrs.find(a => a.name === "onChange");
907
+ if (onChangeAttr && onChangeAttr.value?.kind === "str") {
908
+ let raw = onChangeAttr.value.value;
909
+ if (raw.startsWith("__EXPR__")) {
881
910
  try {
882
- const expr: Expression = JSON.parse(vName.substring(8));
883
- if (expr.kind === "variable" && expr.name) vName = expr.name;
911
+ const expr: Expression = JSON.parse(raw.substring(8));
912
+ const jsExpr = compileExprToJS(expr);
913
+ attrParts.push(`onchange="(function(){ ${jsExpr}; sinthRender(); })(event)"`);
914
+ } catch {}
915
+ } else {
916
+ attrParts.push(`onchange="${escAttr(raw)};sinthRender()"`);
917
+ }
918
+ } else if (!onChangeAttr && checkedAttr) {
919
+ const raw = checkedAttr.value?.kind === "str" ? checkedAttr.value.value : "";
920
+ if (raw.startsWith("__EXPR__")) {
921
+ try {
922
+ const expr: Expression = JSON.parse(raw.substring(8));
923
+ if (expr.kind === "variable" && expr.name) {
924
+ attrParts.push(`onchange="(function(e){ ${expr.name} = e.target.checked; sinthRender(); })(event)"`);
925
+ }
884
926
  } catch {}
927
+ } else if (raw.match(/^[a-zA-Z_][a-zA-Z0-9_]*$/)) {
928
+ attrParts.push(`onchange="(function(e){ ${raw} = e.target.checked; sinthRender(); })(event)"`);
885
929
  }
886
- attrParts.push(`onchange="(function(e){ ${vName} = e.target.checked; sinthRender(); })(event)"`);
887
- attrParts.push(`data-sinth-checked="${escAttr(vName)}"`);
930
+ }
931
+
932
+ const labelAttr = use.attrs.find(a => a.name === "label");
933
+ const labelText = labelAttr?.value?.kind === "str" ? esc(interpolateAttr(labelAttr.value.value, params)) : "";
934
+
935
+ if (labelText) {
936
+ const checkAttrStr = attrParts.length ? " " + attrParts.join(" ") : "";
937
+ return `<label data-s="${ctx.scopeHash}"><input${checkAttrStr}> ${labelText}</label>`;
888
938
  }
889
939
  }
890
940
 
@@ -1094,6 +1144,9 @@ function compileFunctionDef(fn: FunctionDef, ctx: CompileCtx): string {
1094
1144
  break;
1095
1145
  case "for":
1096
1146
  break;
1147
+ case "return":
1148
+ bodyStatements.push(`return ${child.expression ? compileExprToJS(child.expression) : ""};`);
1149
+ break;
1097
1150
  }
1098
1151
  }
1099
1152
  return `function ${fn.name}(${paramsJS}) {\n${bodyStatements.map(s => ` ${s}`).join("\n")}\n}`;
@@ -1458,6 +1511,14 @@ function sinthForBlock(t) {
1458
1511
  renderBody += ` document.querySelectorAll('[data-sinth-checked]').forEach(function(el) {
1459
1512
  try { el.checked = !!window[el.dataset.sinthChecked]; } catch(e) {}
1460
1513
  });\n`;
1514
+ if (bodyHTML.includes("data-sinth-checked-expr")) {
1515
+ renderBody += ` document.querySelectorAll('[data-sinth-checked-expr]').forEach(function(el) {
1516
+ try {
1517
+ var exprFn = __X[el.dataset.sinthCheckedExpr];
1518
+ if (exprFn) el.checked = !!exprFn({});
1519
+ } catch(e) {}
1520
+ });\n`;
1521
+ }
1461
1522
  if (needsExpr) {
1462
1523
  renderBody += ` document.querySelectorAll('.sinth-expr').forEach(sinthExpr);\n`;
1463
1524
  }
package/src/core/expr.ts CHANGED
@@ -1,4 +1,5 @@
1
- import { Expression, Child, AssignStmt, IfBlock, LitStr, LitNum, LitBool } from "./types.ts";
1
+ import { Expression, Child, AssignStmt, IfBlock, ReturnStmt, LitStr, LitNum, LitBool } from "./types.ts";
2
+
2
3
 
3
4
  export function compileExprToJS(expr: Expression, loopVars?: Set<string>): string {
4
5
  switch (expr.kind) {
@@ -55,9 +56,10 @@ export function compileIfToJS(ifBlock: IfBlock): string {
55
56
 
56
57
  export function bodyToJS(children: Child[]): string {
57
58
  return children
58
- .filter(c => c.kind === "assign_stmt" || c.kind === "if")
59
+ .filter(c => c.kind === "assign_stmt" || c.kind === "if" || c.kind === "return")
59
60
  .map(c => {
60
61
  if (c.kind === "assign_stmt") return ` ${compileExprToJS((c as AssignStmt).expression)};\n`;
62
+ if (c.kind === "return") return ` return ${(c as ReturnStmt).expression ? compileExprToJS((c as ReturnStmt).expression!) : ""};\n`;
61
63
  if (c.kind === "if") return compileIfToJS(c as IfBlock).replace(/^/gm, " ") + "\n";
62
64
  return "";
63
65
  })
package/src/core/lexer.ts CHANGED
@@ -22,6 +22,7 @@ const KEYWORDS: Record<string, TT> = {
22
22
  "in": TT.KW_IN,
23
23
  "remove": TT.KW_REMOVE,
24
24
  "function": TT.KW_FUNCTION,
25
+ "return": TT.KW_RETURN,
25
26
  };
26
27
 
27
28
  export class Lexer {
@@ -53,6 +54,7 @@ export class Lexer {
53
54
  // two-character operators ->
54
55
  if (ch === "=" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_EQEQ, value: "==", loc }); continue; }
55
56
  if (ch === "!" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_NEQ, value: "!=", loc }); continue; }
57
+ if (ch === "!") { tokens.push(this.single(TT.OP_NOT, loc)); continue; }
56
58
  if (ch === ">" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_GTEQ, value: ">=", loc }); continue; }
57
59
  if (ch === "<" && this.src[this.pos + 1] === "=") { this.adv(); this.adv(); tokens.push({ type: TT.OP_LTEQ, value: "<=", loc }); continue; }
58
60
 
@@ -1,4 +1,4 @@
1
- import { TT, Token, Loc, Literal, Expression, Attr, Child, CompUse, IfBlock, ForLoop, RemoveStmt, ParamDecl, StyleBlock, ScriptBlock, CompDef, CustomElDecl, VarDeclaration, ImportNode, MetaEntry, FunctionDef, SinthFile, SinthError, SinthWarning, AssignOp, VarType, BinaryOp } from "./types.ts";
1
+ import { TT, Token, Loc, Literal, Expression, Attr, Child, CompUse, IfBlock, ForLoop, RemoveStmt, ReturnStmt, ParamDecl, StyleBlock, ScriptBlock, CompDef, CustomElDecl, VarDeclaration, ImportNode, MetaEntry, FunctionDef, SinthFile, SinthError, SinthWarning, AssignOp, VarType, BinaryOp } from "./types.ts";
2
2
  import { Lexer } from "./lexer.ts";
3
3
  import { compileExprToJS, compileIfToJS } from "./expr.ts";
4
4
  import { tagNameToPascal } from "../utils.ts";
@@ -262,8 +262,8 @@ private parseArrayLiteral(): Literal {
262
262
  const tok = this.peek();
263
263
 
264
264
  // Unary 'not'
265
- if (tok.type === TT.IDENT && tok.value === "not") {
266
- this.consume(TT.IDENT);
265
+ if ((tok.type === TT.IDENT && tok.value === "not") || tok.type === TT.OP_NOT) {
266
+ if (tok.type === TT.IDENT) this.consume(TT.IDENT); else this.consume(TT.OP_NOT);
267
267
  const operand = this.parseExpression();
268
268
  if (!operand) throw new SinthError("Expected expression after 'not'", tok.loc);
269
269
  const unaryExpr: Expression = { kind: "unary", op: "not", operand };
@@ -433,6 +433,15 @@ private parseArrayLiteral(): Literal {
433
433
  return { sinthName, tagName, params, loc };
434
434
  }
435
435
 
436
+ private parseReturnStmt(): ReturnStmt {
437
+ const loc = this.consume(TT.KW_RETURN).loc;
438
+ let expression: Expression | undefined;
439
+ if (!this.check(TT.RBRACE) && !this.check(TT.EOF) && !this.check(TT.KW_IF) && !this.check(TT.KW_FOR) && !this.check(TT.KW_RETURN)) {
440
+ expression = this.parseExpression() ?? undefined;
441
+ }
442
+ return { kind: "return", expression, loc };
443
+ }
444
+
436
445
  private parseParamDecls(): ParamDecl[] {
437
446
  const params: ParamDecl[] = [];
438
447
  if (!this.check(TT.LPAREN)) return params;
@@ -481,6 +490,7 @@ private parseArrayLiteral(): Literal {
481
490
  if (vd) this._varDecls.push(vd);
482
491
  continue;
483
492
  }
493
+ else if (this.check(TT.KW_RETURN)){ body.push(this.parseReturnStmt()); }
484
494
  else if (this.check(TT.KW_IF)) { body.push(this.parseIfBlock()); }
485
495
  else if (this.check(TT.KW_FOR)) { body.push(this.parseForLoop()); }
486
496
  else if (this.check(TT.IDENT)) { body.push(this.parseCompUse()); }
@@ -524,6 +534,7 @@ private parseArrayLiteral(): Literal {
524
534
  const children: Child[] = [];
525
535
 
526
536
  while (!this.check(TT.RBRACE) && !this.check(TT.EOF)) {
537
+ if (this.check(TT.KW_RETURN)) { children.push(this.parseReturnStmt()); continue; }
527
538
  if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); continue; }
528
539
  if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); continue; }
529
540
  if (this.check(TT.LPAREN)) {
@@ -534,6 +545,23 @@ private parseArrayLiteral(): Literal {
534
545
  }
535
546
  continue;
536
547
  }
548
+ if (this.check(TT.IDENT)) {
549
+ const savedPos = this.pos;
550
+ const name = this.peek().value;
551
+ const nextType = this.tokens[this.pos + 1]?.type;
552
+ if (nextType === TT.EQUALS || (nextType === TT.OP_PLUS && this.tokens[this.pos + 2]?.type === TT.EQUALS) || (nextType === TT.OP_MINUS && this.tokens[this.pos + 2]?.type === TT.EQUALS)) {
553
+ this.consume(TT.IDENT);
554
+ let op: AssignOp = "=";
555
+ if (this.check(TT.OP_PLUS)) { this.consume(TT.OP_PLUS); op = "+="; }
556
+ else if (this.check(TT.OP_MINUS)) { this.consume(TT.OP_MINUS); op = "-="; }
557
+ this.consume(TT.EQUALS);
558
+ const rhs = this.parseExpression();
559
+ if (!rhs) throw new SinthError("Expected expression after assignment", this.peek().loc);
560
+ children.push({ kind: "assign_stmt", expression: { kind: "assign", target: name, op, right: rhs }, loc: this.peek().loc });
561
+ continue;
562
+ }
563
+ this.pos = savedPos;
564
+ }
537
565
  if (this.check(TT.STRING)) {
538
566
  const loc = this.peek().loc;
539
567
  children.push({ kind: "text", value: this.consume(TT.STRING).value, loc });
@@ -875,6 +903,7 @@ private parseArrayLiteral(): Literal {
875
903
  if (vd) this._varDecls.push(vd);
876
904
  continue;
877
905
  }
906
+ else if (this.check(TT.KW_RETURN)) { children.push(this.parseReturnStmt()); continue; }
878
907
  else if (this.check(TT.KW_IF)) { children.push(this.parseIfBlock()); }
879
908
  else if (this.check(TT.KW_FOR)) { children.push(this.parseForLoop()); }
880
909
  else if (this.check(TT.IDENT)) {
@@ -882,6 +911,18 @@ private parseArrayLiteral(): Literal {
882
911
  const rawName = this.peek().value;
883
912
  const nextType = this.tokens[this.pos + 1]?.type;
884
913
 
914
+ if (nextType === TT.EQUALS || (nextType === TT.OP_PLUS && this.tokens[this.pos + 2]?.type === TT.EQUALS) || (nextType === TT.OP_MINUS && this.tokens[this.pos + 2]?.type === TT.EQUALS)) {
915
+ this.consume(TT.IDENT);
916
+ let op: AssignOp = "=";
917
+ if (this.check(TT.OP_PLUS)) { this.consume(TT.OP_PLUS); op = "+="; }
918
+ else if (this.check(TT.OP_MINUS)) { this.consume(TT.OP_MINUS); op = "-="; }
919
+ this.consume(TT.EQUALS);
920
+ const rhs = this.parseExpression();
921
+ if (!rhs) throw new SinthError("Expected expression after assignment", this.peek().loc);
922
+ children.push({ kind: "assign_stmt", expression: { kind: "assign", target: rawName, op, right: rhs }, loc });
923
+ continue;
924
+ }
925
+
885
926
  if (nextType === TT.DOT) {
886
927
  let fullName = this.consume(TT.IDENT).value;
887
928
  this.consume(TT.DOT);
package/src/core/types.ts CHANGED
@@ -19,10 +19,11 @@ export enum TT {
19
19
  KW_GLOBAL, KW_PAGE, KW_LANG,
20
20
  KW_CUSTOM_EL, KW_CUSTOM,
21
21
  KW_IF, KW_ELSE, KW_FOR, KW_IN, KW_REMOVE,
22
- KW_FUNCTION,
22
+ KW_FUNCTION, KW_RETURN,
23
23
  OP_PLUS, OP_MINUS, OP_STAR, OP_SLASH, OP_SEMI,
24
24
  OP_LT, OP_GT, OP_NEQ, OP_EQEQ, OP_LTEQ, OP_GTEQ,
25
25
  OP_ARROW,
26
+ OP_NOT,
26
27
  EOF,
27
28
  }
28
29
 
@@ -62,10 +63,11 @@ export interface AssignStmt { kind: "assign_stmt"; expression: Expression;
62
63
  export interface IfBlock { kind: "if"; condition: Expression; body: Child[]; elseBody?: Child[]; loc: Loc }
63
64
  export interface ForLoop { kind: "for"; keyVar?: string; itemVar: string; indexVar?: string; arrayVar: string; body: Child[]; loc: Loc }
64
65
  export interface RemoveStmt { kind: "remove"; target: string; loc: Loc }
66
+ export interface ReturnStmt { kind: "return"; expression?: Expression; loc: Loc }
65
67
  export interface ComponentExpr { kind: "component_expr"; children: Child[]; loc: Loc }
66
68
 
67
69
 
68
- export type Child = TextNode | CompUse | ExprNode | AssignStmt | IfBlock | ForLoop | RemoveStmt | ComponentExpr;
70
+ export type Child = TextNode | CompUse | ExprNode | AssignStmt | IfBlock | ForLoop | RemoveStmt | ReturnStmt | ComponentExpr;
69
71
 
70
72
  export interface ParamDecl { name: string; defaultVal?: Literal; loc: Loc }
71
73