Skip to content

Commit

Permalink
fix(basic-interpreter): fix commandPrintUsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ThornWalli committed Aug 1, 2023
1 parent bdeaeeb commit 51f7564
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
23 changes: 11 additions & 12 deletions src/web-workbench/classes/BasicInterpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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(/((#+)(\.(#+))?)/);
Expand Down
31 changes: 29 additions & 2 deletions test/basicInterpreter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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',
Expand Down

0 comments on commit 51f7564

Please sign in to comment.