@@ -1,8 +1,11 @@
11
22module Ast where
33
4+import Data.Char (isDigit)
5+import Data.List (intercalate)
6+
47data Pattern = PData String [Pattern] | PVar String | PAnything
5− deriving (Show, Eq)
8+ deriving (Eq)
69
710data Expr = Number Int
811 | Chr Char
@@ -37,3 +40,17 @@ pcons h t = PData "Cons" [h,t]
3740pnil = PData "Nil" []
3841plist = foldr pcons pnil
3942ptuple es = PData ("Tuple" ++ show (length es)) es
43+
44+parens s = "(" ++ s ++ ")"
45+
46+instance Show Pattern where
47+ show (PVar x) = x
48+ show PAnything = "_"
49+ show (PData "Cons" [hd@(PData "Cons" _),tl]) =
50+ parens (show hd) ++ " : " ++ show tl
51+ show (PData "Cons" [hd,tl]) = show hd ++ " : " ++ show tl
52+ show (PData "Nil" []) = "[]"
53+ show (PData name ps) =
54+ if take 5 name == "Tuple" && all isDigit (drop 5 name) then
55+ parens . intercalate ", " $ map show ps
56+ else (if null ps then id else parens) $ unwords (name : map show ps)