1{-# LANGUAGE OverloadedStrings #-}
2module Elm.Compiler.Type
7import Control.Applicative ((<$>), (<*>))
8import Control.Arrow (second)
9import Data.Aeson ((.:), (.=))
10import qualified Data.Aeson as Json
11import qualified Data.ByteString.Lazy.Char8 as BS
12import qualified Data.List as List
13import qualified Data.Text as Text
15import qualified AST.Helpers as Help
16import Elm.Utils ((|>))
24 | Record [(String, Type)] (Maybe Type)
29data Context = None | ADT | Function
32toString :: Type -> String
34 toStringHelp None tipe
37toStringHelp :: Context -> Type -> String
38toStringHelp context tipe =
41 let string = toStringHelp Function t1 ++ " -> " ++ toString t2
52 | Help.isTuple name ->
54 |> List.intercalate ", "
58 let string = name ++ spacePrefix (map (toStringHelp ADT) args)
60 case (context, args) of
61 (ADT, _ : _) -> parens string
64 Record fields maybeExtension ->
65 let viewField (key, value) =
66 key ++ " : " ++ toString value
68 viewExtension maybeType =
71 Just t -> toString t ++ " | "
75 [ viewExtension maybeExtension
76 , map viewField fields
77 |> List.intercalate ", "
81parens :: String -> String
83 sandwich "(" ")" string
86spacePrefix :: [String] -> String
88 concatMap (" " ++) strings
91sandwich :: String -> String -> String -> String
92sandwich start stop string =
93 start ++ string ++ stop
98instance Json.ToJSON Type where
100 Json.object (getFields tipe)
105 [ "tag" .= ("lambda" :: Text.Text)
106 , "in" .= Json.toJSON t1
107 , "out" .= Json.toJSON t2
111 [ "tag" .= ("var" :: Text.Text)
112 , "name" .= Json.toJSON x
116 [ "tag" .= ("type" :: Text.Text)
117 , "name" .= Json.toJSON name
121 [ "tag" .= ("app" :: Text.Text)
122 , "func" .= Json.toJSON t
123 , "args" .= Json.toJSON ts
127 [ "tag" .= ("record" :: Text.Text)
128 , "fields" .= Json.toJSON (map (Json.toJSON . second Json.toJSON) fields)
129 , "extension" .= Json.toJSON ext
133instance Json.FromJSON Type where
134 parseJSON (Json.Object obj) =
135 do tag <- obj .: "tag"
136 case (tag :: String) of
138 Lambda <$> obj .: "in" <*> obj .: "out"
141 Var <$> obj .: "name"
144 Type <$> obj .: "name"
147 App <$> obj .: "func" <*> obj .: "args"
150 Record <$> obj .: "fields" <*> obj .: "extension"
153 fail $ "Error when decoding type with tag: " ++ tag
157 fail $ "Cannot decode Value from: " ++ BS.unpack (Json.encode value)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list