Source

BrainMachine / OutCppState.py

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

import NameSpace
import VarDefine
import RunState

g_1 = False
def Output_DynamicSupport(f):
    global g_1
    if g_1:
        return
    g_1 = True
    f.write('''
class DynamicSupport
{
public:
};
''')

class CppVar():
    def __init__(self, name):
        self.name = name
class CppOp():
    def __init__(self, cppvar, value):
        self.cppvar = cppvar
        self.value = value

class OutCppState():
    def __init__(self, rs):
        self.rs = rs # weak ref
        self.vlist = []
        self.oplist = []
    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):
        f = open('c:/Temp/1.cpp', 'a')
        f.write('// 1.cpp \n')
        f.write('// gcc -c -I d:\python27\include 1.cpp\n')
        f.write('#include <python.h>\n')
        Output_DynamicSupport(f)
        try:
            if self.rs.__class__ is RunState.FuncInstance:
                return

            if self.rs.__class__ is RunState.ClassInstance:
                for s in self.OutClass():
                    f.write(s + '\n')
        finally:
            f.close()
    def OutClass(self):
        yield 'class ' + self.rs.classns.name
        yield '{'
        yield '    DynamicSupport m_ds;'
        yield 'public:'
        yield '};'
    def AddOp_if(self):
        pass
    def Enter_iftrue(self):
        pass
    def Leave(self):
        pass
    def Assign(self, var, value):
        assert isinstance(value, VarDefine.Value) # value.__class__ is VarDefine.Value
        if var.__class__ is VarDefine.NameToken:
            self.SetVar(var.name, value)
            return
        if var.__class__ is VarDefine.CombineToken:
            return
        assert False
    def SetVar(self, varname, value):
        cppvar = self.SearchVar(varname)
        if not cppvar:
            cppvar = self.NewVar(varname)
        self.AssignVar(cppvar, value)
    def AssignVar(self, cppvar, value):
        op = CppOp(cppvar, value)
        self.oplist.append(op)
        pass
    def SearchVar(self, name):
        return None
    def NewVar(self, name):
        v = CppVar(name)
        self.vlist.append(v)
        return v