kopscript 0.5.0 → 0.5.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/dist/codegen.js +19 -3
- package/package.json +1 -1
package/dist/codegen.js
CHANGED
|
@@ -147,10 +147,10 @@ export class CodeGenerator {
|
|
|
147
147
|
const init = stmt.init
|
|
148
148
|
? stmt.init.kind === "VarDecl"
|
|
149
149
|
? `${stmt.init.isConst ? "const" : "let"} ${stmt.init.name} = ${this.genExpr(stmt.init.init)}`
|
|
150
|
-
: this.
|
|
150
|
+
: this.genExprAtStatementLevel(stmt.init.expression)
|
|
151
151
|
: "";
|
|
152
152
|
const cond = stmt.condition ? this.genExpr(stmt.condition) : "";
|
|
153
|
-
const update = stmt.update ? this.
|
|
153
|
+
const update = stmt.update ? this.genExprAtStatementLevel(stmt.update) : "";
|
|
154
154
|
return `${pad}for (${init}; ${cond}; ${update}) ${this.genBlock(stmt.body, indent).trimStart()}`;
|
|
155
155
|
}
|
|
156
156
|
case "ForInStatement":
|
|
@@ -162,7 +162,7 @@ export class CodeGenerator {
|
|
|
162
162
|
case "ContinueStatement":
|
|
163
163
|
return `${pad}continue;`;
|
|
164
164
|
case "ExpressionStatement":
|
|
165
|
-
return `${pad}${this.
|
|
165
|
+
return `${pad}${this.genExprAtStatementLevel(stmt.expression)};`;
|
|
166
166
|
case "TryStatement":
|
|
167
167
|
return this.genTry(stmt, indent);
|
|
168
168
|
case "ThrowStatement":
|
|
@@ -245,6 +245,22 @@ export class CodeGenerator {
|
|
|
245
245
|
return `${pad}constructor(${params}) ${bodyStr}`;
|
|
246
246
|
}
|
|
247
247
|
// ---------- expressions ----------
|
|
248
|
+
// genExpr wraps an assignment in parens unconditionally (`(x = y)`), since
|
|
249
|
+
// it has to stay safely embeddable wherever an expression can appear —
|
|
250
|
+
// inside a binary expression, a call argument, anywhere precedence could
|
|
251
|
+
// matter. At true statement level (an expression-statement, or a for
|
|
252
|
+
// loop's own init/update clause) nothing surrounds it that parens could
|
|
253
|
+
// ever matter for, so they're pure noise there — `(this.Count = 0);`
|
|
254
|
+
// reads like defensive generated code, not something a person would
|
|
255
|
+
// write by hand. Used at exactly those three call sites, not inside
|
|
256
|
+
// genExpr itself, since every other embedding context still needs the
|
|
257
|
+
// defensive parens.
|
|
258
|
+
genExprAtStatementLevel(expr) {
|
|
259
|
+
if (expr.kind === "AssignExpr") {
|
|
260
|
+
return `${this.genExpr(expr.target)} = ${this.genExpr(expr.value)}`;
|
|
261
|
+
}
|
|
262
|
+
return this.genExpr(expr);
|
|
263
|
+
}
|
|
248
264
|
genExpr(expr) {
|
|
249
265
|
switch (expr.kind) {
|
|
250
266
|
case "NumberLiteral":
|
package/package.json
CHANGED