Skip to content

Commit

Permalink
20595: Support newlines in eval command expression (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
fulpm authored Jun 18, 2024
1 parent d66ad95 commit c0e8f01
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "amalgam-lang",
"version": "1.3.0",
"version": "1.3.1",
"type": "commonjs",
"publisher": "howso",
"displayName": "Amalgam Language",
Expand Down
15 changes: 4 additions & 11 deletions src/debugger/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { platform } from "os";
import { queue, QueueObject } from "async";
import { logger } from "../logging";
import { InvalidRuntimeResponse, RuntimeCommandCancelled, RuntimeNotStarted } from "./errors";
import { NotifySubject } from "./utils";
import { NotifySubject, collapseWhitespace, prepareExpression } from "./utils";

export type DataSource = "stdout" | "stderr" | "stdin";

Expand Down Expand Up @@ -399,14 +399,7 @@ export class AmalgamRuntime extends EventEmitter {
let { value } = await this.sendCommand("pp", signal, v);
if (value != null) {
// Get rid of excess whitespace and newlines
value = value.split("\n").reduce((accumulator, line) => {
const trimmedLine = line.trim();
if (trimmedLine) {
if (accumulator) accumulator += " ";
accumulator += trimmedLine;
}
return accumulator;
}, "");
value = collapseWhitespace(value);
}
return new RuntimeVariable(v, value);
})
Expand Down Expand Up @@ -481,7 +474,7 @@ export class AmalgamRuntime extends EventEmitter {
break;
}
if (command !== "") {
await this.sendCommand("eval", command);
await this.sendCommand("eval", prepareExpression(command));
}
}

Expand Down Expand Up @@ -525,7 +518,7 @@ export class AmalgamRuntime extends EventEmitter {
* @returns The expression result.
*/
public async evaluate(expression: string): Promise<RuntimeVariable> {
const { value } = await this.sendCommand("eval", expression);
const { value } = await this.sendCommand("eval", prepareExpression(expression));
return new RuntimeVariable("eval", value);
}

Expand Down
42 changes: 42 additions & 0 deletions src/debugger/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { homedir } from "os";
export * from "./execute";
export * from "./notify";

/**
* Expand user home in filepath to absolute filepath.
* @param filePath The filepath to expand.
* @returns The expanded filepath.
*/
export function expandUserHome(filePath: string): string {
if (!filePath || typeof filePath !== "string") {
return "";
Expand All @@ -13,3 +18,40 @@ export function expandUserHome(filePath: string): string {

return filePath;
}

/**
* Collapse newlines and excess whitespace from value.
* @param value The value to collapse.
* @returns The modified value.
*/
export function collapseWhitespace(value: string): string {
return value.split("\n").reduce((accumulator, line) => {
const trimmedLine = line.trim();
if (trimmedLine) {
if (accumulator) accumulator += " ";
accumulator += trimmedLine;
}
return accumulator;
}, "");
}

/**
* Prepare an expression before evaluation.
* @param expression The expression to prepare.
* @returns The modified expression.
*/
export function prepareExpression(expression: string): string {
return expression.split("\n").reduce((accumulator, line) => {
let trimmedLine = line.trim();
const commentIndex = trimmedLine.indexOf(";");
if (commentIndex != -1) {
// Remove comments
trimmedLine = trimmedLine.substring(0, commentIndex);
}
if (trimmedLine) {
if (accumulator) accumulator += " ";
accumulator += trimmedLine;
}
return accumulator;
}, "");
}

0 comments on commit c0e8f01

Please sign in to comment.