From 51f7564bdb8fddfc410dfd7f1fb560c50b7d42a0 Mon Sep 17 00:00:00 2001 From: Thorn Walli Date: Tue, 1 Aug 2023 17:35:38 +0200 Subject: [PATCH] fix(basic-interpreter): fix `commandPrintUsing` --- src/web-workbench/classes/BasicInterpreter.js | 23 +++++++------- test/basicInterpreter.test.js | 31 +++++++++++++++++-- 2 files changed, 40 insertions(+), 14 deletions(-) diff --git a/src/web-workbench/classes/BasicInterpreter.js b/src/web-workbench/classes/BasicInterpreter.js index 51df82271..e3a33116f 100644 --- a/src/web-workbench/classes/BasicInterpreter.js +++ b/src/web-workbench/classes/BasicInterpreter.js @@ -46,7 +46,7 @@ class Parser { parse (lines) { lines = lines || [].concat(this.#lines); - const line = lines.shift().replace(/^ */, ''); + const line = lines.shift().trim(); // eslint-disable-next-line complexity return new Promise((resolve) => { @@ -314,7 +314,7 @@ class Parser { } // eslint-disable-next-line complexity - parseValue (value, silent = true) { + parseValue (value, silent = true, error = false) { if (value === undefined) { // Undefined return Promise.resolve(value); @@ -344,6 +344,8 @@ class Parser { if (this.#memory.has(value.replace(/ /g, ''))) { value = value.replace(/ /g, ''); return this.parseValue(this.#memory.get(value), true); + } else if (error) { + throw new Error('test'); } else { return this.#cb(value, { message: !silent ? value : undefined }).then((item) => { return item; @@ -517,20 +519,17 @@ class Parser { async commandPrintUsing (value, args) { const parsedValue = await this.parseValue(value, true); try { - // if (args.includes(',')) { - // throw new Error('has ,'); ; - // } + args = (await Promise.all( + CommandParser.resolveValues(CommandParser.extractValues(args.trim(), ',')).map((arg) => { + return this.parseValue(arg, true, true); + }) + )); + } catch (error) { args = [ await this.parseValue(args, true) ]; - } catch (error) { - args = (await Promise.all( - CommandParser.resolveValues(CommandParser.extractValues(args, ',')).reduce((result, arg) => { - result.push(this.parseValue(arg, true)); - return result; - }, []) - )); } + console.log('args2', args); const parsedArgs = args.reduce((result, val) => { // eslint-disable-next-line security/detect-unsafe-regex const match = parsedValue.match(/((#+)(\.(#+))?)/); diff --git a/test/basicInterpreter.test.js b/test/basicInterpreter.test.js index 17d1359bd..4806b02dc 100644 --- a/test/basicInterpreter.test.js +++ b/test/basicInterpreter.test.js @@ -38,10 +38,12 @@ describe('BasicInterpreter', () => { const lines = [ 'PRINT "Hello World"', 'PRINT LEN("ABC")', - 'PRINT USING "Hello #"; "World")' + 'PRINT USING "Hello #"; "World"', + 'PRINT USING "# #"; "hello", "world"' ]; const results = [ - 'Hello World', '3', 'Hello World' + 'Hello World', '3', 'Hello World', + 'hello world' ]; const output = await executeCommands(lines); @@ -50,8 +52,33 @@ describe('BasicInterpreter', () => { }); }); + it('Built-in Functions', async () => { + const lines = [ + 'PRINT USING "LEN(\\"ABC\\") #"; LEN("ABC")', + 'PRINT USING "ASC(\\"D\\") ##"; ASC("D")', + 'PRINT USING "CHR$(68) ##"; CHR$(68)', + 'PRINT USING "LEFT$(\\"XYZ\\", LEN(\\"XYZ\\")-1) #"; LEFT$("XYZ", LEN("XYZ")-1)', + 'PRINT USING "RIGHT$(\\"XYZ\\", LEN(\\"XYZ\\")-1) #"; RIGHT$("XYZ", LEN("XYZ")-1)' + ]; + + const results = [ + 'LEN("ABC") 3', + 'ASC("D") 68', + 'CHR$(68) D', + 'LEFT$("XYZ", LEN("XYZ")-1) XY', + 'RIGHT$("XYZ", LEN("XYZ")-1) YZ' + ]; + + const output = await executeCommands(lines); + + results.forEach((result, i) => { + expect(output[Number(i)]).toBe(result); + }); + }); + it('Function', async () => { const lines = [ + '// Functions', 'SUB Separator(stars) STATIC',