10 import Control.Applicative
12 toEnumValue :: GenericConsDescriptor -> String
13 toEnumValue gcd = gcd.gcd_type_def.gtd_name +++ "_" +++ gcd.gcd_name
15 toEnumType :: GenericTypeDefDescriptor -> String
16 toEnumType gtd = "enum cleanc_" +++ gtd.gtd_name
18 class gGenerateC a | gToCType{|*|}, gToCValue{|*|}, gToCEnums{|*|} a
20 :: CInfo a = {header :: String , toValue :: a -> String}
22 generateCInfo :: CInfo a | gGenerateC a
25 (CEnums enums) = cast res gToCEnums{|*|}
26 (SM types) = cast res gToCType{|*|}
29 [ join "\n" (removeDup (sort enums))
31 , concat (types {fresh=0,inRecord=False,indent=0} [])
34 , toValue = \a->concat (gToCValue{|*|} a [])
38 cast :: (v a) -> ((w a) -> w a)
41 generic gToCType a :: Structmaker a
42 :: Structmaker a = SM (SData [String] -> [String]) | StructMakerOnzin a
43 :: SData = {indent :: Int, fresh :: Int, inRecord :: Bool}
44 indent s c = [createArray s.indent '\t':c]
45 show str s c = indent s [str:c]
47 gToCType{|Int|} = SM (show "uint64_t")
48 gToCType{|Real|} = SM (show "double")
49 gToCType{|Bool|} = SM (show "bool")
50 gToCType{|UNIT|} = SM \_->id
51 gToCType{|EITHER|} (SM fl) (SM fr) = SM \s->fl s o fr s
52 gToCType{|PAIR|} (SM fl) (SM fr)
54 | s.inRecord = fl s (fr s c)
55 = fl s [" f", toString s.fresh, ";\n":fr {s & fresh=s.fresh+1} c]
56 gToCType{|OBJECT of gtd|} (SM f)
58 | gtd.gtd_num_conses == 0 = SM f
60 //Enumeration (no data)
61 | and [gcd.gcd_arity == 0\\gcd<-gtd.gtd_conses] = indent s [toEnumType gtd:c]
63 # s` = {s & indent = s.indent + 1}
64 = indent s ["struct clean_", gtd.gtd_name, " {\n":
65 indent s` [toEnumType gtd, " cons;\n":
66 indent s` ["union {\n":
67 f {s` & indent=s`.indent+1, inRecord=False} (indent s` ["} data;\n":
69 gToCType{|CONS of gcd|} (SM f)
71 | gcd.gcd_arity == 0 = SM \_->id
73 | gcd.gcd_arity == 1 = SM \s c->f s [" ", gcd.gcd_name, ";\n":c]
74 = SM \s c->indent s ["struct {\n":f {s & indent=s.indent+1} [" f", toString (gcd.gcd_arity - 1), ";\n":indent s ["} ", gcd.gcd_name, ";\n":c]]]
75 gToCType{|RECORD of grd|} (SM f)
76 = SM \s c->indent s ["struct clean_", grd.grd_name, " {\n": f {s & indent=s.indent+1, inRecord=True} (indent s ["}":c])]
77 gToCType{|FIELD of gfd|} (SM f) = SM \s c->f s [" ", gfd.gfd_name,";\n":c]
79 :: CEnums a = CEnums [String] | CEnumsOnzin a
80 generic gToCEnums a :: CEnums a
81 gToCEnums{|a|} = CEnums []
82 gToCEnums{|UNIT|} = CEnums []
83 gToCEnums{|EITHER|} (CEnums fl) (CEnums fr) = CEnums (fl ++ fr)
84 gToCEnums{|PAIR|} (CEnums fl) (CEnums fr) = CEnums (fl ++ fr)
85 gToCEnums{|OBJECT of gtd|} (CEnums f) = CEnums [concat [toEnumType gtd, " {", join ", " (map toEnumValue gtd.gtd_conses), "};"]:f]
86 gToCEnums{|CONS|} (CEnums f) = CEnums f
87 gToCEnums{|RECORD|} (CEnums f) = CEnums f
88 gToCEnums{|FIELD|} (CEnums f) = CEnums f
90 generic gToCValue a :: a [String] -> [String]
91 gToCValue{|Int|} i c = [toString i:c]
92 gToCValue{|Real|} r c = [toString r:c]
93 gToCValue{|Bool|} b c = [if b "true" "false":c]
94 gToCValue{|UNIT|} _ _ = []
95 gToCValue{|EITHER|} fl _ (LEFT l) c = fl l c
96 gToCValue{|EITHER|} _ fr (RIGHT l) c = fr l c
97 gToCValue{|PAIR|} fl fr (PAIR l r) c = fl l [", ":fr r c]
98 gToCValue{|OBJECT of gtd|} f (OBJECT a) c
100 | gtd.gtd_num_conses == 0 = f a c
101 | and [gcd.gcd_arity == 0\\gcd<-gtd.gtd_conses] = f a c
103 gToCValue{|CONS of gcd|} f (CONS a) c
105 | gcd.gcd_arity == 0 = [toEnumValue gcd:c]
106 | gcd.gcd_arity == 1 = [" .cons=",toEnumValue gcd,", .data.",gcd.gcd_name,"=":f a c]
107 = [" .cons=",toEnumValue gcd,", .data.",gcd.gcd_name,"={":f a ["} ":c]]
108 gToCValue{|RECORD|} f (RECORD a) c = ["{":f a ["}":c]]
109 gToCValue{|FIELD of gfd|} f (FIELD a) c = [" .", gfd.gfd_name, "=": f a c]
115 | XXY Int Int DHTType
119 :: DHTType = DHT11 | DHT12 | DHT22
121 derive class gGenerateC DHTDetails, DHTType, Addr, Record
123 Start :: CInfo Record
124 Start = generateCInfo
130 , field4 :: DHTDetails