aboutsummaryrefslogtreecommitdiff
path: root/ast/CC/AST/Typed.hs
diff options
context:
space:
mode:
authorTom Smeding <tom.smeding@gmail.com>2020-07-24 22:24:44 +0200
committerTom Smeding <tom.smeding@gmail.com>2020-07-24 22:24:44 +0200
commitd951a11b1141b9f4c1ee50c7f89b68c552883c16 (patch)
treea3097eab42081e5af45e516a3e3c8c956503bd57 /ast/CC/AST/Typed.hs
parentea681150783d1feae265a045edefaeb0e10dd8c7 (diff)
Move CC.{Source,Typed} to CC.AST.*
Diffstat (limited to 'ast/CC/AST/Typed.hs')
-rw-r--r--ast/CC/AST/Typed.hs67
1 files changed, 67 insertions, 0 deletions
diff --git a/ast/CC/AST/Typed.hs b/ast/CC/AST/Typed.hs
new file mode 100644
index 0000000..f4c4d56
--- /dev/null
+++ b/ast/CC/AST/Typed.hs
@@ -0,0 +1,67 @@
+module CC.AST.Typed(
+ module CC.AST.Typed,
+ module CC.Types
+) where
+
+import Data.List
+
+import CC.Pretty
+import CC.Types
+
+
+data ProgramT = ProgramT [DefT]
+ deriving (Show, Read)
+
+data DefT = DefT Name ExprT
+ deriving (Show, Read)
+
+data TypeT = TFunT TypeT TypeT
+ | TIntT
+ | TTupT [TypeT]
+ | TyVar Int
+ deriving (Show, Read)
+
+data ExprT = LamT TypeT Occ ExprT
+ | CallT TypeT ExprT ExprT
+ | IntT Int
+ | TupT [ExprT]
+ | VarT Occ
+ deriving (Show, Read)
+
+data Occ = Occ Name TypeT
+ deriving (Show, Read)
+
+exprType :: ExprT -> TypeT
+exprType (LamT typ _ _) = typ
+exprType (CallT typ _ _) = typ
+exprType (IntT _) = TIntT
+exprType (TupT es) = TTupT (map exprType es)
+exprType (VarT (Occ _ typ)) = typ
+
+instance Pretty TypeT where
+ prettyPrec _ TIntT = "Int"
+ prettyPrec p (TFunT a b) =
+ precParens p 2 (prettyPrec 3 a ++ " -> " ++ prettyPrec 2 b)
+ prettyPrec _ (TTupT ts) =
+ "(" ++ intercalate ", " (map pretty ts) ++ ")"
+ prettyPrec _ (TyVar i) = 't' : show i
+
+instance Pretty ExprT where
+ prettyPrec p (LamT ty (Occ n t) e) =
+ precParens p 2 $
+ "(\\(" ++ n ++ " :: " ++ pretty t ++ ") -> "
+ ++ prettyPrec 2 e ++ ") :: " ++ pretty ty
+ prettyPrec p (CallT ty e1 e2) =
+ precParens p 2 $
+ prettyPrec 3 e1 ++ " " ++ prettyPrec 3 e2 ++ " :: " ++ pretty ty
+ prettyPrec _ (IntT i) = show i
+ prettyPrec _ (TupT es) = "(" ++ intercalate ", " (map pretty es) ++ ")"
+ prettyPrec p (VarT (Occ n t)) =
+ precParens p 2 $
+ show n ++ " :: " ++ pretty t
+
+instance Pretty DefT where
+ pretty (DefT n e) = n ++ " = " ++ pretty e
+
+instance Pretty ProgramT where
+ pretty (ProgramT defs) = concatMap ((++ "\n") . pretty) defs