prettyPrint → prettifyIR (make return string instead of printing)

This commit is contained in:
Juhani Krekelä 2018-05-23 21:57:07 +03:00
parent 8d00baafd0
commit 7186a895d2
1 changed files with 20 additions and 16 deletions

36
gir.js
View File

@ -40,7 +40,7 @@ const jumpIfNonZero = Symbol('jumpIfNonZero');
class ParsingError extends Error {} class ParsingError extends Error {}
// (string) → [commandObjects] // (string) → [commandObjects]
// May throw // May throw ParsingError
function parse(program) { function parse(program) {
// (string, int, bool) → {parsed: [commandObjects], lastIndex: int} // (string, int, bool) → {parsed: [commandObjects], lastIndex: int}
// index is the index of the next character to consume // index is the index of the next character to consume
@ -159,10 +159,12 @@ function parse(program) {
return parsed; return parsed;
} }
// ([commandObjects/offsetCommandObjects]) <io> // ([commandObjects/offsetCommandObjects]) → str
function prettyPrint(parsed) { function prettifyIR(parsed) {
// ([commandObjects/offsetCommandObjects], string) <io> // ([commandObjects/offsetCommandObjects], string) → str
function printIndented(parsed, indent = '') { function worker(parsed, indent = '') {
let lines = [];
for(let i = 0; i < parsed.length; i++) { for(let i = 0; i < parsed.length; i++) {
let command = parsed[i]; let command = parsed[i];
@ -172,48 +174,50 @@ function prettyPrint(parsed) {
if('offset' in command) { if('offset' in command) {
line += ` (${command.offset})`; line += ` (${command.offset})`;
} }
console.log(line); lines.push(line);
} else if(command.type == moveHead) { } else if(command.type == moveHead) {
line += `moveHead ${command.value}`; line += `moveHead ${command.value}`;
console.log(line); lines.push(line);
} else if(command.type == writeByte) { } else if(command.type == writeByte) {
line += 'writeByte'; line += 'writeByte';
if('offset' in command) { if('offset' in command) {
line += ` (${command.offset})`; line += ` (${command.offset})`;
} }
console.log(line); lines.push(line);
} else if(command.type == readByte) { } else if(command.type == readByte) {
line += 'readByte'; line += 'readByte';
if('offset' in command) { if('offset' in command) {
line += ` (${command.offset})`; line += ` (${command.offset})`;
} }
console.log(line); lines.push(line);
} else if(command.type == loop) { } else if(command.type == loop) {
line += 'loop'; line += 'loop';
if('isBalanced' in command) { if('isBalanced' in command) {
line += ` (balanced: ${command.isBalanced})`; line += ` (balanced: ${command.isBalanced})`;
} }
console.log(line); lines.push(line);
printIndented(command.contents, indent + ' '); lines = lines.concat(worker(command.contents, indent + ' '));
} else if(command.type == clear) { } else if(command.type == clear) {
line += 'clear'; line += 'clear';
if('offset' in command) { if('offset' in command) {
line += ` (${command.offset})`; line += ` (${command.offset})`;
} }
console.log(line); lines.push(line);
} else if(command.type == jumpIfZero) { } else if(command.type == jumpIfZero) {
line += `jumpIfZero ${command.target}`; line += `jumpIfZero ${command.target}`;
console.log(line); lines.push(line);
} else if(command.type == jumpIfNonZero) { } else if(command.type == jumpIfNonZero) {
line += `jumpIfNonZero ${command.target}`; line += `jumpIfNonZero ${command.target}`;
console.log(line); lines.push(line);
} else { } else {
line += `unknown ${command.type}`; line += `unknown ${command.type}`;
console.log(line); lines.push(line);
} }
} }
return lines.join('\n');
} }
printIndented(parsed); return worker(parsed);
} }
// ------------------------------------------------------------------ // ------------------------------------------------------------------