Skip to content

Commit 24338c8

Browse files
committed
Operation results are parsed
1 parent d5637d6 commit 24338c8

4 files changed

Lines changed: 27 additions & 5 deletions

File tree

dice_notation/algebra.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ def __repr__(self):
3232
return '<class %s>(left=%r, right=%r, operator=%r)' % \
3333
(self.__class__.__name__, self._left, self._right, self._operator)
3434

35+
def value(self):
36+
return self._operation(self._left.value(), self._right.value())
37+
3538
@property
3639
def left(self):
3740
"""
@@ -70,3 +73,23 @@ def operator(self):
7073
@operator.setter
7174
def operator(self, operator):
7275
self._operator = operator
76+
77+
78+
class Number(object):
79+
"""
80+
A numeric constant
81+
"""
82+
83+
def __init__(self, value):
84+
super(Number, self).__init__()
85+
self._value = value
86+
87+
def __str__(self):
88+
return '%s' % (self._value)
89+
90+
def __repr__(self):
91+
return '<class %s>(value=%r)' % \
92+
(self.__class__.__name__, self._value)
93+
94+
def value(self):
95+
return self._value

dice_notation/parser/DiceNotationListener.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import logging
55
from dice_notation.dice import Dice
6-
from dice_notation.algebra import BinaryOperation
6+
from dice_notation.algebra import BinaryOperation, Number
77

88
# This class defines a complete listener for a parse tree produced by DiceNotationParser.
99
class DiceNotationListener(ParseTreeListener):
@@ -132,7 +132,7 @@ def exitDice(self, ctx):
132132
# Enter a parse tree produced by DiceNotationParser#number.
133133
def enterNumber(self, ctx):
134134
self._logger.debug("Entering number %s", ctx.getText())
135-
value = int(ctx.getText())
135+
value = Number(int(ctx.getText()))
136136
self._nodes.append(value)
137137

138138
# Exit a parse tree produced by DiceNotationParser#number.

dice_notation/parser/dice.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,4 @@ def parse(self, input):
2727

2828
expression = listener.expression()
2929
self._logger.debug("Parsed expression %s", expression)
30-
return expression
30+
return expression.value()

tests/parser/number/test_dice_parser_number_add.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ def test_add_toNegative(self):
3737
"""
3838
result = self.parser.parse("-1+2")
3939

40-
# TODO: currently not supported
41-
# self.assertEqual(1, result)
40+
self.assertEqual(1, result)
4241

4342

4443
class TestAddLong(unittest.TestCase):

0 commit comments

Comments
 (0)