Source

BrainMachine / NameSpace.py

Full commit
#!/usr/bin/env python

import string
import OpcodeDef
import RunState
import VarDefine
import NameSpace

class BaseNameSpace():
    def __init__(self):
        self.tem_s = ''
        self.sIden = ''
        self.oplist = []
        self.vars = []

    def tem_char_in(self, c):
        self.tem_s += c

    def Output(self):
        for op in self.oplist:
            for s in op.Output():
                yield s

    def OutputCpp(self):
        yield '// head file -------------'
        for s in self.OutputCpp_H():
            yield s
        yield '//-------------'
        for s in self.OutputCpp_Cpp():
            yield s

    def OutputCpp_H(self):
        assert False
        runstate = RunState.RunState(self, None)

        value = VarDefine.Value('string', 'UserCpp')
        var = VarDefine.NameToken('__name__')
        runstate.SetVarValue(var, value)

        self.Run(runstate)

        for s in runstate.OutputCpp_H():
            yield '    ' + s
        pass

    def OutputCpp_Cpp(self):
        yield '// cpp file -------------'

    def Run(self, rs):
        for op in self.oplist:
            result = op.Run(rs)
            if result:
                assert type(result) == tuple
                assert len(result) == 2
                if result[0] == 1:
                    return result
                assert False

    def RegisterVar(self, v):
        self.vars.append(v)

class ModuleNS(BaseNameSpace):
    def __init__(self, name):
        BaseNameSpace.__init__(self)
        self.name = name
    def OutputCpp_H(self):
        runstate = RunState.RunState(self, None)

        value = VarDefine.Value('string', 'UserCpp')
        var = VarDefine.NameToken('__name__')
        runstate.SetVarValue(var, value)

        self.Run(runstate)

        yield 'class class_%s' % self.name
        yield '{'
        for s in runstate.OutputCpp_H():
            yield '    ' + s
        yield '};'
        yield 'extern class class_%s the_%s;' % (self.name, self.name)

class ClassNS(BaseNameSpace):
    def __init__(self, name):
        BaseNameSpace.__init__(self)
        self.name = name
    def Output(self):
        yield 'class ' + self.name + '():'
        for op in self.oplist:
            for s in op.Output():
                yield '    ' + s
    def CreateClassInstance(self, params, rs):
        classInstance = RunState.ClassInstance(self, rs)
        value = VarDefine.Value("classinstance", classInstance)
        self.Run(classInstance)
        initfunc = [k for k in classInstance.funcs if k.name == '__init__']
        if initfunc:
            assert len(initfunc) == 1
            thefunc = initfunc[0]
            assert thefunc.__class__ is NameSpace.FuncNS
            assert len(thefunc.params) > 0 # the first should be 'self'
            assert len(thefunc.params) == len(params) + 1 # +1 is self, not support default param
            funcInstance = RunState.FuncInstance(thefunc, thefunc.params, classInstance)
            wordself = thefunc.params[0]
            funcInstance.DeclareParameter(wordself, value)
            for i in range(len(params)):
                wd = params[i]
                v = rs.GetVarRunState(wd)
                funcInstance.DeclareParameter(thefunc.params[i + 1], v)
            thefunc.Run(funcInstance)
            if funcInstance.OutCppState:
                funcInstance.OutCppState.Output()
        else:
            assert not params #no param, it must be no __init__
        if classInstance.OutCppState:
            classInstance.OutCppState.Output()
        return value
    def OutputCpp_H(self, rs):
        classInstance = RunState.ClassInstance(self, rs)
        self.Run(classInstance)

        yield 'class ' + self.name
        yield '{'
        initfunc = [k for k in classInstance.funcs if k.name == '__init__']
        if initfunc:
            assert len(initfunc) == 1
            thefunc = initfunc[0]
            classInstance.funcs.remove(thefunc)
            pass

        for s in classInstance.OutputCpp_H():
            yield '    ' + s
        yield '};'

class FuncNS(BaseNameSpace):
    def __init__(self, name, params):
        BaseNameSpace.__init__(self)
        self.name = name
        self.params = params
    def Output(self):
        s = string.join(self.params, ',')
        yield 'def ' + self.name + '(%s):' % s
        for op in self.oplist:
            for s in op.Output():
                yield '    ' + s
    def RunFunc(self, rs):
        result = BaseNameSpace.Run(self, rs)
        if result:
            return result[1]

class BranchNS(BaseNameSpace):
    def __init__(self, name):
        BaseNameSpace.__init__(self)
        self.name = name