cleanup and small escape character fix
This commit is contained in:
@@ -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:
|
||||
|
Reference in New Issue
Block a user