summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJed Barber <jjbarber@y7mail.com>2014-08-30 22:06:50 +1000
committerJed Barber <jjbarber@y7mail.com>2014-08-30 22:06:50 +1000
commitafde234fd91c2037025b8d44d8c78c69e2b70574 (patch)
treeee316fc8da3c3b57ecb6a5d66168af483471b86a
parentedcb54083308a86a9a73bf9930a41891986fa830 (diff)
Implicit push instruction added
-rw-r--r--src/Grasp/Interpreter.hs41
1 files changed, 29 insertions, 12 deletions
diff --git a/src/Grasp/Interpreter.hs b/src/Grasp/Interpreter.hs
index 5781f73..4d9853c 100644
--- a/src/Grasp/Interpreter.hs
+++ b/src/Grasp/Interpreter.hs
@@ -212,20 +212,40 @@ pushI g node =
in if (length stackN /= 1) then error ("Instruction " ++ (show node) ++
" should only have one stack argument")
else do
- let newN = head (Graph.newNodes 1 g)
- labelN <- if (inL == []) then return ""
+ label <- if (inL == []) then return ""
else (getStdRandom (randomR (0,length inL - 1))) >>= (\x -> return (inL !! x))
- let edgesToDel = Graph.inn g (head stackN)
- edgesToAdd = map (\(x,y,z) -> (x,newN,z)) edgesToDel
+ doPushI g (head stackN) label
- nextE = (newN,(head stackN),"next")
- g' = Graph.insNode (newN,labelN) g
- g'' = foldl' (flip Graph.delLEdge) g' edgesToDel
- g''' = Graph.insEdges (nextE:edgesToAdd) g''
- return g'''
+implicitPushI :: GraspProgram -> Node -> IO GraspProgram
+implicitPushI g node =
+ let edges = Graph.out g node
+
+ stackN = targetNodes (getByLabel "stack" edges)
+ label = fromJust $ Graph.lab g node
+
+ in if (length stackN /= 1) then error ("Instruction " ++ (show node) ++
+ " should only have one stack argument")
+ else doPushI g (head stackN) label
+
+
+
+doPushI :: GraspProgram -> Node -> String -> IO GraspProgram
+doPushI g s l =
+ let newN = head (Graph.newNodes 1 g)
+
+ edgesToDel = Graph.inn g s
+ edgesToAdd = map (\(x,y,z) -> (x,newN,z)) edgesToDel
+
+ nextE = (newN,s,"next")
+
+ g' = Graph.insNode (newN,l) g
+ g'' = foldl' (flip Graph.delLEdge) g' edgesToDel
+ g''' = Graph.insEdges (nextE:edgesToAdd) g''
+
+ in return g'''
@@ -380,6 +400,3 @@ getsI g node = return g
putsI :: GraspProgram -> Node -> IO GraspProgram
putsI g node = return g
-implicitPushI :: GraspProgram -> Node -> IO GraspProgram
-implicitPushI g node = return g
-