Skip to content

Commit 07d1e9d

Browse files
committed
More inputs checking; SFT works correctly
1 parent 5608b6f commit 07d1e9d

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/lpc/cmd.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,19 @@ def __init__(self):
7575

7676
def parse(self, **kwargs):
7777
try:
78+
self._line = kwargs['line']
7879
self._arg = kwargs['arg'][0]
7980
self._val = int(kwargs['arg'][1:3])
8081
except KeyError:
8182
pass
83+
except:
84+
print(f"[ERROR] Invalid instruction at line {self._line}.")
8285
if self._arg == "9":
8386
self._arg = kwargs['arg']
8487
try:
8588
return self._syntax[self._arg]
8689
except:
90+
print(f"[ERROR] Invalid instruction at line {self._line}.")
8791
return
8892

8993
@accumulate
@@ -98,7 +102,7 @@ def __sub(self, acc, storage) -> int:
98102

99103
@storage
100104
def __sta(self, acc, storage):
101-
storage._spaces[self._val] = acc.value
105+
storage._spaces[self._val] = str(acc.value)[-3:]
102106

103107
@storage
104108
def __lda(self, acc, storage):
@@ -129,15 +133,17 @@ def __sft(self, acc, storage):
129133
# Left shift first
130134
if int(self._val[0]) > 0:
131135
shifts = int(self._val[0])
136+
tmp = str(acc.value)
132137
for _ in range(shifts):
133-
acc.value = f"{acc.value}0"
134-
acc.value = acc.value[-3:]
138+
tmp = f"{tmp}0"
139+
acc.value = tmp[-3:]
135140
# Right shift second
136141
if int(self._val[1]) > 0:
137142
shifts = int(self._val[1])
143+
tmp = str(acc.value)
138144
for _ in range(shifts):
139-
acc.value = f"0{acc.value}"
140-
acc.value = acc.value[0:3]
145+
tmp = f"0{tmp}"
146+
acc.value = tmp[0:3]
141147
acc.value = int(acc.value)
142148

143149
@inputs

src/lpc/main.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,27 @@ def main() -> None:
4848
# convert to a tuple if supplied with
4949
# comma-separated list
5050
inputs = Inputs(cliarg.optional.inputs)
51-
51+
len_inputs = len(inputs._values)
5252
# Step through instruction list, translate to
5353
# functions
5454
while True:
5555

5656
cmd = commands.parse(
57+
line = storage._counter,
5758
arg = storage.retrieve(storage._counter)
5859
)
5960

6061
arg_types = get_signature(Commands)[cmd.__name__]
6162

6263
if 'inputs' in arg_types:
63-
cmd(acc, storage, inputs._values.pop(0))
64+
try:
65+
cmd(acc, storage, inputs._values.pop(0))
66+
except IndexError:
67+
# This is the last case to consider
68+
print(f"[ERROR] Reached end of inputs.")
69+
print(f" Expected:\t{storage._expected_inputs}")
70+
print(f" Given:\t\t{len_inputs}")
71+
sys.exit(1)
6472
else:
6573
if cliarg.optional.debug:
6674
debug_log(acc, storage)

src/lpc/parts.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ def __init__(self, instructions):
1313
r"\s{2,}|\t{2,}",
1414
instruction
1515
) for instruction in instructions)
16+
self._expected_inputs = 0
1617
self.__initialize_storage()
1718

1819
def __initialize_storage(self):
@@ -24,6 +25,8 @@ def __initialize_storage(self):
2425
self._spaces.append(None)
2526
for instruction in self._program:
2627
self._spaces[int(instruction[0])] = instruction[1]
28+
if instruction[1] == "901":
29+
self._expected_inputs += 1
2730
try:
2831
comment = str(instruction[2])
2932
if not comment.startswith("@"):
@@ -39,6 +42,7 @@ def __initialize_storage(self):
3942

4043
def retrieve(self, addr):
4144
if self._spaces[addr] == None:
45+
print(f"[ERROR] Program ends abruptly at line {addr}. Did you meant to HALT?")
4246
sys.exit(1)
4347
return self._spaces[addr]
4448

0 commit comments

Comments
 (0)