Commits

bookaa committed a6c0a4d

CheckOpPriority

Comments (0)

Files changed (3)

         self.Complete_CombineToken_verb(tokenstack, None)
 
     def Complete_CombineToken_verb(self, tokenstack, verb):
-        if len(tokenstack) == 1:
-            return
-        if not verb:
+        def func1():
+            v = VarDefine.CombineToken(tokenstack[-3], tokenstack[-1], tokenstack[-2])
+            tokenstack[-3:] = [v]
+
+        while True:
+            if len(tokenstack) == 1:
+                break
+            if not verb:
+                if len(tokenstack) >= 3:
+                    func1()
+                    continue
+                assert False
             if len(tokenstack) >= 3:
-                v = VarDefine.CombineToken(tokenstack[-3], tokenstack[-1], tokenstack[-2])
-                tokenstack[-3:] = [v]
-                self.Complete_CombineToken_verb(tokenstack, verb) #recursive
-                return
+                verb0 = tokenstack[-2]
+                from VarDefine import CheckOpPriority
+                b = CheckOpPriority(verb0, verb)
+                if b:
+                    func1()
+                    continue
+                break
             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
 
     def ExpectOneToken(self):
         tp = self.tp
 #!/usr/bin/env python
 import LangDef
-from MainBrain import MainBrain
+import MainBrain
 
 src = '''
 class C():
 print the.a - (5-2), the.b, the.c
 '''
 
-mb = MainBrain()
+mb = MainBrain.MainBrain()
+MainBrain.g_mb = mb
 mb.DefineInLang(LangDef.python)
 mb.ReadInBuf(src)
 mb.DefineOutLang(LangDef.python_without_dyna)
             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):
-        return str(self.vleft) + self.op + str(self.vright)
+        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)