support older python versions
This commit is contained in:
3
error.py
3
error.py
@@ -1,8 +1,9 @@
|
||||
from console import console
|
||||
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:
|
||||
console.print(f"[bold red]{error_type} on line {line}: [/]{error_message}\n")
|
||||
lines = code.split("\n")[line-1:line+2]
|
||||
|
80
main.py
80
main.py
@@ -1,40 +1,40 @@
|
||||
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
|
||||
|
||||
|
||||
def main():
|
||||
in_path = "test2.grnd"
|
||||
out_path = "out"
|
||||
arch = "x86_64"
|
||||
|
||||
start = time()
|
||||
file = open(in_path, "r")
|
||||
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)
|
||||
else:
|
||||
traceback(code, "fatal error", f"unkown architecture \"{arch}\"")
|
||||
|
||||
generator.init()
|
||||
compile_time = time()-start
|
||||
print(f"Compiled in {round(compile_time, 1)} seconds.")
|
||||
|
||||
system(f"nasm -felf64 {out_path}.asm")
|
||||
system(f"ld -o {out_path} {out_path}.o -m elf_{arch}")
|
||||
remove(out_path + ".o")
|
||||
#remove(out_path + ".asm")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
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
|
||||
|
||||
|
||||
def main():
|
||||
in_path = "test2.grnd"
|
||||
out_path = "out"
|
||||
arch = "x86_64"
|
||||
|
||||
start = time()
|
||||
file = open(in_path, "r")
|
||||
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)
|
||||
else:
|
||||
traceback(code, "fatal error", f"unkown architecture \"{arch}\"")
|
||||
|
||||
generator.init()
|
||||
compile_time = time()-start
|
||||
print(f"Compiled in {round(compile_time, 1)} seconds.")
|
||||
|
||||
system(f"nasm -felf64 {out_path}.asm")
|
||||
system(f"ld -arch elf_{arch} -o {out_path} {out_path}.o")
|
||||
remove(out_path + ".o")
|
||||
#remove(out_path + ".asm")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Reference in New Issue
Block a user