reject negative repeat counts

This commit is contained in:
Zachary Snow 2019-09-30 23:11:16 -04:00
parent b7959c7aa2
commit e2570303d0
1 changed files with 9 additions and 1 deletions

View File

@ -164,7 +164,15 @@ readNumber n =
simplify :: Expr -> Expr
simplify (UniOp LogNot (Number "1")) = Number "0"
simplify (UniOp LogNot (Number "0")) = Number "1"
simplify (Repeat (Number "0") _) = Concat []
simplify (orig @ (Repeat (Number n) exprs)) =
case readNumber n of
Nothing -> orig
Just 0 -> Concat []
Just 1 -> Concat exprs
Just x ->
if x < 0
then error $ "negative repeat count: " ++ show orig
else orig
simplify (Concat [expr]) = expr
simplify (Concat exprs) =
Concat $ filter (/= Concat []) exprs