Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/struktolab/src/common/code-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const TRANSLATIONS = {
HeadLoopNode: { pre: "while ", post: ":\n" },
FootLoopNode: {
prepre: "while True:\n",
pre: " if not ",
post: ":\n break\n",
pre: "if not ",
post: ":\n",
postpost: "break\n",
},
FunctionNode: { pre: "def ", between: "(", post: "):\n" },
CaseNode: { pre: "if ", post: ":\n" },
Expand Down Expand Up @@ -228,8 +229,9 @@ function transform(node, level, t, lang) {
if (t.pseudoSwitch) {
// Python: the pre/post contain the break logic
lines.push(
indent(level) + t.FootLoopNode.pre + text + t.FootLoopNode.post,
indent(level + 1) + t.FootLoopNode.pre + text + t.FootLoopNode.post,
);
lines.push(indent(level + 2) + t.FootLoopNode.postpost);
} else {
if (t.rightBracket) lines.push(indent(level) + t.rightBracket + " ");
lines.push(t.FootLoopNode.pre + text + t.FootLoopNode.post);
Expand Down
14 changes: 14 additions & 0 deletions packages/struktolab/src/common/code-generator.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import assert from "node:assert/strict";
import { test } from "node:test";
import { generateCode } from "./code-generator.js";
import { KEYWORDS_EN, parsePseudocode } from "./pseudocode-parser.js";

const python = (code) => generateCode(parsePseudocode(code, KEYWORDS_EN), "python");

test("indents the break of a nested foot loop in Python", () => {
const code = "function f(n):\n repeat:\n n = n - 1\n while n > 0";
assert.equal(
python(code),
"def f(n):\n while True:\n n = n - 1\n if not n > 0:\n break\n",
);
});
Loading