you can output to executables now

This commit is contained in:
SpookyDervish
2025-10-19 19:01:14 +11:00
parent 31099835de
commit e04bf9053a
3 changed files with 21 additions and 33 deletions

25
main.py
View File

@@ -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)
print(f"\n\nProgram returned: {result}\n=== Executed in {round((et - st) * 1000, 6)} ms. ===")
exit(result)

View File

@@ -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);
}

View File

@@ -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;
}