type-inference.js
// We annotate each scope with the set of variables they declare.
We annotate each scope with the set of variables they declare.
We annotate each scope with the set of variables they declare .
function buildEnvs(node, scope) {
    if (node.type === 'Program') {
        scope = node;
        scope.$env = new Map;
    }
    switch (node.type) {
        case 'FunctionDeclaration':
        case 'FunctionExpression':
        case 'ArrowFunctionExpression':
            if (node.type == 'FunctionDeclaration') {
                scope.$env.put(node.id.name, node.id);
            }
            scope = node;
            node.$env = new Map;
            for (var i=0; i<node.params.length; i++) {
                scope.$env.put(node.params[i].name, node.params[i]);
            }
            if (node.type !== 'ArrowFunctionExpression') {
                node.$env.put("arguments", node);
            }
            break;
        case 'VariableDeclarator':
            scope.$env.put(node.id.name, node.id);
            break;
        case 'CatchClause':
            node.$env = new Map;
            node.$env.put(node.param.name, node.id);
            break;
    }
    var list = children(node);
    for (var i=0; i<list.length; i++) {
        buildEnvs(list[i], scope);
    }
}
function buildEnvs ( node , scope ) { if ( node . type === 'Program' ) { scope = node ; scope . $env = new Map ; } switch ( node . type ) { case 'FunctionDeclaration' : case 'FunctionExpression' : case 'ArrowFunctionExpression' : if ( node . type == 'FunctionDeclaration' ) { scope . $env . put ( node . id . name , node . id ) ; } scope = node ; node . $env = new Map ; for ( var i = 0 ; i < node . params . length ; i ++ ) { scope . $env . put ( node . params [ i ] . name , node . params [ i ] ) ; } if ( node . type !== 'ArrowFunctionExpression' ) { node . $env . put ( "arguments" , node ) ; } break ; case 'VariableDeclarator' : scope . $env . put ( node . id . name , node . id ) ; break ; case 'CatchClause' : node . $env = new Map ; node . $env . put ( node . param . name , node . id ) ; break ; } var list = children ( node ) ; for ( var i = 0 ; i < list . length ; i ++ ) { buildEnvs ( list [ i ] , scope ) ; } }
We annotate each scope with the set of variables they declare.
We annotate each scope with the set of variables they declare .