def main(): import exprparse import sys from errors import subscribe_errors lexer = exprlex.make_lexer() parser = exprparse.make_parser() with subscribe_errors(lambda msg: sys.stdout.write(msg + "\n")): program = parser.parse(open(sys.argv[1]).read()) # Check the program check_program(program)
# ---------------------------------------------------------------------- # DO NOT MODIFY ANYTHING BELOW # ---------------------------------------------------------------------- def generate_code(node): ''' Generate SSA code from the supplied AST node. ''' gen = GenerateCode() gen.visit(node) return gen if __name__ == '__main__': import exprlex import exprparse import exprcheck import sys from errors import subscribe_errors, errors_reported lexer = exprlex.make_lexer() parser = exprparse.make_parser() with subscribe_errors(lambda msg: sys.stdout.write(msg+"\n")): program = parser.parse(open(sys.argv[1]).read()) # Check the program exprcheck.check_program(program) # If no errors occurred, generate code if not errors_reported(): code = generate_code(program) # Emit the code sequence exprblock.PrintBlocks().visit(code.start_block) #for inst in code.code: # print(inst)
Perform constant folding optimization on the AST. ''' return ConstantFolder(debug=debug).visit(node) if __name__ == '__main__': import exprlex import exprparse import exprcheck import exprcode import exprpygen import sys from errors import subscribe_errors, errors_reported lexer = exprlex.make_lexer() parser = exprparse.make_parser() with subscribe_errors(lambda msg: sys.stdout.write(msg + "\n")): program = parser.parse(open(sys.argv[1]).read()) # Check the program exprcheck.check_program(program) # If no errors occurred, generate code if not errors_reported(): # Perform constant constants program = fold_constants(program, debug=True) # Create 3-address IR code = exprcode.generate_code(program) # Generate Python code output exprpygen.emit_pycode("main", code) print("if __name__ == '__main__':") print(" main()")