on master help sign in
Move RawType and CanonicalType into AST.Type
4cd7514Evan Czaplicki
compiler/AST/Declaration.hs+1−1
@@ -22,7 +22,7 @@ data Assoc = L | N | R
22 deriving (Eq)
22 deriving (Eq)
23
23
24data RawPort
24data RawPort
25− = PPAnnotation String (T.Type Var.Raw)
25+ = PPAnnotation String T.RawType
26 | PPDef String Source.Expr
26 | PPDef String Source.Expr
27 deriving (Show)
27 deriving (Show)
28
28
compiler/AST/Expression/Canonical.hs+2−2
@@ -5,7 +5,7 @@ module AST.Expression.Canonical where
5import AST.PrettyPrint
5import AST.PrettyPrint
6import Text.PrettyPrint as P
6import Text.PrettyPrint as P
7import qualified AST.Expression.General as General
7import qualified AST.Expression.General as General
8−import AST.Type (Type)
8+import AST.Type (CanonicalType)
9import qualified AST.Annotation as Annotation
9import qualified AST.Annotation as Annotation
10import qualified AST.Pattern as Pattern
10import qualified AST.Pattern as Pattern
11import qualified AST.Variable as Var
11import qualified AST.Variable as Var
@@ -17,7 +17,7 @@ they came from.
17type Expr = General.Expr Annotation.Region Def Var.Canonical
17type Expr = General.Expr Annotation.Region Def Var.Canonical
18type Expr' = General.Expr' Annotation.Region Def Var.Canonical
18type Expr' = General.Expr' Annotation.Region Def Var.Canonical
19
19
20−data Def = Definition (Pattern.Pattern Var.Canonical) Expr (Maybe (Type Var.Canonical))
20+data Def = Definition Pattern.CanonicalPattern Expr (Maybe CanonicalType)
21 deriving (Show)
21 deriving (Show)
22
22
23instance Pretty Def where
23instance Pretty Def where
compiler/AST/Expression/Source.hs+3−3
@@ -5,7 +5,7 @@ module AST.Expression.Source where
5import AST.PrettyPrint
5import AST.PrettyPrint
6import Text.PrettyPrint as P
6import Text.PrettyPrint as P
7import qualified AST.Expression.General as General
7import qualified AST.Expression.General as General
8−import AST.Type (Type)
8+import AST.Type (RawType)
9import qualified AST.Variable as Var
9import qualified AST.Variable as Var
10import qualified AST.Annotation as Annotation
10import qualified AST.Annotation as Annotation
11import qualified AST.Pattern as Pattern
11import qualified AST.Pattern as Pattern
@@ -19,8 +19,8 @@ type Expr = General.Expr Annotation.Region Def Var.Raw
19type Expr' = General.Expr' Annotation.Region Def Var.Raw
19type Expr' = General.Expr' Annotation.Region Def Var.Raw
20
20
21data Def
21data Def
22− = Definition (Pattern.Pattern Var.Raw) Expr
22+ = Definition Pattern.RawPattern Expr
23− | TypeAnnotation String (Type Var.Raw)
23+ | TypeAnnotation String RawType
24 deriving (Show)
24 deriving (Show)
25
25
26instance Pretty Def where
26instance Pretty Def where
compiler/AST/Expression/Valid.hs+2−2
@@ -5,7 +5,7 @@ module AST.Expression.Valid where
5import AST.PrettyPrint
5import AST.PrettyPrint
6import Text.PrettyPrint as P
6import Text.PrettyPrint as P
7import qualified AST.Expression.General as General
7import qualified AST.Expression.General as General
8−import AST.Type (Type)
8+import AST.Type (RawType)
9import qualified AST.Variable as Var
9import qualified AST.Variable as Var
10import qualified AST.Annotation as Annotation
10import qualified AST.Annotation as Annotation
11import qualified AST.Pattern as Pattern
11import qualified AST.Pattern as Pattern
@@ -18,7 +18,7 @@ them into a Def that is easier to work with in later phases of compilation.
18type Expr = General.Expr Annotation.Region Def Var.Raw
18type Expr = General.Expr Annotation.Region Def Var.Raw
19type Expr' = General.Expr' Annotation.Region Def Var.Raw
19type Expr' = General.Expr' Annotation.Region Def Var.Raw
20
20
21−data Def = Definition (Pattern.Pattern Var.Raw) Expr (Maybe (Type Var.Raw))
21+data Def = Definition Pattern.RawPattern Expr (Maybe RawType)
22 deriving (Show)
22 deriving (Show)
23
23
24instance Pretty Def where
24instance Pretty Def where
compiler/AST/Module.hs+3−3
@@ -39,9 +39,9 @@ type CanonicalModule = Module [Var.Value] CanonicalBody
39
39
40type Interfaces = Map.Map String Interface
40type Interfaces = Map.Map String Interface
41
41
42−type Types = Map.Map String (Type.Type Var.Canonical)
42+type Types = Map.Map String Type.CanonicalType
43−type ADTs = Map.Map String ( [String], [(String, [Type.Type Var.Canonical])] )
43+type ADTs = Map.Map String ( [String], [(String, [Type.CanonicalType])] )
44−type Aliases = Map.Map String ( [String], Type.Type Var.Canonical )
44+type Aliases = Map.Map String ( [String], Type.CanonicalType )
45
45
46data Interface = Interface
46data Interface = Interface
47 { iVersion :: Version.Version
47 { iVersion :: Version.Version
compiler/AST/Type.hs+7−5
@@ -5,7 +5,6 @@ import Control.Applicative ((<$>), (<*>))
5import Data.Binary
5import Data.Binary
6import qualified Data.Map as Map
6import qualified Data.Map as Map
7
7
8−import AST.Variable (Raw(Raw))
9import qualified AST.Variable as Var
8import qualified AST.Variable as Var
10import AST.PrettyPrint
9import AST.PrettyPrint
11import qualified AST.Helpers as Help
10import qualified AST.Helpers as Help
@@ -19,6 +18,9 @@ data Type var
19 | Aliased Var.Canonical (Type var)
18 | Aliased Var.Canonical (Type var)
20 deriving (Eq,Show)
19 deriving (Eq,Show)
21
20
21+type RawType = Type Var.Raw
22+type CanonicalType = Type Var.Canonical
23+
22fieldMap :: [(String,a)] -> Map.Map String [a]
24fieldMap :: [(String,a)] -> Map.Map String [a]
23fieldMap fields =
25fieldMap fields =
24 foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
26 foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
@@ -26,11 +28,11 @@ fieldMap fields =
26recordOf :: [(String, Type var)] -> Type var
28recordOf :: [(String, Type var)] -> Type var
27recordOf fields = Record fields Nothing
29recordOf fields = Record fields Nothing
28
30
29−listOf :: Type Raw -> Type Raw
31+listOf :: RawType -> RawType
30−listOf t = Data (Raw "_List") [t]
32+listOf t = Data (Var.Raw "_List") [t]
31
33
32−tupleOf :: [Type Raw] -> Type Raw
34+tupleOf :: [RawType] -> RawType
33−tupleOf ts = Data (Raw $ "_Tuple" ++ show (length ts)) ts
35+tupleOf ts = Data (Var.Raw $ "_Tuple" ++ show (length ts)) ts
34
36
35instance (Var.ToString var, Pretty var) => Pretty (Type var) where
37instance (Var.ToString var, Pretty var) => Pretty (Type var) where
36 pretty tipe =
38 pretty tipe =
compiler/Parse/Type.hs+9−11
@@ -9,21 +9,19 @@ import qualified AST.Type as T
9import qualified AST.Variable as Var
9import qualified AST.Variable as Var
10import Parse.Helpers
10import Parse.Helpers
11
11
12−type RawType = T.Type Var.Raw
13−
14−tvar :: IParser RawType
12+tvar :: IParser T.RawType
15tvar = T.Var <$> lowVar <?> "type variable"
13tvar = T.Var <$> lowVar <?> "type variable"
16
14
17−list :: IParser RawType
15+list :: IParser T.RawType
18list = T.listOf <$> braces expr
16list = T.listOf <$> braces expr
19
17
20−tuple :: IParser RawType
18+tuple :: IParser T.RawType
21tuple = do ts <- parens (commaSep expr)
19tuple = do ts <- parens (commaSep expr)
22 return $ case ts of
20 return $ case ts of
23 [t] -> t
21 [t] -> t
24 _ -> T.tupleOf ts
22 _ -> T.tupleOf ts
25
23
26−record :: IParser RawType
24+record :: IParser T.RawType
27record =
25record =
28 do char '{' ; whitespace
26 do char '{' ; whitespace
29 rcrd <- extended <|> normal
27 rcrd <- extended <|> normal
@@ -46,15 +44,15 @@ record =
46capTypeVar :: IParser String
44capTypeVar :: IParser String
47capTypeVar = intercalate "." <$> dotSep1 capVar
45capTypeVar = intercalate "." <$> dotSep1 capVar
48
46
49−constructor0 :: IParser RawType
47+constructor0 :: IParser T.RawType
50constructor0 =
48constructor0 =
51 do name <- capTypeVar
49 do name <- capTypeVar
52 return (T.Data (Var.Raw name) [])
50 return (T.Data (Var.Raw name) [])
53
51
54−term :: IParser RawType
52+term :: IParser T.RawType
55term = list <|> tuple <|> record <|> tvar <|> constructor0
53term = list <|> tuple <|> record <|> tvar <|> constructor0
56
54
57−app :: IParser RawType
55+app :: IParser T.RawType
58app =
56app =
59 do name <- capTypeVar <|> try tupleCtor <?> "type constructor"
57 do name <- capTypeVar <|> try tupleCtor <?> "type constructor"
60 args <- spacePrefix term
58 args <- spacePrefix term
@@ -64,7 +62,7 @@ app =
64 n <- length <$> parens (many (char ','))
62 n <- length <$> parens (many (char ','))
65 return $ "_Tuple" ++ show (if n == 0 then 0 else n+1)
63 return $ "_Tuple" ++ show (if n == 0 then 0 else n+1)
66
64
67−expr :: IParser RawType
65+expr :: IParser T.RawType
68expr =
66expr =
69 do t1 <- app <|> term
67 do t1 <- app <|> term
70 arr <- optionMaybe $ try (whitespace >> arrow)
68 arr <- optionMaybe $ try (whitespace >> arrow)
@@ -72,6 +70,6 @@ expr =
72 Just _ -> T.Lambda t1 <$> (whitespace >> expr)
70 Just _ -> T.Lambda t1 <$> (whitespace >> expr)
73 Nothing -> return t1
71 Nothing -> return t1
74
72
75−constructor :: IParser (String, [RawType])
73+constructor :: IParser (String, [T.RawType])
76constructor = (,) <$> (capTypeVar <?> "another type constructor")
74constructor = (,) <$> (capTypeVar <?> "another type constructor")
77 <*> spacePrefix term
75 <*> spacePrefix term