@yannosay/sinth 0.17.0 → 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 +6 -6
- package/package.json +1 -1
- package/src/core/compiler.ts +92 -0
- package/src/core/parser.ts +33 -15
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
|
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
|
@@ -483,7 +483,7 @@ private parseArrayLiteral(): Literal {
|
|
|
483
483
|
}
|
|
484
484
|
if (!this.check(TT.LBRACE)) throw new SinthError("Expected '{' after function signature", this.peek().loc);
|
|
485
485
|
this.consume(TT.LBRACE);
|
|
486
|
-
const body = this.
|
|
486
|
+
const body = this.parseUnifiedBody();
|
|
487
487
|
this.consume(TT.RBRACE);
|
|
488
488
|
return { name, params, returnType, body, loc };
|
|
489
489
|
}
|
|
@@ -523,9 +523,10 @@ private parseArrayLiteral(): Literal {
|
|
|
523
523
|
while (!this.check(TT.RPAREN)) {
|
|
524
524
|
const ploc = this.peek().loc;
|
|
525
525
|
let pname: string;
|
|
526
|
+
let paramType: VarType | undefined;
|
|
526
527
|
if (this.check(TT.IDENT) && ["int","str","bool","str[]","obj"].includes(this.peek().value) &&
|
|
527
528
|
this.tokens[this.pos + 1]?.type === TT.IDENT) {
|
|
528
|
-
this.consume(TT.IDENT)
|
|
529
|
+
paramType = this.consume(TT.IDENT).value as VarType;
|
|
529
530
|
pname = this.consume(TT.IDENT).value;
|
|
530
531
|
} else {
|
|
531
532
|
const nameTok = this.check(TT.IDENT) ? this.consume(TT.IDENT) : this.consume(this.peek().type);
|
|
@@ -533,7 +534,7 @@ private parseArrayLiteral(): Literal {
|
|
|
533
534
|
}
|
|
534
535
|
let defaultVal: Literal | undefined;
|
|
535
536
|
if (this.check(TT.EQUALS)) { this.consume(TT.EQUALS); defaultVal = this.parseLiteral(); }
|
|
536
|
-
params.push({ name: pname, defaultVal, loc: ploc });
|
|
537
|
+
params.push({ name: pname, paramType, defaultVal, loc: ploc });
|
|
537
538
|
if (!this.check(TT.RPAREN)) this.consume(TT.COMMA);
|
|
538
539
|
}
|
|
539
540
|
this.consume(TT.RPAREN);
|
|
@@ -1079,7 +1080,19 @@ private parseArrayLiteral(): Literal {
|
|
|
1079
1080
|
rhs = { kind: "literal", value: { kind: "str", value: this.consume(TT.STRING).value } };
|
|
1080
1081
|
} else if (this.check(TT.IDENT)) {
|
|
1081
1082
|
const rhsName = this.consume(TT.IDENT).value;
|
|
1082
|
-
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)) {
|
|
1083
1096
|
this.consume(TT.DOT);
|
|
1084
1097
|
rhs = { kind: "variable", name: rhsName + "." + this.consume(TT.IDENT).value };
|
|
1085
1098
|
} else if (this.check(TT.LBRACKET)) {
|
|
@@ -1226,19 +1239,24 @@ private parseArrayLiteral(): Literal {
|
|
|
1226
1239
|
children.push(this.parseCompUse());
|
|
1227
1240
|
continue;
|
|
1228
1241
|
} else {
|
|
1229
|
-
this.
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
this.
|
|
1236
|
-
|
|
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 };
|
|
1237
1257
|
}
|
|
1258
|
+
children.push({ kind: "expr", expression: finalExpr, loc });
|
|
1238
1259
|
}
|
|
1239
|
-
this.consume(TT.RPAREN);
|
|
1240
|
-
const callExpr: Expression = { kind: "call", callee: { kind: "variable", name: identName }, args };
|
|
1241
|
-
children.push({ kind: "expr", expression: callExpr, loc });
|
|
1242
1260
|
continue;
|
|
1243
1261
|
}
|
|
1244
1262
|
} else if (nextType === TT.LBRACE || nextType === TT.RAW_BLOCK) {
|