From 81a63fac6da6bc8cd0f994d59c0bff2e1553b80d Mon Sep 17 00:00:00 2001 From: Boyan Date: Mon, 25 Sep 2023 16:15:02 +0200 Subject: [PATCH] Added ucompile tool --- ucompile | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100755 ucompile diff --git a/ucompile b/ucompile new file mode 100755 index 0000000..cdecc04 --- /dev/null +++ b/ucompile @@ -0,0 +1,65 @@ +#!/bin/python3 +import argparse +from os import system,popen +from glob import glob + +def run_input_cases(program, verbose=False): + input_files = glob("./*.in") + if len(input_files) < 1: + return system(program) + for input_file in input_files: + print(f"Running {input_file}") + print(popen(f"{program} < {input_file}").read()) + if verbose: + print("Input:") + system(f"cat {input_file}") + return + +def python_compile(file_name, verbose=False): + run_input_cases("python3 " + file_name, verbose) + return + +def java_compile(file_name, maven=False, verbose=False): + if maven: + system("mvn clean verify") + system("java -jar target/*.jar") + return + + system("javac " + file_name) + run_input_cases("java " + file_name[:file_name.rfind(".")], verbose) + return + +def c_compile(file_name, verbose=False): + system("gcc -Wall -pedantic --std=c99 -g -o program -lm -Wno-unused-result " + file_name) + run_input_cases("./program", verbose) + return + +def main(): + SUPPORTED_LANGS = [".c", ".py"] + parser = argparse.ArgumentParser( + prog='UCompiler', + description='Compiles based on language', + epilog='LOL') + parser.add_argument("filename", metavar="N", type=str) + parser.add_argument("-v", "--verbose", action="store_true", default=False) + parser.add_argument("-a", "--alternative", action="store_true", default=False) + args = parser.parse_args() + file_name = args.filename + file_ext = file_name[file_name.rfind("."):] + if file_ext not in SUPPORTED_LANGS: + print("Unsupported language") + return + + if file_ext == ".c": + c_compile(file_name, args.verbose) + elif file_ext == ".py": + python_compile(file_name, args.verbose) + elif file_ext == ".java": + java_compile(file_name, args.verbose, maven=args.alternative) + return + + +if __name__ == "__main__": + main() + +