Files
Plasma/main.py

82 lines
1.8 KiB
Python
Raw Normal View History

2025-10-13 06:55:35 +11:00
from lexer import Lexer
2025-10-13 17:41:07 +11:00
from plasma_parser import Parser
2025-10-13 21:05:03 +11:00
from compiler import Compiler
2025-10-13 17:41:07 +11:00
from AST import Program
import json
import time
2025-10-13 06:55:35 +11:00
2025-10-13 21:05:03 +11:00
from llvmlite import ir
import llvmlite.binding as llvm
2025-10-13 21:05:03 +11:00
from ctypes import CFUNCTYPE, c_int, c_float
LEXER_DEBUG: bool = False
2025-10-18 07:21:19 +11:00
PARSER_DEBUG: bool = False
COMPILER_DEBUG: bool = False
2025-10-16 17:35:00 +11:00
RUN_CODE: bool = True
2025-10-13 06:55:35 +11:00
if __name__ == "__main__":
2025-10-13 21:05:03 +11:00
with open("tests/test.pla") as f:
2025-10-13 06:55:35 +11:00
code: str = f.read()
if LEXER_DEBUG:
debug_lex: Lexer = Lexer(source=code)
while debug_lex.current_char is not None:
2025-10-13 17:41:07 +11:00
print(debug_lex.next_token())
l: Lexer = Lexer(source=code)
p: Parser = Parser(lexer=l)
2025-10-13 21:05:03 +11:00
program: Program = p.parse_program()
if len(p.errors) > 0:
for err in p.errors:
print(err)
exit(1)
2025-10-13 17:41:07 +11:00
if PARSER_DEBUG:
print("===== PARSER DEBUG =====")
2025-10-13 21:05:03 +11:00
#program: Program = p.parse_program()
2025-10-13 17:41:07 +11:00
with open("debug/ast.json", "w") as f:
json.dump(program.json(), f, indent=4)
2025-10-13 21:05:03 +11:00
print("Wrote AST to debug/ast.json successfully.")
c: Compiler = Compiler()
c.compile(program)
module: ir.Module = c.module
module.triple = llvm.get_default_triple()
2025-10-13 21:05:03 +11:00
if COMPILER_DEBUG:
with open("debug/ir.ll", "w") as f:
f.write(str(module))
if RUN_CODE:
#llvm.initialize()
llvm.initialize_native_target()
llvm.initialize_native_asmparser()
llvm.initialize_native_asmprinter()
try:
llvm_ir_parsed = llvm.parse_assembly(str(module))
llvm_ir_parsed.verify()
except Exception as e:
print(e)
raise
target_machine = llvm.Target.from_default_triple().create_target_machine()
engine = llvm.create_mcjit_compiler(llvm_ir_parsed, target_machine)
engine.finalize_object()
entry = engine.get_function_address("main")
cfunc = CFUNCTYPE(c_int)(entry)
st = time.time()
result = cfunc()
et = time.time()
print(f"\n\nProgram returned: {result}\n=== Executed in {round((et - st) * 1000, 6)} ms. ===")