revert back to old itasks, add sds support in bytecode
[mTask.git] / gdynamic.icl
1 implementation module gdynamic
2
3 /*
4 Pieter Koopman 2015
5 pieter@cs.ru.nl
6 Radboud University, Nijmegen, The Netherlands
7 ARDSL project
8 */
9
10 import StdEnv, StdGeneric, StdMaybe
11
12 :: DYNAMIC :== [String]
13 :: Dyn = Dyn DYNAMIC
14
15 class dyn a | toGenDynamic{|*|}, fromGenDynamic{|*|} a
16 derive class dyn [], (,), (,,), (,,,)
17
18 //derive fromGenDynamic [], (,), (,,)
19 //derive toGenDynamic [], (,), (,,)
20
21 generic toGenDynamic a :: a -> [String]
22
23 toGenDynamic{|Int|} x = [toString x]
24 toGenDynamic{|Real|} x = [toString x]
25 toGenDynamic{|Char|} x = ["'" +++ toString x +++ "'"]
26 toGenDynamic{|String|} x = ["\"" +++ toString x +++ "\""]
27 toGenDynamic{|Bool|} x = [toString x]
28 toGenDynamic{|UNIT|} x = []
29 toGenDynamic{|PAIR|} f g (PAIR x y) = f x ++ g y
30 toGenDynamic{|EITHER|} f g (LEFT x) = f x
31 toGenDynamic{|EITHER|} f g (RIGHT y) = g y
32 toGenDynamic{|OBJECT|} f (OBJECT x) = f x
33 toGenDynamic{|CONS of gcd|} f (CONS x) = [gcd.gcd_name:f x]
34 toGenDynamic{|FIELD|} f (FIELD x) = f x
35 toGenDynamic{|RECORD of r|} f (RECORD x) = [r.grd_name:f x]
36
37 // ----------------------------
38
39 generic fromGenDynamic a :: [String] -> Maybe (a, [String])
40 fromGenDynamic{|UNIT|} l = Just(UNIT,l)
41 fromGenDynamic{|Char|} [a:x]
42 | size a == 3 && a.[0] == '\'' && a.[2] == '\''
43 = Just (toChar a.[1],x)
44 = Nothing
45 fromGenDynamic{|String|} [a:x]
46 # len = size a
47 | len >= 2 && a.[0] == '"' && a.[len-1] == '"'
48 = Just (a%(1,len-2),x)
49 = Nothing
50 fromGenDynamic{|String|} [] = Nothing
51 fromGenDynamic{|Bool|} ["True" :l] = Just (True ,l)
52 fromGenDynamic{|Bool|} ["False":l] = Just (False,l)
53 fromGenDynamic{|Bool|} l = Nothing
54 fromGenDynamic{|Int|} [a:x] = if (toString i == a) (Just (i,x)) Nothing where i = toInt a
55 fromGenDynamic{|Int|} [] = Nothing
56 fromGenDynamic{|Real|} [a:x] = if (toString i == a) (Just (i,x)) Nothing where i = toReal a
57 fromGenDynamic{|Real|} [] = Nothing
58 fromGenDynamic{|PAIR|} f g l =
59 case f l of
60 Just (x,l) = case g l of
61 Just (y,l) = Just (PAIR x y, l)
62 _ = Nothing
63 _ = Nothing
64 fromGenDynamic{|EITHER|} f g l =
65 case f l of
66 Just (x,l) = Just (LEFT x,l)
67 _ = case g l of
68 Just (x,l) = Just (RIGHT x,l)
69 _ = Nothing
70 fromGenDynamic{|OBJECT|} f l = case f l of Just (x,l) = Just (OBJECT x,l); _ = Nothing
71 fromGenDynamic{|FIELD|} f l = case f l of Just (x,l) = Just (FIELD x,l); _ = Nothing
72 fromGenDynamic{|RECORD of r|} f [n:l] | n == r.grd_name
73 = case f l of
74 Just (x,l) = Just (RECORD x,l)
75 _ = Nothing
76 = Nothing
77 fromGenDynamic{|CONS of gcd|} f l
78 | [gcd.gcd_name] == take 1 l
79 = case f (tl l) of Just (x,l) = Just (CONS x,l); _ = Nothing
80 = Nothing
81
82 // ----------------------------
83
84 toDyn :: a -> Dyn | dyn a
85 toDyn a = Dyn (toGenDynamic{|*|} a)
86
87 fromDyn :: Dyn -> Maybe a | dyn a
88 fromDyn (Dyn a) =
89 case fromGenDynamic{|*|} a of
90 Just (x,[]) = Just x
91 _ = Nothing
92
93 Start1 = toLIBC (toDyn [(1,True,'a'),(2,False,'1')])
94 Start = toGenDynamic{|*->*|} (\b.if b ["I"] ["O"]) [True,False,True]
95
96 toLIBC :: Dyn -> Maybe [(Int,Bool,Char)]
97 toLIBC l = fromDyn l
98