1 implementation module mTaskInterpret
4 import gdynamic, gCons, GenEq, StdMisc, StdArray, GenBimap
16 from Data.Func import $
17 from Text import class Text(join,toUpperCase), instance Text String
19 toByteVal :: BC -> String
22 derive gPrint BC, AnalogPin
25 derive conses BC, AnalogPin
27 toReadableByteVal :: BC -> String
28 toReadableByteVal a = printToString a
30 instance arith ByteCode where
31 lit x = BC [BCPush $ toCode x]
32 (+.) x y = x <++> y <+-> [BCAdd]
33 (-.) x y = x <++> y <+-> [BCSub]
34 (*.) x y = x <++> y <+-> [BCMul]
35 (/.) x y = x <++> y <+-> [BCDiv]
37 instance boolExpr ByteCode where
38 (&.) x y = x <++> y <+-> [BCAnd]
39 (|.) x y = x <++> y <+-> [BCOr]
40 Not x = x <+-> [BCNot]
41 (==.) x y = x <++> y <+-> [BCEq]
42 (!=.) x y = x <++> y <+-> [BCNeq]
43 (<.) x y = x <++> y <+-> [ BCLes]
44 (>.) x y = x <++> y <+-> [BCGre]
45 (<=.) x y = x <++> y <+-> [BCLeq]
46 (>=.) x y = x <++> y <+-> [BCGeq]
48 instance analogIO ByteCode where
49 analogRead p = BC [BCAnalogRead $ toCode p]
50 analogWrite p b = b <+-> [BCAnalogWrite $ toCode p]
52 instance digitalIO ByteCode where
53 digitalRead p = BC [BCDigitalRead $ toCode p]
54 digitalWrite p b = b <+-> [BCDigitalWrite $ toCode p]
56 instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e
57 instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e
58 instance If ByteCode Stmt Stmt e where If b t e = BCIfStmt b t e
59 instance If ByteCode x y Expr where If b t e = BCIfStmt b t e
60 instance IF ByteCode where
61 IF b t e = BCIfStmt b t e
62 (?) b t = BCIfStmt b t $ BC []
63 BCIfStmt b t e = b <+-> [BCJmpF $ length <$> t + 1] <++> t
64 <+-> [BCJmp $ length <$> e] <++> e
66 instance noOp ByteCode where noOp = BC []
68 instance serial ByteCode where
69 serialAvailable = BC [BCSerialAvail]
70 serialPrint s = BC [BCSerialPrint]
71 serialPrintln s = BC [BCSerialPrintln]
72 serialRead = BC [BCSerialRead]
73 serialParseInt = BC [BCSerialParseInt]
76 (<++>) (BC x) (BC y) = BC $ x ++ y
78 (<+->) (BC x) y = BC $ x ++ y
80 (<-+>) x (BC y) = BC $ x ++ y
85 instance zero BCState where
88 //Start :: ByteCode Int Expr
89 //Start = (lit 36 +. lit 42) +. lit 84
91 //Run test programma en pretty print
92 Start :: ByteCode Int Expr
93 Start = If (lit True) (analogRead A1) (analogRead A0)
94 //Start = If ((lit 36) ==. (lit 42)) (noOp) (noOp)
96 //Generate header file
99 //# io = io <<< "#ifndef MTASK_H\n#define MTASK_H\n"
100 //# io = io <<< join "\n" ["#define " <+ toUpperCase (consName{|*|} x) <+ " " <+ consIndex{|*|} x\\x<-allBC]
103 // allBC = conses{|*|}
104 //# (ok, w) = fclose (io <<< "\n#endif\n") w
105 //| not ok = abort "Couldn't close stdio"