WE CAN RETURN VALUES FROM FUNCTIONS LESS GOOO

This commit is contained in:
SpookyDervish
2025-09-10 07:54:11 +10:00
parent 59bef834c4
commit 3103d17026
5 changed files with 46 additions and 15 deletions

View File

@@ -167,6 +167,7 @@ class X86_64Generator(Generator):
self.function_lines.append(node.name + ":")
for inst in node.statements:
self.generate_InstructionNode(inst, self.function_lines)
self.add_function(node)
def generate_InstructionNode(self, node: InstructionNode, lines = None):
if lines == None:
@@ -420,13 +421,33 @@ class X86_64Generator(Generator):
elif node.instruction == "endfun":
return
elif node.instruction == "return":
self.clamp_instruction_args(node, 0, 1)
if len(node.arguments) == 1:
if isinstance(node.arguments[0], IntNode):
lines.append(f"mov rax, {node.arguments[0].value}")
elif isinstance(node.arguments[0], BoolNode):
lines.append(f"mov rax, {int(node.arguments[0].value)}")
elif isinstance(node.arguments[0], FloatNode):
lines.append(f"mov xmm0, {node.arguments[0].value}")
else:
lines.append("mov rax, 0\n\t")
lines.append("ret\n\t")
elif node.instruction == "call":
self.clamp_instruction_args(node, 1, 1)
self.clamp_instruction_args(node, 1, 2)
if not isinstance(node.arguments[0], FunctionCallNode):
traceback(self.code, "TypeError", "Argument 1 of call needs to be a function reference.")
if not isinstance(node.arguments[1], VarPointerNode):
traceback(self.code, "TypeError", "Argument 1 of call needs to be a variable pointer.")
lines.append(f"call {node.arguments[0].func_name}\n\t")
if len(node.arguments) == 2:
if self.variables.get(node.arguments[1].var_name, None):
self.change_variable(lines, node.arguments[1].var_name, "rax")
else:
self.create_variable(lines, node.arguments[1].var_name, "rax", self.ground_type_to_node(self.functions.get(node.arguments[0].func_name).return_type))
else:
raise NotImplementedError(f"A generate method hasn't been made for the \"{node.instruction}\" instruction.")