Skip to content

Commit

Permalink
feat: Add memory logic (store and load commands)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVaultdweller13 committed Dec 28, 2024
1 parent e9df9bc commit 00dae2b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
27 changes: 27 additions & 0 deletions executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@
class UnknownSubroutine(Exception):
pass

class InvalidMemoryAddressException(Exception):
pass

class ImperivmExecutor:
def __init__(self, ast):
self.subroutines = {}
self.stack = []
self.heap = []
for _, (_, identifier), block in ast:
self.subroutines[identifier] = block

Expand Down Expand Up @@ -83,6 +86,30 @@ def execute_instruction(self, instruction, bindings: Bindings):
self.invoke_subroutine(subroutine, Bindings())
elif operation == "stop":
return True
elif operation == "store":
while len(self.stack) >= 2:
pos = self.stack.pop()
value = self.stack.pop()

if pos < 0:
raise InvalidMemoryAddressException(f"Invalid position {pos}")

if pos >= len(self.heap):
self.heap.extend([None] * (pos - len(self.heap) + 1))

self.heap[pos] = value
elif operation == "load":
temp_positions = []
while len(self.stack) > 0:
pos = self.stack.pop()
temp_positions.append(pos)

for pos in reversed(temp_positions):
if pos < 0 or pos >= len(self.heap):
raise InvalidMemoryAddressException(f"Invalid memory access at position: {pos}")

value = self.heap[pos]
self.stack.append(value)
else:
print("error", instruction)

Expand Down
6 changes: 4 additions & 2 deletions grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
program = subroutine (sp_0n br ws_0n subroutine)* ws_0n
subroutine = identifier ws_1n block
block = begin ws_1n (instruction (sp_0n br ws_0n instruction)*)? ws_1n end
instruction = assignment / conditional / loop / stack_op / arithmetic_op / io_op / stop / identifier / halt
instruction = assignment / conditional / loop / stack_op / arithmetic_op / io_op / stop / identifier / halt / store / load
assignment = assign sp_1n value sp_1n identifier
conditional = if sp_1n value ws_1n block (ws_1n elif sp_1n value ws_1n block)* (ws_1n else ws_1n block)?
Expand Down Expand Up @@ -33,7 +33,7 @@
sp_0n = ~r"[ \t]*"
sp_1n = ~r"[ \t]+"
reserved = begin / end / stop / if / elif / else / while / push / pop / assign / add / subtract / multiply / divide / print / exit
reserved = begin / end / stop / if / elif / else / while / push / pop / assign / add / subtract / multiply / divide / print / exit / store / load
begin = ~r"begin"i / ~r"do"i
exit = ~r"exit"i
end = ~r"end"i
Expand All @@ -50,5 +50,7 @@
multiply = ~r"multiply"i
divide = ~r"divide"i
print = ~r"print"i
store = ~r"store"i
load = ~r"load"i
"""
)
18 changes: 18 additions & 0 deletions tests/memory_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
main begin
push 5
push 0
push 8
push 1
push 0
push 2
store

push 0
push 1
push 2
load

pop x
exit x
exit 1
end
6 changes: 6 additions & 0 deletions visitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ def visit_subroutine(self, _, visited_children):
identifier, _, block = visited_children
return "subroutine", identifier, block

def visit_store(self, _, __):
return ("store",)

def visit_load(self, _, __):
return ("load",)

def visit_block(self, _, visited_children):
_, _, instructions, _, _ = visited_children
[child] = instructions
Expand Down

0 comments on commit 00dae2b

Please sign in to comment.