From 363eb37c521ef9aa6778d7b7cdf6a0756dd7b074 Mon Sep 17 00:00:00 2001 From: Robert Bradshaw Date: Mon, 15 Jan 2007 18:21:26 -0800 Subject: [PATCH] FloorDiv operation --- Cython/Compiler/ExprNodes.py | 12 ++++++++++++ Cython/Compiler/Lexicon.py | 2 +- Cython/Compiler/Parsing.py | 2 +- 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/Cython/Compiler/ExprNodes.py b/Cython/Compiler/ExprNodes.py index 66c00c82..ac09bcf9 100644 --- a/Cython/Compiler/ExprNodes.py +++ b/Cython/Compiler/ExprNodes.py @@ -2421,6 +2421,7 @@ class NumBinopNode(BinopNode): "-": "PyNumber_Subtract", "*": "PyNumber_Multiply", "/": "PyNumber_Divide", + "//": "PyNumber_FloorDivide", "%": "PyNumber_Remainder", "**": "PyNumber_Power" } @@ -2479,6 +2480,16 @@ class MulNode(NumBinopNode): return NumBinopNode.is_py_operation(self) +class FloorDivNode(NumBinopNode): + # '//' operator. + + def calculate_result_code(self): + return "(%s %s %s)" % ( + self.operand1.result_code, + "/", # c division is by default floor-div + self.operand2.result_code) + + class ModNode(IntBinopNode): # '%' operator. @@ -2841,6 +2852,7 @@ binop_node_classes = { "-": SubNode, "*": MulNode, "/": NumBinopNode, + "//": FloorDivNode, "%": ModNode, "**": PowNode } diff --git a/Cython/Compiler/Lexicon.py b/Cython/Compiler/Lexicon.py index 479b6cdf..2e241c68 100644 --- a/Cython/Compiler/Lexicon.py +++ b/Cython/Compiler/Lexicon.py @@ -66,7 +66,7 @@ def make_lexicon(): bra = Any("([{") ket = Any(")]}") punct = Any(":,;+-*/|&<>=.%`~^?") - diphthong = Str("==", "<>", "!=", "<=", ">=", "<<", ">>", "**", "+=", "-=", "*=", "/=", "%=", "|=", "^=", "&=") + diphthong = Str("==", "<>", "!=", "<=", ">=", "<<", ">>", "**", "+=", "-=", "*=", "/=", "%=", "|=", "^=", "&=", "//") spaces = Rep1(Any(" \t\f")) comment = Str("#") + Rep(AnyBut("\n")) escaped_newline = Str("\\\n") diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py index ab1ee178..fa6bb204 100644 --- a/Cython/Compiler/Parsing.py +++ b/Cython/Compiler/Parsing.py @@ -155,7 +155,7 @@ def p_arith_expr(s): #term: factor (('*'|'/'|'%') factor)* def p_term(s): - return p_binop_expr(s, ('*', '/', '%'), p_factor) + return p_binop_expr(s, ('*', '/', '%', '//'), p_factor) #factor: ('+'|'-'|'~'|'&'|typecast|sizeof) factor | power -- 2.26.2