Skip to content

Commit 8b5e353

Browse files
committed
Renamed nodes to expressions
1 parent 0810cb2 commit 8b5e353

3 files changed

Lines changed: 42 additions & 39 deletions

File tree

dice_notation/parser/dice.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
from operator import add, sub
55

66
from dice_notation.parser.common import PlyParser
7-
from dice_notation.parser.node import BinaryOperationNode, ConstantNode, \
8-
DiceNode
7+
from dice_notation.parser.notation import BinaryOperationExpression, ConstantExpression, \
8+
DiceExpression
99

1010
"""
1111
Dice notation parsers.
@@ -60,7 +60,7 @@ def p_statement_expr(self, p):
6060

6161
def p_expression_dice(self, p):
6262
'expression : DIGIT DSEPARATOR DIGIT'
63-
p[0] = DiceNode(p[1], p[3])
63+
p[0] = DiceExpression(p[1], p[3])
6464
self._logger.debug("Dice %s", p[0])
6565

6666
def p_expression_binop(self, p):
@@ -70,17 +70,17 @@ def p_expression_binop(self, p):
7070
"""
7171
# print [repr(p[i]) for i in range(0,4)]
7272
if p[2] == '+':
73-
p[0] = BinaryOperationNode(add, p[1], p[3])
73+
p[0] = BinaryOperationExpression(add, p[1], p[3])
7474
elif p[2] == '-':
75-
p[0] = BinaryOperationNode(sub, p[1], p[3])
75+
p[0] = BinaryOperationExpression(sub, p[1], p[3])
7676
self._logger.debug("Binary operation %s", p[0])
7777

7878
def p_expression_digit(self, p):
7979
'expression : DIGIT'
80-
if isinstance(p[1], ConstantNode):
80+
if isinstance(p[1], ConstantExpression):
8181
p[0] = p[1]
8282
else:
83-
p[0] = ConstantNode(p[1])
83+
p[0] = ConstantExpression(p[1])
8484
self._logger.debug("Constant %s", p[0])
8585

8686
def p_error(self, p):
Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
from dice_notation.dice import RollableDice, Rollable
66

77
"""
8-
Dice notation nodes.
8+
Dice notation classes.
99
10-
Used to generate the parsed tree from a dice notation expression.
10+
Used to generate the parsed tree from a dice notation expression. These
11+
expressions will be nodes in that tree.
1112
12-
All the nodes are rollable, to allow generating a roll from any point of the
13+
All the classes are rollable, to allow generating a roll from any point of the
1314
expression.
1415
"""
1516

1617

17-
class ConstantNode(Rollable):
18+
class ConstantExpression(Rollable):
1819
"""
19-
Node containing a constant value.
20+
Expression for a constant value.
2021
2122
It just wraps any value, allowing it to interface with the other nodes.
2223
@@ -25,30 +26,30 @@ class ConstantNode(Rollable):
2526
"""
2627

2728
def __init__(self, constant):
28-
super(ConstantNode, self).__init__()
29+
super(ConstantExpression, self).__init__()
2930
self._constant = constant
3031
# If it received a constant node this is unwrapped
31-
# TODO: Maybe the problem should be fixed somewhere else
32-
while isinstance(self._constant, ConstantNode):
32+
# TODO: Maybe this problem should be fixed somewhere else
33+
while isinstance(self._constant, ConstantExpression):
3334
self._constant = constant.constant
3435

3536
def __add__(self, other):
36-
return ConstantNode(other + self.constant)
37+
return ConstantExpression(other + self.constant)
3738

3839
def __sub__(self, other):
39-
return ConstantNode(self.constant - other)
40+
return ConstantExpression(self.constant - other)
4041

4142
def __radd__(self, other):
42-
return ConstantNode(self.constant + other)
43+
return ConstantExpression(self.constant + other)
4344

4445
def __rsub__(self, other):
45-
return ConstantNode(other - self.constant)
46+
return ConstantExpression(other - self.constant)
4647

4748
def __lt__(self, other):
4849
return self.constant < other
4950

5051
def __le__(self, other):
51-
if isinstance(other, ConstantNode):
52+
if isinstance(other, ConstantExpression):
5253
return self.constant <= other.constant
5354
elif isinstance(other, (int, float)):
5455
return self.constant <= other
@@ -65,7 +66,7 @@ def __gt__(self, other):
6566
return self.constant > other
6667

