From 968851b7509533b68f25fd0f7f99770b1f7d1220 Mon Sep 17 00:00:00 2001 From: Jed Barber Date: Wed, 23 Jul 2014 01:42:22 +1000 Subject: getc instruction added --- src/Grasp/Interpreter.hs | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) 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) -- cgit