~boringcactus/crowbar-reference-compiler

cc30b361cd6e7bbfe313f610cd057a4426664f2b — Melody Horn 1 year, 9 months ago 3380490
add more arguments to behave more like gcc
2 files changed, 16 insertions(+), 4 deletions(-)

M README.md
M crowbar_reference_compiler/__init__.py
M README.md => README.md +7 -1
@@ 7,7 7,8 @@ requirements:
usage (i probably will forget to update this so [check directly](https://git.sr.ht/~boringcactus/crowbar-reference-compiler/tree/main/crowbar_reference_compiler/__init__.py)):
```
usage: crowbarc-reference [-h] [-V] [-g] [--stop-at-parse-tree]
                          [--stop-at-qbe-ssa] [-S] [-o OUT]
                          [--stop-at-qbe-ssa] [-S] [-c] [-D DEFINE_CONSTANT]
                          [-I INCLUDE_DIR] [-o OUT]
                          input

The reference compiler for the Crowbar programming language


@@ 22,5 23,10 @@ optional arguments:
  --stop-at-parse-tree
  --stop-at-qbe-ssa
  -S, --stop-at-assembly
  -c, --stop-at-object
  -D DEFINE_CONSTANT, --define-constant DEFINE_CONSTANT
                        define a constant with some literal value
  -I INCLUDE_DIR, --include-dir INCLUDE_DIR
                        folder to look for included headers within
  -o OUT, --out OUT     output file
```

M crowbar_reference_compiler/__init__.py => crowbar_reference_compiler/__init__.py +9 -3
@@ 14,6 14,9 @@ def main():
    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('-c', '--stop-at-object', action='store_true')
    args.add_argument('-D', '--define-constant', help='define a constant with some literal value')
    args.add_argument('-I', '--include-dir', help='folder to look for included headers within')
    args.add_argument('-o', '--out', help='output file')
    args.add_argument('input', help='input file')



@@ 38,20 41,23 @@ def main():
    # 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')
            args.out = args.input.replace('.cro', '.s')
        with open(args.out, 'w', encoding='utf-8') as output_file:
            output_file.write(asm)
        return
    if args.out is None:
        args.out = args.input.replace('.cro', '.out')
    # TODO don't assume gcc is always the right thing
    extra_gcc_flags = []
    if args.stop_at_object:
        if args.out is None:
            args.out = args.input.replace('.cro', '.o')
        extra_gcc_flags.append('-c')
    gcc_result = subprocess.run(['gcc', '-x', 'assembler', '-o', args.out, '-'], input=asm, text=True)
    sys.exit(gcc_result.returncode)