added sds operators, timing and task deletion
[mTask.git] / mTaskInterpret.icl
1 implementation module mTaskInterpret
2
3 //import iTasks
4 import gdynamic, gCons, GenEq, StdMisc, StdArray, GenBimap
5 import GenPrint
6 import mTask
7
8 import StdFile
9 import StdString
10
11 from StdFunc import o
12 import StdBool
13 import StdTuple
14 import Data.Tuple
15 import StdList
16 from Data.Func import $
17 from Text import class Text(concat,join,toUpperCase), instance Text String
18
19 toByteVal :: BC -> [Char]
20 toByteVal b
21 # bt = toByteCode b
22 = [bt:case b of
23 (BCPush i) = [toChar i]
24 (BCAnalogRead i) = [toChar i]
25 (BCAnalogWrite i) = [toChar i]
26 (BCDigitalRead i) = [toChar i]
27 (BCDigitalWrite i) = [toChar i]
28 (BCJmp i) = [toChar i]
29 (BCJmpT i) = [toChar i]
30 (BCJmpF i) = [toChar i]
31 _ = []]
32 where
33 toByteCode b = toChar $ consIndex{|*|} b + 1
34
35 instance toByteCode Bool where
36 toByteCode True = [toChar 1]
37 toByteCode False = [toChar 0]
38 instance toByteCode Int where toByteCode n = map toChar [n/(2<<7),n rem 265]
39 instance toByteCode Long where toByteCode (L n) = toByteCode n
40 instance toByteCode Char where toByteCode c = [c]
41 instance toByteCode String where toByteCode s = undef
42 instance toByteCode Button where toByteCode s = [toChar $ consIndex{|*|} s]
43
44 instance toChar Pin where
45 toChar (Digital p) = toChar $ consIndex{|*|} p + 1
46 toChar (Analog p) = toChar $ consIndex{|*|} p + 1
47
48 derive gPrint BC, AnalogPin, Pin, DigitalPin
49 derive consIndex BC, Pin, Button
50 derive consName BC, Pin, Button
51
52 toReadableByteVal :: BC -> String
53 toReadableByteVal a = printToString a
54
55 instance arith ByteCode where
56 lit x = BC [BCPush 1]
57 (+.) x y = x <++> y <+-> [BCAdd]
58 (-.) x y = x <++> y <+-> [BCSub]
59 (*.) x y = x <++> y <+-> [BCMul]
60 (/.) x y = x <++> y <+-> [BCDiv]
61
62 instance boolExpr ByteCode where
63 (&.) x y = x <++> y <+-> [BCAnd]
64 (|.) x y = x <++> y <+-> [BCOr]
65 Not x = x <+-> [BCNot]
66 (==.) x y = x <++> y <+-> [BCEq]
67 (!=.) x y = x <++> y <+-> [BCNeq]
68 (<.) x y = x <++> y <+-> [ BCLes]
69 (>.) x y = x <++> y <+-> [BCGre]
70 (<=.) x y = x <++> y <+-> [BCLeq]
71 (>=.) x y = x <++> y <+-> [BCGeq]
72
73 instance analogIO ByteCode where
74 analogRead p = BC [BCAnalogRead $ pin p]
75 analogWrite p b = b <+-> [BCAnalogWrite $ pin p]
76
77 instance digitalIO ByteCode where
78 digitalRead p = BC [BCDigitalRead $ pin p]
79 digitalWrite p b = b <+-> [BCDigitalWrite $ pin p]
80
81 instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e
82 instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e
83 instance If ByteCode Stmt Stmt e where If b t e = BCIfStmt b t e
84 instance If ByteCode x y Expr where If b t e = BCIfStmt b t e
85 instance IF ByteCode where
86 IF b t e = BCIfStmt b t e
87 (?) b t = BCIfStmt b t $ BC []
88 BCIfStmt b t e = b <+-> [BCJmpF $ length <$> t + 1] <++> t
89 <+-> [BCJmp $ length <$> e] <++> e
90
91 instance noOp ByteCode where noOp = BC []
92
93 instance sds ByteCode where
94 sds f = undef/*{main =
95 let var = 42
96 (v In body) = f var
97 in unMain body
98 }*/
99 con f = undef
100
101 instance serial ByteCode where
102 serialAvailable = BC [BCSerialAvail]
103 serialPrint s = BC [BCSerialPrint]
104 serialPrintln s = BC [BCSerialPrintln]
105 serialRead = BC [BCSerialRead]
106 serialParseInt = BC [BCSerialParseInt]
107
108 (<++>) infixl 7
109 (<++>) (BC x) (BC y) = BC $ x ++ y
110 (<+->) infixl 7
111 (<+->) (BC x) y = BC $ x ++ y
112 (<-+>) infixl 7
113 (<-+>) x (BC y) = BC $ x ++ y
114
115 (<$>) infixl 9
116 (<$>) f (BC x) = f x
117
118 instance zero BCState where
119 zero = {a=()}
120
121 toRealByteCode :: (ByteCode a Expr) -> String
122 toRealByteCode (BC x) = concat $ map (toString o toByteVal) x
123
124 //Start :: ByteCode Int Expr
125 //Start = (lit 36 +. lit 42) +. lit 84
126
127 to16bit :: Int -> String
128 to16bit i = toString (toChar (i/265)) +++ toString (toChar (i rem 265))
129
130 //Run test programma en pretty print
131 //Start :: String
132 //Start = "t" +++ to16bit (size b) +++ b
133 Start :: Main (ByteCode Int Expr)
134 Start = bc
135 where
136 bc = sds \x=43 In {main = If (x ==. lit 42) (analogRead A1) (analogRead A0)}
137 b = toRealByteCode bc
138 //Start :: ByteCode Int Expr
139 //Start = If (lit True) (analogRead A1) (analogRead A0)
140 //Start = If ((lit 36) ==. (lit 42)) (noOp) (noOp)