Coding_Tools/ucompile

66 lines
1.9 KiB
Plaintext
Raw Normal View History

2023-09-25 16:15:02 +02:00
#!/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()