summaryrefslogtreecommitdiff
path: root/Language.hs
diff options
context:
space:
mode:
Diffstat (limited to 'Language.hs')
-rw-r--r--Language.hs56
1 files changed, 38 insertions, 18 deletions
diff --git a/Language.hs b/Language.hs
index 1950c6e..d4cd1b6 100644
--- a/Language.hs
+++ b/Language.hs
@@ -6,6 +6,8 @@ import Data.Aeson (ToJSON(..),FromJSON(..))
6import Control.Monad.Writer 6import Control.Monad.Writer
7import Data.String 7import Data.String
8import Data.List 8import Data.List
9import Data.Map (Map)
10import qualified Data.Map as Map
9 11
10instance IsString Type where 12instance IsString Type where
11 fromString a = Data a 13 fromString a = Data a
@@ -14,6 +16,7 @@ data DataDef
14 = DataDef 16 = DataDef
15 { dataName :: String 17 { dataName :: String
16 , constructors :: [ConstructorDef] 18 , constructors :: [ConstructorDef]
19 , instances :: [Instance]
17 } 20 }
18 | TypeAlias 21 | TypeAlias
19 { aliasName :: String 22 { aliasName :: String
@@ -35,6 +38,11 @@ data Field
35 } 38 }
36 deriving (Show,Generic) 39 deriving (Show,Generic)
37 40
41data Instance
42 = Show
43 | Eq
44 deriving (Show,Generic)
45
38data Type 46data Type
39 = Int 47 = Int
40 | Int32 48 | Int32
@@ -60,8 +68,14 @@ parens a
60 | 1 == length (words a) = a 68 | 1 == length (words a) = a
61 | otherwise = "(" ++ a ++ ")" 69 | otherwise = "(" ++ a ++ ")"
62 70
63psType :: Type -> String 71type AliasMap = Map String Type
64psType = \case 72
73normalize :: AliasMap -> Type -> Type
74normalize aliasMap t@(Data n) = Map.findWithDefault t n aliasMap
75normalize _ t = t
76
77psType :: AliasMap -> Type -> String
78psType aliasMap = \case
65 Int -> "Int" 79 Int -> "Int"
66 Int32 -> "Int32" 80 Int32 -> "Int32"
67 Word -> "Word" 81 Word -> "Word"
@@ -94,17 +108,18 @@ psType = \case
94 V4 (V3 Float) -> "M34F" 108 V4 (V3 Float) -> "M34F"
95 V4 (V4 Float) -> "M44F" 109 V4 (V4 Float) -> "M44F"
96 110
97 Array t -> "Array " ++ parens (hsType t) 111 Array t -> "Array " ++ parens (psType aliasMap t)
98 List t -> "List " ++ parens (hsType t) 112 List t -> "List " ++ parens (psType aliasMap t)
99 Maybe t -> "Maybe " ++ parens (hsType t) 113 Maybe t -> "Maybe " ++ parens (psType aliasMap t)
100 Map String v -> "StrMap " ++ parens (hsType v) 114 Map k v
101 Map k v -> "Map " ++ parens (hsType k) ++ " " ++ parens (hsType v) 115 | String <- normalize aliasMap k -> "StrMap " ++ parens (psType aliasMap v)
116 | otherwise -> "Map " ++ parens (psType aliasMap k) ++ " " ++ parens (psType aliasMap v)
102 -- user defined 117 -- user defined
103 Data t -> t 118 Data t -> t
104 x -> error $ "unknown type: " ++ show x 119 x -> error $ "unknown type: " ++ show x
105 120
106hsType :: Type -> String 121hsType :: AliasMap -> Type -> String
107hsType = \case 122hsType aliasMap = \case
108 Int -> "Int" 123 Int -> "Int"
109 Int32 -> "Int32" 124 Int32 -> "Int32"
110 Word -> "Word" 125 Word -> "Word"
@@ -137,10 +152,10 @@ hsType = \case
137 V4 (V3 Float) -> "M34F" 152 V4 (V3 Float) -> "M34F"
138 V4 (V4 Float) -> "M44F" 153 V4 (V4 Float) -> "M44F"
139 154
140 Array t -> "[" ++ hsType t ++ "]" 155 Array t -> "[" ++ hsType aliasMap t ++ "]"
141 List t -> "[" ++ hsType t ++ "]" 156 List t -> "[" ++ hsType aliasMap t ++ "]"
142 Maybe t -> "Maybe " ++ parens (hsType t) 157 Maybe t -> "Maybe " ++ parens (hsType aliasMap t)
143 Map k v -> "Map " ++ parens (hsType k) ++ " " ++ parens (hsType v) 158 Map k v -> "Map " ++ parens (hsType aliasMap k) ++ " " ++ parens (hsType aliasMap v)
144 -- user defined 159 -- user defined
145 Data t -> t 160 Data t -> t
146 x -> error $ "unknown type: " ++ show x 161 x -> error $ "unknown type: " ++ show x
@@ -154,19 +169,21 @@ constType = head . words . show
154 169
155instance ToJSON ConstructorDef 170instance ToJSON ConstructorDef
156instance ToJSON DataDef 171instance ToJSON DataDef
172instance ToJSON Instance
157instance ToJSON Field 173instance ToJSON Field
158instance ToJSON Type 174instance ToJSON Type
159 175
160instance FromJSON ConstructorDef 176instance FromJSON ConstructorDef
161instance FromJSON DataDef 177instance FromJSON DataDef
178instance FromJSON Instance
162instance FromJSON Field 179instance FromJSON Field
163instance FromJSON Type 180instance FromJSON Type
164 181
165type DDef = Writer [DataDef] 182type DDef = Writer [DataDef]
166type CDef = Writer [ConstructorDef] 183type CDef = Writer ([ConstructorDef],[Instance])
167 184
168data_ :: forall a . String -> CDef () -> DDef () 185data_ :: forall a . String -> CDef () -> DDef ()
169data_ n l = tell [DataDef n $ execWriter l] 186data_ n l = tell [let (c,i) = execWriter l in DataDef n c i]
170 187
171alias_ :: String -> Type -> DDef () 188alias_ :: String -> Type -> DDef ()
172alias_ n t = tell [TypeAlias n t] 189alias_ n t = tell [TypeAlias n t]
@@ -182,14 +199,17 @@ instance IsField Field where
182instance IsField Type where 199instance IsField Type where
183 toField a = Field "" a 200 toField a = Field "" a
184 201
202deriving_ :: [Instance] -> CDef ()
203deriving_ l = tell (mempty,l)
204
185const_ :: String -> [Type] -> CDef () 205const_ :: String -> [Type] -> CDef ()
186const_ n t = tell [ConstructorDef n [Field a b | Field a b <- map toField t]] 206const_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty)
187 207
188constR_ :: String -> [Field] -> CDef () 208constR_ :: String -> [Field] -> CDef ()
189constR_ n t = tell [ConstructorDef n [Field a b | Field a b <- map toField t]] 209constR_ n t = tell ([ConstructorDef n [Field a b | Field a b <- map toField t]],mempty)
190 210
191enum_ :: String -> CDef () 211enum_ :: String -> CDef ()
192enum_ n = tell [ConstructorDef n []] 212enum_ n = tell ([ConstructorDef n []],mempty)
193 213
194v2b = V2 Bool 214v2b = V2 Bool
195v3b = V3 Bool 215v3b = V3 Bool