Skip to content

Commit ab604d4

Browse files
committed
Added binary operation parsing method
1 parent 99dc532 commit ab604d4

4 files changed

Lines changed: 105 additions & 4 deletions

File tree

dice_notation/algebra.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# -*- coding: utf-8 -*-
2+
3+
from abc import ABCMeta, abstractmethod
4+
from random import randint
5+
6+
"""
7+
Algebraic classes.
8+
9+
These allow working with algebraic operations for dice notation.
10+
"""
11+
12+
__author__ = 'Benardo Martínez Garrido'
13+
__license__ = 'MIT'
14+
15+
16+
class BinaryOperation(object):
17+
"""
18+
A binary operation. Matching an operator with two operands.
19+
"""
20+
21+
def __init__(self, left, right, operator, operation):
22+
super(BinaryOperation, self).__init__()
23+
self._left = left
24+
self._right = right
25+
self._operator = operator
26+
self._operation = operation
27+
28+
def __str__(self):
29+
return '%s%s%s' % (self._left, self._operator, self._right)
30+
31+
def __repr__(self):
32+
return '<class %s>(left=%r, right=%r, operator=%r)' % \
33+
(self.__class__.__name__, self._left, self._right, self._operator)
34+
35+
@property
36+
def left(self):
37+
"""
38+
The left operand.
39+
40+
:return: the left operand
41+
"""
42+
return self._left
43+
44+
@left.setter
45+
def left(self, left):
46+
self._left = left
47+
48+
@property
49+
def right(self):
50+
"""
51+
The right operand.
52+
53+
:return: the right operand
54+
"""
55+
return self._right
56+
57+
@right.setter
58+
def right(self, right):
59+
self._right = right
60+
61+
@property
62+
def operator(self):
63+
"""
64+
The operator.
65+
66+
:return: the operator
67+
"""
68+
return self._operator
69+
70+
@operator.setter
71+
def operator(self, operator):
72+
self._operator = operator

dice_notation/dice.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ def __init__(self, quantity, sides):
6767
self._sides = sides
6868

6969
def __str__(self):
70-
return '%sd%s' % (self.quantity, self.sides)
70+
return '%sd%s' % (self._quantity, self._sides)
7171

7272
def __repr__(self):
7373
return '<class %s>(quantity=%r, sides=%r)' % \
74-
(self.__class__.__name__, self.quantity, self.sides)
74+
(self.__class__.__name__, self._quantity, self._sides)
7575

7676
@property
7777
def quantity(self):

dice_notation/parser/DiceNotationListener.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import logging
55
from dice_notation.dice import Dice
6+
from dice_notation.algebra import BinaryOperation
67

78
# This class defines a complete listener for a parse tree produced by DiceNotationParser.
89
class DiceNotationListener(ParseTreeListener):
@@ -12,7 +13,7 @@ def __init__(self):
1213
self._logger = logging.getLogger("DiceNotationListener")
1314
self._nodes = []
1415

15-
def getExpression(self):
16+
def expression(self):
1617
expression = None
1718

1819
self._logger.debug("Checking stack %s for parsed expression", self._nodes)
@@ -24,6 +25,34 @@ def getExpression(self):
2425

2526
return expression
2627

28+
def binary_operation(self, operators):
29+
operands = []
30+
31+
# There are as many operands as operators plus one
32+
for i in range(0, len(operators)):
33+
operands.append(self._nodes.pop())
34+
35+
for operator in operators:
36+
left = operands.pop()
37+
right = operands.pop()
38+
39+
if("+" == operator):
40+
func = lambda a, b: a + b
41+
elif("-" == operator):
42+
func = lambda a, b: a - b
43+
elif("*" == operator):
44+
func = lambda a, b: a * b
45+
elif("/" == operator):
46+
func = lambda a, b: a / b
47+
else:
48+
func = None
49+
self._logger.error("The %s operator is invalid", operator)
50+
operation = BinaryOperation(left, right, operator, func)
51+
self._logger.debug("Parsed operation %s", operation)
52+
operands.append(operation)
53+
54+
return operands.pop()
55+
2756
# Enter a parse tree produced by DiceNotationParser#notation.
2857
def enterNotation(self, ctx):
2958
self._logger.debug("Entering notation %s", ctx.getText())

dice_notation/parser/dice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,6 @@ def parse(self, input):
2525
listener = DiceNotationListener()
2626
walker.walk(listener, tree)
2727

28-
expression = listener.getExpression()
28+
expression = listener.expression()
2929
self._logger.debug("Parsed expression %s", expression)
3030
return expression

0 commit comments

Comments
 (0)