Last active
March 13, 2019 17:50
-
-
Save appositum/1334d77461947741c206d518e0e8d2c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import argparse | |
parser = argparse.ArgumentParser(description='Compile ReasonML using the OCaml Compiler preprocessor.') | |
parser.add_argument('-f', '--files', nargs='+', metavar='<input files>', dest='files', | |
type=str, help='specify the Reason filename to be compiled', required=True) | |
parser.add_argument('-o', metavar='<output file>', | |
dest='output', | |
help='specify the output filename') | |
def remove_extension(filename): | |
return filename.split('.', 1)[0] | |
args = parser.parse_args() | |
args.output = remove_extension(args.files[0]) if not args.output else args.output | |
def ocaml_files_gen(filenames): | |
for f in filenames: | |
os.system(f'refmt --parse re --print ml --interface false {f} > {remove_extension(f)}.ml') | |
def extension_re_to_ml(filename): | |
return filename.split('.', 1)[0] + '.ml' | |
def compile_ml(filenames): | |
os.system(f'ocamlopt -o {args.output} {" ".join(map(extension_re_to_ml, filenames))}') | |
def remove_by_extension(extensions): | |
for file in os.listdir(os.getcwd()): | |
filename, ext = os.path.splitext(file) | |
if ext in extensions: | |
os.remove(filename + ext) | |
def run(files): | |
ocaml_files_gen(files) | |
compile_ml(files) | |
remove_by_extension(['.o', '.cmi', '.cmx', '.cmo', '.cmj', '.ml']) | |
run(args.files) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment