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
51 App (Type "List") [t] ->
52 sandwich "[" "]" (toString t)
55 | Help.isTuple name ->
57 |> List.intercalate ", "
61 let string = name ++ spacePrefix (map (toStringHelp ADT) args)
63 case (context, args) of
64 (ADT, _ : _) -> parens string
67 Record fields maybeExtension ->
68 let viewField (key, value) =
69 key ++ " : " ++ toString value
71 viewExtension maybeType =
74 Just t -> toString t ++ " | "
78 [ viewExtension maybeExtension
79 , map viewField fields
80 |> List.intercalate ", "
84parens :: String -> String
86 sandwich "(" ")" string
89spacePrefix :: [String] -> String
91 concatMap (" " ++) strings
94sandwich :: String -> String -> String -> String
95sandwich start stop string =
96 start ++ string ++ stop
101instance Json.ToJSON Type where
103 Json.object (getFields tipe)
108 [ "tag" .= ("lambda" :: Text.Text)
109 , "in" .= Json.toJSON t1
110 , "out" .= Json.toJSON t2
114 [ "tag" .= ("var" :: Text.Text)
115 , "name" .= Json.toJSON x
119 [ "tag" .= ("type" :: Text.Text)
120 , "name" .= Json.toJSON name
124 [ "tag" .= ("app" :: Text.Text)
125 , "func" .= Json.toJSON t
126 , "args" .= Json.toJSON ts
130 [ "tag" .= ("record" :: Text.Text)
131 , "fields" .= Json.toJSON (map (Json.toJSON . second Json.toJSON) fields)
132 , "extension" .= Json.toJSON ext
136instance Json.FromJSON Type where
137 parseJSON (Json.Object obj) =
138 do tag <- obj .: "tag"
139 case (tag :: String) of
141 Lambda <$> obj .: "in" <*> obj .: "out"
144 Var <$> obj .: "name"
147 Type <$> obj .: "name"
150 App <$> obj .: "func" <*> obj .: "args"
153 Record <$> obj .: "fields" <*> obj .: "extension"
156 fail $ "Error when decoding type with tag: " ++ tag
160 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