From 88ecd32abbce398503ac78dd1671e8d317b14e39 Mon Sep 17 00:00:00 2001 From: "matti.bat" <138713607+mattibat@users.noreply.github.com> Date: Mon, 28 Sep 2026 19:00:44 +0200 Subject: [PATCH] Parse input/output lines that contain an expression --- .../struktolab/src/common/pseudocode-parser.js | 15 +++++++++++---- .../struktolab/src/common/tree-to-pseudocode.js | 8 ++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/packages/struktolab/src/common/pseudocode-parser.js b/packages/struktolab/src/common/pseudocode-parser.js index 479ca73..22d2274 100644 --- a/packages/struktolab/src/common/pseudocode-parser.js +++ b/packages/struktolab/src/common/pseudocode-parser.js @@ -123,6 +123,13 @@ function extractColumnWidths(str) { return { text: str, columnWidths: null }; } +/** A lone string literal loses its quotes, an expression like "Summe: " + s is kept as written. */ +function ioText(arg) { + const trimmed = arg.trim(); + const literal = /^"([^"]*)"$/.exec(trimmed); + return literal ? literal[1] : trimmed; +} + /** * Tokenize source into lines with their indent level. */ @@ -393,8 +400,8 @@ function mergeBlocks(lines, baseIndent, keywords) { const reFuncDef = new RegExp(`^${eFunction}\\s+(\\w+)\\s*\\(([^)]*)\\)\\s*:$`, "i"); const reSwitchBlock = new RegExp(`^${eSwitch}\\s+(.+)\\s*:$`, "i"); const reCaseLabel = new RegExp(`^${eCase}\\s+(.+)\\s*:$`, "i"); - const reInput = new RegExp(`^${eInput}\\s*\\(\\s*"?([^"]*)"?\\s*\\)$`, "i"); - const reOutput = new RegExp(`^${eOutput}\\s*\\(\\s*"?([^"]*)"?\\s*\\)$`, "i"); + const reInput = new RegExp(`^${eInput}\\s*\\((.*)\\)$`, "i"); + const reOutput = new RegExp(`^${eOutput}\\s*\\((.*)\\)$`, "i"); // First, parse all blocks individually const parsed = []; @@ -595,7 +602,7 @@ function mergeBlocks(lines, baseIndent, keywords) { const inputMatch = text.match(reInput); if (inputMatch) { tail = makeInsert({ - id: uid(), type: "InputNode", text: inputMatch[1], + id: uid(), type: "InputNode", text: ioText(inputMatch[1]), followElement: tail, }); continue; @@ -605,7 +612,7 @@ function mergeBlocks(lines, baseIndent, keywords) { const outputMatch = text.match(reOutput); if (outputMatch) { tail = makeInsert({ - id: uid(), type: "OutputNode", text: outputMatch[1], + id: uid(), type: "OutputNode", text: ioText(outputMatch[1]), followElement: tail, }); continue; diff --git a/packages/struktolab/src/common/tree-to-pseudocode.js b/packages/struktolab/src/common/tree-to-pseudocode.js index 1f767ad..d8c5d0f 100644 --- a/packages/struktolab/src/common/tree-to-pseudocode.js +++ b/packages/struktolab/src/common/tree-to-pseudocode.js @@ -30,6 +30,10 @@ function colWidthSuffix(node) { return ""; } +function ioArg(text) { + return text.includes('"') ? text : '"' + text + '"'; +} + function serialize(node, level, lines, kw) { if (!node) return; @@ -47,12 +51,12 @@ function serialize(node, level, lines, kw) { return; case "InputNode": - lines.push(ind(level) + kw.input + '("' + (node.text || "") + '")'); + lines.push(ind(level) + kw.input + "(" + ioArg(node.text || "") + ")"); serialize(node.followElement, level, lines, kw); return; case "OutputNode": - lines.push(ind(level) + kw.output + '("' + (node.text || "") + '")'); + lines.push(ind(level) + kw.output + "(" + ioArg(node.text || "") + ")"); serialize(node.followElement, level, lines, kw); return;