@yannosay/sinth 0.16.1 → 0.18.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 +7 -7
- package/package.json +1 -1
- package/src/core/compiler.ts +92 -0
- package/src/core/parser.ts +44 -30
package/README.md
CHANGED
|
@@ -48,7 +48,7 @@ page
|
|
|
48
48
|
|
|
49
49
|
title = "Functions"
|
|
50
50
|
|
|
51
|
-
var str userName = "
|
|
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
|
-
|
|
64
|
+
greet(userName) + " - welcome back!"
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
-
|
|
68
|
-
|
|
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
|
|
90
|
+
Checkbox(checked: isAdmin label: "Admin mode?")
|
|
91
91
|
|
|
92
92
|
Paragraph {
|
|
93
|
-
"Admin mode is " +
|
|
93
|
+
"Admin mode is " + getStatus() + "!"
|
|
94
94
|
}
|
|
95
95
|
```
|
|
96
96
|
#### NEW (0.13.5): Fullscreen activation + if-else inside Component
|
|
@@ -146,7 +146,7 @@ var int count = 0
|
|
|
146
146
|
Main {
|
|
147
147
|
Heading(level: 1) { count }
|
|
148
148
|
(alert("Fires only once!")) -- no dependencies, runs once and never again
|
|
149
|
-
Button(onClick: count
|
|
149
|
+
Button(onClick: count += 1) { "Increment" }
|
|
150
150
|
}
|
|
151
151
|
```
|
|
152
152
|
Count updates every click because count changed. The alert fires only on page load — zero dependencies means it's cached forever. Just a meta flag.
|
package/package.json
CHANGED
package/src/core/compiler.ts
CHANGED
|
@@ -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"))
|
package/src/core/parser.ts
CHANGED
|
@@ -104,9 +104,14 @@ sinthRender();`;
|
|
|
104
104
|
continue;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
|
|
108
|
-
this.
|
|
109
|
-
|
|
107
|
+
if (nextType === TT.LPAREN && identName[0] === identName[0].toLowerCase()) {
|
|
108
|
+
this.consume(TT.IDENT);
|
|
109
|
+
const expr = this.parseCallExpr({ kind: "variable", name: identName });
|
|
110
|
+
uses.push({ kind: "use", name: "__IF_ROOT__", attrs: [], children: [{ kind: "expr", expression: expr, loc: this.peek().loc }], loc: this.peek().loc });
|
|
111
|
+
} else {
|
|
112
|
+
this.pos = savedPos;
|
|
113
|
+
uses.push(this.parseCompUse());
|
|
114
|
+
}
|
|
110
115
|
}
|
|
111
116
|
else throw new SinthError(
|
|
112
117
|
`Unexpected token '${this.peek().value}' (${TT[this.peek().type]}) at top level`,
|
|
@@ -478,7 +483,7 @@ private parseArrayLiteral(): Literal {
|
|
|
478
483
|
}
|
|
479
484
|
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after function signature", this.peek().loc);
|
|
480
485
|
this.consume(TT.LBRACE);
|
|
481
|
-
const body = this.
|
|
486
|
+
const body = this.parseUnifiedBody();
|
|
482
487
|
this.consume(TT.RBRACE);
|
|
483
488
|
return { name, params, returnType, body, loc };
|
|
484
489
|
}
|
|
@@ -518,9 +523,10 @@ private parseArrayLiteral(): Literal {
|
|
|
518
523
|
while (!this.check(TT.RPAREN)) {
|
|
519
524
|
const ploc = this.peek().loc;
|
|
520
525
|
let pname: string;
|
|
526
|
+
let paramType: VarType | undefined;
|
|
521
527
|
if (this.check(TT.IDENT) && ["int","str","bool","str[]","obj"].includes(this.peek().value) &&
|
|
522
528
|
this.tokens[this.pos + 1]?.type === TT.IDENT) {
|
|
523
|
-
this.consume(TT.IDENT)
|
|
529
|
+
paramType = this.consume(TT.IDENT).value as VarType;
|
|
524
530
|
pname = this.consume(TT.IDENT).value;
|
|
525
531
|
} else {
|
|
526
532
|
const nameTok = this.check(TT.IDENT) ? this.consume(TT.IDENT) : this.consume(this.peek().type);
|
|
@@ -528,7 +534,7 @@ private parseArrayLiteral(): Literal {
|
|
|
528
534
|
}
|
|
529
535
|
let defaultVal: Literal | undefined;
|
|
530
536
|
if (this.check(TT.EQUALS)) { this.consume(TT.EQUALS); defaultVal = this.parseLiteral(); }
|
|
531
|
-
params.push({ name: pname, defaultVal, loc: ploc });
|
|
537
|
+
params.push({ name: pname, paramType, defaultVal, loc: ploc });
|
|
532
538
|
if (!this.check(TT.RPAREN)) this.consume(TT.COMMA);
|
|
533
539
|
}
|
|
534
540
|
this.consume(TT.RPAREN);
|
|
@@ -809,19 +815,10 @@ private parseArrayLiteral(): Literal {
|
|
|
809
815
|
if (identName[0] === identName[0].toUpperCase() && identName[0] !== identName[0].toLowerCase()) {
|
|
810
816
|
children.push(this.parseCompUse());
|
|
811
817
|
} else {
|
|
812
|
-
this.
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
if (!this.check(TT.RPAREN)) {
|
|
816
|
-
args.push(this.parseExpression()!);
|
|
817
|
-
while (this.check(TT.COMMA)) {
|
|
818
|
-
this.consume(TT.COMMA);
|
|
819
|
-
args.push(this.parseExpression()!);
|
|
820
|
-
}
|
|
818
|
+
const expr = this.parseExpression();
|
|
819
|
+
if (expr) {
|
|
820
|
+
children.push({ kind: "expr", expression: expr, loc });
|
|
821
821
|
}
|
|
822
|
-
this.consume(TT.RPAREN);
|
|
823
|
-
const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
|
|
824
|
-
children.push({ kind: "expr", expression: callExpr, loc });
|
|
825
822
|
}
|
|
826
823
|
continue;
|
|
827
824
|
}
|
|
@@ -1083,7 +1080,19 @@ private parseArrayLiteral(): Literal {
|
|
|
1083
1080
|
rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
|
|
1084
1081
|
} else if (this.check(TT.IDENT)) {
|
|
1085
1082
|
const rhsName = this.consume(TT.IDENT).value;
|
|
1086
|
-
if (this.check(TT.
|
|
1083
|
+
if (this.check(TT.LPAREN)) {
|
|
1084
|
+
this.consume(TT.LPAREN);
|
|
1085
|
+
const args: Expression[] = [];
|
|
1086
|
+
if (!this.check(TT.RPAREN)) {
|
|
1087
|
+
args.push(this.parseExpression()!);
|
|
1088
|
+
while (this.check(TT.COMMA)) {
|
|
1089
|
+
this.consume(TT.COMMA);
|
|
1090
|
+
args.push(this.parseExpression()!);
|
|
1091
|
+
}
|
|
1092
|
+
}
|
|
1093
|
+
this.consume(TT.RPAREN);
|
|
1094
|
+
rhs = { kind: "call", callee: { kind: "variable", name: rhsName }, args };
|
|
1095
|
+
} else if (this.check(TT.DOT)) {
|
|
1087
1096
|
this.consume(TT.DOT);
|
|
1088
1097
|
rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
|
|
1089
1098
|
} else if (this.check(TT.LBRACKET)) {
|
|
@@ -1230,19 +1239,24 @@ private parseArrayLiteral(): Literal {
|
|
|
1230
1239
|
children.push(this.parseCompUse());
|
|
1231
1240
|
continue;
|
|
1232
1241
|
} else {
|
|
1233
|
-
this.
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
this.
|
|
1240
|
-
|
|
1242
|
+
const expr = this.parseExpression();
|
|
1243
|
+
if (expr) {
|
|
1244
|
+
let finalExpr = expr;
|
|
1245
|
+
while (this.check(TT.OP_PLUS)) {
|
|
1246
|
+
this.consume(TT.OP_PLUS);
|
|
1247
|
+
let rhs: Expression | null = null;
|
|
1248
|
+
if (this.check(TT.STRING)) {
|
|
1249
|
+
rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
|
|
1250
|
+
} else if (this.check(TT.IDENT)) {
|
|
1251
|
+
rhs = this.parseExpression();
|
|
1252
|
+
} else {
|
|
1253
|
+
rhs = this.parseExpression(true);
|
|
1254
|
+
}
|
|
1255
|
+
if (!rhs) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
1256
|
+
finalExpr = { kind: "binary", left: finalExpr, op: "+", right: rhs };
|
|
1241
1257
|
}
|
|
1258
|
+
children.push({ kind: "expr", expression: finalExpr, loc });
|
|
1242
1259
|
}
|
|
1243
|
-
this.consume(TT.RPAREN);
|
|
1244
|
-
const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
|
|
1245
|
-
children.push({ kind: "expr", expression: callExpr, loc });
|
|
1246
1260
|
continue;
|
|
1247
1261
|
}
|
|
1248
1262
|
} else if (nextType === TT.LBRACE || nextType === TT.RAW_BLOCK) {
|