git @ Cat's Eye Technologies SixtyPical / 41432b5
Syntax is syntax, test it there. Register built-ins as symbols. Chris Pressey 6 years ago
3 changed file(s) with 64 addition(s) and 52 deletion(s). Raw diff Collapse all Expand all
8181 def __init__(self, text):
8282 self.scanner = Scanner(text)
8383 self.symbols = {} # token -> SymEntry
84 for token in ('a', 'x', 'y'):
85 self.symbols[token] = SymEntry(None, LocationRef(TYPE_BYTE, token))
86 for token in ('c', 'z', 'n', 'v'):
87 self.symbols[token] = SymEntry(None, LocationRef(TYPE_BIT, token))
8488
8589 def lookup(self, name):
90 if name not in self.symbols:
91 raise SyntaxError('Undefined symbol "%s"' % name)
8692 return self.symbols[name].model
8793
8894 def program(self):
9298 defn = self.defn()
9399 name = defn.name
94100 if name in self.symbols:
95 raise KeyError(name)
101 raise SyntaxError(name)
96102 self.symbols[name] = SymEntry(defn, LocationRef(TYPE_BYTE, name))
97103 defns.append(defn)
98104 while self.scanner.on('routine'):
99105 routine = self.routine()
100106 name = routine.name
101107 if name in self.symbols:
102 raise KeyError(name)
108 raise SyntaxError(name)
103109 self.symbols[name] = SymEntry(routine, None)
104110 routines.append(routine)
105111 self.scanner.check_type('EOF')
150156 return accum
151157
152158 def locexpr(self):
153 if self.scanner.token in ('a', 'x', 'y'):
154 loc = LocationRef(TYPE_BYTE, self.scanner.token)
155 self.scanner.scan()
156 return loc
157 elif self.scanner.token in ('c', 'z', 'n', 'v'):
158 loc = LocationRef(TYPE_BIT, self.scanner.token)
159 self.scanner.scan()
160 return loc
161 elif self.scanner.token in ('on', 'off'):
159 if self.scanner.token in ('on', 'off'):
162160 loc = ConstantRef(TYPE_BIT, 1 if self.scanner.token == 'on' else 0)
163161 self.scanner.scan()
164162 return loc
4444 = y: 0
4545 = z: 0
4646
47 Can't access an undeclared memory location.
48
49 | routine main {
50 | ld a, 0
51 | st a, lives
52 | }
53 ? KeyError
54
55 Can't define two memory locations with the same name.
56
57 | byte lives
58 | byte lives
59 |
60 | routine main {
61 | ld a, 0
62 | st a, lives
63 | }
64 ? KeyError
65
6647 Add honours carry.
6748
6849 | routine main {
292273 = x: 2
293274 = y: 3
294275 = z: 0
295
296 Can't call routine that hasn;t been defined.
297
298 | routine main {
299 | ld x, 0
300 | ld y, 1
301 | call up
302 | call up
303 | }
304 ? KeyError
305
306 Can't define two routines with the same name.
307
308 | routine main {
309 | inc x
310 | inc y
311 | }
312 | routine main {
313 | ld x, 0
314 | ld y, 1
315 | }
316 ? KeyError
317276
318277 If.
319278
110110 | st a, screen
111111 | }
112112 = ok
113
114 Can't access an undeclared memory location.
115
116 | routine main {
117 | ld a, 0
118 | st a, lives
119 | }
120 ? SyntaxError
121
122 Can't define two memory locations with the same name.
123
124 | byte lives
125 | byte lives
126 |
127 | routine main {
128 | ld a, 0
129 | st a, lives
130 | }
131 ? SyntaxError
132
133 Can't shadow the name of a register or a flag.
134
135 | byte a
136 |
137 | routine main {
138 | }
139 ? SyntaxError
140
141 | byte z
142 |
143 | routine main {
144 | }
145 ? SyntaxError
146
147 > Can't call routine that hasn;t been defined.
148 >
149 > | routine main {
150 > | ld x, 0
151 > | ld y, 1
152 > | call up
153 > | call up
154 > | }
155 > ? SyntaxError
156
157 Can't define two routines with the same name.
158
159 | routine main {
160 | inc x
161 | inc y
162 | }
163 | routine main {
164 | ld x, 0
165 | ld y, 1
166 | }
167 ? SyntaxError