Skip to content

Commit 526d4fb

Browse files
committed
Updating error states and example program
1 parent 6698d0c commit 526d4fb

File tree

3 files changed

+17
-7
lines changed

3 files changed

+17
-7
lines changed

examples/.pcconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
storage:
22
stack_size: 18
3-
stack_base: 80
3+
stack_base: 50

examples/stack.ppc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
8 903 @ Push new value to stack
99
9 905 @ Get the stack pointer value
1010
10 902 @ Output the value
11-
11 906 @ Calculate the stack height
12-
12 902 @ Output the value
13-
13 000 @ Halt
11+
11 904 @ Pop the last value off the stack
12+
12 902 @ Print that value
13+
13 906 @ Calculate the stack height
14+
14 902 @ Output the value
15+
15 000 @ Halt

src/paperpc/cmd.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,15 +155,23 @@ def __push(self, acc, storage):
155155
if storage.stack_ptr - storage.stack_base > storage.stack_size:
156156
print("[ERROR] Stack overflow!")
157157
sys.exit(1)
158-
storage._spaces[storage.stack_ptr] = acc.value
158+
try:
159+
storage._spaces[storage.stack_ptr] = acc.value
160+
except IndexError:
161+
print(f"[ERROR] Stack pointer set beyond storage limit!")
162+
sys.exit(1)
159163

160164
@storage
161165
def __pop(self, acc, storage):
162166
stack_len = len(storage.stack)
163167
stack_pos = storage.stack_base + stack_len
164168
storage._spaces[storage.stack_ptr] = "---"
165-
acc.value = int(storage.stack.pop())
166-
storage.stack_ptr -= 1
169+
try:
170+
acc.value = int(storage.stack.pop())
171+
storage.stack_ptr -= 1
172+
except IndexError:
173+
print(f"[ERROR] Stack pointer set beyond storage limit!")
174+
sys.exit(1)
167175

168176
@storage
169177
def __ptr(self, acc, storage):

0 commit comments

Comments
 (0)