on master help sign in
Get rid of unused file
commit2f8b048a7b455bfe7a2884929c66fc776f413823 authorEvan Czaplicki <info@elm-lang.org> dateSat 12 Apr 2014, 13:38 (13 years ago) parent368e33e
5 files changed−494
compiler/Language/Elm/DevTools.hs−46 compiler/SourceSyntax/Expression.hs−229 compiler/SourceSyntax/Module.hs−119 compiler/SourceSyntax/Type.hs−89 compiler/SourceSyntax/Variable.hs−11
compiler/Language/Elm/DevTools.hsdeleted−46
@@ -1,46 +0,0 @@
1−{- | This module aims to make it easier to create dev tools for Elm.
2−
3− It provides the ability to:
4−
5− * Detect problems in a program
6− * Figure out types of particular expressions
7− * Canonicalize variables
8− * Convert expressions to JS and HTML
9−
10− This module gives limeted access to the structure of an Elm Program.
11− Giving full access would expose many internal details that are likely to
12− change as Elm progresses. These details have been hidden to avoid breaking
13− changes.
14−-}
15−module Language.Elm.DevTools where
16−
17−data Program
18−
19−-- | Parse an Elm program into an AST.
20−parse :: String -> Either [Problem] Program
21−
22−-- | Get the type of a region of the program. Lets you look
23−-- up the type of particular values and expressions.
24−typeOf :: Program -> Region -> Either [Problem] String
25−
26−-- | Canonicalize a variable. So in a normal module `map` would be canonicalized
27−-- to `List.map`.
28−canonicalize :: Program -> Region -> [String]
29−
30−-- | Any problem that may come up while parsing or typing a program.
31−-- All problems come with a region that is causing the problem.
32−data Problem = Problem { message :: String, region :: Region }
33−
34−type Row = Int
35−type Column = Int
36−
37−-- | Represents a region, spanning two points in the source code.
38−data Region = Region { start :: (Row,Column), end :: (Row,Column) }
39−
40−-- | Takes a specific point in the source code, such as the cursor position.
41−-- It then determines the region that best represents that point. When the
42−-- cursor is within a variable, it chooses the variable. When the cursor is
43−-- on parentheses, it selects the region within the parentheses. When the
44−-- cursor is on keywords like let, it chooses the whole let-expression.
45−region :: (Row,Column) -> Region
46−region pos = Region pos pos
compiler/SourceSyntax/Expression.hsdeleted−229
@@ -1,229 +0,0 @@
1−{-# OPTIONS_GHC -Wall #-}
2−
3−{-| The Abstract Syntax Tree (AST) for expressions comes in a couple formats.
4−The first is the fully general version and is labeled with a prime (Expr').
5−The others are specialized versions of the AST that represent specific phases
6−of the compilation process. I expect there to be more phases as we begin to
7−enrich the AST with more information.
8−-}
9−module SourceSyntax.Expression where
10−
11−import SourceSyntax.PrettyPrint
12−import Text.PrettyPrint as P
13−import qualified SourceSyntax.Helpers as Help
14−import qualified SourceSyntax.Annotation as Annotation
15−import qualified SourceSyntax.Pattern as Pattern
16−import qualified SourceSyntax.Type as SrcType
17−import qualified SourceSyntax.Literal as Literal
18−import qualified SourceSyntax.Variable as Variable
19−
20−---- GENERAL AST ----
21−
22−{-| This is a fully general Abstract Syntax Tree (AST) for expressions. It has
23−"type holes" that allow us to enrich the AST with additional information as we
24−move through the compilation process. The type holes are used to represent:
25−
26− ann: Annotations for arbitrary expressions. Allows you to add information
27− to the AST like position in source code or inferred types.
28−
29− def: Definition style. The source syntax separates type annotations and
30− definitions, but after parsing we check that they are well formed and
31− collapse them.
32−
33− var: Representation of variables. Starts as strings, but is later enriched
34− with information about what module a variable came from.
35−
36−-}
37−type GeneralExpr annotation definition variable =
38− Annotation.Annotated annotation (GeneralExpr' annotation definition variable)
39−
40−data GeneralExpr' ann def var
41− = Literal Literal.Literal
42− | Var var
43− | Range (GeneralExpr ann def var) (GeneralExpr ann def var)
44− | ExplicitList [GeneralExpr ann def var]
45− | Binop String (GeneralExpr ann def var) (GeneralExpr ann def var)
46− | Lambda Pattern.Pattern (GeneralExpr ann def var)
47− | App (GeneralExpr ann def var) (GeneralExpr ann def var)
48− | MultiIf [(GeneralExpr ann def var,GeneralExpr ann def var)]
49− | Let [def] (GeneralExpr ann def var)
50− | Case (GeneralExpr ann def var) [(Pattern.Pattern, GeneralExpr ann def var)]
51− | Data String [GeneralExpr ann def var]
52− | Access (GeneralExpr ann def var) String
53− | Remove (GeneralExpr ann def var) String
54− | Insert (GeneralExpr ann def var) String (GeneralExpr ann def var)
55− | Modify (GeneralExpr ann def var) [(String, GeneralExpr ann def var)]
56− | Record [(String, GeneralExpr ann def var)]
57− | Markdown String String [GeneralExpr ann def var]
58− -- for type checking and code gen only
59− | PortIn String SrcType.Type
60− | PortOut String SrcType.Type (GeneralExpr ann def var)
61− deriving (Show)
62−
63−
64−---- SPECIALIZED ASTs ----
65−
66−{-| Expressions created by the parser. These use a split representation of type
67−annotations and definitions, which is how they appear in source code and how
68−they are parsed.
69−-}
70−type ParseExpr = GeneralExpr Annotation.Region ParseDef Variable.Raw
71−type ParseExpr' = GeneralExpr' Annotation.Region ParseDef Variable.Raw
72−
73−data ParseDef
74− = Def Pattern.Pattern ParseExpr
75− | TypeAnnotation String SrcType.Type
76− deriving (Show)
77−
78−{-| "Normal" expressions. When the compiler checks that type annotations and
79−ports are all paired with definitions in the appropriate order, it collapses
80−them into a Def that is easier to work with in later phases of compilation.
81−-}
82−type Expr = GeneralExpr Annotation.Region Def Variable.Raw
83−type Expr' = GeneralExpr' Annotation.Region Def Variable.Raw
84−
85−data Def = Definition Pattern.Pattern Expr (Maybe SrcType.Type)
86− deriving (Show)
87−
88−
89−
90−---- UTILITIES ----
91−
92−rawVar :: String -> GeneralExpr' ann def Variable.Raw
93−rawVar x = Var (Variable.Raw x)
94−
95−tuple :: [GeneralExpr ann def var] -> GeneralExpr' ann def var
96−tuple es = Data ("_Tuple" ++ show (length es)) es
97−
98−delist :: GeneralExpr ann def var -> [GeneralExpr ann def var]
99−delist (Annotation.A _ (Data "::" [h,t])) = h : delist t
100−delist _ = []
101−
102−saveEnvName :: String
103−saveEnvName = "_save_the_environment!!!"
104−
105−dummyLet :: (Pretty def) => [def] -> GeneralExpr Annotation.Region def Variable.Raw
106−dummyLet defs =
107− Annotation.none $ Let defs (Annotation.none $ rawVar saveEnvName)
108−
109−instance (Pretty def, Pretty var) => Pretty (GeneralExpr' ann def var) where
110− pretty expr =
111− case expr of
112− Literal lit -> pretty lit
113−
114− Var x -> pretty x
115−
116− Range e1 e2 -> P.brackets (pretty e1 <> P.text ".." <> pretty e2)
117−
118− ExplicitList es -> P.brackets (commaCat (map pretty es))
119−
120− Binop "-" (Annotation.A _ (Literal (Literal.IntNum 0))) e ->
121− P.text "-" <> prettyParens e
122−
123− Binop op e1 e2 -> P.sep [ prettyParens e1 <+> P.text op', prettyParens e2 ]
124− where
125− op' = if Help.isOp op then op else "`" ++ op ++ "`"
126−
127− Lambda p e -> P.text "\\" <> args <+> P.text "->" <+> pretty body
128− where
129− (ps,body) = collectLambdas (Annotation.A undefined $ Lambda p e)
130− args = P.sep (map Pattern.prettyParens ps)
131−
132− App _ _ -> P.hang func 2 (P.sep args)
133− where
134− func:args = map prettyParens (collectApps (Annotation.A undefined expr))
135−
136− MultiIf branches -> P.text "if" $$ nest 3 (vcat $ map iff branches)
137− where
138− iff (b,e) = P.text "|" <+> P.hang (pretty b <+> P.text "->") 2 (pretty e)
139−
140− Let defs e ->
141− P.sep [ P.hang (P.text "let") 4 (P.vcat (map pretty defs))
142− , P.text "in" <+> pretty e ]
143−
144− Case e pats ->
145− P.hang pexpr 2 (P.vcat (map pretty' pats))
146− where
147− pexpr = P.sep [ P.text "case" <+> pretty e, P.text "of" ]
148− pretty' (p,b) = pretty p <+> P.text "->" <+> pretty b
149−
150− Data "::" [hd,tl] -> pretty hd <+> P.text "::" <+> pretty tl
151− Data "[]" [] -> P.text "[]"
152− Data name es
153− | Help.isTuple name -> P.parens (commaCat (map pretty es))
154− | otherwise -> P.hang (P.text name) 2 (P.sep (map prettyParens es))
155−
156− Access e x -> prettyParens e <> P.text "." <> variable x
157−
158− Remove e x -> P.braces (pretty e <+> P.text "-" <+> variable x)
159−
160− Insert (Annotation.A _ (Remove e y)) x v ->
161− P.braces $ P.hsep [ pretty e, P.text "-", variable y, P.text "|"
162− , variable x, P.equals, pretty v ]
163−
164− Insert e x v ->
165− P.braces (pretty e <+> P.text "|" <+> variable x <+> P.equals <+> pretty v)
166−
167− Modify e fs ->
168− P.braces $ P.hang (pretty e <+> P.text "|")
169− 4
170− (commaSep $ map field fs)
171− where
172− field (k,v) = variable k <+> P.text "<-" <+> pretty v
173−
174− Record fs ->
175− P.braces $ P.nest 2 (commaSep $ map field fs)
176− where
177− field (x,e) = variable x <+> P.equals <+> pretty e
178−
179− Markdown _ _ _ -> P.text "[markdown| ... |]"
180−
181− PortIn name _ -> P.text $ "<port:" ++ name ++ ">"
182−
183− PortOut _ _ signal -> pretty signal
184−
185−instance Pretty ParseDef where
186− pretty def =
187− case def of
188− TypeAnnotation name tipe ->
189− variable name <+> P.colon <+> pretty tipe
190− Def pattern expr ->
191− pretty pattern <+> P.equals <+> pretty expr
192−
193−instance Pretty Def where
194− pretty (Definition pattern expr maybeTipe) =
195− P.vcat [ annotation, definition ]
196− where
197− definition = pretty pattern <+> P.equals <+> pretty expr
198− annotation = case maybeTipe of
199− Nothing -> P.empty
200− Just tipe -> pretty pattern <+> P.colon <+> pretty tipe
201−
202−collectApps :: GeneralExpr ann def var -> [GeneralExpr ann def var]
203−collectApps annExpr@(Annotation.A _ expr) =
204− case expr of
205− App a b -> collectApps a ++ [b]
206− _ -> [annExpr]
207−
208−collectLambdas :: GeneralExpr ann def var -> ([Pattern.Pattern], GeneralExpr ann def var)
209−collectLambdas lexpr@(Annotation.A _ expr) =
210− case expr of
211− Lambda pattern body ->
212− let (ps, body') = collectLambdas body
213− in (pattern : ps, body')
214−
215− _ -> ([], lexpr)
216−
217−prettyParens :: (Pretty def, Pretty var) => GeneralExpr ann def var -> Doc
218−prettyParens (Annotation.A _ expr) = parensIf needed (pretty expr)
219− where
220− needed =
221− case expr of
222− Binop _ _ _ -> True
223− Lambda _ _ -> True
224− App _ _ -> True
225− MultiIf _ -> True
226− Let _ _ -> True
227− Case _ _ -> True
228− Data name (_:_) -> name /= "::"
229− _ -> False
compiler/SourceSyntax/Module.hsdeleted−119
@@ -1,119 +0,0 @@
1−{-# OPTIONS_GHC -W #-}
2−module SourceSyntax.Module where
3−
4−import Data.Binary
5−import qualified Data.List as List
6−import qualified Data.Map as Map
7−import Control.Applicative ((<$>), (<*>))
8−import Text.PrettyPrint as P
9−
10−import SourceSyntax.Expression (Expr)
11−import SourceSyntax.Declaration
12−import SourceSyntax.PrettyPrint
13−import SourceSyntax.Type
14−
15−import qualified Elm.Internal.Version as Version
16−
17−data Module def =
18− Module [String] Exports Imports [def]
19− deriving (Show)
20−
21−type Exports = [String]
22−
23−type Imports = [(String, ImportMethod)]
24−data ImportMethod = As String | Importing [String] | Hiding [String]
25− deriving (Eq, Ord, Show)
26−
27−instance (Pretty def) => Pretty (Module def) where
28− pretty (Module modNames exports imports decls) =
29− P.vcat [modul, P.text "", prettyImports, P.text "", prettyDecls]
30− where
31− prettyDecls = P.sep $ map pretty decls
32−
33− modul = P.text "module" <+> moduleName <+> prettyExports <+> P.text "where"
34− moduleName = P.text $ List.intercalate "." modNames
35− prettyExports =
36− case exports of
37− [] -> P.empty
38− _ -> P.parens . commaCat $ map P.text exports
39−
40− prettyImports = P.vcat $ map prettyImport imports
41−
42− prettyImport (name, method) =
43− P.text "import" <+>
44− case method of
45− As alias ->
46− P.text $ name ++ (if name == alias then "" else " as " ++ alias)
47−
48− Importing values ->
49− P.text name <+> P.parens (commaCat (map P.text values))
50−
51− Hiding [] -> P.text ("open " ++ name)
52− Hiding _ -> error "invalid import declaration"
53−
54−instance Binary ImportMethod where
55− put method =
56− let put' n info = putWord8 n >> put info in
57− case method of
58− As s -> put' 0 s
59− Importing ss -> put' 1 ss
60− Hiding ss -> put' 2 ss
61−
62− get = do tag <- getWord8
63− case tag of
64− 0 -> As <$> get
65− 1 -> Importing <$> get
66− 2 -> Hiding <$> get
67− _ -> error "Error reading valid ImportMethod type from serialized string"
68−
69−data MetadataModule =
70− MetadataModule
71− { names :: [String]
72− , path :: FilePath
73− , exports :: [String]
74− , imports :: [(String, ImportMethod)]
75− , program :: Expr
76− , types :: Map.Map String Type
77− , fixities :: [(Assoc, Int, String)]
78− , aliases :: [Alias]
79− , datatypes :: [ADT]
80− , ports :: [String]
81− } deriving Show
82−
83−type Interfaces = Map.Map String ModuleInterface
84−type ADT = (String, [String], [(String,[Type])])
85−type Alias = (String, [String], Type)
86−
87−data ModuleInterface =
88− ModuleInterface
89− { iVersion :: Version.Version
90− , iTypes :: Map.Map String Type
91− , iImports :: [(String, ImportMethod)]
92− , iAdts :: [ADT]
93− , iAliases :: [Alias]
94− , iFixities :: [(Assoc, Int, String)]
95− , iPorts :: [String]
96− } deriving Show
97−
98−metaToInterface :: MetadataModule -> ModuleInterface
99−metaToInterface metaModule =
100− ModuleInterface
101− { iVersion = Version.elmVersion
102− , iTypes = types metaModule
103− , iImports = imports metaModule
104− , iAdts = datatypes metaModule
105− , iAliases = aliases metaModule
106− , iFixities = fixities metaModule
107− , iPorts = ports metaModule
108− }
109−
110−instance Binary ModuleInterface where
111− get = ModuleInterface <$> get <*> get <*> get <*> get <*> get <*> get <*> get
112− put modul = do
113− put (iVersion modul)
114− put (iTypes modul)
115− put (iImports modul)
116− put (iAdts modul)
117− put (iAliases modul)
118− put (iFixities modul)
119− put (iPorts modul)
compiler/SourceSyntax/Type.hsdeleted−89
@@ -1,89 +0,0 @@
1−{-# OPTIONS_GHC -W #-}
2−module SourceSyntax.Type where
3−
4−import Control.Applicative ((<$>), (<*>))
5−import Data.Binary
6−import qualified Data.Map as Map
7−import SourceSyntax.PrettyPrint
8−import qualified SourceSyntax.Helpers as Help
9−import Text.PrettyPrint as P
10−
11−data Type = Lambda Type Type
12− | Var String
13− | Data String [Type]
14− | Record [(String,Type)] (Maybe String)
15− deriving (Eq,Show)
16−
17−fieldMap :: [(String,a)] -> Map.Map String [a]
18−fieldMap fields =
19− foldl (\r (x,t) -> Map.insertWith (++) x [t] r) Map.empty fields
20−
21−recordOf :: [(String,Type)] -> Type
22−recordOf fields = Record fields Nothing
23−
24−listOf :: Type -> Type
25−listOf t = Data "_List" [t]
26−
27−tupleOf :: [Type] -> Type
28−tupleOf ts = Data ("_Tuple" ++ show (length ts)) ts
29−
30−instance Pretty Type where
31− pretty tipe =
32− case tipe of
33− Lambda _ _ -> P.sep [ t, P.sep (map (P.text "->" <+>) ts) ]
34− where
35− t:ts = map prettyLambda (collectLambdas tipe)
36− prettyLambda t = case t of
37− Lambda _ _ -> P.parens (pretty t)
38− _ -> pretty t
39−
40− Var x -> P.text x
41− Data "_List" [t] -> P.brackets (pretty t)
42− Data name tipes
43− | Help.isTuple name -> P.parens . P.sep . P.punctuate P.comma $ map pretty tipes
44− | otherwise -> P.hang (P.text name) 2 (P.sep $ map prettyParens tipes)
45− Record fields ext ->
46− P.braces $ case ext of
47− Nothing -> prettyFields
48− Just x -> P.hang (P.text x <+> P.text "|") 4 prettyFields
49− where
50− prettyField (f,t) = P.text f <+> P.text ":" <+> pretty t
51− prettyFields = commaSep . map prettyField $ fields
52−
53−collectLambdas :: Type -> [Type]
54−collectLambdas tipe =
55− case tipe of
56− Lambda arg body -> arg : collectLambdas body
57− _ -> [tipe]
58−
59−prettyParens :: Type -> Doc
60−prettyParens tipe = parensIf needed (pretty tipe)
61− where
62− needed =
63− case tipe of
64− Lambda _ _ -> True
65− Data "_List" [_] -> False
66− Data _ [] -> False
67− Data _ _ -> True
68− _ -> False
69−
70−instance Binary Type where
71− put tipe =
72− case tipe of
73− Lambda t1 t2 ->
74− putWord8 0 >> put t1 >> put t2
75− Var x ->
76− putWord8 1 >> put x
77− Data ctor tipes ->
78− putWord8 2 >> put ctor >> put tipes
79− Record fs ext ->
80− putWord8 3 >> put fs >> put ext
81−
82− get = do
83− n <- getWord8
84− case n of
85− 0 -> Lambda <$> get <*> get
86− 1 -> Var <$> get
87− 2 -> Data <$> get <*> get
88− 3 -> Record <$> get <*> get
89− _ -> error "Error reading a valid type from serialized string"
compiler/SourceSyntax/Variable.hsdeleted−11
@@ -1,11 +0,0 @@
1−
2−module SourceSyntax.Variable where
3−
4−import qualified Text.PrettyPrint as P
5−import SourceSyntax.PrettyPrint
6−
7−newtype Raw = Raw String
8− deriving (Eq,Ord,Show)
9−
10−instance Pretty Raw where
11− pretty (Raw var) = variable var