Source

BrainMachine / VarDefine.py

Full commit
#!/usr/bin/env python

import string
import NameSpace
import RunState

class Value():
    def __init__(self, typestr, val):
        self.typestr = typestr #can be bool, string, number, classinstance
        self.val = val
    def __str__(self):
        return self.typestr + ' ' + self.val
    def ToString(self):
        if self.typestr == 'number':
            return self.val
        if self.typestr == 'string':
            return self.val
        assert False
    def IsSameValue(self, v):
        if self.typestr == v.typestr:
            if self.val == v.val:
                return True
        return False
    def IfTrue(self):
        if self.typestr == 'bool':
            return self.val
        assert False
    def Plus(self, v):
        if self.typestr == v.typestr == 'number':
            v1 = int(self.val)
            v2 = int(v.val)
            return Value('number', str(v1 + v2))
        if self.typestr == v.typestr == 'string':
            return Value('string', self.val + v.val)
        assert False
    def Minus(self, v):
        if self.typestr == v.typestr == 'number':
            v1 = int(self.val)
            v2 = int(v.val)
            return Value('number', str(v1 - v2))
        assert False
    def Times(self, v):
        if self.typestr == v.typestr == 'number':
            v1 = int(self.val)
            v2 = int(v.val)
            return Value('number', str(v1 * v2))
        assert False

def IsPythonNumber(s):
    # support more number format later
    import re
    k = re.match(r'\A\d+\Z', s)
    if k:
        return True
    return False

class BaseToken():
    def GetValue(self, rs):
        raise 'not implement'
    def GetValueInList(self, rs):
        v = self.GetValue(rs)
        return [v]

class NameToken(BaseToken):
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return self.name
    def GetValue(self, rs):
        if IsPythonNumber(self.name):
            return Value("number", self.name)
        return rs._GetAttr(self.name)

def CheckOpPriority(op1, op2):
    import MainBrain
    lv = MainBrain.g_mb.langDef.linkverbs
    if op1 == op2:
        me = lv[op2]
        if me[1] == 'left':
            return True
        assert False
    lv1 = lv[op1]
    lv2 = lv[op2]
    if lv1[0] > lv2[0]:
        return False
    if lv1[0] < lv2[0]:
        return True
    assert False

class CombineToken(BaseToken):
    def __init__(self, vleft, vright, op):
        self.vleft = vleft
        self.vright = vright
        self.op = op
    def __str__(self):
        s = self.op
        if s == ',':
            s = ', '
        if self.vright.__class__ is CombineToken:
            b = CheckOpPriority(self.op, self.vright.op)
            if b:
                return str(self.vleft) + s + '(' + str(self.vright) + ')'
            return str(self.vleft) + s + str(self.vright)
            pass
        return str(self.vleft) + s + str(self.vright)
    def GetValue(self, rs):
        if self.op == '==':
            v1 = self.vleft.GetValue(rs)
            v2 = self.vright.GetValue(rs)
            b = v1.IsSameValue(v2)
            return Value('bool', b)
        if self.op == '.':
            runstate = rs.GetVarRunState(self.vleft)
            value = self.vright.GetValue(runstate)
            return value
        if self.op == '+':
            v1 = self.vleft.GetValue(rs)
            v2 = self.vright.GetValue(rs)
            return v1.Plus(v2)
        if self.op == '-':
            v1 = self.vleft.GetValue(rs)
            v2 = self.vright.GetValue(rs)
            return v1.Minus(v2)
        if self.op == '*':
            v1 = self.vleft.GetValue(rs)
            v2 = self.vright.GetValue(rs)
            return v1.Times(v2)
        assert False
    def GetValueInList(self, rs):
        if self.op == ',':
            v1 = self.vleft.GetValueInList(rs)
            v2 = self.vright.GetValueInList(rs)
            return v1 + v2
        return BaseToken.GetValueInList(self, rs)

class StringToken(BaseToken):
    def __init__(self, s):
        self.s = s
    def __str__(self):
        return self.s
    def GetValue(self, rs):
        return Value('string', self.s[1:-1])

class FuncToken(BaseToken):
    # not really function, but somethine like word(..)
    # it may be many meaning in Python
    def __init__(self, func, params):
        self.func = func
        self.params = params
    def __str__(self):
        return str(self.func)+'('+string.join(self.params, ',')+')'
    def GetValue(self, rs):
        if self.func.__class__ is NameToken:
            funcname = self.func.name
            what = rs.GetNameToken(funcname)
            if what:
                if what.__class__ is NameSpace.ClassNS:
                    # create Class instance
                    return what.CreateClassInstance(self.params, rs)
        assert False