3 import StdEnv, StdGeneric
11 import Control.GenBimap
13 import System.FilePath
16 import Data.GenType.CType
17 import Data.GenType.CParser
20 derive gType Either, T, R, Frac, Tr, Fix, Odd, Even, SR, List, Enum, NT, Blurp, EnumList, ER, CP, RA, Nest, MR, P
22 :: P m = P (Tr m Int) | P2 (m Bool Bool)
26 :: SR = {f1 :: Int, f2 :: Bool, f3 :: Tr Either Bool, f4 :: Enum}
27 :: R a = {f1 :: ? (R a), f2 :: Bool, f3 :: T a, f4 :: Char -> Int,
28 f5 :: [([#Int], [#Int!], [!Int!], [!Int], [Int!])],
29 f6 :: ({!Int}, {R Bool}, {#Char}, {32#Int}),/*({!Int}, {#Char}, {R Bool})*/
31 :: Tr m b= Tr (m Int b) | TrBork
32 :: Frac a = (/.) infixl 7 a a | Flurp
33 :: Fix f = Fix (f (Fix f))
35 :: List a = Cons a (List a) | Nil
37 :: Blurp a = Blurp (List a) | Blorp
39 :: EnumList = ECons Enum EnumList | ENil
41 :: ER = {nat :: Int, bool :: Bool}
42 :: RA a = {a1 :: a, a2 :: Int}
43 :: MR m = {b1 :: m Int}
45 :: CP = CLeft Int Bool | CRight Char Char
48 ////Start = foldr (\i c->print i ["\n":c]) [] $ catMaybes $ map gTypeToType $ flattenGType $ unBox t
49 //:: Pair a b = Pair a b
50 //instance == (Pair a b) | == a where (==) (Pair a1 _) (Pair a2 _) = a1 == a2
51 //instance < (Pair a b) | < a where (<) (Pair a1 _) (Pair a2 _) = a1 < a2
52 :: Odd a = Odd (Even a) | OddBlurp
53 :: Even a = Even (Odd a) | EvenBlurp
55 includes = "#include <stdint.h>\n#include <stdbool.h>\n"
57 genFiles :: String (Box GType a) *World -> *World | gType{|*|} a
59 // # tds = map (map gTypeToType) $ map (filter (not o isBasic)) $ flattenGType $ unBox t
60 # tds = map (map gTypeToType) $ flattenGType $ unBox t
61 # (ok, h, w) = fopen (bn <.> "h") FWriteText w
62 | not ok = abort ("Couldn't open: " +++ bn <.> "h")
63 # (ok, c, w) = fopen (bn <.> "c") FWriteText w
64 | not ok = abort ("Couldn't open: " +++ bn <.> "c")
65 # h = h <<< "#ifndef " <<< toUpperCase bn <<< "_H\n"
66 <<< "#define " <<< toUpperCase bn <<< "_H\n"
69 <<< "#include <stdlib.h>\n"
70 <<< "#include \"" <<< (bn <.> "h") <<< "\"\n\n"
71 # h = case typedefs tds of
72 Left e = abort ("Couldn't generate typedef: " +++ e)
73 Right d = foldl (<<<) h d
74 # (h, c) = case parsers tds of
75 Left e = abort ("Couldn't generate parser: " +++ e)
76 Right (hd, cd) = (foldl (<<<) h hd, foldl (<<<) c cd)
77 # h = h <<< "\n#endif"
78 # (ok, w) = fclose h w
79 | not ok = abort ("Couldn't close: " +++ bn <.> "h")
80 # (ok, w) = fclose c w
81 | not ok = abort ("Couldn't close: " +++ bn <.> "c")
84 genFilesFlat :: String (Box GType a) *World -> *World | gType{|*|} a
86 # ty = gTypeToType (unBox t)
87 # (ok, h, w) = fopen (bn <.> "h") FWriteText w
88 | not ok = abort ("Couldn't open: " +++ bn <.> "h")
89 # (ok, c, w) = fopen (bn <.> "c") FWriteText w
90 | not ok = abort ("Couldn't open: " +++ bn <.> "c")
91 # h = h <<< "#ifndef " <<< toUpperCase bn <<< "_H\n"
92 <<< "#define " <<< toUpperCase bn <<< "_H\n"
95 <<< "#include \"" <<< (bn <.> "h") <<< "\"\n\n"
96 # h = case flatTypedef ty of
97 Left e = abort ("Couldn't generate typedef: " +++ e)
98 Right d = foldl (<<<) h d
99 # (h, c) = case flatParser ty of
100 Left e = abort ("Couldn't generate parser: " +++ e)
101 Right (hd, cd) = (foldl (<<<) h hd, foldl (<<<) c cd)
102 # h = h <<< "\n#endif"
103 # (ok, w) = fclose h w
104 | not ok = abort ("Couldn't close: " +++ bn <.> "h")
105 # (ok, w) = fclose c w
106 | not ok = abort ("Couldn't close: " +++ bn <.> "c")
109 Start w = foldr ($) w
110 [ genFiles "maybeInt" maybeInt
111 , genFiles "eitherIntChar" eitherIntChar
112 , genFiles "eitherIntMaybeChar" eitherIntMaybeChar
114 , genFiles "raint" raInt
115 , genFiles "lmint" lmInt
116 , genFiles "trEitherInt" trEitherInt
117 , genFiles "mrMaybe" mrMaybe
118 , genFiles "pEither" pEither
120 // ( flatTypedef $ gTypeToType $ unBox t
121 // , typedefs $ map (map gTypeToType) $ map (filter (not o isBasic)) $ flattenGType $ unBox t
122 // , flatParser $ gTypeToType $ unBox t
123 // , parsers $ map (map gTypeToType) $ map (filter (not o isBasic)) $ flattenGType $ unBox t
125 maybeInt :: Box GType (?Int)
126 maybeInt = gType{|*|}
128 eitherIntChar :: Box GType (Either Int Char)
129 eitherIntChar = gType{|*|}
131 eitherIntMaybeChar :: Box GType (Either Int (?Char))
132 eitherIntMaybeChar = gType{|*|}
137 raInt :: Box GType (RA Int)
140 lmInt :: Box GType [?Int]
143 trEitherInt :: Box GType (Tr Either Int)
144 trEitherInt = gType{|*|}
146 mrMaybe :: Box GType (MR ?)
149 pEither :: Box GType (P Either)
152 //Start = typedefs //$ (\x->[[gTypeToType x]])
153 // $ map (/*filter (not o isBuiltin) o*/ catMaybes o map gTypeToType)
155 // $ map (map gTypeToType)
156 // $ map (filter (not o isBasic))
160 :: Nest m = Nest (m (m (m Int))) | NestBlurp
162 //t :: Box GType (?# Int)
163 //t :: Box GType (Maybe [Maybe (Either Bool String)])
164 //t :: Box GType ([SR], Enum, T Int, NT, Blurp Int)
165 //t :: Box GType [EnumList]
166 t :: Box GType (Nest ?, Tr Either (?(Int, Enum)))
167 //t :: Box GType (Odd Int, (), [Either (R (T *World)) (Frac Real)], Tr Either Bool, Fix Maybe)