Merge branch 'master' of dumtaxat-git:boyan_k/Coding_Tools

This commit is contained in:
Boyan 2024-11-20 19:39:57 +01:00
commit 4bee3f28df

View File

@ -3,6 +3,17 @@ import argparse
import os import os
from glob import glob from glob import glob
import subprocess import subprocess
from colorama import Fore, Style
def check_dependencies() -> None:
# Necessary commands list
commands = ["astyle", "gcc", "ghc", "java", "javac", "mvn", "python3"]
for command in commands:
if os.system(f"which {command} > /dev/null") != 0:
print(f"{Fore.RED}Error: {command} not found{Style.RESET_ALL}")
return
def run_input_cases(program, verbose, output=False, tests_dir="."): def run_input_cases(program, verbose, output=False, tests_dir="."):
input_files = glob(os.path.join(tests_dir, "*.in")) input_files = glob(os.path.join(tests_dir, "*.in"))
@ -46,12 +57,13 @@ def run_input_cases(program, verbose, output=False, tests_dir="."):
print("✅") print("✅")
else: else:
print("❌") print("❌")
print("Expected output:") # green expected output and red program output
print(expected_output.strip()) print(f"{Fore.GREEN}Expected output:{Style.RESET_ALL}")
print("Program output:") print(expected_output)
print(f"{Fore.RED}Program output:{Style.RESET_ALL}")
print(program_output.strip()) print(program_output.strip())
else: else:
print("Expected output file not found; cannot compare outputs.") print(f"{Fore.YELLOW}Expected output file not found; cannot compare outputs.{Style.RESET_ALL}")
if not verbose: if not verbose:
print("Program output:") print("Program output:")
print(program_output.strip()) print(program_output.strip())
@ -75,6 +87,11 @@ def java_compile(file_name, maven=False, verbose=False, tests_dir="."):
run_input_cases("java " + file_name[:file_name.rfind(".")], verbose, tests_dir=tests_dir) run_input_cases("java " + file_name[:file_name.rfind(".")], verbose, tests_dir=tests_dir)
return return
def haskell_compile(file_name, verbose=False, tests_dir="."):
os.system("ghc -O2 " + file_name + " -o program")
run_input_cases("./program", verbose, tests_dir=tests_dir)
return
def c_compile(file_name, verbose=False, output=False, tests_dir="."): def c_compile(file_name, verbose=False, output=False, tests_dir="."):
# Style the code in Meijster's way # Style the code in Meijster's way
os.system("astyle -A2s2cxgk3W3xbj " + file_name) os.system("astyle -A2s2cxgk3W3xbj " + file_name)
@ -83,7 +100,7 @@ def c_compile(file_name, verbose=False, output=False, tests_dir="."):
return return
def main(): def main():
SUPPORTED_LANGS = [".c", ".py", ".java"] SUPPORTED_LANGS = [".c", ".py", ".java", ".hs"]
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog='UCompiler', prog='UCompiler',
description='Compiles based on language', description='Compiles based on language',
@ -110,8 +127,10 @@ def main():
python_compile(file_name, args.verbose, tests_dir=tests_dir) python_compile(file_name, args.verbose, tests_dir=tests_dir)
elif file_ext == ".java": elif file_ext == ".java":
java_compile(file_name, args.alternative, args.verbose, tests_dir=tests_dir) java_compile(file_name, args.alternative, args.verbose, tests_dir=tests_dir)
elif file_ext == ".hs":
haskell_compile(file_name, args.verbose, tests_dir=tests_dir)
return return
if __name__ == "__main__": if __name__ == "__main__":
check_dependencies()
main() main()