1
0
mirror of https://github.com/catseye/SixtyPical.git synced 2024-11-25 23:49:17 +00:00

Syntax is syntax, test it there. Register built-ins as symbols.

This commit is contained in:
Chris Pressey 2015-10-18 17:32:12 +01:00
parent e1cf162a5b
commit 41432b5cb3
3 changed files with 64 additions and 52 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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