puffnfresh / roy

Small functional language that compiles to JavaScript.
http://roy.brianmckenna.org/
MIT License
834 stars 74 forks source link

Low-order source #184

Closed johanatan closed 10 years ago

johanatan commented 11 years ago

Is there some reason that the source code for Roy is repetitious and has a low signal:noise ratio in many cases? For example, the following 3 functions from TypeInferrence.js are nearly identical and could be replaced with a single function parameterized on 'resultType' and appropriate calls to such.

    visitBinaryNumberOperator: function() {
        var resultType = new t.NumberType();
        var leftType = analyse(node.left, env, nonGeneric, aliases, constraints);
        var rightType = analyse(node.right, env, nonGeneric, aliases, constraints);
        unify(leftType, resultType, node.left.lineno);
        unify(rightType, resultType, node.right.lineno);

        return resultType;
    },
    visitBinaryBooleanOperator: function() {
        var resultType = new t.BooleanType();
        var leftType = analyse(node.left, env, nonGeneric, aliases, constraints);
        var rightType = analyse(node.right, env, nonGeneric, aliases, constraints);
        unify(leftType, resultType, node.left.lineno);
        unify(rightType, resultType, node.right.lineno);

        return resultType;
    },
    visitBinaryStringOperator: function() {
        var resultType = new t.StringType();
        var leftType = analyse(node.left, env, nonGeneric, aliases, constraints);
        var rightType = analyse(node.right, env, nonGeneric, aliases, constraints);
        unify(leftType, resultType, node.left.lineno);
        unify(rightType, resultType, node.right.lineno);

        return resultType;
    },

For a so-called 'functional' language, I expect its implementation to be more functional than this.

puffnfresh commented 11 years ago

Thanks for the code review. New type inference engine is being worked on in the constraints branch (see 904f611f9a04b0dc189eb58a2fcb6e65c47a08fe) - it suffers the same problem so a pull request would be appreciated there.