1 implementation module mTaskCode
4 import gdynamic, gCons, GenEq, StdMisc, StdArray
7 instance toCode MTask where toCode (MTask x) = "Task " + toCode x
9 argType :: (((Code a p)->Code b q)->In ((Code a p)->Code b q) (Code c s)) -> a | type a
12 instance argTypes (Code a p) | showType a where argTypes f = showType
13 instance argTypes (Code a p, Code b q) | showType a & showType b where argTypes f = (showType, showType)
14 instance argTypes (Code a p, Code b q, Code c r) | showType a & showType b & showType c where argTypes f = (showType, showType, showType)
16 resType :: ((t->Code b Expr)->In (t->Code b q) (Main (Code c s))) -> Code b q | showType b
19 var2Type :: (Code t p) -> Code t p | showType t
22 resType2 :: ((t->Code b Expr)->In (t->Code b q) (Main (Code c s))) -> SV b | showType2 b
23 resType2 f = showType2
25 instance toCode (SV t) where toCode (SV s) = s
27 instance arith Code where
29 (+.) x y = codeOp2 x " + " y
30 (-.) x y = codeOp2 x " - " y
31 (*.) x y = codeOp2 x " * " y
32 (/.) x y = codeOp2 x " / " y
33 instance boolExpr Code where
34 (&.) x y = codeOp2 x " && " y
35 (|.) x y = codeOp2 x " || " y
36 Not x = embed (brac (c "! " +.+ brac x))
37 (==.) x y = codeOp2 x " == " y
38 (!=.) x y = codeOp2 x " != " y
39 (<.) x y = codeOp2 x " < " y
40 (<=.) x y = codeOp2 x " <= " y
41 (>.) x y = codeOp2 x " > " y
42 (>=.) x y = codeOp2 x " >= " y
43 instance If Code Stmt Stmt Stmt where If c t e = IfStmt c t e
44 instance If Code e Stmt Stmt where If c t e = IfStmt c t e
45 instance If Code Stmt e Stmt where If c t e = IfStmt c t e
46 instance If Code x y Expr where If c t e = IfExpr c t e
47 IfExpr b t e = embed (brac (b +.+ indent +.+ nl +.+ c " ? " +.+ t +.+ nl +.+ c " : " +.+ e +.+ unindent))
56 _ = if (v == "") NoReturn (Assign v)
59 c "if " +.+ brac b +.+ c " {" +.+
60 indent +.+ nl +.+ setMode newMode +.+ t +.+ unindent +.+ nl +.+ c "} else {" +.+
61 indent +.+ nl +.+ setMode newMode +.+ e +.+ unindent +.+ nl +.+ c "}"
62 instance IF Code where
63 IF b t e = IfStmt b t e
66 c "if " +.+ setMode SubExp +.+ brac b +.+ c " {" +.+
67 indent +.+ nl +.+ setMode mode +.+ t +.+ c ";" +.+ unindent +.+ nl +.+ c "}"
69 instance sds Code where
72 let name = "sds"+toCode n
73 (v In body) = f (C (var name))
74 in setCode Var +.+ c (type2string v + " " + name + " = " + toCode v +
75 ";\n") +.+ setCode Setup +.+ unMain body}
79 defCode :: ((Code t p) -> In t (Main (Code u q))) -> Main (Code u r) | type t
82 let name = "sds"+toCode n
83 (v In body) = f (C (var name))
84 in setCode Var +.+ c (type2string v + " " + name + " = " + toCode v +
85 ";\n") +.+ setCode Setup +.+ unMain body}
87 var :: String (ReadWrite (Code v q)) CODE -> CODE
88 var sds Rd s = unC (embed (c sds)) Rd s
89 var sds (Wrt v) s = unC (embed (c ("(" + sds + " = ") +.+ v +.+ c ")")) Rd s
91 instance assign Code where
92 (=.) (C v) e = embed (setMode SubExp +.+ C \rw c.v (Wrt (toExpr e)) c)
93 instance seq Code where
95 getMode \mode. fresh \n. let v = "b" + toCode n in
96 addBinds v +.+ var2Type x +.+ c (v + ";") +.+ nl +.+
97 setMode (Assign v) +.+ x +.+ nl +.+ setMode mode +.+ f (embed (c v))
98 (:.) x y = getMode \mode. setMode NoReturn +.+ embed x +.+ nl +.+ setMode mode +.+ y
99 instance step` Code where
101 getMode \mode. fresh \n.
102 let v = "s" + toCode n in
103 c "while(true) {" +.+ indent +.+ nl +.+
104 var2Type x +.+ c (v + ";") +.+ nl +.+
105 setMode (Assign v) +.+ x +.+ nl +.+
106 setMode mode +.+ codeSteps (f (c v)) +.+
107 unindent +.+ nl +.+ c "}"
108 codeSteps :: [Step Code t] -> Code u p
109 codeSteps [] = C \rw c.c
110 codeSteps [Cond b e:x] =
111 getMode \mode. setMode SubExp +.+
112 c "if (" +.+ b +.+ c ") {" +.+ indent +.+ nl +.+
113 setMode mode +.+ e +.+
114 optBreak mode +.+ unindent +.+ nl +.+ c "}" +.+ nl +.+ setMode mode +.+ codeSteps x
115 codeSteps [Ever e:x] =
116 getMode \mode. e +.+ optBreak mode
118 optBreak :: Mode -> Code u p
122 _ = nl +.+ c "break;"
124 instance setDelay Code where
125 setDelay d t = embed (c "setDelay" +.+ brac (t +.+ c ", " +.+ d))
126 instance mtask Code a | taskImp2 a & types a where
128 {main = freshMTask \n.
129 let (app, a) = taskImp2 n types
130 (b In main) = f (\d a.app (long d) a)
131 in codeMTaskBody (loopCode n (b a)) (unMain main)}
132 instance mtasks Code a b | taskImp2 a & types a & taskImp2 b & types b where
137 let (app1, a1) = taskImp2 t1 types
138 (app2, a2) = taskImp2 t2 types
139 ((b1, b2) In main) = f ((\d a.app1 (long d) a),(\d a.app2 (long d) a))
140 in codeMTaskBody (loopCode t2 (b2 a2)) (codeMTaskBody (loopCode t1 (b1 a1)) (unMain main))}
142 loopCode :: Int (Code a b) -> Code c d
144 nl +.+ c "case " +.+ c n +.+ c ": {" +.+ indent +.+ nl +.+
145 setMode NoReturn +.+ b +.+ nl +.+ c "break;" +.+
146 unindent +.+ nl +.+ c "} "
148 instance taskImp2 () where
149 taskImp2 n () = (app, ())
150 where app d a = setBinds [] +.+ embed (c "newTask(" +.+ c n +.+ c ", " +.+ d +.+ c ")")
151 instance taskImp2 (Code t p) where
152 taskImp2 n type1 = (app, ta)
157 setBinds [n0] +.+ embed (c "newTask(" +.+
161 instance taskImp2 (Code a p, Code b q) where
162 taskImp2 n (type1, type2) = (app, (ta1, ta2)) where
168 setBinds [n0,n1] +.+ embed (c "newTask(" +.+ c n +.+ c ", " +.+ long d +.+
169 c ", " +.+ a1 +.+ c ", " +.+ a2 +.+ c ")")
170 instance taskImp2 (Code a p, Code b q, Code c r) where
171 taskImp2 n (type1, type2, type3) = (app, (ta1, ta2, ta3))
180 setBinds [n0,n1,n2] +.+ embed (c "newTask(" +.+
186 instance taskImp2 (Code a p, Code b q, Code c r, Code d s) where
187 taskImp2 n (type1, type2, type3, type4) = (app, (ta1, ta2, ta3, ta4))
197 app d (a1, a2, a3, a4) =
198 setBinds [n0,n1,n2,n3] +.+ embed (c "newTask(" +.+
206 class taskImp a :: Int a -> (Int a->Code MTask Expr, a)
207 instance taskImp () where
208 taskImp n () = (app, ())
209 where app i a = embed (c "newTask(" +.+ c n +.+ c ", " +.+ c i +.+ c ")")
210 instance taskImp (Code t p) where
211 taskImp n type1 = (app, ta)
213 ta = c "t0p->a[0]" +.+ type1
215 embed (c "newTask(" +.+
219 instance taskImp (Code a p, Code b q) where
220 taskImp n (type1, type2) = (app, (ta1, ta2)) where
221 ta1 = c "t0p->a[0]" +.+ type1
222 ta2 = c "t0p->a[1]" +.+ type2
224 embed (c "newTask(" +.+ c n +.+ c ", " +.+ c i +.+
225 c ", " +.+ a1 +.+ c ", " +.+ a2 +.+ c ")")
226 instance taskImp (Code a p, Code b q, Code c r) where
227 taskImp n (type1, type2, type3) = (app, (ta1, ta2, ta3))
229 ta1 = c "t0p->a[0]" +.+ type1
230 ta2 = c "t0p->a[1]" +.+ type2
231 ta3 = c "t0p->a[2]" +.+ type3
233 embed (c "newTask(" +.+
239 instance taskImp (Code a p, Code b q, Code c r, Code d s) where
240 taskImp n (type1, type2, type3, type4) = (app, (ta1, ta2, ta3, ta4))
242 ta1 = c "t0p->a[0]" +.+ type1
243 ta2 = c "t0p->a[1]" +.+ type2
244 ta3 = c "t0p->a[2]" +.+ type3
245 ta4 = c "t0p->a[3]" +.+ type4
246 app i (a1, a2, a3, a4) =
247 embed (c "newTask(" +.+
255 tasksMain :: Int Int ((a->Code MTask Expr,b->Code MTask Expr) -> In (a->Code c d,b->Code e f) (Main (Code g h))) -> Main (Code i j) | taskImp a & types a & taskImp b & types b
258 freshMTask \n. freshMTask \m.
260 (app1, a1) = taskImp n types
261 (app2, a2) = taskImp m types
262 ((b1, b2) In {main = e}) = f (app1 i, app2 j)
264 codeMTaskBody (loopCode n (b1 a1) +.+ setMode NoReturn +.+ loopCode m (b2 a2)) e
267 instance types () where types = ()
268 instance types (Code a p) | typeSelector a & isExpr p
269 where types = typeSelector
270 instance types (Code a p, Code b q) | typeSelector a & isExpr p & typeSelector b & isExpr q
271 where types = (typeSelector, typeSelector)
272 instance types (Code a p, Code b q, Code c r) | typeSelector a & isExpr p & typeSelector b & isExpr q & typeSelector c & isExpr r
273 where types = (typeSelector, typeSelector, typeSelector)
274 instance types (Code a p, Code b q, Code c r, Code d s) | typeSelector a & isExpr p & typeSelector b & isExpr q & typeSelector c & isExpr r & typeSelector d & isExpr s
275 where types = (typeSelector, typeSelector, typeSelector, typeSelector)
277 codeMTaskBody :: (Code v w) (Code c d) -> Code e f
278 codeMTaskBody loop e =
281 setCode Loop +.+ loop +.+
282 setMode mode +.+ setCode Setup +.+ embed e
283 instance fun Code () where
285 {main = getMode \mode. fresh \n.
286 let fname = c ("f" + toCode n)
287 (g In {main=e}) = f (\x.embed (fname +.+ c " ()"))
288 in setCode Fun +.+ c "\n" +.+ resType f +.+ c " " +.+ fname +.+ c " () " +.+
289 funBody (setMode (Return (toCode (resType2 f))) +.+ setBinds [] +.+ g ()) +.+ setCode Setup +.+ setMode mode +.+ e
291 instance fun Code (Code t p) | type, showType t & isExpr p where
294 getMode \mode. fresh \n.
295 let fname = c ("f" + toCode n)
296 aname = "a" + toCode n
297 (g In {main=e}) = f (\x.embed (fname +.+ c " " +.+ brac x))
298 in setCode Fun +.+ c "\n" +.+ resType f +.+ c " " +.+ fname +.+ c " " +.+
299 brac (argTypes f +.+ c (" " + aname)) +.+
300 funBody (setMode (Return (toCode (resType2 f))) +.+ setBinds [aname] +.+ g (embed (c aname))) +.+ setCode Setup +.+ setMode mode +.+ e
302 instance fun Code (Code a p, Code b q) | showType a & showType b where
305 getMode \mode. fresh \n.
306 let fname = c ("f" + toCode n + " ")
307 aname = "a" + toCode n //+ " "
308 bname = "b" + toCode n //+ " "
309 (atype, btype) = argTypes f
311 = f (\(x,y).embed (fname +.+ codeOp2 x ", " y))
312 in setCode Fun +.+ nl +.+ resType f +.+ fname +.+
313 codeOp2 (atype +.+ c aname) ", " (btype +.+ c bname) +.+
314 funBody (setMode (Return (toCode (resType2 f))) +.+
315 setBinds [aname,bname] +.+ g (embed (c aname), embed (c bname))) +.+
316 setCode Setup +.+ setMode mode +.+ unMain main
318 instance fun Code (Code a p, Code b q, Code c r) | showType a & showType b & showType c where
321 getMode \mode. fresh \n.
322 let fname = c ("f" + toCode n)
323 aname = "a" + toCode n
324 bname = "b" + toCode n
325 cname = "c" + toCode n
326 (atype,btype,ctype) = argTypes f
327 (g In {main=e}) = f (\(x,y,z).embed (fname +.+ c " " +.+ brac (x +.+ c ", " +.+ y +.+ c ", " +.+ z)))
328 in setCode Fun +.+ c "\n" +.+ resType f +.+ c " " +.+ fname +.+ c " " +.+
329 brac (atype +.+ c (" " + aname + ", ") +.+ btype +.+ c (" " + bname + ", ") +.+ ctype +.+ c (" " + cname)) +.+
330 funBody (setMode (Return (toCode (resType2 f))) +.+
331 setBinds [aname,bname,cname] +.+ g (embed (c aname), embed (c bname), embed (c cname))) +.+ setCode Setup +.+ setMode mode +.+ e
333 instance output Code where
334 output x = embed (c "Serial.println(" +.+ x +.+ c ")")
335 instance pinMode Code where
336 pinmode p m = embed (c ("pinMode(" + toCode p + ", " + consName{|*|} m + ")"))
337 instance digitalIO Code where
338 digitalRead p = embed (c ("digitalRead(" + toCode p + ")"))
339 digitalWrite p b = embed (c ("digitalWrite(" + toCode p + ", ") +.+ b +.+ c ")")
340 instance dIO Code where
341 dIO p = C (ioc p) where
342 ioc :: p (ReadWrite (Code t q)) CODE -> CODE | pin, readPinD p
343 ioc p Rd s = f Rd s where (C f) = embed (c ("digitalRead(" + toCode p + ")"))
344 ioc p (Wrt v) s = f Rd s where (C f) = embed (c ("pWrite(" + toCode p + ", ") +.+ v +.+ c ")")
345 instance aIO Code where
346 aIO p = C (ioc p) where
347 ioc :: p (ReadWrite (Code t q)) CODE -> CODE | pin p
348 ioc p Rd s = unC (embed (c ("analogRead(" + toCode p + ")"))) Rd s
349 ioc p (Wrt v) s = unC (embed (c ("pWrite(" + toCode p + ", ") +.+ v +.+ c ")")) Rd s
350 instance analogIO Code where
351 analogRead p = embed (c ("analogRead(" + toCode p + ")"))
352 analogWrite p b = embed (c ("analogWrite(" + toCode p + ", ") +.+ b +.+ c ")")
353 instance noOp Code where noOp = C \rw c.c
355 :: Code a p = C ((ReadWrite (Code a Expr)) CODE -> CODE)
356 unC :: (Code a p) -> ((ReadWrite (Code a Expr)) CODE -> CODE)
359 :: Def = Var | Fun | Setup | Loop
360 :: Mode = /*MainMode |*/ NoReturn | Return String | SubExp | Assign String
362 setMode :: Mode -> Code a p
363 setMode m = C \rw c.{CODE|c & mode` = m}
365 getMode :: (Mode -> Code a p) -> Code a p
366 getMode f = C \rw c.unC (f c.mode`) rw c
368 embed :: (Code a p) -> Code a p
370 getMode \m. case m of
371 NoReturn = setMode SubExp +.+ e +.+ c ";"
372 Return "void" = setMode SubExp +.+ e +.+ c ";"
373 Return t = c "return " +.+ setMode SubExp +.+ e +.+ c ";"
374 Assign s = c (s + " = ") +.+ setMode SubExp +.+ e +.+ c ";"
376 _ = abort "\n\nembed: unknown mode.\n"
378 (+.+) infixl 5 :: (Code a p) (Code b q) -> Code c r
379 (+.+) (C f) (C g) = C \rw c.g Rd (f Rd c)
381 fresh :: (Int -> (Code a p)) -> (Code a p)
382 fresh f = C \rw c.unC (f c.fresh) rw {CODE | c & fresh = c.fresh + 1}
384 freshMTask :: (Int -> (Code a p)) -> (Code a p)
385 freshMTask f = C \rw c.unC (f c.freshMTask) rw {CODE | c & freshMTask = c.freshMTask + 1}
387 setCode :: Def -> (Code a p)
388 setCode d = C \rw c.{c & def = d}
390 getCode :: (Def -> Code a p) -> (Code a p)
391 getCode f = C \rw c.unC (f c.def) rw c
393 brac :: (Code a p) -> Code b q
394 brac e = c "(" +.+ e +.+ c ")"
396 funBody :: (Code a p) -> Code b q
397 funBody e = c "{" +.+ indent +.+ nl +.+ e +.+ unindent +.+ nl +.+ c "}" +.+ nl
399 codeOp2 :: (Code a p) String (Code b q) -> Code c r
400 codeOp2 x n y = embed (brac (x +.+ c n +.+ y))
402 include :: String -> Code a b
403 include lib = C \rw c.{c & includes = [lib:c.includes]}
405 argList :: [a] -> String | toCode a
406 argList [a] = toCode a
407 argList [a:x] = toCode a + "," + argList x
410 c :: a -> Code b p | toCode a
411 c a = C \rw c.case c.def of
412 Fun = {c & funs = [toCode a: c.funs]}
413 Var = {c & vars = [toCode a: c.vars]}
414 Setup = {c & setup = [toCode a: c.setup]}
415 Loop = {c & loop = [toCode a: c.loop]}
419 C \rw c.case c.def of
420 Fun = {c & ifuns = inc c.ifuns}
421 Var = {c & ivars = inc c.ivars}
422 Setup = {c & isetup = inc c.isetup}
423 Loop = {c & iloop = inc c.iloop}
427 C \rw c.case c.def of
428 Fun = {c & ifuns = dec c.ifuns}
429 Var = {c & ivars = dec c.ivars}
430 Setup = {c & isetup = dec c.isetup}
431 Loop = {c & iloop = dec c.iloop}
439 C \rw c.case c.def of
440 Fun = {c & funs = [str c.ifuns: c.funs]}
441 Var = {c & vars = [str c.ivars: c.vars]}
442 Setup = {c & setup = [str c.isetup: c.setup]}
443 Loop = {c & loop = [str c.iloop: c.loop]}
445 str n = toString ['\n':repeatn (tabSize * n) ' ']
447 setBinds :: [String] -> Code a p
448 setBinds list = C \rw c.{c & binds = list}
450 addBinds :: String -> Code a p
451 addBinds name = C \rw c.{c & binds = [name:c.binds]}
453 getBinds :: ([String] -> Code a p) -> (Code a p)
454 getBinds f = C \rw c.unC (f c.binds) rw c
456 // ----- driver ----- //
458 compile :: (Main (Code a p)) -> [String]
459 compile {main=(C f)} =
461 ," Generated code for Arduino\n"
462 ," Pieter Koopman, pieter@cs.ru.nl\n"
465 ,"#define MAX_ARGS 4\n"
466 ,"#define MAX_TASKS 20\n"
467 ,"#define MAX_TASK_NO MAX_TASKS - 1\n"
468 ,"#define NEXT_TASK(n) ((n) == MAX_TASK_NO ? 0 : (n) + 1)\n"
470 ,"typedef union Arg {\n"
474 // ," float f;\n" // requires 4 bytes
478 ,"typedef struct Task {\n"
481 ," ARG a[MAX_ARGS];\n"
485 foldr (\lib c.["#include <":lib:".h>\n":c]) [] (mkset c.includes) ++
486 ["\n// --- variables ---\n"
487 ,"TASK tasks[MAX_TASKS];\n"
488 ,"byte t0 = 0, tc = 0, tn = 0;\n"
495 ,"unsigned long time = 0;\n"
498 ["\n// --- functions ---\n"
499 ,"byte newTask(byte id, long wait, word a0 = 0, word a1 = 0, word a2 = 0, word a3 = 0) {\n"
500 ," TASK *tnp = &tasks[tn];\n"
502 ," tnp->wait = wait;\n"
503 ," tnp->a[0].w = a0;\n"
504 ," tnp->a[1].w = a1;\n"
505 ," tnp->a[2].w = a2;\n"
506 ," tnp->a[3].w = a3;\n"
508 ," tn = NEXT_TASK(tn);\n"
512 ,"byte setDelay(byte t, long d) {\n"
513 ," tasks[t].wait = d;\n"
516 ,"boolean pressed(int b) {\n"
517 ," pinMode(A0, INPUT);\n"
518 ," int a0 = analogRead(A0);\n"
520 ," case 0: return a0 < ",toString RightBound,"; // right\n"
521 ," case 1: return ",toString RightBound," < a0 && a0 < ",toString UpBound,"; // up\n"
522 ," case 2: return ",toString UpBound," < a0 && a0 < ",toString DownBound,";// down\n"
523 ," case 3: return ",toString DownBound," < a0 && a0 < ",toString LeftBound,";//left\n"
524 ," case 4: return ",toString LeftBound," < a0 && a0 < ",toString SelectBound,";//select\n"
525 ," default: return ",toString SelectBound," < a0; //no button\n"
528 ,"boolean pWrite (int pin, boolean b) {\n"
529 ," pinMode(pin, OUTPUT);\n"
530 ," digitalWrite(pin, b);\n"
533 ,"int pWrite (int pin, int i) {\n"
534 ," pinMode(pin, OUTPUT);\n"
535 ," analogWrite(pin, i);\n"
540 ["\n// --- setup --- \n"
542 ," Serial.begin(9600);\n"
547 ,"\n// --- loop --- \n"
549 ," if (t0 != tn) {\n"
550 ," if (t0 == tc) {\n"
551 ," unsigned long time2 = millis();\n"
552 ," delta = time2 - time;\n"
556 ," TASK* t0p = &tasks[t0];\n"
557 ," t0p->wait -= delta;\n"
558 ," if (t0p->wait > 0L) {\n"
559 ," newTask(t0p->id, t0p->wait, t0p->a[0].w, t0p->a[1].w, t0p->a[2].w, t0p->a[3].w);\n"
561 ," switch (t0p->id) {"
566 ," Serial.println(\"stopped\");\n"
567 ," t0 = tn; // no known task: force termination of tasks\n"
571 ," t0 = NEXT_TASK(t0);\n"
575 where c = f Rd newCode
577 mkset :: [a] -> [a] | Eq a
578 mkset [a:x] = [a:mkset (filter ((<>) a) x)]
601 instance toCode () where toCode _ = ""
603 class toCode a :: a -> String
605 instance toCode Long where toCode (L i) = toCode i + "L"
607 instance toCode Bool where toCode b = if b "true" "false"
608 instance toCode Int where toCode a = toString a
609 instance toCode Real where toCode a = toString a
610 instance toCode Char where
611 toCode '\0' = "'\\0'"
612 toCode '\n' = "'\\n'"
614 toCode a = "'" + toString a + "'"
615 instance toCode String where toCode s = s
616 instance toCode DigitalPin where toCode x = s%(1, size s - 1) where s = consName{|*|} x
617 instance toCode AnalogPin where toCode x = consName{|*|} x
619 instance toCode Pin where
620 toCode (Digital p) = toCode p
621 toCode (Analog p) = toCode p