[pypy-commit] pypy py3.5-raffael_t: Write matmul method as binary function in ast

raff...@gmail.com pypy.commits at gmail.com
Tue Apr 26 19:32:37 EDT 2016


Author: raffael.tfirst at gmail.com
Branch: py3.5-raffael_t
Changeset: r83939:dc70c88997e4
Date: 2016-03-21 23:01 +0100
http://bitbucket.org/pypy/pypy/changeset/dc70c88997e4/

Log:	Write matmul method as binary function in ast

diff --git a/pypy/interpreter/astcompiler/assemble.py b/pypy/interpreter/astcompiler/assemble.py
--- a/pypy/interpreter/astcompiler/assemble.py
+++ b/pypy/interpreter/astcompiler/assemble.py
@@ -567,6 +567,7 @@
     ops.BINARY_SUBSCR: -1,
     ops.BINARY_FLOOR_DIVIDE: -1,
     ops.BINARY_TRUE_DIVIDE: -1,
+    ops.BINARY_MAT_MUL: -1,
     ops.BINARY_LSHIFT: -1,
     ops.BINARY_RSHIFT: -1,
     ops.BINARY_AND: -1,
diff --git a/pypy/interpreter/astcompiler/ast.py b/pypy/interpreter/astcompiler/ast.py
--- a/pypy/interpreter/astcompiler/ast.py
+++ b/pypy/interpreter/astcompiler/ast.py
@@ -2970,6 +2970,8 @@
             return 11
         if space.isinstance_w(w_node, get(space).w_FloorDiv):
             return 12
+        if space.isinstance_w(w_node, get(space).w_MatMul):
+            return 13
         raise oefmt(space.w_TypeError,
                 "Expected operator node, got %T", w_node)
 State.ast_type('operator', 'AST', None)
@@ -3034,6 +3036,11 @@
         return space.call_function(get(space).w_FloorDiv)
 State.ast_type('FloorDiv', 'operator', None)
 
+class _MatMul(operator):
+    def to_object(self, space):
+        return space.call_function(get(space).w_MatMul)
+State.ast_type('MatMul', 'operator', None)
+
 Add = 1
 Sub = 2
 Mult = 3
@@ -3046,7 +3053,7 @@
 BitXor = 10
 BitAnd = 11
 FloorDiv = 12
-MatMult = 13
+MatMul = 13
 
 operator_to_class = [
     _Add,
@@ -3061,6 +3068,7 @@
     _BitXor,
     _BitAnd,
     _FloorDiv,
+    _MatMul,
 ]
 
 class unaryop(AST):
diff --git a/pypy/interpreter/astcompiler/astbuilder.py b/pypy/interpreter/astcompiler/astbuilder.py
--- a/pypy/interpreter/astcompiler/astbuilder.py
+++ b/pypy/interpreter/astcompiler/astbuilder.py
@@ -17,7 +17,7 @@
     '/='  : ast.Div,
     '//=' : ast.FloorDiv,
     '%='  : ast.Mod,
-    '@='  : ast.MatMult,
+    '@='  : ast.MatMul,
     '<<='  : ast.LShift,
     '>>='  : ast.RShift,
     '&='  : ast.BitAnd,
@@ -39,7 +39,7 @@
     tokens.SLASH : ast.Div,
     tokens.DOUBLESLASH : ast.FloorDiv,
     tokens.PERCENT : ast.Mod,
-    tokens.AT : ast.MatMult
+    tokens.AT : ast.MatMul
 })
 
 
diff --git a/pypy/interpreter/astcompiler/codegen.py b/pypy/interpreter/astcompiler/codegen.py
--- a/pypy/interpreter/astcompiler/codegen.py
+++ b/pypy/interpreter/astcompiler/codegen.py
@@ -65,7 +65,8 @@
     ast.BitOr: ops.BINARY_OR,
     ast.BitAnd: ops.BINARY_AND,
     ast.BitXor: ops.BINARY_XOR,
