Commits

bookaa committed 1afdb0f

628

Comments (0)

Files changed (5)

 define_python = {
                  'keywords' : 'class def'.split(),
                  'opverbs' : '= += -='.split(),
-                 'linkverbs' : '+ - == ,'.split(),
+                 'linkverbs' : {'.':(1,'left'),
+                                '+':(3,'left'),
+                                '-':(3,'left'),
+                                '==':(4,'left'),
+                                ',':(9,'left'),
+                                },
                  'fLikePython' : True, # avoid this, try more specific
                  'dynamic' : True,
                  }
     def ExpectVRight(self):
         v = self.ExpectOneToken()
         v = self.Complete_MostToken(v)
-        return self.Complete_CombineToken(v)
+        tokenstack = [v]
+        self.Complete_CombineToken(tokenstack)
+        assert len(tokenstack) == 1
+        return tokenstack[0]
 
     def Complete_MostToken(self, vleft):
         tp = self.tp
             v = VarDefine.CombineToken(vleft, var2, '.')
             return v
         return vleft
-    def Complete_CombineToken(self, vleft):
+    def Complete_CombineToken(self, tokenstack):
         tp = self.tp
         verb = self.CheckIfStartWith(self.langdef.linkverbs)
         if verb:
+            self.Complete_CombineToken_verb(tokenstack, verb)
             tp.SkipTheWord(verb)
+            tokenstack.append(verb)
             vright = self.ExpectOneToken()
-            vright = self.Complete_MostToken(vright)
-            return self.Complete_CombineToken_2(vleft, vright, verb)
-        return vleft
+            tokenstack.append(vright)
+            self.Complete_CombineToken(tokenstack)
+            return
+        self.Complete_CombineToken_verb(tokenstack, None)
 
-    def Complete_CombineToken_2(self, vleft, vright, verb):
-        tp = self.tp
-        bEnd = tp.IsLineEnd()
-        if not bEnd:
-            c = tp.PickChar()
-            if self.langdef.fLikePython:
-                if c == ':':
-                    bEnd = True
-        if bEnd:
-            return VarDefine.CombineToken(vleft, vright, verb)
-
-        verb2 = self.CheckIfStartWith(self.langdef.linkverbs)
-        if verb2:
-            if verb == verb2 and verb2 == ',':
-                v = VarDefine.CombineToken(vleft, vright, verb)
-                tp.SkipTheWord(verb2)
-                vright = self.ExpectOneToken()
-                vright = self.Complete_MostToken(vright)
-                return self.Complete_CombineToken_2(v, vright, verb)
-                pass
-
-
+    def Complete_CombineToken_verb(self, tokenstack, verb):
+        if len(tokenstack) == 1:
+            return
+        if not verb:
+            if len(tokenstack) >= 3:
+                v = VarDefine.CombineToken(tokenstack[-3], tokenstack[-1], tokenstack[-2])
+                tokenstack[-3:] = [v]
+                self.Complete_CombineToken_verb(tokenstack, verb) #recursive
+                return
+            assert False
+        if len(tokenstack) >= 3:
+            verb0 = tokenstack[-2]
+            des0 = self.langdef.linkverbs[verb0]
+            des = self.langdef.linkverbs[verb]
+            if des0[0] < des[0]: # not same level
+                v = VarDefine.CombineToken(tokenstack[-3], tokenstack[-1], tokenstack[-2])
+                tokenstack[-3:] = [v]
+                self.Complete_CombineToken_verb(tokenstack, verb) #recursive
+                return
+            if des0[0] == des[0] and des0[1] == des[1] == 'left':
+                v = VarDefine.CombineToken(tokenstack[-3], tokenstack[-1], tokenstack[-2])
+                tokenstack[-3:] = [v]
+                self.Complete_CombineToken_verb(tokenstack, verb) #recursive
+                return
+            return
         assert False
-        return v
 
     def ExpectOneToken(self):
         tp = self.tp
         return 'unknown'
 
 the = C()
-print the.a, the.b, the.c
+print the.a + 5, the.b, the.c
 '''
 
 mb = MainBrain()
     def Run(self, rs):
         def PrintArgToString(v):
             if v.__class__ is VarDefine.CombineToken:
-                if v.op == ',':
-                    s1 = PrintArgToString(v.vleft)
-                    s2 = PrintArgToString(v.vright)
-                    return s1 + ' ' + s2
-                if v.op == '.':
-                    runstate = rs.GetVarRunState(v.vleft)
-                    value = v.vright.GetValue(runstate)
-                    return value.ToString()
-                assert False
+                values = v.GetValueInList(rs)
+                s = ''
+                for m in values:
+                    if s:
+                        s = s + ' ';
+                    s = s + m.ToString()
+                return s
             value = v.GetValue(rs)
             return value.ToString()
         s = PrintArgToString(self.v)
     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 == 'bool':
             return self.val
         assert False
+    def Plus(self, v):
+        if self.typestr == 'number' and 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
 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):
             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)
         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):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.