@@ -0,0 +1,116 @@
2+-- Dump the core functional representation in JSON format for consumption
3+-- by third-party code generators
5+module Language.PureScript.CoreFn.ToJSON
12+import Data.Version (Version, showVersion)
13+import Data.Text (pack)
15+import Language.PureScript.AST.Literals
16+import Language.PureScript.CoreFn
17+import Language.PureScript.Names
19+literalToJSON :: (a -> Value) -> Literal a -> Value
20+literalToJSON _ (NumericLiteral (Left n)) = toJSON ("IntLiteral", n)
21+literalToJSON _ (NumericLiteral (Right n)) = toJSON ("NumberLiteral", n)
22+literalToJSON _ (StringLiteral s) = toJSON ("StringLiteral", s)
23+literalToJSON _ (CharLiteral c) = toJSON ("CharLiteral", c)
24+literalToJSON _ (BooleanLiteral b) = toJSON ("BooleanLiteral", b)
25+literalToJSON t (ArrayLiteral xs) = toJSON ("ArrayLiteral", map t xs)
26+literalToJSON t (ObjectLiteral xs) = toJSON ("ObjectLiteral", recordToJSON t xs)
28+identToJSON :: Ident -> Value
29+identToJSON = toJSON . runIdent
31+properNameToJSON :: ProperName a -> Value
32+properNameToJSON = toJSON . runProperName
34+qualifiedToJSON :: (a -> String) -> Qualified a -> Value
35+qualifiedToJSON f = toJSON . showQualified f
37+moduleNameToJSON :: ModuleName -> Value
38+moduleNameToJSON = toJSON . runModuleName
40+moduleToJSON :: Version -> Module a -> Value
41+moduleToJSON v m = object [ pack "imports" .= map (moduleNameToJSON . snd) (moduleImports m)
42+ , pack "exports" .= map identToJSON (moduleExports m)
43+ , pack "foreign" .= map (identToJSON . fst) (moduleForeign m)
44+ , pack "decls" .= map bindToJSON (moduleDecls m)
45+ , pack "builtWith" .= toJSON (showVersion v)
48+bindToJSON :: Bind a -> Value
49+bindToJSON (NonRec _ n e) = object [ pack (runIdent n) .= exprToJSON e ]
50+bindToJSON (Rec bs) = object $ map (\((_, n), e) -> pack (runIdent n) .= exprToJSON e) bs
52+recordToJSON :: (a -> Value) -> [(String, a)] -> Value
53+recordToJSON f = object . map (\(label, a) -> pack label .= f a)
55+exprToJSON :: Expr a -> Value
56+exprToJSON (Var _ i) = toJSON ( "Var"
57+ , qualifiedToJSON runIdent i
59+exprToJSON (Literal _ l) = toJSON ( "Literal"
60+ , literalToJSON (exprToJSON) l
62+exprToJSON (Constructor _ d c is) = toJSON ( "Constructor"
67+exprToJSON (Accessor _ f r) = toJSON ( "Accessor"
71+exprToJSON (ObjectUpdate _ r fs) = toJSON ( "ObjectUpdate"
73+ , recordToJSON exprToJSON fs
75+exprToJSON (Abs _ p b) = toJSON ( "Abs"
79+exprToJSON (App _ f x) = toJSON ( "App"
83+exprToJSON (Case _ ss cs) = toJSON ( "Case"
85+ , map caseAlternativeToJSON cs
87+exprToJSON (Let _ bs e) = toJSON ( "Let"
92+caseAlternativeToJSON :: CaseAlternative a -> Value
93+caseAlternativeToJSON (CaseAlternative bs r') =
94+ toJSON [ toJSON (map binderToJSON bs)
96+ Left rs -> toJSON $ map (\(g, e) -> (exprToJSON g, exprToJSON e)) rs
97+ Right r -> exprToJSON r
100+binderToJSON :: Binder a -> Value
101+binderToJSON (VarBinder _ v) = toJSON ( "VarBinder"
104+binderToJSON (NullBinder _) = toJSON "NullBinder"
105+binderToJSON (LiteralBinder _ l) = toJSON ( "LiteralBinder"
106+ , literalToJSON binderToJSON l
108+binderToJSON (ConstructorBinder _ d c bs) = toJSON ( "ConstructorBinder"
109+ , qualifiedToJSON runProperName d
110+ , qualifiedToJSON runProperName c
111+ , map binderToJSON bs
113+binderToJSON (NamedBinder _ n b) = toJSON ( "NamedBinder"