dice: implementing randomized dice rolling with EBNF.

db4
John Benediktsson 2012-07-24 14:26:47 -07:00
parent fe7b482efe
commit 56404b8b89
3 changed files with 26 additions and 0 deletions

1
extra/dice/authors.txt Normal file
View File

@ -0,0 +1 @@
John Benediktsson

24
extra/dice/dice.factor Normal file
View File

@ -0,0 +1,24 @@
! 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 ;
IN: dice
EBNF: parse-roll
number = ([0-9])+ => [[ >string string>number ]]
dice = "d" number => [[ second '[ _ random ] ]]
roll = number dice => [[ first2 '[ 0 _ [ @ + 1 + ] times ] ]]
added = "+" number => [[ second '[ _ + ] ]]
total = roll added? => [[ first2 [ append ] when* ]]
error = .* => [[ "unknown dice" throw ]]
rolls = total | error
;EBNF
MACRO: roll ( string -- ) parse-roll ;
SYNTAX: ROLL: scan-token parse-roll append ;

1
extra/dice/summary.txt Normal file
View File

@ -0,0 +1 @@
Random dice rolls (with modifiers)