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:
|
||||
|
12
out.asm
12
out.asm
@@ -1,26 +1,28 @@
|
||||
; ~~~ Auto generated by the GroundPY compiler for Linux x86_64 targets. ~~~
|
||||
|
||||
section .data
|
||||
.LC0: db "Hello, World!", 0
|
||||
.LC0: db "Hello, World!", 10, 0
|
||||
.LC1: equ $ - .LC0
|
||||
|
||||
section .text
|
||||
global _start
|
||||
|
||||
_start:
|
||||
; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdlnout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, String])
|
||||
; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, String])
|
||||
lea rax, [.LC0]
|
||||
push rax
|
||||
mov rax, .LC1
|
||||
push rax
|
||||
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), ..., InstructionNode(instruction='stdlnout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Int])
|
||||
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), ..., InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Int])
|
||||
mov rax, 123
|
||||
push rax
|
||||
; InstructionNode(instruction='stdlnout', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariableReference])
|
||||
; InstructionNode(instruction='stdout', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariableReference])
|
||||
mov rsi, [rsp + 16]
|
||||
mov rdx, [rsp + 8]
|
||||
mov rax, 1
|
||||
mov rdi, 1
|
||||
syscall
|
||||
; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdlnout', parent=..., arguments=[VariableReference]), ...]), arguments=[VariableReference])
|
||||
; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, String]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Int]), InstructionNode(instruction='stdout', parent=..., arguments=[VariableReference]), ...]), arguments=[VariableReference])
|
||||
mov rax, 60
|
||||
mov rdi, [rsp + 0]
|
||||
syscall
|
||||
|
@@ -1,4 +1,4 @@
|
||||
set &x "Hello, World!"
|
||||
set &x "Hello, World!\n"
|
||||
set &y 123
|
||||
stdlnout $x
|
||||
stdout $x
|
||||
end $y
|
@@ -37,11 +37,11 @@ def tokenize(input_string: str):
|
||||
current_token = ""
|
||||
|
||||
instructions = [
|
||||
"stdlnout", "stdout", "stdin", "end", "return", "fun", "endfun", "getstrcharat",
|
||||
"stdout", "stdin", "end", "return", "fun", "endfun", "getstrcharat", "if",
|
||||
"getstrsize", "pusharg", "call", "set", "add", "subtract", "multiply", "divide",
|
||||
"equal", "inequal", "not", "greater", "lesser", "stoi", "stod", "tostring", "use",
|
||||
"extern", "jump", "gettype", "exists", "setlist", "setlistat", "getlistat", "getlistsize",
|
||||
"listappend", "if"
|
||||
"listappend"
|
||||
]
|
||||
|
||||
while pos < len(input_string):
|
||||
|
Reference in New Issue
Block a user