git @ Cat's Eye Technologies Eightebed / 7689f99
Fully pep8-ify parser. catseye 10 years ago
1 changed file(s) with 58 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
346346 self.name = decl.name
347347
348348 def __repr__(self):
349 return "%s(%s, %s)" % (self.__class__.__name__, repr(self.name), repr(self.type))
349 return "%s(%r, %r)" % (self.__class__.__name__, self.name, self.type)
350350
351351 def typecheck(self, types, vars):
352352 self.type.typecheck(types, vars)
376376 self.block = data[2]
377377
378378 def __repr__(self):
379 return "%s(%s, %s)" % (self.__class__.__name__, repr(self.expr), repr(self.block))
379 return "%s(%r, %r)" % (self.__class__.__name__, self.expr, self.block)
380380
381381 def typecheck(self, types, vars):
382382 self.expr.typecheck(types, vars)
406406 self.else_ = Block(['{', [], '}'])
407407
408408 def __repr__(self):
409 return "%s(%s, %s, %s)" % (self.__class__.__name__, repr(self.expr), repr(self.then), repr(self.else_))
409 return "%s(%r, %r, %r)" % (self.__class__.__name__,
410 self.expr, self.then, self.else_)
410411
411412 def typecheck(self, types, vars):
412413 self.expr.typecheck(types, vars)
487488 self.expr = data[2]
488489
489490 def __repr__(self):
490 return "%s(%s, %s)" % (self.__class__.__name__, repr(self.ref), repr(self.expr))
491 return "%s(%r, %r)" % (self.__class__.__name__, self.ref, self.expr)
491492
492493 def typecheck(self, types, vars):
493494 tlhs = self.ref.typecheck(types, vars)
529530 self.rhs = data[3]
530531
531532 def __repr__(self):
532 return "%s(%s, %s, %s)" % (self.__class__.__name__, repr(self.lhs), repr(self.op), repr(self.rhs))
533 return "%s(%r, %r, %r)" % (self.__class__.__name__,
534 self.lhs, self.op, self.rhs)
533535
534536 def typecheck(self, types, vars):
535537 trhs = self.lhs.typecheck(types, vars)
599601 self.member_name = data[4]
600602
601603 def __repr__(self):
602 return "%s(%s, %s)" % (self.__class__.__name__, repr(self.source), repr(self.member_name))
604 return "%s(%r, %r)" % (self.__class__.__name__,
605 self.source, self.member_name)
603606
604607 def typecheck(self, types, vars):
605608 source_type = self.source.typecheck(types, vars)
688691 g['Eightebed'] = Sequence(Asteration(NonTerminal('TypeDecl')),
689692 Asteration(NonTerminal('VarDecl')),
690693 NonTerminal('Block')).construct(Eightebed)
691 g['Block'] = Sequence(Terminal('{'),
692 Asteration(NonTerminal('Stmt')),
693 Terminal('}')).construct(Block)
694 g['Block'] = Sequence(Terminal('{'),
695 Asteration(NonTerminal('Stmt')),
696 Terminal('}')).construct(Block)
694697 g['TypeDecl'] = Sequence(Terminal('type'),
695698 NonTerminal('TypeName'),
696699 NonTerminal('Type'),
697700 Terminal(';')).construct(TypeDecl)
698 g['Type'] = Alternation(Terminal('int').construct(TypeInt),
699 Sequence(Terminal('struct'), Terminal('{'), Asteration(NonTerminal('Decl')), Terminal('}')).construct(TypeStruct),
700 Sequence(Terminal('ptr'), Terminal('to'), NonTerminal('Type')).construct(TypePtr),
701 NonTerminal('TypeName').construct(TypeNamed))
702 g['Decl'] = Sequence(NonTerminal('Type'),
703 NonTerminal('VarName'),
704 Terminal(';')).construct(Decl)
705 g['VarDecl'] = Sequence(Terminal('var'), NonTerminal('Decl')).construct(VarDecl)
706 g['Stmt'] = Alternation(Sequence(Terminal('while'), NonTerminal('Expr'), NonTerminal('Block')).construct(WhileStmt),
707 Sequence(Terminal('if'), NonTerminal('Expr'), NonTerminal('Block'),
708 Optional(Sequence(Terminal('else'), NonTerminal('Block')))).construct(IfStmt),
709 Sequence(Terminal('free'), NonTerminal('Ref'), Terminal(';')).construct(FreeStmt),
710 Sequence(Terminal('print'), NonTerminal('Expr'), Terminal(';')).construct(PrintStmt),
711 Sequence(NonTerminal('Ref'), Terminal('='), NonTerminal('Expr'), Terminal(';')).construct(AssignStmt))
712 g['Ref'] = Alternation(Sequence(Terminal('['), NonTerminal('Ref'), Terminal(']'), Terminal('.'), NonTerminal('VarName')).construct(DottedRef),
713 Sequence(Terminal('@'), NonTerminal('Ref')).construct(DeRef),
714 NonTerminal('VarName').construct(VarRef))
715 g['Expr'] = Alternation(Sequence(Terminal('('), NonTerminal('Expr'), NonTerminal('BinOp'), NonTerminal('Expr'), Terminal(')')).construct(BinOpExpr),
716 Sequence(Terminal('malloc'), NonTerminal('Type')).construct(MallocExpr),
717 Sequence(Terminal('valid'), NonTerminal('Expr')).construct(ValidExpr),
718 NonTerminal('IntLit').construct(IntConst),
719 NonTerminal('Ref'))
720 g['BinOp'] = Alternation(Terminal('+'), Terminal('-'), Terminal('*'),
721 Terminal('/'), Terminal('='), Terminal('>'),
722 Terminal('&'), Terminal('|'))
701 g['Type'] = Alternation(Terminal('int').construct(TypeInt),
702 Sequence(Terminal('struct'), Terminal('{'),
703 Asteration(NonTerminal('Decl')),
704 Terminal('}')).construct(TypeStruct),
705 Sequence(Terminal('ptr'), Terminal('to'),
706 NonTerminal('Type')).construct(TypePtr),
707 NonTerminal('TypeName').construct(TypeNamed))
708 g['Decl'] = Sequence(NonTerminal('Type'),
709 NonTerminal('VarName'),
710 Terminal(';')).construct(Decl)
711 g['VarDecl'] = Sequence(Terminal('var'),
712 NonTerminal('Decl')).construct(VarDecl)
713 g['Stmt'] = Alternation(Sequence(Terminal('while'), NonTerminal('Expr'),
714 NonTerminal('Block')).construct(WhileStmt),
715 Sequence(Terminal('if'), NonTerminal('Expr'),
716 NonTerminal('Block'),
717 Optional(Sequence(Terminal('else'),
718 NonTerminal('Block')))
719 ).construct(IfStmt),
720 Sequence(Terminal('free'), NonTerminal('Ref'),
721 Terminal(';')).construct(FreeStmt),
722 Sequence(Terminal('print'), NonTerminal('Expr'),
723 Terminal(';')).construct(PrintStmt),
724 Sequence(NonTerminal('Ref'), Terminal('='),
725 NonTerminal('Expr'), Terminal(';')
726 ).construct(AssignStmt))
727 g['Ref'] = Alternation(Sequence(Terminal('['), NonTerminal('Ref'),
728 Terminal(']'), Terminal('.'),
729 NonTerminal('VarName')).construct(DottedRef),
730 Sequence(Terminal('@'),
731 NonTerminal('Ref')).construct(DeRef),
732 NonTerminal('VarName').construct(VarRef))
733 g['Expr'] = Alternation(Sequence(Terminal('('), NonTerminal('Expr'),
734 NonTerminal('BinOp'), NonTerminal('Expr'),
735 Terminal(')')).construct(BinOpExpr),
736 Sequence(Terminal('malloc'),
737 NonTerminal('Type')).construct(MallocExpr),
738 Sequence(Terminal('valid'),
739 NonTerminal('Expr')).construct(ValidExpr),
740 NonTerminal('IntLit').construct(IntConst),
741 NonTerminal('Ref'))
742 g['BinOp'] = Alternation(Terminal('+'), Terminal('-'), Terminal('*'),
743 Terminal('/'), Terminal('='), Terminal('>'),
744 Terminal('&'), Terminal('|'))
723745
724746 g['TypeName'] = Terminal(lambda x: re.match('^[a-zA-Z]\w*$', x))
725 g['VarName'] = Terminal(lambda x: re.match('^[a-zA-Z]\w*$', x))
726 g['IntLit'] = Terminal(lambda x: re.match('^\d+$', x))
747 g['VarName'] = Terminal(lambda x: re.match('^[a-zA-Z]\w*$', x))
748 g['IntLit'] = Terminal(lambda x: re.match('^\d+$', x))
727749
728750
729751 def parse(text):