Skip to content
This repository has been archived by the owner on Jun 15, 2024. It is now read-only.

Commit

Permalink
feat: load symbol ord
Browse files Browse the repository at this point in the history
  • Loading branch information
FEgor04 committed Jun 1, 2024
1 parent f0b2735 commit 96be24b
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
12 changes: 12 additions & 0 deletions src/integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,15 @@ def test_sum(self):
except StopIteration:
pass
self.assertEqual(data_path.memory[0].arg, 10 + 5)

def test_mul_input(self):
lines = ["RESULT: VAR 0", "LD (2046)", "SUB '0'", "MUL 10", "ST RESULT", "HLT"]
instructions = parse_lines(lines)
data_path = DataPath("5", print, instructions)
control_unit = ControlUnit(1, data_path)
try:
for i in range(100):
control_unit.decode_and_execute()
except StopIteration:
pass
self.assertEqual(data_path.memory[0].arg, 50)
4 changes: 2 additions & 2 deletions src/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ def parse_labels(lines: list[str]) -> dict[str, int]:
return labels


def parse_argument(arg: str, labels: dict[str, int]) -> tuple[str | int, Addressing]:
def parse_argument(arg: str, labels: dict[str, int]) -> tuple[int, Addressing]:
if len(arg) == 0:
return None, None
addressing = parse_addressing(arg)
if addressing is Addressing.IMMEDIATE:
if arg[0] == "'" and arg[-1] == "'": # is literal
return arg[1:-1], addressing
return ord(arg[1]), addressing
if arg.isdecimal():
return int(arg), addressing
return labels[arg], addressing
Expand Down
4 changes: 2 additions & 2 deletions src/translator_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_translate_complex(self):
expected = [
Instruction(Opcode.LD, 2, Addressing.DIRECT),
Instruction(Opcode.JMP, 0, Addressing.DIRECT),
Instruction(Opcode.VAR, "a", Addressing.IMMEDIATE),
Instruction(Opcode.VAR, ord("a"), Addressing.IMMEDIATE),
]
self.assertEqual(transformed, expected)

Expand Down Expand Up @@ -71,7 +71,7 @@ def test_immediate(self):
lines = ["ADD 10", "LD 'a'"]
expected = [
Instruction(Opcode.ADD, 10, Addressing.IMMEDIATE),
Instruction(Opcode.LD, "a", Addressing.IMMEDIATE),
Instruction(Opcode.LD, ord("a"), Addressing.IMMEDIATE),
]
transformed = parse_lines(lines)
self.assertEqual(transformed, expected)

0 comments on commit 96be24b

Please sign in to comment.