summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorp4bl0 <r _at_ uzy .dot. me>2018-06-18 11:52:57 (CEST)
committerp4bl0 <r _at_ uzy .dot. me>2018-06-18 11:52:57 (CEST)
commit748bda05d251910613cfab23f80315ad27cc84f6 (patch)
treec9811b94f28c99f122ec9a4f87288044bbc822a2
parentcc019994f80ca282d1ca1f1ab2857e61cc22cd92 (diff)
downloadseselab-748bda05d251910613cfab23f80315ad27cc84f6.zip
seselab-748bda05d251910613cfab23f80315ad27cc84f6.tar.gz
better label error detection
-rw-r--r--compiler.py41
-rw-r--r--exn.py12
2 files changed, 38 insertions, 15 deletions
diff --git a/compiler.py b/compiler.py
index fbcc140..4c8843a 100644
--- a/compiler.py
+++ b/compiler.py
@@ -58,7 +58,11 @@ class Compiler:
if ':' in line:
line = line.split(':', 1)
- self._labels[line[0].strip()] = self._count
+ lbl = line[0].strip()
+ if lbl in self._labels:
+ raise DuplicateLabel(lbl)
+ else:
+ self._labels[lbl] = self._count, self._files[-1], self._ln[-1]
line = line[1]
line = line.split(';', 1)[0].strip()
@@ -175,23 +179,23 @@ class Compiler:
self.arit(i, 3)
return [i[0], self.addr(i[1]), self.val(i[2]), self.val(i[3])]
- self.err('opcode')
+ raise ParseError('opcode')
def compile_file (self, path):
inp = open(path, 'r')
self._files.append(os.path.basename(path))
- self._linum.append(1)
+ self._ln.append(1)
for l in inp:
if '.include' in l:
self.compile_file(l.split('.include', 1)[1].strip())
else:
a = self.instr(l)
if a is not None:
- self._code.append([a, (self._files[-1], self._linum[-1])])
+ self._code.append([a, (self._files[-1], self._ln[-1])])
self._count += 1
- self._linum[-1] += 1
+ self._ln[-1] += 1
self._files.pop()
- self._linum.pop()
+ self._ln.pop()
inp.close()
def compile (self, path):
@@ -204,15 +208,30 @@ class Compiler:
if instr[0][1][0] == 'lbl':
lbl = instr[0][1][1]
if lbl in self._labels:
- instr[0][1] = 'imm', self._labels[lbl]
+ instr[0][1] = 'imm', self._labels[lbl][0]
instr[1] = instr[1][0], instr[1][1], lbl
else:
- raise LabelError(lbl)
+ raise LabelNotFound(lbl, instr[1][0], instr[1][1])
return self._code
except ParseError as e:
- print('Parse error: ' + e.err + ' in ' + e.file + ' on line ' + str(e.line))
- except LabelError as e:
- print('Label not found: ' + e.lbl)
+ print('Parse error: ' + e.err +
+ ' in ' + self._files[-1] +
+ ' on line ' + str(self._ln[-1]),
+ file=sys.stderr)
+
+ except DuplicateLabel as e:
+ print('Duplicate label: ' + e.lbl +
+ ' in ' + self._files[-1] +
+ ' on line ' + str(self._ln[-1]) +
+ ' (first declared in ' + self._labels[e.lbl][1] +
+ ' on line ' + str(self._labels[e.lbl][2]) + ')',
+ file=sys.stderr)
+
+ except LabelNotFound as e:
+ print('Label not found: ' + e.lbl +
+ ' (called in ' + e.file +
+ ' on line ' + str(e.line) + ')',
+ file=sys.stderr)
sys.exit(1)
diff --git a/exn.py b/exn.py
index cefb983..963e1bb 100644
--- a/exn.py
+++ b/exn.py
@@ -1,13 +1,17 @@
class ParseError (Exception):
- def __init__ (self, err, file, line):
+ def __init__ (self, err):
self.err = err
- self.file = file
- self.line = line
-class LabelError (Exception):
+class DuplicateLabel (Exception):
def __init__ (self, lbl):
self.lbl = lbl
+class LabelNotFound (Exception):
+ def __init__ (self, lbl, file, line):
+ self.lbl = lbl
+ self.file = file
+ self.line = line
+
class AddrError (Exception):
def __init__ (self, addr):
self.addr = addr
Pablo Rauzy — generated by cgit