added some command line args

This commit is contained in:
SpookyDervish
2025-09-13 07:17:32 +10:00
parent 86d59a8177
commit 983e1e8a74
4 changed files with 20 additions and 73 deletions

29
main.py
View File

@@ -5,15 +5,25 @@ from time import time
from generators import x86_64
from os import system, remove
from error import traceback
from argparse import ArgumentParser
def main():
in_path = "test2.grnd"
out_path = "out"
arch = "x86_64"
arg_parser = ArgumentParser(prog="GroundPY", description="A compiler for the Ground Programming Language.")
arg_parser.add_argument("input", help="ground source file path")
arg_parser.add_argument("--output", "-o", default="out", help="output file name")
arg_parser.add_argument("--arch", "-a", default="x86_64", choices=["x86_64"], help="the architecture to compile for")
arg_parser.add_argument("-S", "--save-asm", help="only generate an asm file, don't actually assemble it", action="store_true")
args = arg_parser.parse_args()
in_path = args.input
out_path = args.output
arch = args.arch
start = time()
file = open(in_path, "r")
try:
file = open(in_path, "r")
except FileNotFoundError:
traceback("", "fatal", f"no such file or directory \"{in_path}\"")
code = file.read()
file.close()
@@ -23,15 +33,14 @@ def main():
if arch == "x86_64":
generator = x86_64.X86_64Generator(ast, code, out_path)
else:
traceback(code, "fatal error", f"unkown architecture \"{arch}\"")
generator.init()
system(f"nasm -felf64 {out_path}.asm")
system(f"ld -m elf_{arch} -o {out_path} {out_path}.o")
remove(out_path + ".o")
#remove(out_path + ".asm")
if not args.save_asm:
system(f"nasm -felf64 {out_path}.asm")
system(f"ld -m elf_{arch} -o {out_path} {out_path}.o")
remove(out_path + ".o")
remove(out_path + ".asm")
compile_time = time()-start
print(f"Compiled in {round(compile_time, 3)} seconds.")