コード例 #1
0
ファイル: compiler.py プロジェクト: benekastah/jittery-python
    def compile_Call(self, node, use_kwargs = True):
        func = node.func
        args = node.args
        starargs = node.starargs
        keywords = node.keywords
        kwargs = node.kwargs

        if isinstance(func, ast.Name):
            _type = self.context_stack.find_type(func)
        elif isinstance(func, ast.Attribute):
            _type = self.context_stack.find_type(func.value)
        else:
            _type = None

        if _type and self._is_native_type(_type):
            use_kwargs = False

        if func is "__test__":
            print_node(node)

        if kwargs:
            if keywords:
                fn = ast.Name("__merge__", ast.Load())
                d = utils.keywords_to_dict(keywords)
                kwargs = ast.Call(fn, [kwargs, d], None, None, None)
        else:
            kwargs = ast.Dict([], [])

        if starargs:
            allargs = []
            if args:
                allargs.append(utils.list_to_ast(args))
            allargs.append(starargs)
            if use_kwargs:
                allargs.append(kwargs)

            if len(allargs) is 1:
                args = allargs[0]
            else:
                base = allargs[0]
                rest = allargs[1:]
                concat = ast.Attribute(base, "concat", ast.Load())
                concat_call = ast.Call(concat, rest, None, None, None)
                args = JSCode(self.compile_node(concat_call, use_kwargs = False))

            _apply = ast.Attribute(func, "apply", ast.Load())
            call = ast.Call(_apply, [ast.Name("None", ast.Load()), args], None, None, None)
            return self.compile_node(call, use_kwargs = False)
        else:
            if use_kwargs:
                args = args + [kwargs]
            return "%s(%s)" % (self.compile_node(func), self.compile_node_list(args))
コード例 #2
0
ファイル: compiler.py プロジェクト: benekastah/jittery-python
    def compile(self, bare = False, verbose = False):
        self.bare = bare
        self.verbose = verbose

        if self.input_file_path:
            input_f = open(self.input_file_path, 'r')
            python_code = input_f.read(self.max_file_size)
        else:
            python_code = self.input_text
        file_ast = ast.parse(python_code)

        if self.verbose:
            print_node(file_ast)

        self._nodes = []
        self.compile_node(file_ast)
        if self.is_main_compiler:
            if not self.bare:
                if self.builtins.module_text:
                    self.compile_Import(input_text = self.builtins.module_text, input_name = "builtins")
                import_stmt = self.import_me()
                code = self.modules + [import_stmt]
            else:
                code = self.modules

            compiled = ";\n\n".join(code) + ";\n"
            if not self.bare:
                compiled = "(function () {\n\n" \
                           "%s\n" \
                           "})();" % compiled

            if self.output_file_path:
                output_f = open(self.output_file_path, 'w+')
                output_f.write(compiled)
                output_f.close()
            else:
                return compiled