dice: adding some tests, simplify a bit.

db4
John Benediktsson 2014-12-10 08:46:15 -08:00
parent 5130954591
commit cf3c0d96a1
2 changed files with 8 additions and 6 deletions

View File

@ -0,0 +1,5 @@
USING: math random tools.test ;
IN: dice
{ [ 0 1 [ 4 random + 1 + ] times ] } [ "1d4" parse-roll ] unit-test
{ [ 0 15 [ 45 random + 1 + ] times ] } [ "15d45" parse-roll ] unit-test

View File

@ -1,14 +1,12 @@
! Copyright (C) 2010 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: fry kernel lexer macros math math.parser peg.ebnf random
sequences strings ;
sequences ;
IN: dice
EBNF: parse-roll
number = ([0-9])+ => [[ >string string>number ]]
number = ([0-9])+ => [[ string>number ]]
dice = "d" number => [[ second '[ _ random ] ]]
roll = number dice => [[ first2 '[ 0 _ [ @ + 1 + ] times ] ]]
added = "+" number => [[ second '[ _ + ] ]]
@ -20,5 +18,4 @@ rolls = total | error
MACRO: roll ( string -- ) parse-roll ;
SYNTAX: ROLL: scan-token parse-roll append ;
SYNTAX: ROLL: scan-token parse-roll append! ;