starting work on math instructions and added support for negative numbers and maybe floats
This commit is contained in:
@@ -24,19 +24,24 @@ class X86_64Generator(Generator):
|
|||||||
self.stack_size -= 1
|
self.stack_size -= 1
|
||||||
|
|
||||||
def get_variable(self, var_name: str, reg: str):
|
def get_variable(self, var_name: str, reg: str):
|
||||||
|
var = self.variables.get(var_name, None)
|
||||||
try:
|
try:
|
||||||
self.push(
|
self.push(
|
||||||
f"QWORD [rsp + {(self.stack_size - self.variables.get(var_name, None)['stack_loc'] - 1) * 8}]"
|
f"QWORD [rsp + {(self.stack_size - var['stack_loc'] - 1) * 8}]"
|
||||||
)
|
)
|
||||||
except TypeError: # variable doesnt exist
|
except TypeError: # variable doesnt exist
|
||||||
traceback(self.code, "NameError", f"\"{var_name}\" is not defined.")
|
traceback(self.code, "NameError", f"\"{var_name}\" is not defined.")
|
||||||
self.pop(reg)
|
self.pop(reg)
|
||||||
|
return var["type"]
|
||||||
|
|
||||||
def get_var_pos(self, var_name: str):
|
def get_var_pos(self, var_name: str):
|
||||||
return (self.stack_size - self.variables.get(var_name)['stack_loc'] - 1) * 8
|
return (self.stack_size - self.variables.get(var_name)['stack_loc'] - 1) * 8
|
||||||
|
|
||||||
def create_variable(self, var_name: str, starting_value):
|
def create_variable(self, var_name: str, starting_value, var_type: Any = None):
|
||||||
new_var = {"stack_loc": (self.stack_size), "type": type(starting_value)}
|
if var_type == None:
|
||||||
|
var_type = type(starting_value)
|
||||||
|
|
||||||
|
new_var = {"stack_loc": (self.stack_size), "type": var_type}
|
||||||
if type(starting_value) == NumberNode:
|
if type(starting_value) == NumberNode:
|
||||||
self.lines.append(f"mov rax, {starting_value.value}\n\t")
|
self.lines.append(f"mov rax, {starting_value.value}\n\t")
|
||||||
self.push("rax")
|
self.push("rax")
|
||||||
@@ -44,6 +49,8 @@ class X86_64Generator(Generator):
|
|||||||
self.get_variable(starting_value.var_name, "rax")
|
self.get_variable(starting_value.var_name, "rax")
|
||||||
#self.lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t")
|
#self.lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t")
|
||||||
self.push("rax")
|
self.push("rax")
|
||||||
|
elif type(starting_value) == str:
|
||||||
|
self.push(starting_value)
|
||||||
self.variables[var_name] = new_var
|
self.variables[var_name] = new_var
|
||||||
|
|
||||||
def change_variable(self, var_name: str, new_value):
|
def change_variable(self, var_name: str, new_value):
|
||||||
@@ -57,6 +64,9 @@ class X86_64Generator(Generator):
|
|||||||
self.get_variable(new_value.var_name, "rax")
|
self.get_variable(new_value.var_name, "rax")
|
||||||
self.lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t")
|
self.lines.append(f"mov QWORD [rsp + {var_pos}], rax\n\t")
|
||||||
|
|
||||||
|
elif type(new_value) == str: # we're changing a variable to the value of a register
|
||||||
|
self.lines.append(f"mov QWORD [rsp + {var_pos}], {new_value}\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"
|
||||||
@@ -64,7 +74,7 @@ class X86_64Generator(Generator):
|
|||||||
elif len(node.arguments) > 1: # example: "end 1 1"
|
elif len(node.arguments) > 1: # example: "end 1 1"
|
||||||
traceback(self.code, "TypeError", "end expects only 1 argument.")
|
traceback(self.code, "TypeError", "end expects only 1 argument.")
|
||||||
if not type(node.arguments[0]) in [NumberNode, VarRefNode]: # example: "end true"
|
if not type(node.arguments[0]) in [NumberNode, VarRefNode]: # example: "end true"
|
||||||
traceback(self.code, "TypeError", f"end expects an integer, not {type(node.arguments[0])}")
|
traceback(self.code, "TypeError", f"end expects an integer, not {node.arguments[0]}")
|
||||||
|
|
||||||
self.lines.append("mov rax, 60\n\t")
|
self.lines.append("mov rax, 60\n\t")
|
||||||
if isinstance(node.arguments[0], NumberNode):
|
if isinstance(node.arguments[0], NumberNode):
|
||||||
@@ -80,7 +90,7 @@ class X86_64Generator(Generator):
|
|||||||
elif len(node.arguments) > 2: # example: "set &hi 0 123"
|
elif len(node.arguments) > 2: # example: "set &hi 0 123"
|
||||||
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 \"{node.arguments[0]}\"")
|
||||||
if type(node.arguments[1]) not in [NumberNode, VarRefNode]:
|
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])}\"")
|
||||||
|
|
||||||
@@ -91,7 +101,87 @@ class X86_64Generator(Generator):
|
|||||||
else: # modify the existing one
|
else: # modify the existing one
|
||||||
self.change_variable(node.arguments[0].var_name, node.arguments[1])
|
self.change_variable(node.arguments[0].var_name, node.arguments[1])
|
||||||
|
|
||||||
|
elif node.instruction == "add":
|
||||||
|
if len(node.arguments) < 3: # example: "add" or "add 1" or "add 1 2"
|
||||||
|
traceback(self.code, "TypeError", "add expects atleast 3 arguments.")
|
||||||
|
elif len(node.arguments) > 3: # example: "add 1 2 3 4"
|
||||||
|
traceback(self.code, "TypeError", "add expects only 3 arguments.")
|
||||||
|
elif type(node.arguments[2]) != VarPointerNode:
|
||||||
|
traceback(self.code, "TypeError", f"the destination of the add command must be a variable pointer, not \"{node.arguments[2]}\"")
|
||||||
|
|
||||||
|
# bro this entire god damn instruction is just error handling 😔
|
||||||
|
number1_type = None
|
||||||
|
number2_type = None
|
||||||
|
arg2 = "rbx"
|
||||||
|
|
||||||
|
if isinstance(node.arguments[0], NumberNode):
|
||||||
|
number1_type = NumberNode
|
||||||
|
#arg1 = node.arguments[0].value
|
||||||
|
self.lines.append(f"mov rax, {node.arguments[0].value}\n\t")
|
||||||
|
elif isinstance(node.arguments[0], VarRefNode):
|
||||||
|
number1_type = self.get_variable(node.arguments[0].var_name, "rax")
|
||||||
|
else:
|
||||||
|
traceback(self.code, "TypeError", f"expected a variable reference or number for argument 1 of add, got {node.arguments[0]}")
|
||||||
|
if isinstance(node.arguments[1], NumberNode):
|
||||||
|
number2_type = NumberNode
|
||||||
|
arg2 = node.arguments[1].value
|
||||||
|
#self.lines.append(f"mov rbx, {node.arguments[1].value}\n\t")
|
||||||
|
elif isinstance(node.arguments[1], VarRefNode):
|
||||||
|
number2_type = self.get_variable(node.arguments[1].var_name, "rbx")
|
||||||
|
else:
|
||||||
|
traceback(self.code, "TypeError", f"expected a variable reference or number for argument 2 of add, got {node.arguments[1]}")
|
||||||
|
|
||||||
|
# TODO: numbers can be added to numbers, but numbers cant be added to strings. but strings can be added to strings, etc...
|
||||||
|
if number1_type not in [NumberNode] or number2_type not in [NumberNode]:
|
||||||
|
traceback(self.code, "TypeError", f"Unsupported operation \"add\" for \"{node.arguments[0]}\" and \"{node.arguments[1]}\".")
|
||||||
|
|
||||||
|
self.lines.append(f"add rax, {arg2}\n\t")
|
||||||
|
|
||||||
|
if self.variables.get(node.arguments[2].var_name, None) == None: # we need to create a variable for the destination
|
||||||
|
self.create_variable(node.arguments[2].var_name, "rax", NumberNode)
|
||||||
|
else:
|
||||||
|
self.change_variable(node.arguments[2].var_name, "rax")
|
||||||
|
elif node.instruction == "subtract":
|
||||||
|
if len(node.arguments) < 3: # example: "subtract" or "subtract 1" or "subtract 1 2"
|
||||||
|
traceback(self.code, "TypeError", "subtract expects atleast 3 arguments.")
|
||||||
|
elif len(node.arguments) > 3: # example: "subtract 1 2 3 4"
|
||||||
|
traceback(self.code, "TypeError", "subtract expects only 3 arguments.")
|
||||||
|
elif type(node.arguments[2]) != VarPointerNode:
|
||||||
|
traceback(self.code, "TypeError", f"the destination of the subtract command must be a variable pointer, not \"{node.arguments[2]}\"")
|
||||||
|
|
||||||
|
# bro this entire god damn instruction is just error handling 😔
|
||||||
|
number1_type = None
|
||||||
|
number2_type = None
|
||||||
|
arg2 = "rbx"
|
||||||
|
|
||||||
|
if isinstance(node.arguments[0], NumberNode):
|
||||||
|
number1_type = NumberNode
|
||||||
|
#arg1 = node.arguments[0].value
|
||||||
|
self.lines.append(f"mov rax, {node.arguments[0].value}\n\t")
|
||||||
|
elif isinstance(node.arguments[0], VarRefNode):
|
||||||
|
number1_type = self.get_variable(node.arguments[0].var_name, "rax")
|
||||||
|
else:
|
||||||
|
traceback(self.code, "TypeError", f"expected a variable reference or number for argument 1 of subtract, got {node.arguments[0]}")
|
||||||
|
if isinstance(node.arguments[1], NumberNode):
|
||||||
|
number2_type = NumberNode
|
||||||
|
arg2 = node.arguments[1].value
|
||||||
|
#self.lines.append(f"mov rbx, {node.arguments[1].value}\n\t")
|
||||||
|
elif isinstance(node.arguments[1], VarRefNode):
|
||||||
|
number2_type = self.get_variable(node.arguments[1].var_name, "rbx")
|
||||||
|
else:
|
||||||
|
traceback(self.code, "TypeError", f"expected a variable reference or number for argument 2 of subtract, got {node.arguments[1]}")
|
||||||
|
|
||||||
|
print(number1_type, number2_type)
|
||||||
|
if number1_type not in [NumberNode] or number2_type not in [NumberNode]:
|
||||||
|
traceback(self.code, "TypeError", f"Unsupported operation \"subtract\" for \"{node.arguments[0]}\" and \"{node.arguments[1]}\".")
|
||||||
|
|
||||||
|
self.lines.append(f"sub rax, {arg2}\n\t")
|
||||||
|
|
||||||
|
if self.variables.get(node.arguments[2].var_name, None) == None: # we need to create a variable for the destination
|
||||||
|
self.create_variable(node.arguments[2].var_name, "rax", NumberNode)
|
||||||
|
else:
|
||||||
|
self.change_variable(node.arguments[2].var_name, "rax")
|
||||||
|
|
||||||
else:
|
else:
|
||||||
self.lines.append("; FUCK\n\t")
|
self.lines.append("; FUCK\n\t")
|
||||||
#raise NotImplementedError(f"A generate method hasn't been made for the \"{node.instruction}\" instruction.")
|
#raise NotImplementedError(f"A generate method hasn't been made for the \"{node.instruction}\" instruction.")
|
19
out.asm
19
out.asm
@@ -1,24 +1,15 @@
|
|||||||
global _start
|
global _start
|
||||||
|
|
||||||
_start:
|
_start:
|
||||||
; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number])
|
; InstructionNode(instruction='set', parent=RootNode(statements=[..., InstructionNode(instruction='add', parent=..., arguments=[VariableReference, Number, VariablePointer]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number])
|
||||||
mov rax, 123
|
mov rax, 100
|
||||||
push rax
|
push rax
|
||||||
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, VariableReference])
|
; InstructionNode(instruction='add', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariableReference, Number, VariablePointer])
|
||||||
push QWORD [rsp + 0]
|
push QWORD [rsp + 0]
|
||||||
pop rax
|
pop rax
|
||||||
|
add rax, -5
|
||||||
mov QWORD [rsp + 0], rax
|
mov QWORD [rsp + 0], rax
|
||||||
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, VariableReference])
|
; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='add', parent=..., arguments=[VariableReference, Number, VariablePointer]), ...]), arguments=[VariableReference])
|
||||||
push QWORD [rsp + 0]
|
|
||||||
pop rax
|
|
||||||
push rax
|
|
||||||
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), ..., InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, Number])
|
|
||||||
mov QWORD [rsp + 8], 0
|
|
||||||
; InstructionNode(instruction='set', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), ..., InstructionNode(instruction='end', parent=..., arguments=[VariableReference])]), arguments=[VariablePointer, VariableReference])
|
|
||||||
push QWORD [rsp + 8]
|
|
||||||
pop rax
|
|
||||||
mov QWORD [rsp + 0], rax
|
|
||||||
; InstructionNode(instruction='end', parent=RootNode(statements=[InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, Number]), InstructionNode(instruction='set', parent=..., arguments=[VariablePointer, VariableReference]), ...]), arguments=[VariableReference])
|
|
||||||
mov rax, 60
|
mov rax, 60
|
||||||
push QWORD [rsp + 0]
|
push QWORD [rsp + 0]
|
||||||
pop rdi
|
pop rdi
|
||||||
|
@@ -1,6 +1,3 @@
|
|||||||
set &x 123
|
set &x 100
|
||||||
set &x $x
|
add $x -5 &x
|
||||||
set &y $x
|
end $x
|
||||||
set &x 0
|
|
||||||
set &y $x
|
|
||||||
end $y
|
|
29
tokenizer.py
29
tokenizer.py
@@ -234,7 +234,10 @@ def tokenize(input_string: str):
|
|||||||
|
|
||||||
if current_char == "\n":
|
if current_char == "\n":
|
||||||
traceback(input_string, "SyntaxError", "Expected a type", line, column, column)
|
traceback(input_string, "SyntaxError", "Expected a type", line, column, column)
|
||||||
|
|
||||||
|
is_number = False
|
||||||
|
if current_char in digits:
|
||||||
|
is_number = True
|
||||||
|
|
||||||
start_col = column
|
start_col = column
|
||||||
while pos < len(input_string):
|
while pos < len(input_string):
|
||||||
@@ -246,16 +249,30 @@ def tokenize(input_string: str):
|
|||||||
line += 1
|
line += 1
|
||||||
column = 1
|
column = 1
|
||||||
break
|
break
|
||||||
|
if is_number and not current_char in digits+".":
|
||||||
|
traceback(input_string, "SyntaxError", "Malformed number.", line, start_col, column)
|
||||||
|
|
||||||
current_token += current_char
|
current_token += current_char
|
||||||
|
|
||||||
pos += 1
|
pos += 1
|
||||||
column += 1
|
column += 1
|
||||||
|
|
||||||
tokens.append(Token(
|
if not is_number:
|
||||||
TokenType.TYPE,
|
tokens.append(Token(
|
||||||
value=current_token
|
TokenType.TYPE,
|
||||||
))
|
value=current_token
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
if "." in current_token:
|
||||||
|
tokens.append(Token(
|
||||||
|
TokenType.FLOAT,
|
||||||
|
value=float("-"+current_token)
|
||||||
|
))
|
||||||
|
else:
|
||||||
|
tokens.append(Token(
|
||||||
|
TokenType.INTEGER,
|
||||||
|
value=int("-"+current_token)
|
||||||
|
))
|
||||||
|
|
||||||
current_token = ""
|
current_token = ""
|
||||||
elif current_char == "@":
|
elif current_char == "@":
|
||||||
@@ -423,7 +440,7 @@ def tokenize(input_string: str):
|
|||||||
#column += 1
|
#column += 1
|
||||||
break
|
break
|
||||||
|
|
||||||
if not current_char in digits:
|
if not current_char in digits + ".":
|
||||||
traceback(input_string, "SyntaxError", "Malformed number.", line, start_col, column)
|
traceback(input_string, "SyntaxError", "Malformed number.", line, start_col, column)
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user