Source

BrainMachine / OutCppState.py

Full commit
#!/usr/bin/env python
# by Bookaa http://bitbucket.org/bookaa/brainmachine

import NameSpace
import VarDefine
import RunState

class OutCppState():
    def __init__(self, rs):
        self.rs = rs # weak ref
    def OneOp(self, op, result):
        import OpcodeDef
        if op.__class__ is OpcodeDef.Opcode_ClassDef:
            return # do nothing on class define
        if op.__class__ is OpcodeDef.Opcode_FuncDef:
            return
        if op.__class__ is OpcodeDef.Opcode_AopB:
            if op.verb == '=':
                if self.ns.__class__ is NameSpace.FuncNS and self.ns.name == '__init__':
                    if op.vleft.__class__ is VarDefine.CombineToken and op.vleft.op == '.':
                        theself = op.vleft.vleft
                        if theself.__class__ is VarDefine.NameToken and theself.name == 'self':
                            thename = op.vleft.vright
                            if thename.__class__ is VarDefine.NameToken:
                                name = thename.name
                                return
                            pass
                    pass
        assert False

    def Output(self):
        if self.rs.__class__ is RunState.FuncInstance:
            return

        if self.rs.__class__ is RunState.ClassInstance:
            for s in self.OutClass():
                print s
        pass
    def OutClass(self):
        yield 'class ' + self.rs.classns.name
        yield '{'
        yield '}'