1 implementation module mTaskInterpret
4 import gdynamic, gCons, GenEq, StdMisc, StdArray
15 from Data.Func import $
16 from Text import class Text(join), instance Text String
21 toByteVal :: BC -> String
26 toReadableByteVal :: BC -> String
27 toReadableByteVal a = printToString a
29 instance arith ByteCode where
30 lit x = BC [BCPush $ toCode x]
31 (+.) x y = x <++> y <+-> [BCAdd]
32 (-.) x y = x <++> y <+-> [BCSub]
33 (*.) x y = x <++> y <+-> [BCMul]
34 (/.) x y = x <++> y <+-> [BCDiv]
36 instance boolExpr ByteCode where
37 (&.) x y = x <++> y <+-> [BCAnd]
38 (|.) x y = x <++> y <+-> [BCOr]
39 Not x = x <+-> [BCNot]
40 (==.) x y = x <++> y <+-> [BCEq]
41 (!=.) x y = x <++> y <+-> [BCNeq]
42 (<.) x y = x <++> y <+-> [ BCLes]
43 (>.) x y = x <++> y <+-> [BCGre]
44 (<=.) x y = x <++> y <+-> [BCLeq]
45 (>=.) x y = x <++> y <+-> [BCGeq]
47 instance If ByteCode Stmt Stmt Stmt where If b t e = BCIfStmt b t e
48 instance If ByteCode Stmt e Stmt where If b t e = BCIfStmt b t e
49 //instance If ByteCode Stmt Stmt e where If b t e = BCIfStmt b t e
50 //instance If ByteCode Stmt x y where If b t e = BCIfStmt b t e
51 instance IF ByteCode where
52 IF b t e = BCIfStmt b t e
53 (?) b t = BCIfStmt b t $ BC []
54 BCIfStmt b t e = b <+-> [BCJmpF $ length <$> t + 1] <++> t <+-> [BCJmp $ length <$> e] <++> e
56 instance noOp ByteCode where noOp = BC []
60 (<++>) (BC x) (BC y) = BC $ x ++ y
62 (<+->) (BC x) y = BC $ x ++ y
64 (<-+>) x (BC y) = BC $ x ++ y
69 instance serial ByteCode where
70 serialAvailable = undef
72 serialPrintln _ = undef
74 serialParseInt = undef
76 instance zero BCState where
79 //Start :: ByteCode Int Expr
80 //Start = (lit 36 +. lit 42) +. lit 84
87 # io = io <<< "#ifndef MTASK_H\n#define MTASK_H\n"
88 # io = io <<< join "\n" ["#define " +++ consName{|*|} x +++ " " +++ toString (consIndex{|*|} x)\\x<-allBC]
89 # (ok, w) = fclose (io <<< "\n#endif\n") w
90 | not ok = abort "Couldn't close stdio"
93 allBC = [BCNop, BCPush "", BCPop, BCNeg, BCNot, BCAdd, BCSub, BCMul,
94 BCDiv, BCAnd, BCOr, BCEq, BCNeq, BCLes, BCGre, BCLeq, BCGeq,
95 BCJmp 0, BCJmpT 0, BCJmpF 0]