Commits

Pierre Carbonnelle committed 20940aa

support for string concatenation in lua engine

  • Participants
  • Parent commits 613d152
  • Tags 0.4.0

Comments (0)

Files changed (2)

File pyDatalog/luaEngine.py

 end
 function Expression:eval(env)
     if self.operator == '+' then
-	    return self.operand1:eval(env) + self.operand2:eval(env)
+        lhs = self.operand1:eval(env)
+        if type(lhs) == 'string' then
+            return lhs .. self.operand2:eval(env)
+        else
+    	    return lhs + self.operand2:eval(env)
+    	end
     elseif self.operator == '-' then
         return self.operand1:eval(env) - self.operand2:eval(env)
     elseif self.operator == '*' then

File pyDatalog/test.py

         assert ask(fibonacci(1, F)) == set([(1, 1)])
         assert ask(fibonacci(4, F)) == set([(4, 3)])
         assert ask(fibonacci(18, F)) == set([(18, 2584)])
+        
+        # string manipulation
+        split(X, Y,Z) <= (X == Y+'-'+Z)
+        assert ask(split(X, 'a', 'b')) == set([('a-b', 'a', 'b')])
+        split(X, Y,Z) <= (Y == (lambda X: X.split('-')[0])) & (Z == (lambda X: X.split('-')[1]))
+        assert ask(split('a-b', Y, Z)) == set([('a-b', 'a', 'b')])
+        assert ask(split(X, 'a', 'b')) == set([('a-b', 'a', 'b')])
     
     pyDatalog.clear()
     @pyDatalog.program()