886f463529f73821d8fbec6f3c2f138a061df72b
[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 (BCAnalogRead i) = [toChar i]
26 (BCAnalogWrite i) = [toChar i]
27 (BCDigitalRead i) = [toChar i]
28 (BCDigitalWrite i) = [toChar i]
29 (BCJmp i) = [toChar i]
30 (BCJmpT i) = [toChar i]
31 (BCJmpF i) = [toChar i]
32 _ = []]
33 where
34 toByteCode b = toChar $ consIndex{|*|} b + 1
35
36 instance toChar Pin where
37 toChar (Digital p) = toChar $ consIndex{|*|} p + 1
38 toChar (Analog p) = toChar $ consIndex{|*|} p + 1
39
40 derive gPrint BC, AnalogPin, Pin, DigitalPin
41 derive consIndex BC, Pin
42 derive consName BC, Pin
43
44 toReadableByteVal :: BC -> String
45 toReadableByteVal a = printToString a
46
47 instance arith ByteCode where
48 lit x = BC [BCPush $ 1]
49 (+.) x y = x <++> y <+-> [BCAdd]
50 (-.) x y = x <++> y <+-> [BCSub]
51 (*.) x y = x <++> y <+-> [BCMul]
52 (/.) x y = x <++> y <+-> [BCDiv]
53
54 instance boolExpr ByteCode where
55 (&.) x y = x <++> y <+-> [BCAnd]
56 (|.) x y = x <++> y <+-> [BCOr]
57 Not x = x <+-> [BCNot]
58 (==.) x y = x <++> y <+-> [BCEq]
59 (!=.) x y = x <++> y <+-> [BCNeq]
60 (<.) x y = x <++> y <+-> [ BCLes]
61 (>.) x y = x <++> y <+-> [BCGre]
62 (<=.) x y = x <++> y <+-> [BCLeq]
63 (>=.) x y = x <++> y <+-> [BCGeq]
64
65 instance analogIO ByteCode where
66 analogRead p = BC [BCAnalogRead $ pin p]
67 analogWrite p b = b <+-> [BCAnalogWrite $ pin p]
68
69 instance digitalIO ByteCode where
70 digitalRead p = BC [BCDigitalRead $ pin p]
71 digitalWrite p b = b <+-> [BCDigitalWrite $ pin p]
72
73 instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e
74 instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e
75 instance If ByteCode Stmt Stmt e where If b t e = BCIfStmt b t e
76 instance If ByteCode x y Expr where If b t e = BCIfStmt b t e
77 instance IF ByteCode where
78 IF b t e = BCIfStmt b t e
79 (?) b t = BCIfStmt b t $ BC []
80 BCIfStmt b t e = b <+-> [BCJmpF $ length <$> t + 1] <++> t
81 <+-> [BCJmp $ length <$> e] <++> e
82
83 instance noOp ByteCode where noOp = BC []
84
85 instance serial ByteCode where
86 serialAvailable = BC [BCSerialAvail]
87 serialPrint s = BC [BCSerialPrint]
88 serialPrintln s = BC [BCSerialPrintln]
89 serialRead = BC [BCSerialRead]
90 serialParseInt = BC [BCSerialParseInt]
91
92 (<++>) infixl 7
93 (<++>) (BC x) (BC y) = BC $ x ++ y
94 (<+->) infixl 7
95 (<+->) (BC x) y = BC $ x ++ y
96 (<-+>) infixl 7
97 (<-+>) x (BC y) = BC $ x ++ y
98
99 (<$>) infixl 9
100 (<$>) f (BC x) = f x
101
102 instance zero BCState where
103 zero = {a=()}
104
105 toRealByteCode :: (ByteCode a Expr) -> String
106 toRealByteCode (BC x) = concat $ map (toString o toByteVal) x
107
108 //Start :: ByteCode Int Expr
109 //Start = (lit 36 +. lit 42) +. lit 84
110
111 to16bit :: Int -> String
112 to16bit i = toString (toChar (i/265)) +++ toString (toChar (i rem 265))
113
114 //Run test programma en pretty print
115 Start :: String
116 Start = "t" +++ to16bit (size b) +++ b
117 //Start :: ByteCode Int Expr
118 //Start = bc
119 where
120 bc = If (lit True) (analogRead A1) (analogRead A0)
121 b = toRealByteCode bc
122 //Start :: ByteCode Int Expr
123 //Start = If (lit True) (analogRead A1) (analogRead A0)
124 //Start = If ((lit 36) ==. (lit 42)) (noOp) (noOp)