structs
[clean-tests.git] / struct / struct.icl
1 module struct
2
3 import StdEnv
4 import StdGeneric
5 import StdDebug
6
7 import Text
8
9 import Data.Functor
10 import Control.Applicative
11
12 toEnumValue :: GenericConsDescriptor -> String
13 toEnumValue gcd = gcd.gcd_type_def.gtd_name +++ "_" +++ gcd.gcd_name
14
15 toEnumType :: GenericTypeDefDescriptor -> String
16 toEnumType gtd = "enum cleanc_" +++ gtd.gtd_name
17
18 class gGenerateC a | gToCType{|*|}, gToCValue{|*|}, gToCEnums{|*|} a
19
20 :: CInfo a = {header :: String , toValue :: a -> String}
21
22 generateCInfo :: CInfo a | gGenerateC a
23 generateCInfo =
24 let
25 (CEnums enums) = cast res gToCEnums{|*|}
26 (SM types) = cast res gToCType{|*|}
27 res =
28 { header = concat
29 [ join "\n" (removeDup (sort enums))
30 , "\n\n"
31 , concat (types {fresh=0,inRecord=False,indent=0} [])
32 , ";"
33 ]
34 , toValue = \a->concat (gToCValue{|*|} a [])
35 }
36 in res
37 where
38 cast :: (v a) -> ((w a) -> w a)
39 cast _ = id
40
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]
46
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)
53 = SM \s c
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)
57 //Newtype
58 | gtd.gtd_num_conses == 0 = SM f
59 = SM \s c
60 //Enumeration (no data)
61 | and [gcd.gcd_arity == 0\\gcd<-gtd.gtd_conses] = indent s [toEnumType gtd:c]
62 //Regular ADTs
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":
68 indent s ["}":c]])]]]
69 gToCType{|CONS of gcd|} (SM f)
70 //No data field
71 | gcd.gcd_arity == 0 = SM \_->id
72 //Only one data field
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]
78
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
89
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
99 //Newtype
100 | gtd.gtd_num_conses == 0 = f a c
101 | and [gcd.gcd_arity == 0\\gcd<-gtd.gtd_conses] = f a c
102 = ["{":f a ["}":c]]
103 gToCValue{|CONS of gcd|} f (CONS a) c
104 //No data field
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]
110
111 :: DHTDetails
112 = DHT Int Bool
113 | SHT Addr
114 | XXX Int Int Int
115 | XXY Int Int DHTType
116
117 :: Addr =: Addr Int
118
119 :: DHTType = DHT11 | DHT12 | DHT22
120
121 derive class gGenerateC DHTDetails, DHTType, Addr, Record
122
123 Start :: CInfo Record
124 Start = generateCInfo
125
126 :: Record =
127 { field1 :: Int
128 , field2 :: Bool
129 , field3 :: DHTType
130 , field4 :: DHTDetails
131 }