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 p]
50 analogWrite p b = b <+-> [BCAnalogWrite p]
52 instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e
53 instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e
54 //instance If ByteCode Stmt Stmt e where If b t e = BCIfStmt b t e
55 instance If ByteCode x y Expr where If b t e = BCIfStmt b t e
56 instance IF ByteCode where
57 IF b t e = BCIfStmt b t e
58 (?) b t = BCIfStmt b t $ BC []
59 BCIfStmt b t e = b <+-> [BCJmpF $ length <$> t + 1] <++> t
60 <+-> [BCJmp $ length <$> e] <++> e
62 instance noOp ByteCode where noOp = BC []
64 instance serial ByteCode where
65 serialAvailable = BC [BCSerialAvail]
66 serialPrint s = BC [BCSerialPrint]
67 serialPrintln s = BC [BCSerialPrintln]
68 serialRead = BC [BCSerialRead]
69 serialParseInt = BC [BCSerialParseInt]
72 (<++>) (BC x) (BC y) = BC $ x ++ y
74 (<+->) (BC x) y = BC $ x ++ y
76 (<-+>) x (BC y) = BC $ x ++ y
81 instance zero BCState where
84 //Start :: ByteCode Int Expr
85 //Start = (lit 36 +. lit 42) +. lit 84
87 (<+) infixr 5 :: a b -> String | toString a & toString b
88 (<+) a b = toString a +++ toString b
90 //Run test programma en pretty print
91 Start :: ByteCode Int Expr
93 //Start = If ((lit 36) ==. (lit 42)) (noOp) (noOp)
95 //Generate header file
98 //# io = io <<< "#ifndef MTASK_H\n#define MTASK_H\n"
99 //# io = io <<< join "\n" ["#define " <+ toUpperCase (consName{|*|} x) <+ " " <+ consIndex{|*|} x\\x<-allBC]
102 // allBC = conses{|*|}
103 //# (ok, w) = fclose (io <<< "\n#endif\n") w
104 //| not ok = abort "Couldn't close stdio"