1
1
import assign from 'object-assign' ;
2
2
import convert from './convert' ;
3
3
4
- function reduce ( node , precision ) {
4
+ function reduce ( node , precision , item ) {
5
5
if ( node . type === "MathExpression" )
6
- return reduceMathExpression ( node , precision ) ;
6
+ return reduceMathExpression ( node , precision , item ) ;
7
7
8
8
return node ;
9
9
}
@@ -12,10 +12,10 @@ function isEqual(left, right) {
12
12
return left . type === right . type && left . value === right . value ;
13
13
}
14
14
15
- function convertMathExpression ( node , precision ) {
15
+ function convertMathExpression ( node , precision , item ) {
16
16
let nodes = convert ( node . left , node . right , precision ) ;
17
- let left = reduce ( nodes . left , precision ) ;
18
- let right = reduce ( nodes . right , precision ) ;
17
+ let left = reduce ( nodes . left , precision , item ) ;
18
+ let right = reduce ( nodes . right , precision , item ) ;
19
19
20
20
if ( left . type === "MathExpression" && right . type === "MathExpression" ) {
21
21
@@ -30,8 +30,8 @@ function convertMathExpression(node, precision) {
30
30
else if ( isEqual ( left . right , right . left ) )
31
31
nodes = convert ( left . left , right . right , precision ) ;
32
32
33
- left = reduce ( nodes . left , precision ) ;
34
- right = reduce ( nodes . right , precision ) ;
33
+ left = reduce ( nodes . left , precision , item ) ;
34
+ right = reduce ( nodes . right , precision , item ) ;
35
35
36
36
}
37
37
}
@@ -41,7 +41,7 @@ function convertMathExpression(node, precision) {
41
41
return node ;
42
42
}
43
43
44
- function reduceAddSubExpression ( node , precision ) {
44
+ function reduceAddSubExpression ( node , precision , item ) {
45
45
let op = node . operator ;
46
46
let left = node . left ;
47
47
let right = node . right ;
@@ -78,9 +78,9 @@ function reduceAddSubExpression(node, precision) {
78
78
operator : op ,
79
79
left : left ,
80
80
right : right . left
81
- } , precision ) ;
81
+ } , precision , item ) ;
82
82
node . right = right . right ;
83
- return reduce ( node , precision ) ;
83
+ return reduce ( node , precision , item ) ;
84
84
}
85
85
// value + (something + value) => (value + value) + something
86
86
// value + (something - value) => (value - value) + something
@@ -93,9 +93,9 @@ function reduceAddSubExpression(node, precision) {
93
93
operator : right . operator ,
94
94
left : left ,
95
95
right : right . right
96
- } , precision ) ;
96
+ } , precision , item ) ;
97
97
node . right = right . left ;
98
- return reduce ( node , precision ) ;
98
+ return reduce ( node , precision , item ) ;
99
99
}
100
100
}
101
101
@@ -112,8 +112,8 @@ function reduceAddSubExpression(node, precision) {
112
112
operator : op ,
113
113
left : left . left ,
114
114
right : right
115
- } , precision ) ;
116
- return reduce ( node , precision ) ;
115
+ } , precision , item ) ;
116
+ return reduce ( node , precision , item ) ;
117
117
}
118
118
// (something + value) + value => something + (value + value)
119
119
// (something - value) + value => something - (value + value)
@@ -126,19 +126,22 @@ function reduceAddSubExpression(node, precision) {
126
126
operator : op ,
127
127
left : left . right ,
128
128
right : right
129
- } , precision ) ;
130
- return reduce ( node , precision ) ;
129
+ } , precision , item ) ;
130
+ return reduce ( node , precision , item ) ;
131
131
}
132
132
}
133
133
return node ;
134
134
}
135
135
136
- function reduceDivisionExpression ( node ) {
137
- if ( node . right . type !== 'Value ')
136
+ function reduceDivisionExpression ( node , item ) {
137
+ if ( node . right . type === 'MathExpression ')
138
138
return node ;
139
139
140
+ if ( node . right . type !== 'Value' )
141
+ throw item . error ( `Cannot divide by "${ node . right . unit } ", number expected` ) ;
142
+
140
143
if ( node . right . value === 0 )
141
- return node ; // throw error?
144
+ throw item . error ( 'Cannot divide by zero' ) ;
142
145
143
146
// (expr) / value
144
147
if ( node . left . type === 'MathExpression' ) {
@@ -194,15 +197,15 @@ function reduceMultiplicationExpression(node) {
194
197
return node ;
195
198
}
196
199
197
- function reduceMathExpression ( node , precision ) {
198
- node = convertMathExpression ( node , precision ) ;
200
+ function reduceMathExpression ( node , precision , item ) {
201
+ node = convertMathExpression ( node , precision , item ) ;
199
202
200
203
switch ( node . operator ) {
201
204
case "+" :
202
205
case "-" :
203
- return reduceAddSubExpression ( node , precision ) ;
206
+ return reduceAddSubExpression ( node , precision , item ) ;
204
207
case "/" :
205
- return reduceDivisionExpression ( node ) ;
208
+ return reduceDivisionExpression ( node , item ) ;
206
209
case "*" :
207
210
return reduceMultiplicationExpression ( node ) ;
208
211
}
0 commit comments