Example #1
0
            outname = os.path.splitext(filename)[0] + '.s'

            file = open(filename)
            data = file.read()
            file.close()

            if ("-lex" in sys.argv):
                paslex.run_lexer(data)
            else:
                parser = pasparser.make_parser()
                clear_errors()
                try:
                    with subscribe_errors(
                            lambda msg: sys.stdout.write(msg + "\n")):
                        root_ast = parser.parse(data)
                        check.check_program(root_ast)
                    errors = errors_reported()
                    if errors == 0:
                        if ("-ast" in sys.argv):
                            # dump_class_tree(root_ast)
                            dot = DotVisitor()
                            dot.generic_visit(root_ast)
                            print dot
                        # Generate code
                        outfile = open(outname, "w")
                        mpasgen.generate(outfile, root_ast)
                        outfile.close()
                    else:
                        sys.stderr.write("Number of errors: %d" % errors)
                except parseError as e:
                    sys.stderr.write("Program couldn't be compiled\n")
Example #2
0
            filename =  sys.argv[-1]
            outname = os.path.splitext(filename)[0] + '.s'

            file = open(filename)
            data = file.read()
            file.close()

            if ("-lex" in sys.argv):
                paslex.run_lexer(data)
            else:
                parser = pasparser.make_parser()
                clear_errors()
                try:
                    with subscribe_errors(lambda msg: sys.stdout.write(msg+"\n")):
                        root_ast = parser.parse(data)
                        check.check_program(root_ast);
                    errors = errors_reported();
                    if errors == 0:
                        if ("-ast" in sys.argv):
                            # dump_class_tree(root_ast)
                            dot = DotVisitor()
                            dot.generic_visit(root_ast)
                            print dot
                        # Generate code
                        outfile = open(outname, "w")
                        mpasgen.generate(outfile, root_ast)
                        outfile.close()
                    else:
                        sys.stderr.write("Number of errors: %d" % errors)
                except parseError as e:
                    sys.stderr.write("Program couldn't be compiled\n")

    def visit_WhileBlock(self,block):
        # Emit a conditional jump around the if-branch
        #inst = ('while', block.condvar)
        #block+=inst)
        self.visit_BasicBlock(block)
        self.visit(block.truebranch)


if __name__ == '__main__':
    import lexer
    import parser as parser 
    import check
    import sys
    from errors import subscribe_errors, errors_reported
    lexer = lexer.make_lexer()
    parser = parser.make_parser()
    with subscribe_errors(lambda msg: sys.stdout.write(msg+"\n")):
        program = parser.parse(open(sys.argv[1]).read())
        # Check the program
        env = check.check_program(program)
        # If no errors occurred, generate code
        if not errors_reported():
            code = generate_code(program)
            print code
            # Emit the code sequence
            JumpGenerator().visit(code)
            #for inst in code:
            #    print(inst)