support older python versions

This commit is contained in:
2025-09-02 15:55:21 +10:00
parent b5852cde02
commit 91fa1577ab
2 changed files with 42 additions and 41 deletions

View File

@@ -1,8 +1,9 @@
from console import console from console import console
from sys import exit from sys import exit
from typing import Union
def traceback(code: str, error_type: str, error_message: str, line: int | None = None, start_column: int | None = None, end_column: int | None = None): def traceback(code: str, error_type: str, error_message: str, line: Union[int, None] = None, start_column: Union[int, None] = None, end_column: Union[int, None] = None):
if line != None: if line != None:
console.print(f"[bold red]{error_type} on line {line}: [/]{error_message}\n") console.print(f"[bold red]{error_type} on line {line}: [/]{error_message}\n")
lines = code.split("\n")[line-1:line+2] lines = code.split("\n")[line-1:line+2]

80
main.py
View File

@@ -1,40 +1,40 @@
from tokenizer import tokenize from tokenizer import tokenize
from ground_ast import generate_ast from ground_ast import generate_ast
from rich import print from rich import print
from time import time from time import time
from generators import x86_64 from generators import x86_64
from os import system, remove from os import system, remove
from error import traceback from error import traceback
def main(): def main():
in_path = "test2.grnd" in_path = "test2.grnd"
out_path = "out" out_path = "out"
arch = "x86_64" arch = "x86_64"
start = time() start = time()
file = open(in_path, "r") file = open(in_path, "r")
code = file.read() code = file.read()
file.close() file.close()
tokens = tokenize(code) tokens = tokenize(code)
ast = generate_ast(tokens, code) ast = generate_ast(tokens, code)
generator = None generator = None
if arch == "x86_64": if arch == "x86_64":
generator = x86_64.X86_64Generator(ast, code, out_path) generator = x86_64.X86_64Generator(ast, code, out_path)
else: else:
traceback(code, "fatal error", f"unkown architecture \"{arch}\"") traceback(code, "fatal error", f"unkown architecture \"{arch}\"")
generator.init() generator.init()
compile_time = time()-start compile_time = time()-start
print(f"Compiled in {round(compile_time, 1)} seconds.") print(f"Compiled in {round(compile_time, 1)} seconds.")
system(f"nasm -felf64 {out_path}.asm") system(f"nasm -felf64 {out_path}.asm")
system(f"ld -o {out_path} {out_path}.o -m elf_{arch}") system(f"ld -arch elf_{arch} -o {out_path} {out_path}.o")
remove(out_path + ".o") remove(out_path + ".o")
#remove(out_path + ".asm") #remove(out_path + ".asm")
if __name__ == "__main__": if __name__ == "__main__":
main() main()