b9org/b9

Fix sequence expressions in the front end compiler

rwy7 opened this issue · 1 comments

rwy7 commented

In JS, a sequence expression is, well, a comma-seperated sequence of expressions. The result of the expression is the result of the last subexpression.

For example:

var a = 1 + 2, 3 + 4, 5 + 6;
console.log(a); // prints "11"

Recently, the frontend compiler (js_compiler/compile.js) was updated, but in the update, sequence expressions were broken. Particularly, the handle function was updated to accept two arguments: the output function, and the expression to compile. Unfortunately, not all callsites were fixed. There is at least one handle call in handleSequenceExpression that's bad. There may be other issues with the code.

The second part of this task is to write tests for sequence expressions (right now, there aren't any). The tests should be implemented in test/interpreter_test.src, which is a test suite written in JS. Each test should return 1 on success, and 0 on failure. Each test must also be added to the test list in b9test.cpp. The b9test program is responsible for actually running the tests. If you forget to add the tests to the list, the tests will not be run.

The bad call to handle:

	// Iterate expressions array
	this.handleSequenceExpression = function (func, sequence) {
		var expressions = sequence.expressions;
		var droplast = !decl.isParameter;

		for (expression in sequence.expressions.slice(0, -1)) {
			this.handle(func, expression);
			func.instructions.push(new Instruction("DROP"));
		}

		var last = sequence.expressions.slice(-1)[0];
		this.handle(last); ////////////////////// bad ///////////////////////

		if (!sequence.isParameter) {
			func.instructions.push(new Instruction("DROP"));
		}
	};
rwy7 commented

@youngar found this while reviewing #118