diff options
author | Jed Barber <jjbarber@y7mail.com> | 2014-07-23 01:42:22 +1000 |
---|---|---|
committer | Jed Barber <jjbarber@y7mail.com> | 2014-07-23 01:42:22 +1000 |
commit | 968851b7509533b68f25fd0f7f99770b1f7d1220 (patch) | |
tree | 4177a3ce4132fe2925db047d7cfc1200092df5bd | |
parent | 5f8fd03bcbe4d98aac3e247a2d39ec275a6fd451 (diff) |
getc instruction added
-rw-r--r-- | src/Grasp/Interpreter.hs | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/Grasp/Interpreter.hs b/src/Grasp/Interpreter.hs index 6662c0f..f100d77 100644 --- a/src/Grasp/Interpreter.hs +++ b/src/Grasp/Interpreter.hs @@ -4,11 +4,13 @@ module Grasp.Interpreter ( import System.Random +import System.IO 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 Data.Char import Grasp.Types import Grasp.Parser @@ -304,7 +306,33 @@ modI g ip = do getcI :: GraspProgram -> IP -> IO (GraspProgram, IP) -getcI g ip = return (g,ip) +getcI g ip = do + let node = fst . head $ ip + edges = Graph.out g node + + outN = targetNodes (getByLabel "out" edges) + fhL = targetLabels g (getByLabel "fh" edges) + nextLN = targetLNodes g (getByLabel "next" edges) + + c <- case fhL of + x | length x == 0 -> getChar >>= return . ord + + x | length x == 1 -> do + h <- openFile (head fhL) ReadMode + c <- hGetChar h + hClose h + return (ord c) + + x -> error ("Instruction " ++ (show node) ++ + " may only have one file handle") + + g' <- return (foldl' (\gr n -> reLabel gr n (show c)) g outN) + + ip' <- updateIP ip nextLN + + return (g',ip') + + putcI :: GraspProgram -> IP -> IO (GraspProgram, IP) putcI g ip = return (g,ip) |