Commits

bookaa committed 40a0cf8

709

Comments (0)

Files changed (5)

     def Run(self):
         runstate = RunState.RunState(self.globalns, None)
 
-        value = VarDefine.Value('string', '__main__')
+        value = VarDefine.RunConstant('str', '__main__')
         var = VarDefine.NameToken('__name__')
         runstate.SetVarValue(var, value)
 
         value = self.vright.GetValue(rs)
         if self.verb == '=':
             rs.SetVarValue(self.vleft, value)
+            if rs.OutCppState:
+                rs.OutCppState.Assign(self.vleft, value)
             return None
         assert False
     def __str__(self):
         for s in self.body.Output():
             yield '    ' + s
     def Run(self, rs):
+        if rs.OutCppState:
+            rs.OutCppState.AddOp_if()
         value = self.condition.GetValue(rs)
         if value.IfTrue():
-            return self.body.Run(rs)
+            rs.OutCppState.Enter_iftrue()
+            r = self.body.Run(rs)
+            rs.OutCppState.Leave()
+            return r
         pass
 
 class Opcode_Return(BaseOpcode):
 import VarDefine
 import RunState
 
+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:
         yield 'class ' + self.rs.classns.name
         yield '{'
         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
             return theclass[0]
         assert False
     def SetVarValue(self, var, value):
-        assert value and value.__class__ is VarDefine.Value
+        assert value and isinstance(value, VarDefine.Value) #value.__class__ is VarDefine.Value
 
         if var.__class__ is VarDefine.NameToken:
             self._SetAttr(var.name, value)
             assert fnc.__class__ is NameSpace.FuncNS
             funcInstance = FuncInstance(fnc, fnc.params, self)
             value = VarDefine.Value("classinstance", self)
-            v = VarDefine.Value("string", name)
+            v = VarDefine.RunConstant("str", name)
             funcInstance.DeclareParameter(fnc.params[0], value)
             funcInstance.DeclareParameter(fnc.params[1], v)
             return fnc.RunFunc(funcInstance)
         self.params = params
         self.owner_rs = owner_rs
     def DeclareParameter(self, var, value):
-        assert value and value.__class__ is VarDefine.Value
+        assert isinstance(value, VarDefine.Value) # value and value.__class__ is VarDefine.Value
         self.varvalue.setdefault(var, value)
 import NameSpace
 import RunState
 
+class DataType():
+    def __init__(self, name=None):
+        if name:
+            assert type(name) is str
+            assert name in 'bool str int float NoneType function classobj classinstance tuple list dict'.split()
+        self.name = name
+
 class Value():
     def __init__(self, typestr, val):
         self.typestr = typestr #can be bool, string, number, classinstance
+        self.datatype = DataType(typestr)
         self.val = val
     def __str__(self):
         return self.typestr + ' ' + self.val
     def ToString(self):
-        if self.typestr == 'number':
+        if self.typestr == 'int':
             return self.val
-        if self.typestr == 'string':
+        if self.typestr == 'str':
             return self.val
         assert False
     def IsSameValue(self, v):
             return self.val
         assert False
     def Plus(self, v):
-        if self.typestr == v.typestr == 'number':
+        if self.typestr == v.typestr == 'int':
             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)
+            return Value('int', str(v1 + v2))
+        if self.typestr == v.typestr == 'str':
+            return RunConstant('str', self.val + v.val)
         assert False
     def Minus(self, v):
-        if self.typestr == v.typestr == 'number':
+        if self.typestr == v.typestr == 'int':
             v1 = int(self.val)
             v2 = int(v.val)
-            return Value('number', str(v1 - v2))
+            return RunConstant('int', str(v1 - v2))
         assert False
     def Times(self, v):
-        if self.typestr == v.typestr == 'number':
+        if self.typestr == v.typestr == 'int':
             v1 = int(self.val)
             v2 = int(v.val)
-            return Value('number', str(v1 * v2))
+            return RunConstant('int', str(v1 * v2))
         assert False
 
+class RunConstant(Value):
+    def __init__(self, typestr, valstr):
+        Value.__init__(self, typestr, valstr)
+
 def IsPythonNumber(s):
     # support more number format later
     import re
         return self.name
     def GetValue(self, rs):
         if IsPythonNumber(self.name):
-            return Value("number", self.name)
+            return RunConstant("int", self.name)
         return rs._GetAttr(self.name)
 
 def CheckOpPriority(op1, op2):
             v1 = self.vleft.GetValue(rs)
             v2 = self.vright.GetValue(rs)
             b = v1.IsSameValue(v2)
-            return Value('bool', b)
+            return RunConstant('bool', b)
         if self.op == '.':
             runstate = rs.GetVarRunState(self.vleft)
             value = self.vright.GetValue(runstate)
     def __str__(self):
         return self.s
     def GetValue(self, rs):
-        return Value('string', self.s[1:-1])
+        return RunConstant('str', self.s[1:-1])
 
 class FuncToken(BaseToken):
     # not really function, but somethine like word(..)
         self.func = func
         self.params = params
     def __str__(self):
-        return str(self.func)+'('+string.join(self.params, ',')+')'
+        return str(self.func) + '(' + string.join(self.params, ',') + ')'
     def GetValue(self, rs):
         if self.func.__class__ is NameToken:
             funcname = self.func.name