Add bitwise operators to core
authorJosh Turmel <jturmel@gmail.com>
Wed, 27 Jul 2011 07:10:01 +0000 (02:10 -0500)
committerJosh Turmel <jturmel@gmail.com>
Thu, 28 Jul 2011 04:06:42 +0000 (23:06 -0500)
* Added bitwise operators (and, xor, or) to core

lib/Twig/Extension/Core.php
lib/Twig/Node/Expression/Binary/BitwiseAnd.php [new file with mode: 0644]
lib/Twig/Node/Expression/Binary/BitwiseOr.php [new file with mode: 0644]
lib/Twig/Node/Expression/Binary/BitwiseXor.php [new file with mode: 0644]

index 2c9bdb6..bf3cd12 100644 (file)
@@ -129,6 +129,9 @@ class Twig_Extension_Core extends Twig_Extension
                 '+'   => array('precedence' => 50, 'class' => 'Twig_Node_Expression_Unary_Pos'),
             ),
             array(
+                'b-and'  => array('precedence' => 5, 'class' => 'Twig_Node_Expression_Binary_BitwiseAnd', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+                'b-xor'  => array('precedence' => 5, 'class' => 'Twig_Node_Expression_Binary_BitwiseXor', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
+                'b-or'   => array('precedence' => 5, 'class' => 'Twig_Node_Expression_Binary_BitwiseOr', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
                 'or'     => array('precedence' => 10, 'class' => 'Twig_Node_Expression_Binary_Or', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
                 'and'    => array('precedence' => 15, 'class' => 'Twig_Node_Expression_Binary_And', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
                 '=='     => array('precedence' => 20, 'class' => 'Twig_Node_Expression_Binary_Equal', 'associativity' => Twig_ExpressionParser::OPERATOR_LEFT),
diff --git a/lib/Twig/Node/Expression/Binary/BitwiseAnd.php b/lib/Twig/Node/Expression/Binary/BitwiseAnd.php
new file mode 100644 (file)
index 0000000..9a46d84
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2009 Fabien Potencier
+ * (c) 2009 Armin Ronacher
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+class Twig_Node_Expression_Binary_BitwiseAnd extends Twig_Node_Expression_Binary
+{
+    public function operator(Twig_Compiler $compiler)
+    {
+        return $compiler->raw('&');
+    }
+}
diff --git a/lib/Twig/Node/Expression/Binary/BitwiseOr.php b/lib/Twig/Node/Expression/Binary/BitwiseOr.php
new file mode 100644 (file)
index 0000000..058a20b
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2009 Fabien Potencier
+ * (c) 2009 Armin Ronacher
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+class Twig_Node_Expression_Binary_BitwiseOr extends Twig_Node_Expression_Binary
+{
+    public function operator(Twig_Compiler $compiler)
+    {
+        return $compiler->raw('|');
+    }
+}
diff --git a/lib/Twig/Node/Expression/Binary/BitwiseXor.php b/lib/Twig/Node/Expression/Binary/BitwiseXor.php
new file mode 100644 (file)
index 0000000..f4da73d
--- /dev/null
@@ -0,0 +1,18 @@
+<?php
+
+/*
+ * This file is part of Twig.
+ *
+ * (c) 2009 Fabien Potencier
+ * (c) 2009 Armin Ronacher
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+class Twig_Node_Expression_Binary_BitwiseXor extends Twig_Node_Expression_Binary
+{
+    public function operator(Twig_Compiler $compiler)
+    {
+        return $compiler->raw('^');
+    }
+}