From 23ec72e961b007e5368c4dae01888e67be36bc14 Mon Sep 17 00:00:00 2001 From: SpookyDervish <78246495+SpookyDervish@users.noreply.github.com> Date: Sat, 6 Sep 2025 21:18:22 +1000 Subject: [PATCH] cleanup and small escape character fix --- generators/x86_64.py | 33 +++++++-------------------------- out | Bin 8880 -> 8880 bytes out.asm | 12 +++++++----- test2.grnd | 4 ++-- tokenizer.py | 4 ++-- 5 files changed, 18 insertions(+), 35 deletions(-) diff --git a/generators/x86_64.py b/generators/x86_64.py index bcd1491..fe810fc 100644 --- a/generators/x86_64.py +++ b/generators/x86_64.py @@ -50,7 +50,7 @@ class X86_64Generator(Generator): except TypeError: # variable doesnt exist traceback(self.code, "NameError", f"\"{var_name}\" is not defined.") - print(var, self.stack_size) + return var["type"] def get_var_pos(self, var_name: str): @@ -81,7 +81,9 @@ class X86_64Generator(Generator): self.stack_size += 1 elif type(starting_value) == StringNode: - string_pointer = self.add_constant(starting_value.value) + string_pointer = self.add_constant( + starting_value.value + ) string_len = self.add_constant(f"equ $ - {string_pointer[1:-1]}", no_string=True) self.lines.append(f"lea rax, {string_pointer}\n\t") self.push("rax") @@ -365,28 +367,6 @@ class X86_64Generator(Generator): arg = node.arguments[0] printed_value = arg.__str__() - if isinstance(arg, VarRefNode): - self.get_variable(arg.var_name, "rax") - string_pointer = "rax" - else: - string_pointer = self.add_constant(printed_value)[1:-1] - string_len = self.add_constant(f"equ $ - {string_pointer}", True)[1:-1] - - self.lines.append("mov rax, 1\n\t") # sys_write syscall - self.lines.append("mov rdi, 1\n\t") # a file descriptor of 1 is stdout - self.lines.append(f"mov rsi, {string_pointer}\n\t") # pointer - self.lines.append(f"mov rdx, {string_len}\n\t") # length - self.lines.append("syscall\n\t") - - elif node.instruction == "stdlnout": - if len(node.arguments) < 1: # example: "stdlnout" - traceback(self.code, "TypeError", "stdlnout expects atleast 1 argument.") - elif len(node.arguments) > 1: # example: "stdlnout "hi" 123" - traceback(self.code, "TypeError", "stdlnout expects at most 1 argument.") - - arg = node.arguments[0] - - printed_value = arg.__str__() + "\n" if isinstance(arg, VarRefNode): self.get_variable(arg.var_name, "rsi", False, 0, True) @@ -396,7 +376,6 @@ class X86_64Generator(Generator): string_len = self.add_constant(f"equ $ - {string_pointer}", True)[1:-1] self.lines.append(f"mov rsi, {string_pointer}\n\t") self.lines.append(f"mov rdx, {string_len}\n\t") # length - print(string_pointer) self.lines.append("mov rax, 1\n\t") # sys_write syscall self.lines.append("mov rdi, 1\n\t") # a file descriptor of 1 is stdout @@ -409,6 +388,8 @@ class X86_64Generator(Generator): def write(self): with open(self.output_path + ".asm", "w") as f: + f.write("; ~~~ Auto generated by the GroundPY compiler for Linux x86_64 targets. ~~~\n\n") + f.write("section .data\n") for name, const in self.constants.items(): value = const["value"] @@ -416,7 +397,7 @@ class X86_64Generator(Generator): value_type = type(value) if value_type == str: if not const["no_string"]: - f.write(f"db \"{value.replace("\n","\", 10, \"")}\", 0".replace(", \"\", ", ", ")) + f.write(f"db \"{value.replace("\\n","\", 10, \"")}\", 0".replace(", \"\", ", ", ")) else: f.write(value) elif value_type == float or value_type == int: diff --git a/out b/out index 706843bd874d290aad251cd599d4804915167343..5e5360f4912d528dcb922531b1cff1cee2dc6cfa 100644 GIT binary patch delta 65 zcmdnsy1{kA1x|hj2;kp%(~zH$e>1lLlK>;v#E(*wKPa+H)=@BF