git @ Cat's Eye Technologies Eightebed / aad57ca
Do some (but by no means all) pep8-ification. catseye 13 years ago
6 changed file(s) with 189 addition(s) and 51 deletion(s). Raw diff Collapse all Expand all
0 syntax: glob
1
2 *.pyc
3
4444 help="printf format to use for pointers in "
4545 "--trace-marking (default: %default)")
4646 optparser.add_option("-i", "--interactive",
47 action="store_true", dest="interactive", default=False,
47 action="store_true", dest="interactive",
48 default=False,
4849 help="enter interactive mode")
4950 optparser.add_option("-m", "--trace-marking",
50 action="store_true", dest="trace_marking", default=False,
51 action="store_true", dest="trace_marking",
52 default=False,
5153 help="trace marking actions in generated C source")
5254 optparser.add_option("-p", "--pedigree",
5355 dest="pedigree", default=__file__,
55
66 notset = object()
77 isset = object()
8
9
810 class Context(dict):
911 """
1012 >>> d = Context({ 'a': 2, 'b': 3 })
4749 return self[name]
4850 if self.parent is None:
4951 if default is notset:
50 raise KeyError, name
52 raise KeyError(name)
5153 return default
5254 return self.parent.lookup(name, default=default)
5355
5456 def declare(self, name, value):
5557 if self.lookup(name, default=isset) is not isset:
56 raise KeyError, "%s already declared" % name
58 raise KeyError("%s already declared" % name)
5759 self[name] = value
5860
5961 def empty(self):
5151 if options.verbose:
5252 sys.stdout.write(output)
5353 if output != '':
54 raise RuntimeError, "Compilation failed!"
54 raise RuntimeError("Compilation failed!")
5555 if options.run:
5656 logger.info("Running...")
5757 output = Popen(["./a.out"], stdout=PIPE).communicate()[0]
6363
6464 def load_and_go(ast, options=None):
6565 class LoadAndGoOptions(object):
66 verbose=False
67 run=True
68 clean=True
69 compiler="gcc"
70 pedigree=__file__ + ":load_and_go"
71 trace_marking=False
72 pointer_format="$%08lx"
66 verbose = False
67 run = True
68 clean = True
69 compiler = "gcc"
70 pedigree = __file__ + ":load_and_go"
71 trace_marking = False
72 pointer_format = "$%08lx"
7373 options = options or LoadAndGoOptions()
7474 file = open("tmp.c", "w")
7575 ast.emit(file, options)
8383 options.run = True
8484 options.clean = True
8585 while True:
86 sys.stdout.write("> ");
86 sys.stdout.write("> ")
8787 cmd = sys.stdin.readline().strip()
8888 if cmd == "quit":
8989 break
2525 self.typedecls = data[0]
2626 self.vardecls = data[1]
2727 self.block = data[2]
28 def __repr__(self):
29 return "%s(%s, %s, %s)" % (self.__class__.__name__, repr(self.typedecls), repr(self.vardecls), repr(self.block))
28
29 def __repr__(self):
30 return "%s(%s, %s, %s)" % (
31 self.__class__.__name__,
32 repr(self.typedecls), repr(self.vardecls), repr(self.block)
33 )
34
3035 def typecheck(self, types, vars):
3136 for typedecl in self.typedecls:
3237 typedecl.typecheck(types, vars)
3338 for vardecl in self.vardecls:
3439 vardecl.typecheck(types, vars)
3540 self.block.typecheck(types, vars)
41
3642 def vanalyze(self, context):
3743 self.block.vanalyze(context)
44
3845 def emit(self, stream, options):
3946 stream.write("""\
4047 /* Achtung! This Source was Automatically Generated by %s! */
110117 class Block(object):
111118 def __init__(self, data):
112119 self.stmts = data[1]
120
113121 def __repr__(self):
114122 return "%s(%s)" % (self.__class__.__name__, repr(self.stmts))
123
115124 def typecheck(self, types, vars):
116125 block_types = Context(parent=types)
117126 block_vars = Context(parent=vars)
118127 for stmt in self.stmts:
119128 stmt.typecheck(block_types, block_vars)
129
120130 def vanalyze(self, context):
121131 for stmt in self.stmts:
122132 stmt.vanalyze(context)
133
123134 def emit(self, stream):
124135 for stmt in self.stmts:
125136 stmt.emit(stream)
126137
138
127139 class TypeDecl(object):
128140 def __init__(self, data):
129141 self.name = data[1]
130142 self.type = data[2]
143
131144 def __repr__(self):
132145 return "TypeDecl(%s, %s)" % (repr(self.name), repr(self.type))
146
133147 def typecheck(self, types, vars):
134148 types.declare(self.name, self.type)
135149 self.type.typecheck(types, vars)
136150 if not isinstance(self.type, TypeStruct):
137 raise TypeError, "Only structs may be named"
151 raise TypeError("Only structs may be named")
138152 return self.type
153
139154 def emit(self, stream, options):
140155 if isinstance(self.type, TypeStruct):
141156 stream.write("typedef \n")
143158 stream.write(" %s;\n" % self.name)
144159 self.type.emit(stream)
145160 stream.write(";\n")
146 stream.write("static void mark_%s(_ptr outcast, %s* p) {" % (self.name, self.name))
147 marking_text = options.pointer_format + (" @%s " % self.name) + options.pointer_format
161 stream.write("static void mark_%s(_ptr outcast, %s* p) {" %
162 (self.name, self.name))
163 marking_text = (options.pointer_format + (" @%s " % self.name) +
164 options.pointer_format)
148165 stream.write(r"""
149166 #ifdef TRACE_MARKING
150167 fprintf(stderr, "-> BEGIN marking %s\n", (long)outcast.p, (long)p);
175192
176193 # These classes double as AST components and as type expressions.
177194
195
178196 class Type(object):
179197 def equiv(self, other):
180198 raise NotImplementedError
199
181200 def points_to(self):
182201 return None
202
183203 def resolve(self, types):
184204 return self
205
185206 def get_member_type(self, name):
186207 return None
208
187209
188210 class TypeVoid(Type):
189211 def __init__(self, data=None):
190212 pass
213
191214 def equiv(self, other):
192215 return isinstance(other, TypeVoid)
216
193217 def emit(self, stream):
194218 stream.write("void")
219
195220
196221 class TypeInt(Type):
197222 def __init__(self, data=None):
198223 pass
224
199225 def __repr__(self):
200226 return "%s()" % (self.__class__.__name__)
227
201228 def typecheck(self, types, vars):
202229 return self
230
203231 def equiv(self, other):
204232 return isinstance(other, TypeInt)
233
205234 def emit(self, stream):
206235 stream.write("int")
207236
237
208238 struct_id = 0
239
240
209241 class TypeStruct(Type):
210242 def __init__(self, data):
211243 global struct_id
212244 self.members = data[2]
213245 self.id = struct_id
214246 struct_id += 1
247
215248 def __repr__(self):
216249 return "%s(%s)" % (self.__class__.__name__, repr(self.members))
250
217251 def typecheck(self, types, vars):
218252 for member in self.members:
219253 type_ = member.typecheck(types, vars)
220254 if isinstance(type_, TypeStruct):
221 raise TypeError, "Structs may not contain other structs"
255 raise TypeError("Structs may not contain other structs")
222256 return self
257
223258 def equiv(self, other):
224259 return False
260
225261 def emit(self, stream):
226262 stream.write("struct s_%s {\n" % self.id)
227263 for member in self.members:
228264 member.emit(stream)
229265 stream.write("}")
266
230267 def emit_forward(self, stream):
231268 stream.write("struct s_%s" % self.id)
269
232270 def get_member_type(self, name):
233271 for decl in self.members:
234272 if decl.name == name:
235273 return decl.type
236274 return None
237275
276
238277 class TypePtr(Type):
239278 def __init__(self, data):
240279 self.target = data[2]
280
241281 def __repr__(self):
242282 return "%s(%s)" % (self.__class__.__name__, repr(self.target))
283
243284 def typecheck(self, types, vars):
244285 self.target.typecheck(types, vars)
245286 if isinstance(self.target, TypeNamed):
246287 return self
247288 else:
248 raise TypeError, "Pointer type must point to named type"
289 raise TypeError("Pointer type must point to named type")
290
249291 def equiv(self, other):
250292 return isinstance(other, TypePtr) and self.target.equiv(other.target)
293
251294 def points_to(self):
252295 return self.target
296
253297 def emit(self, stream):
254298 stream.write("/* ")
255299 self.target.emit(stream)
257301 stream.write(" */ ")
258302 stream.write("_ptr")
259303
304
260305 class TypeNamed(Type):
261306 def __init__(self, data):
262307 self.name = data
308
263309 def __repr__(self):
264310 return "%s(%s)" % (self.__class__.__name__, repr(self.name))
265 def typecheck(self, types, vars):
266 return True # can't look self up yet, might not exist yet
311
312 def typecheck(self, types, vars):
313 return True # can't look self up yet, might not exist yet
314
267315 def equiv(self, other):
268316 return isinstance(other, TypeNamed) and self.name == other.name
317
269318 def emit(self, stream):
270319 stream.write(self.name)
320
271321 def resolve(self, types):
272322 return types.lookup(self.name)
273323
324
274325 class Decl(object):
275326 def __init__(self, data):
276327 self.type = data[0]
277328 self.name = data[1]
278 def __repr__(self):
279 return "%s(%s, %s)" % (self.__class__.__name__, repr(self.name), repr(self.type))
329
330 def __repr__(self):
331 return "%s(%r, %r)" % (self.__class__.__name__, self.name, self.type)
332
280333 def typecheck(self, types, vars):
281334 self.type.typecheck(types, vars)
282335 return self.type
336
283337 def emit(self, stream):
284338 self.type.emit(stream)
285339 stream.write(" %s;\n" % self.name)
340
286341
287342 class VarDecl(object):
288343 def __init__(self, data):
289344 decl = data[1]
290345 self.type = decl.type
291346 self.name = decl.name
347
292348 def __repr__(self):
293349 return "%s(%s, %s)" % (self.__class__.__name__, repr(self.name), repr(self.type))
350
294351 def typecheck(self, types, vars):
295352 self.type.typecheck(types, vars)
296353 vars.declare(self.name, self.type)
297354 return self.type
355
298356 def emit(self, stream):
299357 self.type.emit(stream)
300358 stream.write(" %s;\n" % self.name)
359
301360 def emit_marker(self, stream):
302361 if isinstance(self.type, TypePtr):
303362 stream.write("""
310369 self.type.points_to().emit(stream)
311370 stream.write(" *)%s.p);\n }\n" % self.name)
312371
372
313373 class WhileStmt(object):
314374 def __init__(self, data):
315375 self.expr = data[1]
316376 self.block = data[2]
377
317378 def __repr__(self):
318379 return "%s(%s, %s)" % (self.__class__.__name__, repr(self.expr), repr(self.block))
380
319381 def typecheck(self, types, vars):
320382 self.expr.typecheck(types, vars)
321383 self.block.typecheck(types, vars)
322384 return TypeVoid()
385
323386 def vanalyze(self, context):
324387 self.expr.vanalyze(context)
325388 self.block.vanalyze(context)
389
326390 def emit(self, stream):
327391 stream.write("while(")
328392 self.expr.emit(stream)
329393 stream.write(") {\n")
330394 self.block.emit(stream)
331395 stream.write("}\n")
396
332397
333398 class IfStmt(object):
334399 def __init__(self, data):
339404 self.else_ = elsepart[0][1]
340405 else:
341406 self.else_ = Block(['{', [], '}'])
407
342408 def __repr__(self):
343409 return "%s(%s, %s, %s)" % (self.__class__.__name__, repr(self.expr), repr(self.then), repr(self.else_))
410
344411 def typecheck(self, types, vars):
345412 self.expr.typecheck(types, vars)
346413 self.then.typecheck(types, vars)
347414 self.else_.typecheck(types, vars)
348415 return TypeVoid()
416
349417 def vanalyze(self, context):
350418 self.expr.vanalyze(context)
351419 # If the test expr is exactly "valid x", put x into context,
356424 subcontext[self.expr.expr.name] = True
357425 self.then.vanalyze(subcontext)
358426 self.else_.vanalyze(context)
427
359428 def emit(self, stream):
360429 stream.write("if(")
361430 self.expr.emit(stream)
365434 self.else_.emit(stream)
366435 stream.write("}\n")
367436
437
368438 class FreeStmt(object):
369439 def __init__(self, data):
370440 self.ref = data[1]
441
371442 def __repr__(self):
372443 return "%s(%s)" % (self.__class__.__name__, repr(self.ref))
444
373445 def typecheck(self, types, vars):
374446 ref_type = self.ref.typecheck(types, vars)
375447 if ref_type.points_to() is None:
376 raise TypeError, "%s is not a pointer type" % repr(ref_type)
448 raise TypeError("%r is not a pointer type" % ref_type)
377449 return TypeVoid()
450
378451 def vanalyze(self, context):
379452 self.ref.vanalyze(context)
380453 # End safe area -- remove all assertions of validity hereafter.
381454 context.empty()
455
382456 def emit(self, stream):
383457 stream.write("_8ebed_free(&")
384458 self.ref.emit(stream)
385459 stream.write(");\n")
386460
461
387462 class PrintStmt(object):
388463 def __init__(self, data):
389464 self.expr = data[1]
465
390466 def __repr__(self):
391467 return "%s(%s)" % (self.__class__.__name__, repr(self.expr))
468
392469 def typecheck(self, types, vars):
393470 expr_type = self.expr.typecheck(types, vars)
394471 if not expr_type.equiv(TypeInt()):
395 raise TypeError, "%s is not an int" % repr(expr_type)
472 raise TypeError("%r is not an int" % expr_type)
396473 return TypeVoid()
474
397475 def vanalyze(self, context):
398476 self.expr.vanalyze(context)
477
399478 def emit(self, stream):
400479 stream.write("printf(\"%d \", ")
401480 self.expr.emit(stream)
402481 stream.write(");\n")
403482
483
404484 class AssignStmt(object):
405485 def __init__(self, data):
406486 self.ref = data[0]
407487 self.expr = data[2]
488
408489 def __repr__(self):
409490 return "%s(%s, %s)" % (self.__class__.__name__, repr(self.ref), repr(self.expr))
491
410492 def typecheck(self, types, vars):
411493 tlhs = self.ref.typecheck(types, vars)
412494 trhs = self.expr.typecheck(types, vars)
413495 if trhs.equiv(tlhs):
414496 return TypeVoid()
415497 else:
416 raise TypeError, "%s (%s) not equivalent to %s (%s) for vars %s" % (repr(tlhs), repr(self.ref), repr(trhs), repr(self.expr), vars)
498 raise TypeError("%r (%r) not equivalent to %r (%r) for vars %s" %
499 (tlhs, self.ref, trhs, self.expr, vars))
500
417501 def vanalyze(self, context):
418502 self.ref.vanalyze(context)
419503 self.expr.vanalyze(context)
420504 # End safe area -- remove all assertions of validity hereafter.
421505 context.empty()
506
422507 def emit(self, stream):
423508 self.ref.emit(stream)
424509 stream.write(" = ")
425510 self.expr.emit(stream)
426511 stream.write(";\n")
427512
513
428514 class BinOpExpr(object):
429515 map = {
430 '+' : '+',
431 '-' : '-',
432 '*' : '*',
433 '/' : '/',
434 '=' : '==',
435 '>' : '>',
436 '&' : '&&',
437 '|' : '||',
516 '+': '+',
517 '-': '-',
518 '*': '*',
519 '/': '/',
520 '=': '==',
521 '>': '>',
522 '&': '&&',
523 '|': '||',
438524 }
525
439526 def __init__(self, data):
440527 self.lhs = data[1]
441528 self.op = data[2]
442529 self.rhs = data[3]
530
443531 def __repr__(self):
444532 return "%s(%s, %s, %s)" % (self.__class__.__name__, repr(self.lhs), repr(self.op), repr(self.rhs))
533
445534 def typecheck(self, types, vars):
446535 trhs = self.lhs.typecheck(types, vars)
447536 tlhs = self.rhs.typecheck(types, vars)
448537 if not tlhs.equiv(TypeInt()):
449 raise TypeError, "lhs %s is not an int" % repr(tlhs)
538 raise TypeError("lhs %r is not an int" % tlhs)
450539 if not trhs.equiv(TypeInt()):
451 raise TypeError, "rhs %s is not an int" % repr(trhs)
540 raise TypeError("rhs %r is not an int" % trhs)
452541 return TypeInt()
542
453543 def vanalyze(self, context):
454544 self.lhs.vanalyze(context)
455545 self.rhs.vanalyze(context)
546
456547 def emit(self, stream):
457548 stream.write("(")
458549 self.lhs.emit(stream)
460551 self.rhs.emit(stream)
461552 stream.write(")")
462553
554
463555 class MallocExpr(object):
464556 def __init__(self, data):
465557 self.type = data[1]
558
466559 def __repr__(self):
467560 return "%s(%s)" % (self.__class__.__name__, repr(self.type))
561
468562 def typecheck(self, types, vars):
469563 return TypePtr(['', '', self.type])
564
470565 def vanalyze(self, context):
471566 pass
567
472568 def emit(self, stream):
473569 stream.write("_8ebed_malloc(sizeof(")
474570 self.type.emit(stream)
475571 stream.write("))")
476572
573
477574 class ValidExpr(object):
478575 def __init__(self, data):
479576 self.expr = data[1]
577
480578 def __repr__(self):
481579 return "%s(%s)" % (self.__class__.__name__, repr(self.expr))
580
482581 def typecheck(self, types, vars):
483582 expr_type = self.expr.typecheck(types, vars)
484583 if expr_type.points_to() is None:
485 raise TypeError, "%s is not a pointer type" % repr(expr_type)
584 raise TypeError("%r is not a pointer type" % expr_type)
486585 return TypeInt()
586
487587 def vanalyze(self, context):
488588 self.expr.vanalyze(context)
589
489590 def emit(self, stream):
490591 stream.write("_8ebed_valid(")
491592 self.expr.emit(stream)
492593 stream.write(")")
493594
595
494596 class DottedRef(object):
495597 def __init__(self, data):
496598 self.source = data[1]
497599 self.member_name = data[4]
600
498601 def __repr__(self):
499602 return "%s(%s, %s)" % (self.__class__.__name__, repr(self.source), repr(self.member_name))
603
500604 def typecheck(self, types, vars):
501605 source_type = self.source.typecheck(types, vars)
502606 source_type = source_type.resolve(types)
503607 member_type = source_type.get_member_type(self.member_name)
504608 if member_type is None:
505 raise TypeError, "%s does not have member %s" % (repr(source_type), self.member_name)
609 raise TypeError("%r does not have member %s" %
610 (source_type, self.member_name))
506611 return member_type
612
507613 def vanalyze(self, context, deref=False):
508614 self.source.vanalyze(context, deref=deref)
615
509616 def emit(self, stream):
510617 self.source.emit(stream)
511618 stream.write(".%s" % self.member_name)
512619
620
513621 class DeRef(object):
514622 def __init__(self, data):
515623 self.source = data[1]
516624 self._dest_type = None
625
517626 def __repr__(self):
518627 return "%s(%s)" % (self.__class__.__name__, repr(self.source))
628
519629 def typecheck(self, types, vars):
520630 source_type = self.source.typecheck(types, vars)
521631 dest_type = source_type.points_to()
522632 if dest_type is None:
523 raise TypeError, "%s is not a pointer type" % repr(source_type)
633 raise TypeError("%r is not a pointer type" % source_type)
524634 self._dest_type = dest_type
525635 return dest_type
636
526637 def vanalyze(self, context, deref=False):
527638 self.source.vanalyze(context, deref=True)
639
528640 def emit(self, stream):
529641 stream.write("(*(")
530642 self._dest_type.emit(stream)
532644 self.source.emit(stream)
533645 stream.write(".p)")
534646
647
535648 class VarRef(object):
536649 def __init__(self, data):
537650 self.name = data
651
538652 def __repr__(self):
539653 return "%s(%s)" % (self.__class__.__name__, repr(self.name))
654
540655 def typecheck(self, types, vars):
541656 #if self.name == 'i':
542657 # raise NotImplementedError, vars.lookup(self.name)
543658 return vars.lookup(self.name)
659
544660 def vanalyze(self, context, deref=False):
545661 if deref:
546662 if not context.lookup(self.name, default=False):
547 raise TypeError, "Attempt to dereference %s in non-safe context" % self.name
663 raise TypeError("Attempt to dereference %s "
664 "in non-safe context" % self.name)
665
548666 def emit(self, stream):
549667 stream.write(self.name)
550668
669
551670 class IntConst(object):
552671 def __init__(self, data):
553672 self.value = int(data)
673
554674 def __repr__(self):
555675 return "%s(%s)" % (self.__class__.__name__, repr(self.value))
676
556677 def typecheck(self, types, vars):
557678 return TypeInt()
679
558680 def vanalyze(self, context, deref=False):
559681 pass
682
560683 def emit(self, stream):
561684 stream.write(str(self.value))
562685
594717 Sequence(Terminal('valid'), NonTerminal('Expr')).construct(ValidExpr),
595718 NonTerminal('IntLit').construct(IntConst),
596719 NonTerminal('Ref'))
597 g['BinOp'] = Alternation(Terminal('+'),Terminal('-'),Terminal('*'),Terminal('/'),Terminal('='),Terminal('>'),Terminal('&'), Terminal('|'))
720 g['BinOp'] = Alternation(Terminal('+'), Terminal('-'), Terminal('*'),
721 Terminal('/'), Terminal('='), Terminal('>'),
722 Terminal('&'), Terminal('|'))
598723
599724 g['TypeName'] = Terminal(lambda x: re.match('^[a-zA-Z]\w*$', x))
600725 g['VarName'] = Terminal(lambda x: re.match('^[a-zA-Z]\w*$', x))
610735 s = Stream(r(text))
611736 return g.parse('Eightebed', s)
612737
738
613739 def parse_file(filename):
614740 f = open(filename, "r")
615741 contents = f.read()
55
66 import re
77 import types
8
89
910 class Stream(object):
1011 """
6061
6162 class RegLexer(object):
6263 """
63 An iterator which, given a string, returns a generator which returns sucessive
64 prefixes of the string based on supplied regexes.
64 An iterator which, given a string, returns a generator which returns
65 sucessive prefixes of the string based on supplied regexes.
6566
6667 >>> t = RegLexer()
6768 >>> t.register(r'(\d+)', meta='integer')
9293 has_match = True
9394 while has_match:
9495 has_match = False
95
96
9697 has_ignore = True
9798 while has_ignore:
9899 has_ignore = False
167168 if x(other):
168169 return True
169170 return False
170
171
171172 def __iter__(self):
172173 for x in self._set:
173174 yield x
416417
417418 def _production(self, grammar):
418419 if not grammar:
419 raise TypeError, "need grammar to use NonTerminal"
420 raise TypeError("need grammar to use NonTerminal")
420421 return grammar[self.name]
421422
422423 def parse(self, stream, grammar=None):
434435 """Container for a set of named productions.
435436
436437 >>> g = Grammar()
437 >>> g['Expr'] = Sequence(Terminal('('),Asteration(Terminal('*')),Terminal(')'))
438 >>> g['Expr'] = Sequence(Terminal('('),Asteration(Terminal('*')),
439 ... Terminal(')'))
438440 >>> g.parse('Expr', Stream(['(','*','*',')']))
439441 ['(', ['*', '*'], ')']
440 >>> g['Expr'] = Sequence(Terminal('('),Asteration(NonTerminal('Expr')),Terminal(')'))
442 >>> g['Expr'] = Sequence(Terminal('('),Asteration(NonTerminal('Expr')),
443 ... Terminal(')'))
441444 >>> g.parse('Expr', Stream(['(',')']))
442445 ['(', [], ')']
443446 >>> s = Stream(['(','(',')',')'])
448451 >>> g.parse('Expr', Stream(['(','(',')'])) is None
449452 True
450453 """
451
454
452455 trace = False
453456
454457 def __init__(self, parent=None):
463466 elif self.parent:
464467 return self.parent[key]
465468 else:
466 raise KeyError, "No production '%s' in grammar, and no parent grammar" % key
469 raise KeyError("No production '%s' in grammar, "
470 "and no parent grammar" % key)
467471
468472 def __setitem__(self, key, value):
469473 self.productions[key] = value