1 ! Copyright (C) 2010 John Benediktsson
2 ! See http://factorcode.org/license.txt for BSD license
3 USING: kernel lexer math math.parser namespaces
4 random random.private sequences splitting ;
7 : (random-roll) ( #dice #sides obj -- n )
8 [ 0 ] 3dip '[ _ _ (random-integer) + 1 + ] times ;
10 : random-roll ( #dice #sides -- n )
11 random-generator get (random-roll) ;
13 : random-rolls ( length #dice #sides -- seq )
14 random-generator get '[ _ _ _ (random-roll) ] replicate ;
16 : parse-roll ( string -- #dice #sides #added )
17 "d" split1 "+" split1 [ string>number ] tri@ ;
19 : roll ( string -- n )
20 parse-roll [ random-roll ] dip [ + ] when* ;
22 : roll-quot ( string -- quot: ( -- n ) )
24 '[ _ _ random-roll _ + ]
29 SYNTAX: ROLL: scan-token roll-quot append! ;