Pierre Carbonnelle avatar Pierre Carbonnelle committed e6984b8

define X+(1,) as an Expression, not as X.data+(1,)
Clarify distinction between ._pyD_name, ._pyD_value and ._pyD_lua

Comments (0)

Files changed (3)

pyDatalog/examples/test.py

     assert ( X==(1,2) ) == [((1,2),)]
     assert ( X==(1,(2,)) ) == [((1,(2,)),)] # nested
     assert ( X==(1,) + (2,) ) == [((1,2),)] # expression
+    assert  ((X==(1,2)) & (Y==X+(3,))) == [((1, 2), (1, 2, 3))]
     
     assert ( (X==(1,2)) & (X==(1, X2))) == [((1, 2), 2)]
     assert ( (X==(1,(2,))) & (X==(1, (X2,))) & (Y==X2)) == [((1, (2,)), 2, 2)]

pyDatalog/pyDatalog.py

         name = 'X%i' % id(self) if name is None else name
         pyParser.LazyList.__init__(self)
         pyParser.VarSymbol.__init__(self, name)
+
+    def __add__(self, other):
+        return pyParser.Operation(self, '+', other)
+    def __radd__(self, other):
+        return pyParser.Operation(other, '+', self)
+
 pyDatalog.Variable = Variable
 
 """Keep a dictionary of classes with datalog capabilities.  This list is used by pyEngine to resolve prefixed literals."""

pyDatalog/pyParser.py

     def __init__ (self, name, forced_type=None):
         self._pyD_negated = False # for aggregate with sort in descending order
         if isinstance(name, (list, tuple, xrange)):
-            self._pyD_name = [Expression._for(element) for element in name]
+            self._pyD_value = list(map(Expression._for, name))
+            self._pyD_name = str([element._pyD_name for element in self._pyD_value])
             self._pyD_type = 'tuple'
-            self._pyD_lua = pyEngine.VarTuple([e._pyD_lua for e in self._pyD_name])
+            self._pyD_lua = pyEngine.VarTuple([e._pyD_lua for e in self._pyD_value])
         elif isinstance(name, slice):
             start, stop, step = map(Expression._for, (name.start, name.stop, name.step))
-            self._pyD_name = slice(start, stop, step)
+            self._pyD_value = slice(start, stop, step)
+            self._pyD_name = '[%s:%s:%s]' % (start._pyD_name, stop._pyD_name, step._pyD_name)
             self._pyD_type = 'slice'
-            self._pyD_lua = slice(start, stop, step)
-        elif forced_type=="constant" or isinstance(name, (int, list, tuple, xrange)) or not name or name[0] not in string.ascii_uppercase + '_':
-            self._pyD_name = name
+            self._pyD_lua = slice(start, stop, step) # TODO ._pyD_lua ?
+        elif forced_type=="constant" or isinstance(name, int) or not name or name[0] not in string.ascii_uppercase + '_':
+            self._pyD_value = name
+            self._pyD_name = str(name)
             self._pyD_type = 'constant'
             self._pyD_lua = pyEngine.Const(name)
         else:
+            self._pyD_value = name
             self._pyD_name = name
             self._pyD_type = 'variable'
             self._pyD_lua = pyEngine.Var(name)
 
     def __neg__(self):
         """ called when compiling -X """
-        neg = Symbol(self._pyD_name)
+        neg = Symbol(self._pyD_value)
         neg._pyD_negated = True
 
         expr = 0 - self
         if self._pyD_type == 'variable':
             return pyEngine.Operand('variable', variables.index(self._pyD_name))
         elif self._pyD_type == 'tuple':
-            return pyEngine.Operand('tuple', [element.lua_expr(variables) for element in self._pyD_name])
+            return pyEngine.Operand('tuple', [element.lua_expr(variables) for element in self._pyD_value])
         elif self._pyD_type == 'slice':
-            return pyEngine.Operand('slice', slice(self._pyD_name.start.lua_expr(variables),
-                                                   self._pyD_name.stop.lua_expr(variables),
-                                                   self._pyD_name.step.lua_expr(variables),))
+            return pyEngine.Operand('slice', slice(self._pyD_lua.start.lua_expr(variables),
+                                                   self._pyD_lua.stop.lua_expr(variables),
+                                                   self._pyD_lua.step.lua_expr(variables),))
         else:
-            return pyEngine.Operand('constant', self._pyD_name)
+            return pyEngine.Operand('constant', self._pyD_value)
     
     def _variables(self):
         if self._pyD_type == 'variable':
             return OrderedDict({self._pyD_name : self})
         elif self._pyD_type == 'tuple':
             variables = OrderedDict()
-            for element in self._pyD_name:
+            for element in self._pyD_value:
                 variables.update(element._variables())
             return variables
         elif self._pyD_type == 'slice':
             variables = OrderedDict()
-            variables.update(self._pyD_name.start._variables())
-            variables.update(self._pyD_name.stop._variables())
-            variables.update(self._pyD_name.step._variables())
+            variables.update(self._pyD_lua.start._variables())
+            variables.update(self._pyD_lua.stop._variables())
+            variables.update(self._pyD_lua.step._variables())
             return variables
         else:
             return OrderedDict()
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.