dicegame/lib/parser.fnl

40 lines
892 B
Fennel

;; a map of return value meanings for `parse`
(local dict {
0 :no-value
1 :lose
2 :point-value
3 :point-value
4 :point-value
5 :point-value
6 :win
})
;; returns a code (see `dict`) and an annotation
(fn parse [t]
"parse :: [ x y z ] -> [ n str ]"
(match (doto t (table.sort))
[ x x x ] [ 6 "trips! instawin" ]
[ 4 5 6 ] [ 6 "hi str8 instawin" ]
[ x x 6 ] [ 6 "6pt instawin" ]
[ 1 2 3 ] [ 1 "lo str8 instalose" ]
[ 1 x x ] [ 1 "1pt instalose" ]
[ x x y ] [ y (.. y "point") ]
[ y x x ] [ y (.. y "point") ]
_ [ 0 "nothing" ]
)
)
;; Usage:
;(local {: rolls} (require :dice-roller))
;(let [t (rolls)
; [ n str ] (parse t)
; pp (fn [x] (table.concat x " "))
; ]
; (print (pp t) n (. dict n) str)
; )
{
: parse
: dict
}