Initial try, maybe I'll need a state
[mTask.git] / mTaskInterpret.icl
1 implementation module mTaskInterpret
2
3 //import iTasks
4 import gdynamic, gCons, GenEq, StdMisc, StdArray
5 import GenPrint
6 import mTask
7
8 from StdFunc import o
9 import StdTuple
10 import Data.Tuple
11 import Data.Functor
12 import StdList
13 import Control.Applicative
14 import Control.Monad
15 import Data.Monoid
16 import Control.Monad.State
17 import Control.Monad.Identity
18 from Data.Func import $
19
20
21 toByteVal :: BC -> String
22 toByteVal a = undef
23
24 derive gPrint BC
25
26 toReadableByteVal :: BC -> String
27 toReadableByteVal a = printToString a
28
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]
35
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]
46
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
55
56
57 (<++>) infixl 7
58 (<++>) (BC x) (BC y) = BC $ x ++ y
59 (<+->) infixl 7
60 (<+->) (BC x) y = BC $ x ++ y
61 (<-+>) infixl 7
62 (<-+>) x (BC y) = BC $ x ++ y
63
64 (<$>) infixl 9
65 (<$>) f (BC x) = f x
66
67 instance serial ByteCode where
68 serialAvailable = undef
69 serialPrint _ = undef
70 serialPrintln _ = undef
71 serialRead = undef
72 serialParseInt = undef
73
74 instance zero BCState where
75 zero = {a=()}
76
77 //runByteCode :: (ByteCode Int Expr) BCState -> [BC]
78 //runByteCode (BC f) s = evalState f s
79 //runByteCode (BC f) s = fst (f Rd s)
80
81 //Start :: Main (ByteCode Int Expr)
82 //Start :: [BC]
83 Start :: ByteCode Int Expr
84 Start = (lit 36 +. lit 42) +. lit 84