started with interpreter for basic language
[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 import StdFile
9 import StdString
10
11 from StdFunc import o
12 import StdTuple
13 import Data.Tuple
14 import StdList
15 from Data.Func import $
16 from Text import class Text(join), instance Text String
17
18
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 instance noOp ByteCode where noOp = BC []
57
58
59 (<++>) infixl 7
60 (<++>) (BC x) (BC y) = BC $ x ++ y
61 (<+->) infixl 7
62 (<+->) (BC x) y = BC $ x ++ y
63 (<-+>) infixl 7
64 (<-+>) x (BC y) = BC $ x ++ y
65
66 (<$>) infixl 9
67 (<$>) f (BC x) = f x
68
69 instance serial ByteCode where
70 serialAvailable = undef
71 serialPrint _ = undef
72 serialPrintln _ = undef
73 serialRead = undef
74 serialParseInt = undef
75
76 instance zero BCState where
77 zero = {a=()}
78
79 //Start :: ByteCode Int Expr
80 //Start = (lit 36 +. lit 42) +. lit 84
81 //
82 derive consIndex BC
83 derive consName BC
84
85 Start w
86 # (io, w) = stdio w
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"
91 = w
92 where
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]