diff --git a/package-lock.json b/package-lock.json index cd3e9ec..c175414 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "amalgam-lang", - "version": "1.3.0", + "version": "1.3.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "amalgam-lang", - "version": "1.3.0", + "version": "1.3.1", "license": "AGPL-3.0-only", "dependencies": { "@vscode/debugadapter": "^1.59.0", diff --git a/package.json b/package.json index aa173fa..c79cc45 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "amalgam-lang", - "version": "1.3.0", + "version": "1.3.1", "type": "commonjs", "publisher": "howso", "displayName": "Amalgam Language", diff --git a/src/debugger/runtime.ts b/src/debugger/runtime.ts index 30f2bc2..6a347da 100644 --- a/src/debugger/runtime.ts +++ b/src/debugger/runtime.ts @@ -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"; @@ -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); }) @@ -481,7 +474,7 @@ export class AmalgamRuntime extends EventEmitter { break; } if (command !== "") { - await this.sendCommand("eval", command); + await this.sendCommand("eval", prepareExpression(command)); } } @@ -525,7 +518,7 @@ export class AmalgamRuntime extends EventEmitter { * @returns The expression result. */ public async evaluate(expression: string): Promise { - const { value } = await this.sendCommand("eval", expression); + const { value } = await this.sendCommand("eval", prepareExpression(expression)); return new RuntimeVariable("eval", value); } diff --git a/src/debugger/utils/index.ts b/src/debugger/utils/index.ts index 35472a3..413f35a 100644 --- a/src/debugger/utils/index.ts +++ b/src/debugger/utils/index.ts @@ -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 ""; @@ -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; + }, ""); +}