sqrt function works... finally... ;-;
This commit is contained in:
50
compiler.py
50
compiler.py
@@ -22,11 +22,15 @@ class Compiler:
|
||||
"Short": ir.IntType(16),
|
||||
"Int": ir.IntType(32),
|
||||
"Long": ir.IntType(64),
|
||||
"Float": ir.FloatType(),
|
||||
"Float": ir.DoubleType(),
|
||||
"Double": ir.DoubleType(),
|
||||
"String": ir.PointerType(ir.IntType(8)),
|
||||
"Nil": ir.VoidType()
|
||||
}
|
||||
self.py_type_map: dict[str, type] = {
|
||||
"Int": int,
|
||||
"Float": float
|
||||
}
|
||||
|
||||
self.module: ir.Module = ir.Module("main")
|
||||
self.builder: ir.IRBuilder = ir.IRBuilder()
|
||||
@@ -50,6 +54,14 @@ class Compiler:
|
||||
var_arg=True
|
||||
)
|
||||
return ir.Function(self.module, fnty, "printf")
|
||||
|
||||
def __init_sqrt() -> ir.Function:
|
||||
fnty: ir.FunctionType = ir.FunctionType(
|
||||
self.type_map["Float"],
|
||||
[ir.DoubleType()],
|
||||
var_arg=False
|
||||
)
|
||||
return ir.Function(self.module, fnty, "sqrt")
|
||||
|
||||
def __init_booleans() -> tuple[ir.GlobalVariable, ir.GlobalVariable]:
|
||||
bool_type: ir.Type = self.type_map["Bool"]
|
||||
@@ -65,6 +77,7 @@ class Compiler:
|
||||
return true_var, false_var
|
||||
|
||||
self.environment.define("print", __init_print(), ir.IntType(32))
|
||||
self.environment.define("sqrt", __init_sqrt(), ir.IntType(32))
|
||||
|
||||
true_var, false_var = __init_booleans()
|
||||
self.environment.define("true", true_var, true_var.type)
|
||||
@@ -221,6 +234,8 @@ class Compiler:
|
||||
|
||||
value = None
|
||||
Type = None
|
||||
|
||||
|
||||
match operator:
|
||||
case "=":
|
||||
value = right_value
|
||||
@@ -246,8 +261,10 @@ class Compiler:
|
||||
value = self.builder.fdiv(orig_value, right_value)
|
||||
case _:
|
||||
print("Unsupported assignment operator.")
|
||||
return
|
||||
|
||||
ptr, _ = self.environment.lookup(name)
|
||||
|
||||
self.builder.store(value, ptr)
|
||||
|
||||
def __visit_if_statement(self, node: IfStatement) -> None:
|
||||
@@ -281,9 +298,14 @@ class Compiler:
|
||||
|
||||
test, _ = self.__resolve_value(condition)
|
||||
|
||||
|
||||
|
||||
while_loop_entry = self.builder.append_basic_block(f"while_loop_entry_{self.__increment_counter()}")
|
||||
while_loop_otherwise = self.builder.append_basic_block(f"while_loop_otherwise_{self.counter}")
|
||||
|
||||
self.breakpoints.append(while_loop_otherwise)
|
||||
self.continues.append(while_loop_entry)
|
||||
|
||||
# Creating a condition branch
|
||||
# condition
|
||||
# / \
|
||||
@@ -299,6 +321,9 @@ class Compiler:
|
||||
self.builder.cbranch(test, while_loop_entry, while_loop_otherwise)
|
||||
self.builder.position_at_start(while_loop_otherwise)
|
||||
|
||||
self.breakpoints.pop()
|
||||
self.continues.pop()
|
||||
|
||||
def __visit_break_statement(self, node: BreakStatement) -> None:
|
||||
self.builder.branch(self.breakpoints[-1])
|
||||
|
||||
@@ -458,7 +483,14 @@ class Compiler:
|
||||
case "print":
|
||||
ret = self.builtin_print(params=args, return_type=types[0])
|
||||
ret_type = self.type_map["Int"]
|
||||
case "sqrt":
|
||||
ret = self.builtin_sqrt(params=args)
|
||||
ret_type = self.type_map["Float"]
|
||||
case _:
|
||||
if not self.environment.lookup(name):
|
||||
print(f"The function \"{name}\" is not defined.")
|
||||
exit(1)
|
||||
|
||||
func, ret_type = self.environment.lookup(name)
|
||||
ret = self.builder.call(func, args)
|
||||
|
||||
@@ -563,6 +595,7 @@ class Compiler:
|
||||
|
||||
return global_fmt, global_fmt.type
|
||||
|
||||
# region Builtins
|
||||
def builtin_print(self, params: list[ir.Instruction], return_type: ir.Type) -> None:
|
||||
func, _ = self.environment.lookup("print")
|
||||
|
||||
@@ -582,4 +615,19 @@ class Compiler:
|
||||
# printing from a normal string
|
||||
fmt_arg = self.builder.bitcast(self.module.get_global(f"__str_{self.counter}"), ir.IntType(8).as_pointer())
|
||||
return self.builder.call(func, [fmt_arg, *rest_params])
|
||||
|
||||
def builtin_sqrt(self, params: list[ir.Instruction]) -> None:
|
||||
func, _ = self.environment.lookup("sqrt")
|
||||
|
||||
c_float = self.builder.alloca(self.type_map["Float"])
|
||||
self.builder.store(params[0], c_float)
|
||||
|
||||
if isinstance(params[0], ir.LoadInstr):
|
||||
c_fmt: ir.LoadInstr = params[0]
|
||||
g_var_ptr = c_fmt.operands[0]
|
||||
float_val = self.builder.load(g_var_ptr)
|
||||
return self.builder.call(func, [float_val])
|
||||
else:
|
||||
return self.builder.call(func, [params[0]])
|
||||
# endregion
|
||||
# endregion
|
||||
Reference in New Issue
Block a user