variable reasignment works!!!

This commit is contained in:
SpookyDervish
2025-09-02 16:26:19 +10:00
parent aac6608311
commit ef48978bb5
4 changed files with 29 additions and 11 deletions

View File

@@ -29,6 +29,17 @@ class X86_64Generator(Generator):
) )
self.pop(reg) self.pop(reg)
def change_variable(self, var_name: str, new_value):
var_pos = (self.stack_size - self.variables.get(var_name)['stack_loc'] - 1) * 8
if type(new_value) == NumberNode: # we're changing a variable to a number
self.lines.append(f"mov rax, {new_value.value}\n\t")
self.lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t")
elif type(new_value) == VarRefNode: # we're changing a variable to the value of another variable
self.get_variable(new_value.var_name, "rax")
self.lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t")
def generate_InstructionNode(self, node: InstructionNode): def generate_InstructionNode(self, node: InstructionNode):
if node.instruction == "end": if node.instruction == "end":
if len(node.arguments) == 0: # example: "end" if len(node.arguments) == 0: # example: "end"
@@ -53,13 +64,19 @@ class X86_64Generator(Generator):
traceback(self.code, "TypeError", "set expects only 2 arguments.") traceback(self.code, "TypeError", "set expects only 2 arguments.")
if not isinstance(node.arguments[0], VarPointerNode): if not isinstance(node.arguments[0], VarPointerNode):
traceback(self.code, "TypeError", f"the first argument of set should be a variable pointer, not \"{type(node.arguments[0])}\"") traceback(self.code, "TypeError", f"the first argument of set should be a variable pointer, not \"{type(node.arguments[0])}\"")
if type(node.arguments[1]) not in [NumberNode]: if type(node.arguments[1]) not in [NumberNode, VarRefNode]:
traceback(self.code, "TypeError", f"variables can't be of type \"{type(node.arguments[1])}\"") traceback(self.code, "TypeError", f"variables can't be of type \"{type(node.arguments[1])}\"")
self.variables[node.arguments[0].var_name] = {"stack_loc": self.stack_size} variable_exists = self.variables.get(node.arguments[0].var_name, None) != None
if type(node.arguments[1]) == NumberNode:
self.lines.append(f"mov rax, {node.arguments[1].value}\n\t") if not variable_exists: # create a new variable
self.push("rax") self.variables[node.arguments[0].var_name] = {"stack_loc": (self.stack_size), "type": type(node.arguments[1])}
if type(node.arguments[1]) == NumberNode:
self.lines.append(f"mov rax, {node.arguments[1].value}\n\t")
self.push("rax")
else: # modify the existing one
self.change_variable(node.arguments[0].var_name, node.arguments[1])
else: else:
self.lines.append("; FUCK\n\t") self.lines.append("; FUCK\n\t")

BIN
out

Binary file not shown.

11
out.asm
View File

@@ -1,15 +1,16 @@
global _start global _start
_start: _start:
; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='add', parent=..., arguments=[VariableReference, Number, VariablePointer]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number]) ; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number])
mov rax, 66 mov rax, 66
push rax push rax
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='add', parent=..., arguments=[VariableReference, Number, VariablePointer]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number]) ; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number])
mov rax, 69 mov rax, 69
push rax push rax
; InstructionNode(instruction='add', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariableReference, Number, VariablePointer]) ; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number])
; FUCK mov rax, 123
; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='add', parent=..., arguments=[VariableReference, Number, VariablePointer]), ...]), arguments=[VariableReference]) mov QWORD [rsp + 8], rax
; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ...]), arguments=[VariableReference])
mov rax, 60 mov rax, 60
push QWORD [rsp + 8] push QWORD [rsp + 8]
pop rdi pop rdi

View File

@@ -1,4 +1,4 @@
set &x 66 set &x 66
set &y 69 set &y 69
add $x 1 &x set &x 123
end $x end $x