Fixed Bug, Added cli args
This commit is contained in:
36
examples/insertion_sort.ram
Normal file
36
examples/insertion_sort.ram
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
R[1] := 5 + 0 # Eingabelänge
|
||||||
|
|
||||||
|
R[2] := 3 # erstes Element der Eingabe
|
||||||
|
R[3] := 6
|
||||||
|
R[4] := 1
|
||||||
|
R[5] := 10
|
||||||
|
R[6] := 5 # letzes Element der Eingabe
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
R[0] := R[1] + 2
|
||||||
|
R[R[0]] := R[2] + 0
|
||||||
|
R[0] := R[0] + 1
|
||||||
|
R[R[0]] := R[3] + 0
|
||||||
|
R[0] := R[0] + 1
|
||||||
|
R[R[0]] := R[4] + 0
|
||||||
|
R[0] := R[0] + 1
|
||||||
|
R[R[0]] := R[5] + 0
|
||||||
|
R[2] := 2 + 0
|
||||||
|
if R[2] > R[1] goto 35
|
||||||
|
R[0] := R[2] + 5
|
||||||
|
R[4] := R[R[0]] + 0
|
||||||
|
R[3] := R[2] - 1
|
||||||
|
if R[3] <= 0 goto 31
|
||||||
|
R[0] := R[3] + 5
|
||||||
|
if R[4] >= R[R[0]] goto 31
|
||||||
|
R[5] := R[3] + 6
|
||||||
|
R[R[5]] := R[R[0]] + 0
|
||||||
|
R[3] := R[3] - 1
|
||||||
|
if 0 == 0 goto 24
|
||||||
|
R[5] := R[3] + 6
|
||||||
|
R[R[5]] := R[4] + 0
|
||||||
|
R[2] := R[2] + 1
|
||||||
|
if 0 == 0 goto 20
|
||||||
|
|
||||||
|
R[out] := 1
|
||||||
36
src/RAM.py
36
src/RAM.py
@@ -1,5 +1,6 @@
|
|||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
class Expression:
|
class Expression:
|
||||||
@@ -17,12 +18,12 @@ class Expression:
|
|||||||
p = r"R\[(?:(?P<rd>\d+)|R\[(?P<rrd>\d+)])\]"
|
p = r"R\[(?:(?P<rd>\d+)|R\[(?P<rrd>\d+)])\]"
|
||||||
m = re.match(p, exp)
|
m = re.match(p, exp)
|
||||||
|
|
||||||
if not m: raise Exception("no valid expression")
|
if not m: raise Exception(f"no valid expression in line {mem['pc']}")
|
||||||
rd, rrd = m.group("rd", "rrd")
|
rd, rrd = m.group("rd", "rrd")
|
||||||
|
|
||||||
if rd: return mem[int(rd)]
|
if rd: return mem[int(rd)]
|
||||||
elif rrd: return mem[mem[int(rrd)]]
|
elif rrd: return mem[mem[int(rrd)]]
|
||||||
else: raise Exception("no valid expression")
|
else: raise Exception(f"no valid expression in line {mem['pc']}")
|
||||||
|
|
||||||
def eval(self, exp: str, mem: dict) -> int:
|
def eval(self, exp: str, mem: dict) -> int:
|
||||||
m = re.match(self.named_bi_exp, exp)
|
m = re.match(self.named_bi_exp, exp)
|
||||||
@@ -41,7 +42,8 @@ class Expression:
|
|||||||
if op == "+": return vl + vr
|
if op == "+": return vl + vr
|
||||||
if op == "*": return vl * vr
|
if op == "*": return vl * vr
|
||||||
if op == "div": return vl // vr
|
if op == "div": return vl // vr
|
||||||
raise Exception("no valid expression")
|
|
||||||
|
raise Exception(f"no valid expression in line {mem['pc']}")
|
||||||
else:
|
else:
|
||||||
return self._eval_unary(exp, mem)
|
return self._eval_unary(exp, mem)
|
||||||
|
|
||||||
@@ -57,7 +59,7 @@ class Assignment:
|
|||||||
|
|
||||||
def exec(self, exp: str, mem: dict) -> dict:
|
def exec(self, exp: str, mem: dict) -> dict:
|
||||||
m = re.match(self.pattern, exp)
|
m = re.match(self.pattern, exp)
|
||||||
if not m: raise Exception("not a expression")
|
if not m: raise Exception(f"not a expression in line {mem['pc']}")
|
||||||
e, write_addr = m.group("e", "write_addr")
|
e, write_addr = m.group("e", "write_addr")
|
||||||
|
|
||||||
# Look up Register
|
# Look up Register
|
||||||
@@ -84,7 +86,7 @@ class Condition:
|
|||||||
|
|
||||||
def exec(self, exp: str, mem: dict) -> dict:
|
def exec(self, exp: str, mem: dict) -> dict:
|
||||||
m = re.match(self.pattern, exp)
|
m = re.match(self.pattern, exp)
|
||||||
if not m: raise Exception("not a expression")
|
if not m: raise Exception(f"not a expression in line {mem['pc']}")
|
||||||
e, line = m.group("e", "line")
|
e, line = m.group("e", "line")
|
||||||
|
|
||||||
# Evaluate the expression in the if statement
|
# Evaluate the expression in the if statement
|
||||||
@@ -101,13 +103,29 @@ class RAM:
|
|||||||
def __init__(self, path: str):
|
def __init__(self, path: str):
|
||||||
self.load_file(Path(path))
|
self.load_file(Path(path))
|
||||||
|
|
||||||
print(self.instructions)
|
|
||||||
|
|
||||||
self.mem = {}
|
self.mem = {}
|
||||||
self.mem["pc"] = 0
|
self.mem["pc"] = 0
|
||||||
print(self.run())
|
print(self.run())
|
||||||
|
|
||||||
def load_file(self, path: Path) -> dict:
|
self.instruction_dump()
|
||||||
|
self.mem_dump()
|
||||||
|
|
||||||
|
def instruction_dump(self):
|
||||||
|
print("╭" + "─"*34 + "╮")
|
||||||
|
for key in sorted(self.instructions.keys(), key=lambda x: f"{x:5}"):
|
||||||
|
print(f"│ {str(key):8}{str(self.instructions[key]):24}│")
|
||||||
|
print("╰" + "─"*34 + "╯")
|
||||||
|
|
||||||
|
def mem_dump(self):
|
||||||
|
print("Memory")
|
||||||
|
print("╭" + "─"*22 + "╮")
|
||||||
|
print(f"│ {'address':10}{'val':10}│")
|
||||||
|
print("├" + "─"*22 + "┤")
|
||||||
|
for key in sorted(self.mem.keys(), key=lambda x: f"{x:5}"):
|
||||||
|
print(f"│ {str(key):10}{str(self.mem[key]):10}│")
|
||||||
|
print("╰" + "─"*22 + "╯")
|
||||||
|
|
||||||
|
def load_file(self, path: Path):
|
||||||
with open(path, "r") as f:
|
with open(path, "r") as f:
|
||||||
raw_src_text = "".join(f.readlines())
|
raw_src_text = "".join(f.readlines())
|
||||||
|
|
||||||
@@ -144,4 +162,4 @@ class RAM:
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
RAM("../examples/test.ram")
|
RAM(sys.argv[1])
|
||||||
|
|||||||
Reference in New Issue
Block a user