@yannosay/sinth 0.3.2 → 0.4.1
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/package.json +1 -1
- package/src/sinth.ts +317 -30
package/package.json
CHANGED
package/src/sinth.ts
CHANGED
|
@@ -255,8 +255,12 @@ export class Lexer {
|
|
|
255
255
|
if (ch === "/") { tokens.push(this.single(TT.OP_SLASH, loc)); continue; }
|
|
256
256
|
if (ch === "<") { tokens.push(this.single(TT.OP_LT, loc)); continue; }
|
|
257
257
|
if (ch === ">") { tokens.push(this.single(TT.OP_GT, loc)); continue; }
|
|
258
|
-
|
|
259
|
-
|
|
258
|
+
if (ch === "!" && this.src[this.pos + 1] === "=") {
|
|
259
|
+
this.adv(); // consume '!'
|
|
260
|
+
this.adv(); // consume '='
|
|
261
|
+
tokens.push({ type: TT.OP_NEQ, value: "!=", loc });
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
260
264
|
if (ch === "{") {
|
|
261
265
|
if (this.rawBlockPending) {
|
|
262
266
|
this.rawBlockPending = false;
|
|
@@ -510,6 +514,8 @@ export class Parser {
|
|
|
510
514
|
const vd = this.parseVarDeclaration();
|
|
511
515
|
if (vd) varDecls.push(vd);
|
|
512
516
|
}
|
|
517
|
+
// Store var declarations for type checking in inline logic
|
|
518
|
+
(this as any)._varDecls = varDecls;
|
|
513
519
|
|
|
514
520
|
// 4b. Body: component definitions, usages, style, script, custom-element
|
|
515
521
|
while (!this.check(TT.EOF)) {
|
|
@@ -544,6 +550,9 @@ export class Parser {
|
|
|
544
550
|
else throw new SinthError(`Unexpected token '${this.peek().value}'`, this.peek().loc);
|
|
545
551
|
}
|
|
546
552
|
|
|
553
|
+
if ((this as any)._logicScripts) {
|
|
554
|
+
scripts.push(...(this as any)._logicScripts);
|
|
555
|
+
}
|
|
547
556
|
return { filePath: this.file, isPage, imports, meta, defs, uses, styles, scripts, customEls, varDecls };
|
|
548
557
|
}
|
|
549
558
|
|
|
@@ -669,6 +678,16 @@ export class Parser {
|
|
|
669
678
|
return { kind: "unary", op: "not", operand };
|
|
670
679
|
}
|
|
671
680
|
|
|
681
|
+
// Parenthesized expression
|
|
682
|
+
if (this.check(TT.LPAREN)) {
|
|
683
|
+
this.consume(TT.LPAREN);
|
|
684
|
+
const expr = this.parseExpression();
|
|
685
|
+
if (!expr) throw new SinthError("Expected expression after '('", this.peek().loc);
|
|
686
|
+
if (!this.check(TT.RPAREN)) throw new SinthError("Expected ')'", this.peek().loc);
|
|
687
|
+
this.consume(TT.RPAREN);
|
|
688
|
+
return expr;
|
|
689
|
+
}
|
|
690
|
+
|
|
672
691
|
// Literal
|
|
673
692
|
if (this.check(TT.STRING) || this.check(TT.NUMBER) || this.check(TT.BOOL_TRUE) || this.check(TT.BOOL_FALSE) || this.check(TT.NULL_LIT)) {
|
|
674
693
|
const lit = this.parseLiteral();
|
|
@@ -679,6 +698,15 @@ export class Parser {
|
|
|
679
698
|
if (tok.type === TT.IDENT) {
|
|
680
699
|
const name = this.consume(TT.IDENT).value;
|
|
681
700
|
|
|
701
|
+
// Check for !=
|
|
702
|
+
if (this.check(TT.OP_NEQ)) {
|
|
703
|
+
this.consume(TT.OP_NEQ);
|
|
704
|
+
console.log("DEBUG != right side token:", this.peek().value, "type:", TT[this.peek().type]);
|
|
705
|
+
const right = this.parseExpression();
|
|
706
|
+
if (!right) throw new SinthError("Expected expression after !=", this.peek().loc);
|
|
707
|
+
return { kind: "binary", left: { kind: "variable", name }, op: "!=", right };
|
|
708
|
+
}
|
|
709
|
+
|
|
682
710
|
|
|
683
711
|
// Check for == (comparison)
|
|
684
712
|
if (this.check(TT.OP_EQEQ)) {
|
|
@@ -688,13 +716,7 @@ export class Parser {
|
|
|
688
716
|
return { kind: "binary", left: { kind: "variable", name }, op: "==", right };
|
|
689
717
|
}
|
|
690
718
|
|
|
691
|
-
|
|
692
|
-
if (this.check(TT.OP_NEQ)) {
|
|
693
|
-
this.consume(TT.OP_NEQ);
|
|
694
|
-
const right = this.parseExpression();
|
|
695
|
-
if (!right) throw new SinthError("Expected expression after !=", this.peek().loc);
|
|
696
|
-
return { kind: "binary", left: { kind: "variable", name }, op: "!=", right };
|
|
697
|
-
}
|
|
719
|
+
|
|
698
720
|
|
|
699
721
|
// Check for <
|
|
700
722
|
if (this.check(TT.OP_LT)) {
|
|
@@ -933,6 +955,137 @@ private parseCompUse(): CompUse {
|
|
|
933
955
|
return { kind: "if", condition, body, elseBody, loc };
|
|
934
956
|
}
|
|
935
957
|
|
|
958
|
+
private parseLogicBody(condition: Expression): string {
|
|
959
|
+
let js = "if (" + compileExprToJS(condition) + ") {\n";
|
|
960
|
+
while (!this.check(TT.RBRACE) && !this.check(TT.EOF)) {
|
|
961
|
+
if (this.check(TT.IDENT)) {
|
|
962
|
+
const name = this.consume(TT.IDENT).value;
|
|
963
|
+
|
|
964
|
+
// Assignment: name = value
|
|
965
|
+
if (this.check(TT.EQUALS)) {
|
|
966
|
+
this.consume(TT.EQUALS);
|
|
967
|
+
const expr = this.parseExpression();
|
|
968
|
+
if (expr) {
|
|
969
|
+
// Type check: look up the variable's declared type
|
|
970
|
+
const varDecl = (this as any)._varDecls?.find((v: any) => v.name === name);
|
|
971
|
+
if (varDecl) {
|
|
972
|
+
const exprJS = compileExprToJS(expr);
|
|
973
|
+
// Check type compatibility
|
|
974
|
+
const valueKind = expr.kind;
|
|
975
|
+
const valueLiteralKind = expr.kind === "literal" ? expr.value?.kind : null;
|
|
976
|
+
|
|
977
|
+
if (varDecl.varType === "int" && valueLiteralKind === "str") {
|
|
978
|
+
SinthWarning.emit(`Type mismatch: '${name}' is declared as int but assigned a string value`);
|
|
979
|
+
} else if (varDecl.varType === "str" && valueLiteralKind === "num") {
|
|
980
|
+
SinthWarning.emit(`Type mismatch: '${name}' is declared as str but assigned a number value`);
|
|
981
|
+
} else if (varDecl.varType === "bool" && valueLiteralKind !== "bool" && valueLiteralKind !== null) {
|
|
982
|
+
SinthWarning.emit(`Type mismatch: '${name}' is declared as bool but assigned a non-boolean value`);
|
|
983
|
+
}
|
|
984
|
+
}
|
|
985
|
+
js += " " + compileExprToJS({ kind: "assign", target: name, op: "=", right: expr }) + ";\n";
|
|
986
|
+
}
|
|
987
|
+
}
|
|
988
|
+
// Addition assignment: name += value
|
|
989
|
+
else if (this.check(TT.OP_PLUS) && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
990
|
+
this.consume(TT.OP_PLUS);
|
|
991
|
+
this.consume(TT.EQUALS);
|
|
992
|
+
const expr = this.parseExpression();
|
|
993
|
+
if (expr) {
|
|
994
|
+
js += " " + compileExprToJS({ kind: "assign", target: name, op: "+=", right: expr }) + ";\n";
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
// Subtraction assignment: name -= value
|
|
998
|
+
else if (this.check(TT.OP_MINUS) && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
999
|
+
this.consume(TT.OP_MINUS);
|
|
1000
|
+
this.consume(TT.EQUALS);
|
|
1001
|
+
const expr = this.parseExpression();
|
|
1002
|
+
if (expr) {
|
|
1003
|
+
js += " " + compileExprToJS({ kind: "assign", target: name, op: "-=", right: expr }) + ";\n";
|
|
1004
|
+
}
|
|
1005
|
+
}
|
|
1006
|
+
// Function call: name()
|
|
1007
|
+
else if (this.check(TT.LPAREN)) {
|
|
1008
|
+
this.consume(TT.LPAREN);
|
|
1009
|
+
let args = "";
|
|
1010
|
+
while (!this.check(TT.RPAREN) && !this.check(TT.EOF)) {
|
|
1011
|
+
const arg = this.parseExpression();
|
|
1012
|
+
if (arg) {
|
|
1013
|
+
args += (args ? ", " : "") + compileExprToJS(arg);
|
|
1014
|
+
}
|
|
1015
|
+
if (this.check(TT.COMMA)) this.consume(TT.COMMA);
|
|
1016
|
+
}
|
|
1017
|
+
this.consume(TT.RPAREN);
|
|
1018
|
+
js += " " + name + "(" + args + ");\n";
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
// Nested if
|
|
1022
|
+
else if (this.check(TT.KW_IF)) {
|
|
1023
|
+
this.consume(TT.KW_IF);
|
|
1024
|
+
const innerCond = this.parseExpression();
|
|
1025
|
+
if (innerCond && this.check(TT.LBRACE)) {
|
|
1026
|
+
this.consume(TT.LBRACE);
|
|
1027
|
+
js += " " + this.parseLogicBody(innerCond);
|
|
1028
|
+
}
|
|
1029
|
+
}
|
|
1030
|
+
// var declaration (skip in logic body)
|
|
1031
|
+
else if (this.check(TT.IDENT) && this.peek().value === "var") {
|
|
1032
|
+
this.consume(TT.IDENT); // var
|
|
1033
|
+
this.consume(TT.IDENT); // type
|
|
1034
|
+
this.consume(TT.IDENT); // name
|
|
1035
|
+
if (this.check(TT.EQUALS)) {
|
|
1036
|
+
this.consume(TT.EQUALS);
|
|
1037
|
+
this.parseExpression();
|
|
1038
|
+
}
|
|
1039
|
+
}
|
|
1040
|
+
else {
|
|
1041
|
+
break;
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
this.consume(TT.RBRACE);
|
|
1045
|
+
js += "}\n";
|
|
1046
|
+
|
|
1047
|
+
// Check for else
|
|
1048
|
+
if (this.check(TT.KW_ELSE)) {
|
|
1049
|
+
this.consume(TT.KW_ELSE);
|
|
1050
|
+
if (this.check(TT.LBRACE)) {
|
|
1051
|
+
this.consume(TT.LBRACE);
|
|
1052
|
+
js += "else {\n";
|
|
1053
|
+
while (!this.check(TT.RBRACE) && !this.check(TT.EOF)) {
|
|
1054
|
+
if (this.check(TT.IDENT)) {
|
|
1055
|
+
const name = this.consume(TT.IDENT).value;
|
|
1056
|
+
if (this.check(TT.EQUALS)) {
|
|
1057
|
+
this.consume(TT.EQUALS);
|
|
1058
|
+
const expr = this.parseExpression();
|
|
1059
|
+
if (expr) {
|
|
1060
|
+
js += " " + compileExprToJS({ kind: "assign", target: name, op: "=", right: expr }) + ";\n";
|
|
1061
|
+
}
|
|
1062
|
+
} else if (this.check(TT.OP_PLUS) && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
1063
|
+
this.consume(TT.OP_PLUS);
|
|
1064
|
+
this.consume(TT.EQUALS);
|
|
1065
|
+
const expr = this.parseExpression();
|
|
1066
|
+
if (expr) {
|
|
1067
|
+
js += " " + compileExprToJS({ kind: "assign", target: name, op: "+=", right: expr }) + ";\n";
|
|
1068
|
+
}
|
|
1069
|
+
} else if (this.check(TT.OP_MINUS) && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
1070
|
+
this.consume(TT.OP_MINUS);
|
|
1071
|
+
this.consume(TT.EQUALS);
|
|
1072
|
+
const expr = this.parseExpression();
|
|
1073
|
+
if (expr) {
|
|
1074
|
+
js += " " + compileExprToJS({ kind: "assign", target: name, op: "-=", right: expr }) + ";\n";
|
|
1075
|
+
}
|
|
1076
|
+
}
|
|
1077
|
+
} else {
|
|
1078
|
+
break;
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
this.consume(TT.RBRACE);
|
|
1082
|
+
js += "}\n";
|
|
1083
|
+
}
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
return js;
|
|
1087
|
+
}
|
|
1088
|
+
|
|
936
1089
|
private parseForLoop(): ForLoop {
|
|
937
1090
|
const loc = this.consume(TT.KW_FOR).loc;
|
|
938
1091
|
const itemVar = this.consume(TT.IDENT).value;
|
|
@@ -961,24 +1114,119 @@ private parseCompUse(): CompUse {
|
|
|
961
1114
|
private parseChildList(): Child[] {
|
|
962
1115
|
const children: Child[] = [];
|
|
963
1116
|
while (!this.check(TT.EOF) && !this.check(TT.RBRACE)) {
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
1117
|
+
if (this.check(TT.STRING)) {
|
|
1118
|
+
const loc = this.peek().loc;
|
|
1119
|
+
let leftExpr: Expression = {
|
|
1120
|
+
kind: "literal",
|
|
1121
|
+
value: { kind: "str", value: this.consume(TT.STRING).value }
|
|
1122
|
+
};
|
|
1123
|
+
// Keep chaining while there's a + operator
|
|
1124
|
+
while (this.check(TT.OP_PLUS)) {
|
|
1125
|
+
this.consume(TT.OP_PLUS);
|
|
1126
|
+
let rightExpr: Expression | null = null;
|
|
1127
|
+
if (this.check(TT.STRING)) {
|
|
1128
|
+
rightExpr = {
|
|
1129
|
+
kind: "literal",
|
|
1130
|
+
value: { kind: "str", value: this.consume(TT.STRING).value }
|
|
1131
|
+
};
|
|
1132
|
+
} else if (this.check(TT.IDENT)) {
|
|
1133
|
+
const varName = this.consume(TT.IDENT).value;
|
|
1134
|
+
rightExpr = { kind: "variable", name: varName };
|
|
1135
|
+
} else {
|
|
1136
|
+
rightExpr = this.parseExpression();
|
|
1137
|
+
}
|
|
1138
|
+
if (!rightExpr) throw new SinthError("Expected expression after +", this.peek().loc);
|
|
1139
|
+
leftExpr = {
|
|
972
1140
|
kind: "binary",
|
|
973
|
-
left:
|
|
1141
|
+
left: leftExpr,
|
|
974
1142
|
op: "+",
|
|
975
1143
|
right: rightExpr,
|
|
976
|
-
|
|
1144
|
+
};
|
|
1145
|
+
}
|
|
1146
|
+
children.push({ kind: "expr", expression: leftExpr, loc });
|
|
1147
|
+
} else if (this.check(TT.KW_IF)) {
|
|
1148
|
+
const ifStart = this.pos;
|
|
1149
|
+
this.consume(TT.KW_IF);
|
|
1150
|
+
const condition = this.parseExpression();
|
|
1151
|
+
if (!condition) throw new SinthError("Expected condition after 'if'", this.peek().loc);
|
|
1152
|
+
|
|
1153
|
+
if (!this.check(TT.LBRACE)) {
|
|
1154
|
+
throw new SinthError("Expected '{' after if condition", this.peek().loc);
|
|
1155
|
+
}
|
|
1156
|
+
|
|
1157
|
+
this.consume(TT.LBRACE);
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
// Determine if this is inline logic or DOM rendering
|
|
1161
|
+
let isInlineLogic = false;
|
|
1162
|
+
|
|
1163
|
+
// Check first token inside the body
|
|
1164
|
+
if (this.check(TT.IDENT)) {
|
|
1165
|
+
const identName = this.peek().value;
|
|
1166
|
+
const nextType = this.tokens[this.pos + 1]?.type;
|
|
1167
|
+
|
|
1168
|
+
|
|
1169
|
+
|
|
1170
|
+
// Assignment operators: =, +=, -=
|
|
1171
|
+
if (nextType === TT.EQUALS) {
|
|
1172
|
+
isInlineLogic = true;
|
|
1173
|
+
} else if (nextType === TT.OP_PLUS && this.tokens[this.pos + 2]?.type === TT.EQUALS) {
|
|
1174
|
+
isInlineLogic = true;
|
|
1175
|
+
} else if (nextType === TT.OP_MINUS && this.tokens[this.pos + 2]?.type === TT.EQUALS) {
|
|
1176
|
+
isInlineLogic = true;
|
|
1177
|
+
}
|
|
1178
|
+
// Not a built-in component or keyword
|
|
1179
|
+
else if (identName === "if" || identName === "for" || identName === "var" ||
|
|
1180
|
+
BUILTIN_MAP[identName]) {
|
|
1181
|
+
isInlineLogic = false;
|
|
1182
|
+
}
|
|
1183
|
+
// If next token is not (, it might still be logic (like standalone variable for sinthRender)
|
|
1184
|
+
// But if it's a component usage (IDENT followed by LPAREN or LBRACE), it's DOM
|
|
1185
|
+
else if (nextType === TT.LPAREN || nextType === TT.LBRACE || nextType === TT.RAW_BLOCK) {
|
|
1186
|
+
isInlineLogic = false;
|
|
1187
|
+
}
|
|
1188
|
+
}
|
|
1189
|
+
// Also check for nested if (could be inline or DOM)
|
|
1190
|
+
else if (this.check(TT.KW_IF)) {
|
|
1191
|
+
// Peek ahead to see if nested if contains assignment
|
|
1192
|
+
const nestedStart = this.pos;
|
|
1193
|
+
this.consume(TT.KW_IF);
|
|
1194
|
+
const nestedCond = this.parseExpression();
|
|
1195
|
+
if (nestedCond && this.check(TT.LBRACE)) {
|
|
1196
|
+
this.consume(TT.LBRACE);
|
|
1197
|
+
if (this.check(TT.IDENT) && this.tokens[this.pos + 1]?.type === TT.EQUALS) {
|
|
1198
|
+
isInlineLogic = true;
|
|
1199
|
+
}
|
|
1200
|
+
}
|
|
1201
|
+
// Reset position
|
|
1202
|
+
while (this.pos > nestedStart) {
|
|
1203
|
+
this.pos--;
|
|
1204
|
+
}
|
|
1205
|
+
this.pos = nestedStart;
|
|
1206
|
+
}
|
|
1207
|
+
// Keywords that indicate DOM rendering
|
|
1208
|
+
else if (this.check(TT.KW_STYLE) || this.check(TT.KW_SCRIPT) ||
|
|
1209
|
+
this.check(TT.STRING) || this.check(TT.KW_FOR)) {
|
|
1210
|
+
isInlineLogic = false;
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
if (isInlineLogic) {
|
|
1214
|
+
// Parse as inline logic
|
|
1215
|
+
const jsCode = this.parseLogicBody(condition);
|
|
1216
|
+
const scriptBlock: ScriptBlock = {
|
|
1217
|
+
raw: jsCode,
|
|
1218
|
+
attrs: {},
|
|
1219
|
+
loc: this.peek().loc
|
|
1220
|
+
};
|
|
1221
|
+
children.push({ kind: "text", value: "", loc: this.peek().loc });
|
|
1222
|
+
(this as any)._logicScripts = (this as any)._logicScripts || [];
|
|
1223
|
+
(this as any)._logicScripts.push(scriptBlock);
|
|
977
1224
|
} else {
|
|
978
|
-
|
|
1225
|
+
// Parse as DOM if — reset and use existing parser
|
|
1226
|
+
this.pos = ifStart;
|
|
1227
|
+
children.push(this.parseIfBlock());
|
|
979
1228
|
}
|
|
980
|
-
|
|
981
|
-
children.push(this.parseIfBlock());
|
|
1229
|
+
|
|
982
1230
|
} else if (this.check(TT.KW_FOR)) {
|
|
983
1231
|
children.push(this.parseForLoop());
|
|
984
1232
|
} else if (this.check(TT.IDENT)) {
|
|
@@ -1012,8 +1260,14 @@ private parseCompUse(): CompUse {
|
|
|
1012
1260
|
const loc = this.peek().loc;
|
|
1013
1261
|
const name = this.consume(TT.IDENT).value;
|
|
1014
1262
|
children.push({ kind: "expr", expression: { kind: "variable", name }, loc });
|
|
1015
|
-
} else
|
|
1263
|
+
} else if (this.tokens[this.pos + 1]?.type === TT.LPAREN ||
|
|
1264
|
+
this.tokens[this.pos + 1]?.type === TT.LBRACE ||
|
|
1265
|
+
this.tokens[this.pos + 1]?.type === TT.RAW_BLOCK) {
|
|
1016
1266
|
children.push(this.parseCompUse());
|
|
1267
|
+
} else {
|
|
1268
|
+
const loc = this.peek().loc;
|
|
1269
|
+
const name = this.consume(TT.IDENT).value;
|
|
1270
|
+
children.push({ kind: "expr", expression: { kind: "variable", name }, loc });
|
|
1017
1271
|
}
|
|
1018
1272
|
} else {
|
|
1019
1273
|
throw new SinthError(`Unexpected token '${this.peek().value}' in children`, this.peek().loc);
|
|
@@ -2087,6 +2341,15 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2087
2341
|
|
|
2088
2342
|
const { componentScripts, pageScripts } = collectScripts(file, allDefs);
|
|
2089
2343
|
|
|
2344
|
+
const assignedVars = new Set<string>();
|
|
2345
|
+
for (const s of file.scripts) {
|
|
2346
|
+
const matches = s.raw.matchAll(/(\w+)\s*=/g);
|
|
2347
|
+
for (const m of matches) assignedVars.add(m[1]);
|
|
2348
|
+
}
|
|
2349
|
+
for (const v of file.varDecls) {
|
|
2350
|
+
if (v.value) assignedVars.add(v.name);
|
|
2351
|
+
}
|
|
2352
|
+
|
|
2090
2353
|
const companionJS = (() => {
|
|
2091
2354
|
const base = absPath.replace(/\.sinth$/, ".js");
|
|
2092
2355
|
return fs.existsSync(base) ? path.basename(base) : undefined;
|
|
@@ -2113,14 +2376,20 @@ function compileFile(filePath: string, opts: CompileOptions): string | null {
|
|
|
2113
2376
|
if (componentScripts.length > 0) {
|
|
2114
2377
|
scriptTags.push(`<script>\n${componentScripts.join("\n\n")}\n</script>`);
|
|
2115
2378
|
}
|
|
2379
|
+
let logicScriptsRaw = "";
|
|
2116
2380
|
for (const s of pageScripts) {
|
|
2117
2381
|
const extra = Object.entries(s.attrs)
|
|
2118
2382
|
.map(([k, v]) => v === "true" ? k : `${k}="${escAttr(v)}"`)
|
|
2119
2383
|
.join(" ");
|
|
2120
2384
|
// Convert let/const to var for global accessibility
|
|
2121
2385
|
const globalRaw = s.raw.replace(/\b(let|const)\b/g, 'var');
|
|
2122
|
-
|
|
2123
|
-
|
|
2386
|
+
// Check if this is an inline logic script (starts with "if (")
|
|
2387
|
+
if (globalRaw.trim().startsWith("if (")) {
|
|
2388
|
+
logicScriptsRaw += globalRaw + "\n";
|
|
2389
|
+
} else {
|
|
2390
|
+
scriptTags.push(`<script${extra ? " " + extra : ""}>\n${globalRaw}\n</script>`);
|
|
2391
|
+
}
|
|
2392
|
+
}
|
|
2124
2393
|
|
|
2125
2394
|
const html = [
|
|
2126
2395
|
"<!DOCTYPE html>",
|
|
@@ -2129,17 +2398,31 @@ for (const s of pageScripts) {
|
|
|
2129
2398
|
`<body data-s="${hash}">`,
|
|
2130
2399
|
bodyHTML,
|
|
2131
2400
|
`<script>
|
|
2132
|
-
//
|
|
2401
|
+
//Compiled using Sinth Logic
|
|
2133
2402
|
${file.varDecls.map(v => {
|
|
2134
|
-
if (!v.value)
|
|
2403
|
+
if (!v.value) {
|
|
2404
|
+
if (!assignedVars.has(v.name)) {
|
|
2405
|
+
const defaults: Record<string, string> = { str: '""', int: "0", bool: "false", "str[]": "[]" };
|
|
2406
|
+
const defaultVal = defaults[v.varType] || "undefined";
|
|
2407
|
+
SinthWarning.emit(`Variable '${v.name}' is declared but never assigned. Using default value ${defaultVal}.`, v.loc);
|
|
2408
|
+
}
|
|
2409
|
+
if (v.varType === "str") return `var ${v.name} = "";`;
|
|
2410
|
+
if (v.varType === "int") return `var ${v.name} = 0;`;
|
|
2411
|
+
if (v.varType === "bool") return `var ${v.name} = false;`;
|
|
2412
|
+
if (v.varType === "str[]") return `var ${v.name} = [];`;
|
|
2413
|
+
return `var ${v.name};`;
|
|
2414
|
+
}
|
|
2135
2415
|
const val = litToString(v.value);
|
|
2416
|
+
|
|
2136
2417
|
if (v.varType !== "str[]" && val.startsWith("__VAR__")) {
|
|
2137
2418
|
const refName = val.slice(7);
|
|
2138
2419
|
return `var ${v.name} = ${refName};`;
|
|
2139
2420
|
}
|
|
2421
|
+
|
|
2140
2422
|
if (val.startsWith("__ARR__")) {
|
|
2141
2423
|
return `var ${v.name} = ${val.slice(7)};`;
|
|
2142
2424
|
}
|
|
2425
|
+
|
|
2143
2426
|
if (v.varType === "str") return `var ${v.name} = ${JSON.stringify(val)};`;
|
|
2144
2427
|
return `var ${v.name} = ${val};`;
|
|
2145
2428
|
}).join("\n")}
|
|
@@ -2156,7 +2439,7 @@ ${(bodyHTML.includes('data-sinth-if') || bodyHTML.includes('data-sinth-for')) ?
|
|
|
2156
2439
|
}
|
|
2157
2440
|
const cond = eval(t.dataset.sinthIf);
|
|
2158
2441
|
// Find any previously inserted content
|
|
2159
|
-
const existing = t.parentNode.querySelector('[data-sinth-if-anchor="' + t.dataset.sinthIf + '"]');
|
|
2442
|
+
const existing = t.parentNode.querySelector('[data-sinth-if-anchor="' + CSS.escape(t.dataset.sinthIf) + '"]');
|
|
2160
2443
|
if (cond) {
|
|
2161
2444
|
if (!existing) {
|
|
2162
2445
|
// Insert new content with anchor marker
|
|
@@ -2187,7 +2470,7 @@ ${(bodyHTML.includes('data-sinth-if') || bodyHTML.includes('data-sinth-for')) ?
|
|
|
2187
2470
|
if (!elseT.dataset.sinthOriginal) {
|
|
2188
2471
|
elseT.dataset.sinthOriginal = elseT.innerHTML;
|
|
2189
2472
|
}
|
|
2190
|
-
const elseAnchor = t.parentNode.querySelector('[data-sinth-if-anchor="else-' + t.dataset.sinthIf + '"]');
|
|
2473
|
+
const elseAnchor = t.parentNode.querySelector('[data-sinth-if-anchor="' + CSS.escape('else-' + t.dataset.sinthIf) + '"]');
|
|
2191
2474
|
const elseExisting = elseAnchor ? elseAnchor.nextSibling : null;
|
|
2192
2475
|
if (!elseExisting || elseExisting === t) {
|
|
2193
2476
|
const anchor = document.createElement('span');
|
|
@@ -2220,7 +2503,10 @@ ${(bodyHTML.includes('data-sinth-if') || bodyHTML.includes('data-sinth-for')) ?
|
|
|
2220
2503
|
}
|
|
2221
2504
|
} catch(e) {}
|
|
2222
2505
|
});
|
|
2223
|
-
}, 0);` : ''}
|
|
2506
|
+
}, 0);` : ''}
|
|
2507
|
+
|
|
2508
|
+
${logicScriptsRaw}
|
|
2509
|
+
${bodyHTML.includes('sinth-expr') ? `
|
|
2224
2510
|
document.querySelectorAll('.sinth-expr').forEach(el => {
|
|
2225
2511
|
try { el.textContent = eval(el.dataset.expr); } catch(e) {}
|
|
2226
2512
|
});` : ''}
|
|
@@ -2228,6 +2514,7 @@ ${(bodyHTML.includes('data-sinth-if') || bodyHTML.includes('data-sinth-for')) ?
|
|
|
2228
2514
|
}
|
|
2229
2515
|
|
|
2230
2516
|
sinthRender();` : ''}
|
|
2517
|
+
|
|
2231
2518
|
</script>`,
|
|
2232
2519
|
scriptTags.join("\n"),
|
|
2233
2520
|
"</body>",
|