Created
September 30, 2011 23:01
-
-
Save zkat/1255258 to your computer and use it in GitHub Desktop.
die roll parser with smug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (defun die-roll () | |
| (=let* ((num-dice (=or (natural-number) (result 1))) | |
| (_ (=char #\d)) | |
| (die-faces (natural-number))) | |
| (result (loop repeat num-dice | |
| summing (random die-faces))))) | |
| (defun chained-roll () | |
| (=let* ((first-roll (=or (die-roll) | |
| (smug::sophisticated-int))) | |
| (other-rolls (zero-or-more | |
| (=let* ((modifier (=or (=char #\-) | |
| (=char #\+))) | |
| (value (=or (die-roll) | |
| (natural-number)))) | |
| (result (* value | |
| (case modifier | |
| (#\- -1) | |
| (#\+ 1)))))))) | |
| (result (reduce #'+ other-rolls :initial-value first-roll)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment