summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorp4bl0 <r _at_ uzy .dot. me>2018-07-13 16:55:01 (CEST)
committerp4bl0 <r _at_ uzy .dot. me>2018-07-13 16:55:01 (CEST)
commit414f479af002d609189a5d6297c183d1f0bd1106 (patch)
tree62a3517295d32f7c5d31ca1606246971ecdcf444
parentd1ab62dd30456ab40d31e378c2cdbf4eaa2dcc46 (diff)
downloadseselab-414f479af002d609189a5d6297c183d1f0bd1106.zip
seselab-414f479af002d609189a5d6297c183d1f0bd1106.tar.gz
manage exceptions outside of main functionsHEADmaster
-rw-r--r--bench.py2
-rw-r--r--compiler.py39
-rw-r--r--cpu.py34
3 files changed, 39 insertions, 36 deletions
diff --git a/bench.py b/bench.py
index 353070f..5f47135 100644
--- a/bench.py
+++ b/bench.py
@@ -27,7 +27,7 @@ if sys.argv[1] == '-i':
signal.signal(signal.SIGINT, inject)
sys.argv.pop(1)
-program = Compiler().compile(sys.argv[1])
+program = Compiler().run(sys.argv[1])
cpu = CPU(1048576, 32, program, sys.argv[2])
cpu.run()
diff --git a/compiler.py b/compiler.py
index abf96a0..3addbe3 100644
--- a/compiler.py
+++ b/compiler.py
@@ -200,25 +200,28 @@ class Compiler:
inp.close()
def compile (self, path):
+ self._code.append([['jmp', ('lbl', 'main')], ['_', -1]])
+ self._count += 1
+ self.compile_file(os.path.abspath(path))
+ for instr in self._code:
+ if instr[0][0] in ('cal', 'jmp', 'beq', 'bne'):
+ if instr[0][1][0] == 'lbl':
+ lbl = instr[0][1][1]
+ if lbl in self._labels:
+ instr[0][1] = 'imm', self._labels[lbl][0]
+ instr[1].append(lbl)
+ else:
+ raise LabelNotFound(lbl, instr[1][0], instr[1][1])
+ for lbl in self._labels:
+ instr = self._code[self._labels[lbl][0]]
+ if len(instr[1]) == 2:
+ instr[1].append(None)
+ instr[1].append(lbl)
+ return self._code
+
+ def run (self, path):
try:
- self._code.append([['jmp', ('lbl', 'main')], ['_', -1]])
- self._count += 1
- self.compile_file(os.path.abspath(path))
- for instr in self._code:
- if instr[0][0] in ('cal', 'jmp', 'beq', 'bne'):
- if instr[0][1][0] == 'lbl':
- lbl = instr[0][1][1]
- if lbl in self._labels:
- instr[0][1] = 'imm', self._labels[lbl][0]
- instr[1].append(lbl)
- else:
- raise LabelNotFound(lbl, instr[1][0], instr[1][1])
- for lbl in self._labels:
- instr = self._code[self._labels[lbl][0]]
- if len(instr[1]) == 2:
- instr[1].append(None)
- instr[1].append(lbl)
- return self._code
+ return self.compile(path)
except ParseError as e:
print('Parse error: ' + e.err +
diff --git a/cpu.py b/cpu.py
index cec28ff..484e374 100644
--- a/cpu.py
+++ b/cpu.py
@@ -131,20 +131,24 @@ class CPU:
return None
def cycle (self):
- try:
- ip = self.execute_instruction(self._ip)
- if ip is not None:
- self._ip = ip
- else:
- self._ip += 1
- self._probe.read(self._ram.get_activity())
- self._probe.read(self._reg.get_activity())
- self._probe.output_activity()
- sys.stdout.flush()
+ ip = self.execute_instruction(self._ip)
+ if ip is not None:
+ self._ip = ip
+ else:
+ self._ip += 1
+ self._probe.read(self._ram.get_activity())
+ self._probe.read(self._reg.get_activity())
+ self._probe.output_activity()
+ sys.stdout.flush()
- if self._ip < 0 or self._ip >= self._max_ip:
- return False
- return True
+ if self._ip < 0 or self._ip >= self._max_ip:
+ return False
+ return True
+
+ def run (self):
+ try:
+ while self.cycle():
+ pass
except AddrError as e:
print('Invalid address ' + str(e.addr) +
@@ -157,7 +161,3 @@ class CPU:
' on line ' + str(self._code[self._ip][1][1]) +
' of file ' + self._code[self._ip][1][0],
file=sys.stderr)
-
- def run (self):
- while self.cycle():
- pass
Pablo Rauzy — generated by cgit