-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdice.jison
executable file
·122 lines (113 loc) · 3.31 KB
/
dice.jison
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
%{
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
%}
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
[0-9]+ return 'NUMBER'
"*" return '*'
"/" return '/'
"-" return '-'
"+" return '+'
"^" return '^'
"%" return '%'
"(" return '('
")" return ')'
[dD] return 'D'
[hHkK] return 'H'
[lL] return 'L'
[fF] return 'F'
<<EOF>> return 'EOF'
. throw "Invalid Input Token!"
/lex
/* operator associations and precedence */
%left '+' '-'
%left '*' '/'
%left '^'
%right '%'
%right 'D' 'H' 'L' 'F'
%left UMINUS
%start expressions
%% /* language grammar */
expressions:
e EOF {return $1;}
;
e:
e '+' e {$$ = $1 + $3;}
| e '-' e {$$ = $1 - $3;}
| e '*' e {$$ = $1 * $3;}
| e '/' e {$$ = $1 / $3;}
| e '^' e {$$ = Math.pow($1, $3);}
| e '%' e {$$ = $1 % $3;}
| '-' e %prec UMINUS {$$ = -$2;}
| e 'D' e 'H' e {
if($3 < 1) throw "Dice must have at least one side!";
if($5 < 1) throw "You must keep at least one die!";
if($5 > $1) throw "You can't keep more dice than you roll!";
var rolls = [];
for(var i = $1; i--;) rolls.push(getRandomInt(1, $3));
var roll = rolls.sort(function (a, b) {return a - b;}).slice(rolls.length - $5, rolls.length).reduce(function (a, b) {return a + b;});
console.log("Rolled " + rolls + " = " + roll);
$$ = roll;
}
| e 'D' e 'L' e {
if($3 < 1) throw "Dice must have at least one side!";
if($5 < 1) throw "You must keep at least one die!";
if($5 > $1) throw "You can't keep more dice than you roll!";
var rolls = [];
for(var i = $1; i--;) rolls.push(getRandomInt(1, $3));
var roll = rolls.sort(function (a, b) {return a - b;}).slice(0, $5).reduce(function (a, b) {return a + b;});
console.log("Rolled " + rolls + " = " + roll);
$$ = roll;
}
| e 'D' e {
if($3 < 1) throw "Dice must have at least one side!";
var rolls = [];
for(var i = $1; i--;) rolls.push(getRandomInt(1, $3));
var roll = rolls.reduce(function (a, b) {return a + b;});
console.log("Rolled " + rolls + " = " + roll);
$$ = roll;
}
| 'D' e {
if($2 < 1) throw "Dice must have at least one side!";
var roll = getRandomInt(1, $2);
console.log("Rolled " + roll);
$$ = roll;
}
| e 'D' 'F' 'H' e {
if($5 < 1) throw "You must keep at least one die!";
if($5 > $1) throw "You can't keep more dice than you roll!";
var rolls = [];
for(var i = $1; i--;) rolls.push(getRandomInt(-1, 1));
var roll = rolls.sort(function (a, b) {return a - b;}).slice(rolls.length - $5, rolls.length).reduce(function (a, b) {return a + b;});
console.log("Rolled " + rolls + " = " + roll);
$$ = roll;
}
| e 'D' 'F' 'L' e {
if($5 < 1) throw "You must keep at least one die!";
if($5 > $1) throw "You can't keep more dice than you roll!";
var rolls = [];
for(var i = $1; i--;) rolls.push(getRandomInt(-1, 1));
var roll = rolls.sort(function (a, b) {return a - b;}).slice(0, $5).reduce(function (a, b) {return a + b;});
console.log("Rolled " + rolls + " = " + roll);
$$ = roll;
}
| e 'D' 'F' {
var rolls = [];
for(var i = $1; i--;) rolls.push(getRandomInt(-1, 1));
var roll = rolls.reduce(function (a, b) {return a + b;});
console.log("Rolled " + rolls + " = " + roll);
$$ = roll;
}
| 'D' 'F' {
var roll = getRandomInt(-1, 1);
console.log("Rolled " + roll);
$$ = roll;
}
| '(' e ')' {$$ = $2;}
| NUMBER {$$ = Number(yytext);}
| . {throw "Invalid Input Structure";}
;