@@ -8,11 +8,17 @@ import Control.Applicative ((<$>), (<*>))
8import Control.Arrow (second)
8import Control.Arrow (second)
9import Data.Aeson ((.:), (.=))
9import Data.Aeson ((.:), (.=))
10import qualified Data.Aeson as Json
10import qualified Data.Aeson as Json
11+import qualified Data.Aeson.Types as Json
11import qualified Data.ByteString.Lazy.Char8 as BS
12import qualified Data.ByteString.Lazy.Char8 as BS
12import qualified Data.Text as Text
13import qualified Data.Text as Text
13import Text.PrettyPrint as P
14import Text.PrettyPrint as P
15import qualified AST.Helpers as Help
16import qualified AST.Helpers as Help
17+import qualified AST.Type as Type
18+import qualified AST.Variable as Var
19+import qualified Parse.Helpers as Parse
20+import qualified Parse.Type as Type
21+import qualified Reporting.Annotation as A
@@ -122,62 +128,68 @@ flattenRecord tipe =
122-- JSON for TYPE
128-- JSON for TYPE
124instance Json.ToJSON Type where
130instance Json.ToJSON Type where
125− toJSON tipe =
131+ toJSON tipe =
126− Json.object (getFields tipe)
132+ Json.String (Text.pack (toString tipe))
131− [ "tag" .= ("lambda" :: Text.Text)
132− , "in" .= Json.toJSON t1
133− , "out" .= Json.toJSON t2
137− [ "tag" .= ("var" :: Text.Text)
138− , "name" .= Json.toJSON x
141− Type name ->
135+instance Json.FromJSON Type where
142− [ "tag" .= ("type" :: Text.Text)
136+ parseJSON value =
143− , "name" .= Json.toJSON name
137+ let
140+ "Trying to decode a type string, but could not handle this value:\n"
141+ ++ BS.unpack (Json.encode value)
145+ either failure (return . fromRawType)
146+ (Parse.iParse Type.expr (Text.unpack text))
146− App t ts ->
148+ Json.Object obj ->
147− [ "tag" .= ("app" :: Text.Text)
149+ fromObject obj
148− , "func" .= Json.toJSON t
149− , "args" .= Json.toJSON ts
152− Record fields ext ->
151+ _ ->
153− [ "tag" .= ("record" :: Text.Text)
152+ failure ()
154− , "fields" .= Json.toJSON (map (Json.toJSON . second Json.toJSON) fields)
155− , "extension" .= Json.toJSON ext
159−instance Json.FromJSON Type where
155+fromObject :: Json.Object -> Json.Parser Type
160− parseJSON (Json.Object obj) =
156+fromObject obj =
161− do tag <- obj .: "tag"
157+ do tag <- obj .: "tag"
162− case (tag :: String) of
158+ case (tag :: String) of
163− "lambda" ->
159+ "lambda" ->
164− Lambda <$> obj .: "in" <*> obj .: "out"
160+ Lambda <$> obj .: "in" <*> obj .: "out"
163+ Var <$> obj .: "name"
166+ Type <$> obj .: "name"
169+ App <$> obj .: "func" <*> obj .: "args"
172+ Record <$> obj .: "fields" <*> obj .: "extension"
175+ fail $ "Error when decoding type with tag: " ++ tag
167− Var <$> obj .: "name"
169− "type" ->
178+fromRawType :: Type.Raw -> Type
170− Type <$> obj .: "name"
179+fromRawType (A.A _ astType) =
181+ Type.RLambda t1 t2 ->
182+ Lambda (fromRawType t1) (fromRawType t2)
172− "app" ->
184+ Type.RVar x ->
173− App <$> obj .: "func" <*> obj .: "args"
185+ Var x
175− "record" ->
187+ Type.RType var ->
176− Record <$> obj .: "fields" <*> obj .: "extension"
188+ Type (Var.toString var)
178− _ ->
190+ Type.RApp t ts ->
179− fail $ "Error when decoding type with tag: " ++ tag
191+ App (fromRawType t) (map fromRawType ts)
193+ Type.RRecord fields ext ->
194+ Record (map (second fromRawType) fields) (fmap fromRawType ext)
183− fail $ "Cannot decode Value from: " ++ BS.unpack (Json.encode value)