6768
def __ge__(self, other):
68-
if isinstance(other, ConstantNode):
69+
if isinstance(other, ConstantExpression):
6970
return self.constant >= other.constant
7071
elif isinstance(other, (int, float)):
7172
return self.constant >= other
@@ -101,25 +102,27 @@ def roll(self):
101102
return self.constant
102103

103104

104-
class DiceNode(RollableDice):
105+
class DiceExpression(RollableDice):
105106
"""
106-
Node containing a dice.
107+
Expression for a dice.
108+
109+
It is mostly just its parent class, only adding comparison methods.
107110
"""
108111

109112
def __init__(self, quantity, sides):
110-
super(DiceNode, self).__init__(quantity=quantity, sides=sides)
113+
super(DiceExpression, self).__init__(quantity=quantity, sides=sides)
111114

112115
def __add__(self, other):
113-
return ConstantNode(other + self.roll())
116+
return ConstantExpression(other + self.roll())
114117

115118
def __sub__(self, other):
116-
return ConstantNode(self.roll() - other)
119+
return ConstantExpression(self.roll() - other)
117120

118121
def __radd__(self, other):
119-
return ConstantNode(self.roll() + other)
122+
return ConstantExpression(self.roll() + other)
120123

121124
def __rsub__(self, other):
122-
return ConstantNode(other - self.roll())
125+
return ConstantExpression(other - self.roll())
123126

124127
def __str__(self):
125128
return '%sd%s' % (self.quantity, self.sides)
@@ -129,15 +132,15 @@ def __repr__(self):
129132
(self.__class__.__name__, self.quantity, self.sides)
130133

131134

132-
class BinaryOperationNode(Rollable):
135+
class BinaryOperationExpression(Rollable):
133136
"""
134-
Node for a binary operation.
137+
Exprssion for a binary operation.
135138
136139
Acquiring its value will execute a function with two parameters.
137140
"""
138141

139142
def __init__(self, function, left, right):
140-
super(BinaryOperationNode, self).__init__()
143+
super(BinaryOperationExpression, self).__init__()
141144
self._logger = logging.getLogger(self.__class__.__name__)
142145
self._function = function
143146
self._left = left
@@ -146,28 +149,28 @@ def __init__(self, function, left, right):
146149
def __add__(self, other):
147150
value = self.operate()
148151
self._logger.debug("%s + %s", value, other)
149-
return ConstantNode(other + value)
152+
return ConstantExpression(other + value)
150153

151154
def __sub__(self, other):
152155
value = self.operate()
153156
self._logger.debug("%s - %s", other, value)
154-
return ConstantNode(value - other)
157+
return ConstantExpression(value - other)
155158

156159
def __radd__(self, other):
157160
value = self.operate()
158161
self._logger.debug("(rear) %s + %s", value, other)
159-
return ConstantNode(value + other)
162+
return ConstantExpression(value + other)
160163

161164
def __rsub__(self, other):
162165
value = self.operate()
163166
self._logger.debug("(rear) %s - %s", other, value)
164-
return ConstantNode(other - value)
167+
return ConstantExpression(other - value)
165168

166169
def __lt__(self, other):
167170
return self.operate() < other
168171

169172
def __le__(self, other):
170-
if isinstance(other, ConstantNode):
173+
if isinstance(other, ConstantExpression):
171174
return self.operate() <= other.constant
172175
elif isinstance(other, (int, float)):
173176
return self.operate() <= other
@@ -184,7 +187,7 @@ def __gt__(self, other):
184187
return self.operate() > other
185188

186189
def __ge__(self, other):
187-
if isinstance(other, ConstantNode):
190+
if isinstance(other, ConstantExpression):
188191
return self.operate() >= other.constant
189192
elif isinstance(other, (int, float)):
190193
return self.operate() >= other
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import unittest
44

55
from dice_notation.parser.dice import DiceParser
6-
from dice_notation.parser.node import ConstantNode
6+
from dice_notation.parser.notation import ConstantExpression
77

88
"""
99
Dice parser tests for purely numeric expressions.
@@ -30,4 +30,4 @@ def test_longSub_singleWrap(self):
3030
"""
3131
result = self.parser.parse("1-2-3-4-5").operate()
3232

33-
self.assertFalse(isinstance(result.constant, ConstantNode))
33+
self.assertFalse(isinstance(result.constant, ConstantExpression))

0 commit comments

Comments
 (0)