示例#1
0
def compile_functiondeclaration(context, ast):
    assert(isinstance(ast,ast_objects.FunctionDeclaration))
    # new context, but need access to outer context
    ctx = Context()
    
    fn_index = context.register_variable(ast.name)
    
    for v in context.constants:
        ctx.constants.append(v)
    for k, v in context.variables.iteritems():
        ctx.variables[k] = v
    
    indexes = []
    
    if type(ast.args) is not ast_objects.Null:
        
        for arg in reversed(ast.args.get_statements()):
            assert(isinstance(arg,ast_objects.Variable))
            name = str(arg.getname())
            index = ctx.register_variable(name)
            indexes.append(index)
            #context.emit(bytecode.STORE_VARIABLE, index)
        
    compile_block(ctx,ast.block)
    
    fn = ctx.build(indexes, name=ast.name)
    context.variables[fn_index] = objects.Variable(ast.name,objects.Function(ast.name,fn))
    ctx.variables[fn_index] = objects.Variable(ast.name,objects.Function(ast.name,fn))
    context.emit(bytecode.LOAD_VARIABLE,fn_index)
示例#2
0
    def eval_func_literal(self, ast_node, env):
        params = ast_node.params
        body = ast_node.body
        name = ast_node.name

        func = objects.Function(params, body, closure=env)

        if name is not None:
            env.set(name, func)

        return func
示例#3
0
 def evaluate_function_def(self, node, env):
     func = objects.Function(node.id, node.params, node.body, env)
     env.set(node.id, func)