Exemple #1
0
def compile_script(filename, code):
    code_generator = CodeGenerator()
    ast_, errors = parse(code)
    for lineno, message in errors:
        sys.stderr.write("%s:%d: %s\n" % (filename, lineno, message))

    ast_ = RequireInliner().visit(ast_)
    ast_ = AssignmentScopeAnalyzer().visit(ast_)
    ast_ = TypeAnnotator().visit(ast_)
    ast_ = CallInliner().visit(ast_)
    ast_ = EnumInliner().visit(ast_)
    ast_ = StringInliner().visit(ast_)

    code_generator.visit(ast_)

    return code_generator.code
Exemple #2
0
    def visit_Require(self, require):
        base_filename, extension = splitext(require.path)

        if extension == ".redux":
            with open(require.path, "rt") as file_:
                code = file_.read()
        else:
            with open(require.path + ".redux", "rt") as file_:
                code = file_.read()

        ast_, errors = parse(code)
        if errors:
            for lineno, message in errors:
                sys.stderr.write("%s:%d: %s\n" % (require.path, lineno, message))
            raise RuntimeError
        else:
            class TopLevelCodeError(RuntimeError):
                pass

            class TopLevelCodeChecker(ASTVisitor):
                """Checks there is no top-level code in the required file."""
                def visit_FunctionDefinition(self, funcdef):
                    pass

                def visit_EnumDefinition(self, enumdef):
                    pass

                def visit_BitfieldDefinition(self, bitfielddef):
                    pass

                def visit_Require(self, require):
                    pass

                def visit_Stmt(self, stmt):
                    raise TopLevelCodeError(stmt)

            TopLevelCodeChecker().visit(ast_)
            self.visit(ast_)
            return ast_.statements
Exemple #3
0
def check_valid_parse(code, ast_):
    compiled_ast, errors = parse(code)
    eq_(errors, [])
    eq_(compiled_ast, ast_)