summaryrefslogtreecommitdiff
path: root/src/Grasp/Interpreter.hs
blob: 35a3de0c7c985c765c456d420497d3393a0ca2a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
module Grasp.Interpreter (
    grasp
    ) where


import System.Random
import Text.Read( readMaybe )
import Data.Graph.Inductive.Graph( Node, LNode, LEdge, (&) )
import qualified Data.Graph.Inductive.Graph as Graph
import Data.List
import Data.Maybe
import Grasp.Types
import Grasp.Parser



type IP = [LNode String]




grasp :: GraspProgram -> IO ()
grasp g =
    let ips = map (:[]) (nodesWithName g "grasp:main")
    in interpret g ips



reachable :: GraspProgram -> [IP] -> [Node]
reachable g ips =
    let startNodes = nub . (map fst) $ (namedNodes g) ++ (concat ips)
    in reach g startNodes []



reach :: GraspProgram -> [Node] -> [Node] -> [Node]
reach _ [] f = f
reach g s@(x:xs) f =
    let f' = nub (x:f)
        s' = nub (xs ++ (Graph.suc g x))
        g' = Graph.delNode x g
    in reach g' s' f'



garbageCollect :: GraspProgram -> [IP] -> GraspProgram
garbageCollect g ips =
    let unreachable = (Graph.nodes g) \\ (reachable g ips)
    in Graph.delNodes unreachable g



interpret :: GraspProgram -> [IP] -> IO ()
interpret g ips = if (ips == []) then return () else execute g ips []



execute :: GraspProgram -> [IP] -> [IP] -> IO ()
execute g [] out = interpret g (reverse out)
execute g ([]:ips) out = execute g ips out
execute g (cur:rest) out = do
    (g', cur') <-
            case (snd . head $ cur) of
                "set" -> setI g cur
                "new" -> newI g cur
                "del" -> delI g cur
                "push" -> pushI g cur
                "pop" -> popI g cur
                "pick" -> pickI g cur
                "call" -> callI g cur
                "ret" -> retI g cur
                "add" -> addI g cur
                "mul" -> mulI g cur
                "sub" -> subI g cur
                "div" -> divI g cur
                "mod" -> modI g cur
                "getc" -> getcI g cur
                "putc" -> putcI g cur
                "gets" -> getsI g cur
                "puts" -> putsI g cur

                x | isInteger x -> implicitPushI g cur

                x -> error ("Unknown instruction at " ++ (show x))

    execute g' rest (cur':out)



isInteger :: String -> Bool
isInteger x =
    let check = readMaybe x :: Maybe Int
    in if (isJust check) then True else False



isFloat :: String -> Bool
isFloat x =
    let check = readMaybe x :: Maybe Float
    in if (isJust check) then True else False



reLabel :: GraspProgram -> Node -> String -> GraspProgram
reLabel g n s =
    let (mc,d) = Graph.match n g
        c = fromJust mc
        c' = (\(w,x,y,z) -> (w,x,s,z)) $ c
    in if (isNothing mc) then g else c' & d



getByLabel :: String -> [LEdge String] -> [LEdge String]
getByLabel name = filter (\(_,_,x) -> x == name)



targetLabels :: GraspProgram -> [LEdge String] -> [String]
targetLabels g = map (\(_,x,_) -> fromJust (Graph.lab g x))

targetNodes :: [LEdge String] -> [Node]
targetNodes = map (\(_,x,_) -> x)

targetLNodes :: GraspProgram -> [LEdge String] -> [LNode String]
targetLNodes g = map (\(_,x,_) -> (x, fromJust (Graph.lab g x)) )



updateIP :: IP -> [LNode String] -> IO IP
updateIP _ [] = return []
updateIP ip next =
    getStdRandom (randomR (0,length next)) >>=
    (\x -> return ((next !! x):(tail ip)) )



