diff --git a/src/sixtypical/parser.py b/src/sixtypical/parser.py index 7865482..063b227 100644 --- a/src/sixtypical/parser.py +++ b/src/sixtypical/parser.py @@ -82,8 +82,14 @@ class Parser(object): def __init__(self, text): self.scanner = Scanner(text) self.symbols = {} # token -> SymEntry + for token in ('a', 'x', 'y'): + self.symbols[token] = SymEntry(None, LocationRef(TYPE_BYTE, token)) + for token in ('c', 'z', 'n', 'v'): + self.symbols[token] = SymEntry(None, LocationRef(TYPE_BIT, token)) def lookup(self, name): + if name not in self.symbols: + raise SyntaxError('Undefined symbol "%s"' % name) return self.symbols[name].model def program(self): @@ -93,14 +99,14 @@ class Parser(object): defn = self.defn() name = defn.name if name in self.symbols: - raise KeyError(name) + raise SyntaxError(name) self.symbols[name] = SymEntry(defn, LocationRef(TYPE_BYTE, name)) defns.append(defn) while self.scanner.on('routine'): routine = self.routine() name = routine.name if name in self.symbols: - raise KeyError(name) + raise SyntaxError(name) self.symbols[name] = SymEntry(routine, None) routines.append(routine) self.scanner.check_type('EOF') @@ -151,15 +157,7 @@ class Parser(object): return accum def locexpr(self): - if self.scanner.token in ('a', 'x', 'y'): - loc = LocationRef(TYPE_BYTE, self.scanner.token) - self.scanner.scan() - return loc - elif self.scanner.token in ('c', 'z', 'n', 'v'): - loc = LocationRef(TYPE_BIT, self.scanner.token) - self.scanner.scan() - return loc - elif self.scanner.token in ('on', 'off'): + if self.scanner.token in ('on', 'off'): loc = ConstantRef(TYPE_BIT, 1 if self.scanner.token == 'on' else 0) self.scanner.scan() return loc diff --git a/tests/SixtyPical Execution.md b/tests/SixtyPical Execution.md index 6eab66f..a9d8bf8 100644 --- a/tests/SixtyPical Execution.md +++ b/tests/SixtyPical Execution.md @@ -45,25 +45,6 @@ Program accesses a memory location. = y: 0 = z: 0 -Can't access an undeclared memory location. - - | routine main { - | ld a, 0 - | st a, lives - | } - ? KeyError - -Can't define two memory locations with the same name. - - | byte lives - | byte lives - | - | routine main { - | ld a, 0 - | st a, lives - | } - ? KeyError - Add honours carry. | routine main { @@ -294,28 +275,6 @@ Call routine. = y: 3 = z: 0 -Can't call routine that hasn;t been defined. - - | routine main { - | ld x, 0 - | ld y, 1 - | call up - | call up - | } - ? KeyError - -Can't define two routines with the same name. - - | routine main { - | inc x - | inc y - | } - | routine main { - | ld x, 0 - | ld y, 1 - | } - ? KeyError - If. | routine main { diff --git a/tests/SixtyPical Syntax.md b/tests/SixtyPical Syntax.md index 00713f5..d1df10d 100644 --- a/tests/SixtyPical Syntax.md +++ b/tests/SixtyPical Syntax.md @@ -111,3 +111,58 @@ Extern memory locations | st a, screen | } = ok + +Can't access an undeclared memory location. + + | routine main { + | ld a, 0 + | st a, lives + | } + ? SyntaxError + +Can't define two memory locations with the same name. + + | byte lives + | byte lives + | + | routine main { + | ld a, 0 + | st a, lives + | } + ? SyntaxError + +Can't shadow the name of a register or a flag. + + | byte a + | + | routine main { + | } + ? SyntaxError + + | byte z + | + | routine main { + | } + ? SyntaxError + +> Can't call routine that hasn;t been defined. +> +> | routine main { +> | ld x, 0 +> | ld y, 1 +> | call up +> | call up +> | } +> ? SyntaxError + +Can't define two routines with the same name. + + | routine main { + | inc x + | inc y + | } + | routine main { + | ld x, 0 + | ld y, 1 + | } + ? SyntaxError