From e04bf9053ac24712bf2272c183406d7bb4853102 Mon Sep 17 00:00:00 2001 From: SpookyDervish <78246495+SpookyDervish@users.noreply.github.com> Date: Sun, 19 Oct 2025 19:01:14 +1100 Subject: [PATCH] you can output to executables now --- main.py | 25 ++++++++++++++++--------- tests/math.pla | 19 ------------------- tests/test.pla | 10 +++++----- 3 files changed, 21 insertions(+), 33 deletions(-) delete mode 100644 tests/math.pla diff --git a/main.py b/main.py index 1728927..0728a8c 100644 --- a/main.py +++ b/main.py @@ -23,6 +23,7 @@ def parse_arguments() -> Namespace: arg_parser.add_argument("--ast", default=None, help="export the generated AST to a JSON file") arg_parser.add_argument("--llvm", default=None, help="export the generated LLVM IR to a file") arg_parser.add_argument("--silent", action="store_true", help="don't print anything!!!!") + arg_parser.add_argument("--output", type=str, help="path to where the outputed executable will be. if not provided, the program will be JIT compiled", default=None) return arg_parser.parse_args() @@ -89,17 +90,23 @@ if __name__ == "__main__": target_machine = llvm.Target.from_default_triple().create_target_machine() - engine = llvm.create_mcjit_compiler(llvm_ir_parsed, target_machine) - engine.finalize_object() + if args.output != None: + obj_data = target_machine.emit_object(llvm_ir_parsed) + with open(args.output, "wb") as f: + f.write(obj_data) + else: + engine = llvm.create_mcjit_compiler(llvm_ir_parsed, target_machine) + engine.finalize_object() - entry = engine.get_function_address("main") - cfunc = CFUNCTYPE(c_int)(entry) + entry = engine.get_function_address("main") + cfunc = CFUNCTYPE(c_int)(entry) - st = time.time() + st = time.time() - result = cfunc() + result = cfunc() - et = time.time() + et = time.time() - print(f"\n\nProgram returned: {result}\n=== Executed in {round((et - st) * 1000, 6)} ms. ===") - #exit(result) \ No newline at end of file + print(f"\n\nProgram returned: {result}\n=== Executed in {round((et - st) * 1000, 6)} ms. ===") + exit(result) + \ No newline at end of file diff --git a/tests/math.pla b/tests/math.pla deleted file mode 100644 index 22d5e3e..0000000 --- a/tests/math.pla +++ /dev/null @@ -1,19 +0,0 @@ -pi = Func(): Float { - return 3.141592653589793; -} - -e = Func(): Float { - return 2.718281828459045; -} - -rad = Func(degrees: Float): Float { - return degrees * $pi() / 180; -} - -deg = Func(radians: Float): Float { - return 180 * radians / $pi(); -} - -sqrt = Func(n: Float): Float { - return $pow(n, 0.5); -} \ No newline at end of file diff --git a/tests/test.pla b/tests/test.pla index 1bdf3e7..b141327 100644 --- a/tests/test.pla +++ b/tests/test.pla @@ -1,7 +1,7 @@ -depend "tests/math.pla"; - main = Func(): Int { - $print("%f\n", $sqrt(100.0)); - return 0; -} + for (i: Int = 1; i < 100000; i+=1;) { + $print("%i\n",i); + } + return 0; +} \ No newline at end of file