Source

BrainMachine / RunState.py

#!/usr/bin/env python

import VarDefine
import NameSpace

class RunState():
    def __init__(self, ns, rs):
        self.ns = ns
        self.rs = rs #upper level
        self.classes = []
        self.funcs = []
        self.varvalue = {} # str : value
    def GetNameToken(self, name):
        theclass = [k for k in self.classes if name is k.name]
        if theclass:
            return theclass[0]
        assert False
    def SetVarValue(self, var, value):
        assert value and value.__class__ is VarDefine.Value

        if var.__class__ is VarDefine.NameToken:
            self._SetAttr(var.name, value)
            return
        if var.__class__ is VarDefine.CombineToken:
            if var.op == '.':
                #var.vleft._SetAttr(var.vright, value)
                runstate = self.GetVarRunState(var.vleft)
                assert var.vright.__class__ is VarDefine.NameToken
                runstate._SetAttr(var.vright.name, value)
                return
            assert False
        assert False
    def GetVarValue(self, var):
        if var.__class__ is VarDefine.NameToken:
            if var.name in self.varvalue:
                return self.varvalue[var.name]
        assert False
    def GetVarRunState(self, var):
        v = self.GetVarValue(var)
        if v.typestr == 'classinstance':
            return v.val
        assert False
    def _SetAttr(self, name, value):
        assert type(name) is str
        if name in self.varvalue:
            assert False
        self.varvalue.setdefault(name, value)
    def _GetAttr(self, name):
        assert type(name) is str
        if name in self.varvalue:
            return self.varvalue[name]
        fnc = self.SearchFunc('__getattr__')
        if fnc:
            assert fnc.__class__ is NameSpace.FuncNS
            funcInstance = FuncInstance(fnc, fnc.params, self)
            value = VarDefine.Value("classinstance", self)
            v = VarDefine.Value("string", name)
            funcInstance.DeclareParameter(fnc.params[0], value)
            funcInstance.DeclareParameter(fnc.params[1], v)
            return fnc.RunFunc(funcInstance)
        assert False
    def SearchFunc(self, name):
        for fnc in self.funcs:
            if fnc.name == name:
                return fnc
        return None
    def OutputCpp_H(self):
        for theclass in self.classes:
            for s in theclass.OutputCpp_H(self):
                yield s
        pass

class ClassInstance(RunState):
    def __init__(self, classns, owner_rs):
        RunState.__init__(self, classns, owner_rs)
        self.classns = classns
        self.owner_rs = owner_rs

class FuncInstance(RunState):
    def __init__(self, funcns, params, owner_rs):
        RunState.__init__(self, funcns, owner_rs)
        self.funcns = funcns
        self.params = params
        self.owner_rs = owner_rs
    def DeclareParameter(self, var, value):
        assert value and value.__class__ is VarDefine.Value
        self.varvalue.setdefault(var, value)