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
15 changes: 11 additions & 4 deletions packages/struktolab/src/common/pseudocode-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 6 additions & 2 deletions packages/struktolab/src/common/tree-to-pseudocode.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down
Loading