Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ttulka committed Apr 5, 2021
1 parent aaa651f commit df37638
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 44 deletions.
73 changes: 37 additions & 36 deletions src/Compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,41 +39,41 @@ export default class Compiler {
compileBranch(branch) {
const body = [];
branch.children.forEach(cmd => {
let c;
switch (cmd.kind) {
case '+':
c = this.increment(cmd.amount);
body.push(c);
break;
case '-':
c = this.decrement(cmd.amount);
body.push(c);
break;
case '>':
c = this.moveRight(cmd.amount);
body.push(c);
break;
case '<':
c = this.moveLeft(cmd.amount);
body.push(c);
break;
case '.':
c = this.output();
body.push(c);
break;
case ',':
c = this.input();
body.push(c);
break;
case 'loop':
c = this.loop(cmd, this.loopIdx++);
body.push(c);
break;
case '#':
c = this.debug();
body.push(c);
break;
}
let c;
switch (cmd.kind) {
case '+':
c = this.increment(cmd.amount);
body.push(c);
break;
case '-':
c = this.decrement(cmd.amount);
body.push(c);
break;
case '>':
c = this.moveRight(cmd.amount);
body.push(c);
break;
case '<':
c = this.moveLeft(cmd.amount);
body.push(c);
break;
case '.':
c = this.output();
body.push(c);
break;
case ',':
c = this.input();
body.push(c);
break;
case 'loop':
c = this.loop(cmd, this.loopIdx++);
body.push(c);
break;
case '#':
c = this.debug();
body.push(c);
break;
}
});
return body;
}
Expand Down Expand Up @@ -123,9 +123,10 @@ export default class Compiler {
}

loop(branch, idx) {
const label = 'l' + idx;
const commands = this.compileBranch(branch);

const label = 'l' + idx;

const p = this.module.global.get('p', binaryen.i32);
const val = this.module.i32.load8_u(0, 1, p);
const br = this.module.br_if(label, val);
Expand Down
22 changes: 16 additions & 6 deletions src/Parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class Parser {
}

parse(input) {
this.input = input;
this.source = input;
this.index = 0;
this.ast = [{ kind: 'root', children: [] }];

Expand All @@ -23,35 +23,45 @@ export default class Parser {
}

const root = this.ast.pop();

// are we still in an unclosed branch?
if (root.kind !== 'root') throw new SyntaxError('unmatched [');

return root;
}

parseCommand() {
let code;
if (!this.isWhitespace(code = this.input[this.index++].charCodeAt())) {
const code = this.source[this.index++].charCodeAt();
if (!this.isWhitespace(code)) {
const kind = this.commandFor(code);
const cmd = { kind };

// current branch
const branch = this.ast[this.ast.length - 1];

if (kind === '[') {
// loop begin
cmd.kind = 'loop';
cmd.children = [];
// push to the current branch
branch.children.push(cmd);

// push a new branch for loop
this.ast.push(cmd);

} else if (kind === ']') {
// are we currently in a loop branch?
if (this.ast.pop().kind !== 'loop') throw new SyntaxError('unmatched ]');

} else {
// eliminate commands by grouping them
let eliminated = false;
if (branch.children.length && kind !== '.' && kind !== ',') {
if (branch.children.length
&& kind !== '.' && kind !== ',') { // output and input cannot be grouped
// following the same kind of command?
const last = branch.children[branch.children.length - 1];
if (last.kind === kind) {
last.amount++;
last.amount++; // increase the group amount
eliminated = true;
}
}
Expand All @@ -72,6 +82,6 @@ export default class Parser {
}

eof() {
return this.index >= this.input.length;
return this.index >= this.source.length;
}
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export function main(args) {
fs.writeFileSync(`${moduleName}.wasm`, binary);
}

function compile(input, moduleName) {
function compile(input) {
const parser = new Parser();
const ast = parser.parse(input);

const compiler = new Compiler();
return compiler.compile(ast, moduleName);
return compiler.compile(ast);
}

0 comments on commit df37638

Please sign in to comment.