2023-09-25 16:15:02 +02:00
|
|
|
#!/bin/python3
|
|
|
|
import argparse
|
|
|
|
from os import system,popen
|
|
|
|
from glob import glob
|
|
|
|
|
2023-09-28 22:14:28 +02:00
|
|
|
def run_input_cases(program, verbose, output):
|
2023-09-25 16:15:02 +02:00
|
|
|
input_files = glob("./*.in")
|
|
|
|
if len(input_files) < 1:
|
2023-09-28 22:14:28 +02:00
|
|
|
|
2023-09-25 16:15:02 +02:00
|
|
|
return system(program)
|
|
|
|
for input_file in input_files:
|
|
|
|
print(f"Running {input_file}")
|
2023-09-28 22:14:28 +02:00
|
|
|
end_command = f"{program} < {input_file}"
|
2023-09-25 16:15:02 +02:00
|
|
|
if verbose:
|
|
|
|
print("Input:")
|
|
|
|
system(f"cat {input_file}")
|
2023-09-28 22:14:28 +02:00
|
|
|
print("")
|
2023-10-05 11:06:11 +02:00
|
|
|
try:
|
|
|
|
if (out := glob(input_file.replace(".in", ".out"))[0]):
|
|
|
|
if verbose:
|
|
|
|
print("Expected output:")
|
|
|
|
system(f"cat {out}")
|
|
|
|
print("")
|
|
|
|
if output:
|
|
|
|
end_command += f" | diff {out} -"
|
|
|
|
except IndexError:
|
|
|
|
pass
|
2023-09-28 22:14:28 +02:00
|
|
|
print(popen(end_command).read())
|
|
|
|
|
|
|
|
print("-----------")
|
|
|
|
|
2023-09-25 16:15:02 +02:00
|
|
|
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
|
|
|
|
|
2023-09-28 22:14:28 +02:00
|
|
|
def c_compile(file_name, verbose=False, output=False):
|
2023-10-05 11:06:11 +02:00
|
|
|
# Style the code in Meijster's way
|
|
|
|
system("astyle -A2s2cxgk3W3xbj " + file_name)
|
2023-09-25 16:15:02 +02:00
|
|
|
system("gcc -Wall -pedantic --std=c99 -g -o program -lm -Wno-unused-result " + file_name)
|
2023-09-28 22:14:28 +02:00
|
|
|
run_input_cases("./program", verbose, output)
|
2023-09-25 16:15:02 +02:00
|
|
|
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)
|
2023-09-28 22:14:28 +02:00
|
|
|
parser.add_argument("-o", "--output", action="store_true", default=False)
|
|
|
|
|
2023-09-25 16:15:02 +02:00
|
|
|
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":
|
2023-09-28 22:14:28 +02:00
|
|
|
c_compile(file_name, args.verbose, args.output)
|
2023-09-25 16:15:02 +02:00
|
|
|
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()
|
|
|
|
|
|
|
|
|