diff options
Diffstat (limited to 'crowbar_reference_compiler')
-rw-r--r-- | crowbar_reference_compiler/__init__.py | 41 |
1 files changed, 36 insertions, 5 deletions
diff --git a/crowbar_reference_compiler/__init__.py b/crowbar_reference_compiler/__init__.py index 92b9497..87f5aa7 100644 --- a/crowbar_reference_compiler/__init__.py +++ b/crowbar_reference_compiler/__init__.py @@ -5,24 +5,55 @@ from .ssagen import compile_to_ssa def main(): import argparse + import subprocess + import sys args = argparse.ArgumentParser(description='The reference compiler for the Crowbar programming language') - args.add_argument('-V', '--version', action='version', version='%(prog)s 0.0.2') + args.add_argument('-V', '--version', action='version', version='%(prog)s 0.0.3') args.add_argument('-g', '--include-debug-info', action='store_true') + args.add_argument('--stop-at-parse-tree', action='store_true') + args.add_argument('--stop-at-qbe-ssa', action='store_true') args.add_argument('-S', '--stop-at-assembly', action='store_true') args.add_argument('-o', '--out', help='output file') args.add_argument('input', help='input file') args = args.parse_args() - if args.out is None: - args.out = args.input.replace('.cro', '.ssa') with open(args.input, 'r', encoding='utf-8') as input_file: input_code = input_file.read() tokens = scan(input_code) parse_tree = parse_implementation(tokens) + if args.stop_at_parse_tree: + if args.out is None: + args.out = args.input.replace('.cro', '.cro.txt') + with open(args.out, 'w', encoding='utf-8') as output_file: + output_file.write(str(parse_tree)) + return ssa = compile_to_ssa(parse_tree) - with open(args.out, 'w', encoding='utf-8') as output_file: - output_file.write(ssa) + if args.stop_at_qbe_ssa: + if args.out is None: + args.out = args.input.replace('.cro', '.ssa') + with open(args.out, 'w', encoding='utf-8') as output_file: + output_file.write(ssa) + return + # TODO bundle the qbe binary or something + qbe_result = subprocess.run(['qbe', '-'], input=ssa, capture_output=True, text=True) + if qbe_result.returncode != 0: + if args.out is None: + args.out = args.input.replace('.cro', '.s') + print(qbe_result.stderr, file=sys.stderr) + sys.exit(1) + asm = qbe_result.stdout + if args.stop_at_assembly: + if args.out is None: + args.out = args.input.replace('.cro', '.o') + with open(args.out, 'w', encoding='utf-8') as output_file: + output_file.write(ssa) + return + if args.out is None: + args.out = args.input.replace('.cro', '.out') + # TODO don't assume gcc is always the right thing + gcc_result = subprocess.run(['gcc', '-x', 'assembler', '-o', args.out, '-'], input=asm, text=True) + sys.exit(gcc_result.returncode) if __name__ == '__main__': |