]> gitweb.factorcode.org Git - factor.git/commitdiff
dice: implementing randomized dice rolling with EBNF.
authorJohn Benediktsson <mrjbq7@gmail.com>
Tue, 24 Jul 2012 21:26:47 +0000 (14:26 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Tue, 24 Jul 2012 22:04:31 +0000 (15:04 -0700)
extra/dice/authors.txt [new file with mode: 0644]
extra/dice/dice.factor [new file with mode: 0644]
extra/dice/summary.txt [new file with mode: 0644]

diff --git a/extra/dice/authors.txt b/extra/dice/authors.txt
new file mode 100644 (file)
index 0000000..e091bb8
--- /dev/null
@@ -0,0 +1 @@
+John Benediktsson
diff --git a/extra/dice/dice.factor b/extra/dice/dice.factor
new file mode 100644 (file)
index 0000000..d12939f
--- /dev/null
@@ -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 ;
+
diff --git a/extra/dice/summary.txt b/extra/dice/summary.txt
new file mode 100644 (file)
index 0000000..38299dc
--- /dev/null
@@ -0,0 +1 @@
+Random dice rolls (with modifiers)