b9org / b9

An educational JS virtual machine based on Eclipse OMR
http://www.base9.xyz
Apache License 2.0
45 stars 24 forks source link

Fix sequence expressions in the front end compiler #123

Open rwy7 opened 6 years ago

rwy7 commented 6 years ago

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 6 years ago

@youngar found this while reviewing #118