Files
GroundPY/main.py
2025-09-13 07:17:32 +10:00

51 lines
1.5 KiB
Python

from tokenizer import tokenize
from ground_ast import generate_ast
from rich import print
from time import time
from generators import x86_64
from os import system, remove
from error import traceback
from argparse import ArgumentParser
def main():
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()
try:
file = open(in_path, "r")
except FileNotFoundError:
traceback("", "fatal", f"no such file or directory \"{in_path}\"")
code = file.read()
file.close()
tokens = tokenize(code)
ast = generate_ast(tokens, code)
generator = None
if arch == "x86_64":
generator = x86_64.X86_64Generator(ast, code, out_path)
generator.init()
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.")
if __name__ == "__main__":
main()