-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
58 lines (55 loc) · 1.94 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const P = require('./src/parser.js')
const D = require('./src/dice.js')
const interpret = tree => {
switch(tree.type) {
case 'constant':
return D.constant(tree.value)
case 'd':
return D.d(interpret(tree.left), interpret(tree.right))
case 'E':
return D.explodeAbove(interpret(tree.left), interpret(tree.right))
case 'e':
return D.explodeUnder(interpret(tree.left), interpret(tree.right))
case 'K':
return D.keepHigh(interpret(tree.left), interpret(tree.right))
case 'k':
return D.keepLow(interpret(tree.left), interpret(tree.right))
case 'A':
return D.againAbove(interpret(tree.left), interpret(tree.right))
case 'a':
return D.againUnder(interpret(tree.left), interpret(tree.right))
case 'T':
return D.thresholdHigh(interpret(tree.left), interpret(tree.right))
case 't':
return D.thresholdLow(interpret(tree.left), interpret(tree.right))
case 'add':
return D.add(interpret(tree.left), interpret(tree.right))
case 'subtract':
return D.subtract(interpret(tree.left), interpret(tree.right))
case 'multiply':
return D.multiply(interpret(tree.left), interpret(tree.right))
case 'divide':
return D.divide(interpret(tree.left), interpret(tree.right))
case 'negative':
return D.negative(interpret(tree.value))
case 'bonusAdd':
return D.bonusAdd(interpret(tree.left), interpret(tree.right))
case 'bonusSubtract':
return D.bonusSubtract(interpret(tree.left), interpret(tree.right))
case 'bonusMultiply':
return D.bonusMultiply(interpret(tree.left), interpret(tree.right))
case 'bonusDivide':
return D.bonusDivide(interpret(tree.left), interpret(tree.right))
case 'repeat':
return D.repeat(interpret(tree.left), interpret(tree.right))
case 'collect':
return D.collect(interpret(tree.value))
}
}
const parse = expressionString => {
const tree = P.parse(expressionString)
return interpret(tree)
}
exports.parse = parse
exports.roll = D.roll
exports.pool = D.pool