Skip to content

Commit 3cfa89a

Browse files
committed
Parse ** operator. Bump version.
1 parent 0a41005 commit 3cfa89a

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

testing/test_parser.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,16 @@ def test_arith():
131131
rhs=BinaryOp(op=<STAR:*,11:12>,lhs=z,rhs=h_))""")
132132

133133

134+
def test_pow():
135+
check("a**2",
136+
"""BinaryOp(op=<DOUBLESTAR:**,1:3>,lhs=a,rhs=2)""")
137+
138+
139+
def test_chained_pow():
140+
check("a**b**c",
141+
"""BinaryOp(op=<DOUBLESTAR:**,1:3>,lhs=a,rhs=BinaryOp(op=<DOUBLESTAR:**,4:6>,lhs=b,rhs=c))""")
142+
143+
134144
def test_chained_op():
135145
check("a + b + c",
136146
"""BinaryOp(op=<PLUS:+,6:7>,

testing/test_tree_eval.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ def test_add_mul():
7676
c = 5
7777
check("a+b*c", 23)
7878

79+
def test_pow():
80+
a = 3
81+
b = 4
82+
check("a**b", 3**4)
83+
84+
def test_pow2():
85+
a = 3
86+
b = 4
87+
c = 5
88+
check("a**(b+1)**c", 3**5**5)
89+
7990
def test_parens():
8091
a = 3
8192
b = 4

tsensor/parsing.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
NOTEQUAL, PERCENTEQUAL, AMPEREQUAL, DOUBLESTAREQUAL, STAREQUAL, PLUSEQUAL,\
3131
MINEQUAL, DOUBLESLASHEQUAL, SLASHEQUAL, LEFTSHIFTEQUAL,\
3232
LESSEQUAL, EQUAL, EQEQUAL, GREATEREQUAL, RIGHTSHIFTEQUAL, ATEQUAL,\
33-
CIRCUMFLEXEQUAL, VBAREQUAL
33+
CIRCUMFLEXEQUAL, VBAREQUAL, DOUBLESTAR
3434

3535
import tsensor.ast
3636

@@ -157,15 +157,25 @@ def addexpr(self):
157157

158158
def multexpr(self):
159159
start = self.LT(1)
160-
root = self.unaryexpr()
160+
root = self.powexpr()
161161
while self.LA(1) in MULOP:
162162
op = self.LT(1)
163163
self.t += 1
164-
b = self.unaryexpr()
164+
b = self.powexpr()
165165
stop = self.LT(-1)
166166
root = tsensor.ast.BinaryOp(op, root, b, start, stop)
167167
return root
168168

169+
def powexpr(self):
170+
start = self.LT(1)
171+
root = self.unaryexpr()
172+
if self.LA(1)==DOUBLESTAR:
173+
op = self.match(DOUBLESTAR)
174+
r = self.powexpr()
175+
stop = self.LT(-1)
176+
root = tsensor.ast.BinaryOp(op, root, r, start, stop)
177+
return root
178+
169179
def unaryexpr(self):
170180
start = self.LT(1)
171181
if self.LA(1) in UNARYOP:

tsensor/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@
2121
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
SOFTWARE.
2323
"""
24-
__version__ = '0.1b5'
24+
__version__ = '0.1b6'

0 commit comments

Comments
 (0)