-    ast.FloorDiv: ops.BINARY_FLOOR_DIVIDE
+    ast.FloorDiv: ops.BINARY_FLOOR_DIVIDE,
+    ast.MatMul: ops.BINARY_MAT_MUL
 })
 
 inplace_operations = misc.dict_to_switch({
@@ -80,7 +81,8 @@
     ast.BitOr: ops.INPLACE_OR,
     ast.BitAnd: ops.INPLACE_AND,
     ast.BitXor: ops.INPLACE_XOR,
-    ast.FloorDiv: ops.INPLACE_FLOOR_DIVIDE
+    ast.FloorDiv: ops.INPLACE_FLOOR_DIVIDE,
+    ast.MatMul: ops.INPLACE_MAT_MUL
 })
 
 compare_operations = misc.dict_to_switch({
diff --git a/pypy/interpreter/astcompiler/optimize.py b/pypy/interpreter/astcompiler/optimize.py
--- a/pypy/interpreter/astcompiler/optimize.py
+++ b/pypy/interpreter/astcompiler/optimize.py
@@ -134,6 +134,7 @@
     ast.BitOr : _binary_fold("or_"),
     ast.BitXor : _binary_fold("xor"),
     ast.BitAnd : _binary_fold("and_"),
+    ast.MatMul : _binary_fold("matmul"),
 }
 unrolling_binary_folders = unrolling_iterable(binary_folders.items())
 
diff --git a/pypy/interpreter/astcompiler/tools/Python.asdl b/pypy/interpreter/astcompiler/tools/Python.asdl
--- a/pypy/interpreter/astcompiler/tools/Python.asdl
+++ b/pypy/interpreter/astcompiler/tools/Python.asdl
@@ -95,7 +95,7 @@
     boolop = And | Or 
 
     operator = Add | Sub | Mult | Div | Mod | Pow | LShift 
-                 | RShift | BitOr | BitXor | BitAnd | FloorDiv
+                 | RShift | BitOr | BitXor | BitAnd | FloorDiv | MatMul
 
     unaryop = Invert | Not | UAdd | USub
 
diff --git a/pypy/interpreter/pyopcode.py b/pypy/interpreter/pyopcode.py
--- a/pypy/interpreter/pyopcode.py
+++ b/pypy/interpreter/pyopcode.py
@@ -228,6 +228,8 @@
                 self.BINARY_AND(oparg, next_instr)
             elif opcode == opcodedesc.BINARY_FLOOR_DIVIDE.index:
                 self.BINARY_FLOOR_DIVIDE(oparg, next_instr)
+            elif opcode == opcodedesc.BINARY_MAT_MUL.index:
+                self.BINARY_MAT_MUL(oparg, next_instr)
             elif opcode == opcodedesc.BINARY_LSHIFT.index:
                 self.BINARY_LSHIFT(oparg, next_instr)
             elif opcode == opcodedesc.BINARY_MODULO.index:
@@ -570,6 +572,7 @@
     BINARY_MULTIPLY = binaryoperation("mul")
     BINARY_TRUE_DIVIDE  = binaryoperation("truediv")
     BINARY_FLOOR_DIVIDE = binaryoperation("floordiv")
+    BINARY_MAT_MUL = binaryoperation("matmul")
     BINARY_DIVIDE       = binaryoperation("div")
     # XXX BINARY_DIVIDE must fall back to BINARY_TRUE_DIVIDE with -Qnew
     BINARY_MODULO       = binaryoperation("mod")
@@ -591,6 +594,7 @@
     INPLACE_MULTIPLY = binaryoperation("inplace_mul")
     INPLACE_TRUE_DIVIDE  = binaryoperation("inplace_truediv")
     INPLACE_FLOOR_DIVIDE = binaryoperation("inplace_floordiv")
+    INPLACE_FLOOR_DIVIDE = binaryoperation("inplace_matmul")
     INPLACE_DIVIDE       = binaryoperation("inplace_div")
     # XXX INPLACE_DIVIDE must fall back to INPLACE_TRUE_DIVIDE with -Qnew
     INPLACE_MODULO       = binaryoperation("inplace_mod")


More information about the pypy-commit mailing list