setI :: GraspProgram -> IP -> IO (GraspProgram, IP)
setI g ip = do
    let edges = Graph.out g (fst . head $ ip)

        inL = targetLabels g (getByLabel "in" edges)
        outN = targetNodes (getByLabel "out" edges)
        nextLN = targetLNodes g (getByLabel "next" edges)
    
    g' <- case inL of
            [] -> return g
            _ -> (getStdRandom (randomR (0,length inL))) >>=
                (\x -> return (foldl' (\g n -> reLabel g n (inL !! x)) g outN) )
    
    ip' <- updateIP ip nextLN

    return (g',ip')



newI :: GraspProgram -> IP -> IO (GraspProgram, IP)
newI g ip = do
    let node = fst . head $ ip
        edges = Graph.out g node

        tailN = targetNodes (getByLabel "tail" edges)
        headN = targetNodes (getByLabel "head" edges)
        labelL = targetLabels g (getByLabel "label" edges)

    g' <- case (tailN, headN, labelL) of
            (x,_,_) | length x /= 1 -> error ("Instruction " ++ (show node) ++
                                              " should only have one tail argument")
            (_,y,_) | length y /= 1 -> error ("Instruction " ++ (show node) ++
                                              " should only have one head argument")
            (_,_,z) | length z /= 1 -> error ("Instruction " ++ (show node) ++
                                              " should only have one label argument")
            (_,_,z) | isFloat (head z) -> error ("Instruction " ++ (show node) ++
                                                 " should have non-numeric label argument")
            (x,y,z) -> return (Graph.insEdge (head x, head y, head z) g)

    ip' <- updateIP ip (targetLNodes g' (getByLabel "next" (Graph.out g' node)))

    return (g',ip')



delI :: GraspProgram -> IP -> IO (GraspProgram, IP)
delI g ip = do
    let node = fst . head $ ip
        edges = Graph.out g node

        tailN = targetNodes (getByLabel "tail" edges)
        headN = targetNodes (getByLabel "head" edges)
        labelL = targetLabels g (getByLabel "label" edges)

        edgesToDel = filter (\(x,y,z) -> x `elem` tailN &&
                                        (headN == [] || y `elem` headN) &&
                                        (labelL == [] || z `elem` labelL)) (Graph.labEdges g)

        g' = foldl' (\gr e -> Graph.delLEdge e gr) g edgesToDel

    ip' <- updateIP ip (targetLNodes g' (getByLabel "next" (Graph.out g' node)))

    return (g',ip')



pushI :: GraspProgram -> IP -> IO (GraspProgram, IP)
pushI g ip = return (g,ip)

popI :: GraspProgram -> IP -> IO (GraspProgram, IP)
popI g ip = return (g,ip)

pickI :: GraspProgram -> IP -> IO (GraspProgram, IP)
pickI g ip = return (g,ip)

callI :: GraspProgram -> IP -> IO (GraspProgram, IP)
callI g ip = return (g,ip)

retI :: GraspProgram -> IP -> IO (GraspProgram, IP)
retI g ip = return (g,ip)

addI :: GraspProgram -> IP -> IO (GraspProgram, IP)
addI g ip = return (g,ip)

mulI :: GraspProgram -> IP -> IO (GraspProgram, IP)
mulI g ip = return (g,ip)

subI :: GraspProgram -> IP -> IO (GraspProgram, IP)
subI g ip = return (g,ip)

divI :: GraspProgram -> IP -> IO (GraspProgram, IP)
divI g ip = return (g,ip)

modI :: GraspProgram -> IP -> IO (GraspProgram, IP)
modI g ip = return (g,ip)

getcI :: GraspProgram -> IP -> IO (GraspProgram, IP)
getcI g ip = return (g,ip)

putcI :: GraspProgram -> IP -> IO (GraspProgram, IP)
putcI g ip = return (g,ip)

getsI :: GraspProgram -> IP -> IO (GraspProgram, IP)
getsI g ip = return (g,ip)

putsI :: GraspProgram -> IP -> IO (GraspProgram, IP)
putsI g ip = return (g,ip)

implicitPushI :: GraspProgram -> IP -> IO (GraspProgram, IP)
implicitPushI g ip = return (g,ip)