Skip to content

Instantly share code, notes, and snippets.

@zkat
Created September 30, 2011 23:01
Show Gist options
  • Select an option

  • Save zkat/1255258 to your computer and use it in GitHub Desktop.

Select an option

Save zkat/1255258 to your computer and use it in GitHub Desktop.
die roll parser with smug
(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