diff options
author | Jed Barber <jjbarber@y7mail.com> | 2014-12-06 09:10:25 +1100 |
---|---|---|
committer | Jed Barber <jjbarber@y7mail.com> | 2014-12-06 09:10:25 +1100 |
commit | b7c93bf60c350af983e44d5ee778fd459fa60e97 (patch) | |
tree | 567e9e54c4cbbce54b1a386549a9445e05bb01b7 | |
parent | ef7ed9cc9292554a90e644c92533cfc0e2a50896 (diff) |
Skeleton code for interpreter added
-rw-r--r-- | src/Grasp/Interpreter.hs | 150 | ||||
-rw-r--r-- | src/Grasp/Types/Instruction.hs | 6 |
2 files changed, 155 insertions, 1 deletions
diff --git a/src/Grasp/Interpreter.hs b/src/Grasp/Interpreter.hs new file mode 100644 index 0000000..d6c1f80 --- /dev/null +++ b/src/Grasp/Interpreter.hs @@ -0,0 +1,150 @@ +module Grasp.Interpreter ( + grasp + ) where + + + + +import qualified Control.Monad as Monad + +import qualified Data.Maybe as Maybe + +import Grasp.Monad( GraspM ) +import qualified Grasp.Monad as GMonad + +import Grasp.Types.GNode( GNode ) +import qualified Grasp.Types.GNode as GN + +import Grasp.Types.GEdge( GEdge ) +import qualified Grasp.Types.GEdge as GE + +import Grasp.Types.Instruction( Instruction ) +import qualified Grasp.Types.Instruction as IN + + + + +grasp :: ([GNode],[GEdge]) -> IO ([GNode],[GEdge]) +grasp input = GMonad.finalise $ GMonad.construct input >> interpret + + + +interpret :: GraspM () +interpret = do + n <- GMonad.peekIP + + if (Maybe.isNothing n) then return () else do + case (IN.toString . GN.toInst . Maybe.fromJust $ n) of + "set" -> setI + "new" -> newI + "del" -> delI + "push" -> pushI + "pop" -> popI + "pick" -> pickI + "call" -> callI + "ret" -> retI + "add" -> addI + "mul" -> mulI + "sub" -> subI + "div" -> divI + "mod" -> modI + "getc" -> getcI + "putc" -> putcI + "gets" -> getsI + "puts" -> putsI + inst | Maybe.isJust . IN.toInt . IN.mk $ inst -> implicitPushI + x -> error ("Unknown instruction " ++ x) + + GMonad.nextIP + interpret + + + +setI :: GraspM () +setI = GMonad.updateIP + + + +newI :: GraspM () +newI = GMonad.updateIP + + + +delI :: GraspM () +delI = GMonad.updateIP + + + +pushI :: GraspM () +pushI = GMonad.updateIP + + + +implicitPushI :: GraspM () +implicitPushI = GMonad.updateIP + + + +popI :: GraspM () +popI = GMonad.updateIP + + + +pickI :: GraspM () +pickI = GMonad.updateIP + + + +callI :: GraspM () +callI = GMonad.updateIP + + + +retI :: GraspM () +retI = GMonad.updateIP + + + +addI :: GraspM () +addI = GMonad.updateIP + + + +mulI :: GraspM () +mulI = GMonad.updateIP + + + +subI :: GraspM () +subI = GMonad.updateIP + + + +divI :: GraspM () +divI = GMonad.updateIP + + + +modI :: GraspM () +modI = GMonad.updateIP + + + +getcI :: GraspM () +getcI = GMonad.updateIP + + + +putcI :: GraspM () +putcI = GMonad.updateIP + + + +getsI :: GraspM () +getsI = GMonad.updateIP + + + +putsI :: GraspM () +putsI = GMonad.updateIP + diff --git a/src/Grasp/Types/Instruction.hs b/src/Grasp/Types/Instruction.hs index 0ff4002..e618222 100644 --- a/src/Grasp/Types/Instruction.hs +++ b/src/Grasp/Types/Instruction.hs @@ -4,7 +4,8 @@ module Grasp.Types.Instruction ( mk, toString, - toFloat + toFloat, + toInt ) where @@ -30,3 +31,6 @@ toString (Instruction i) = i toFloat :: Instruction -> Maybe Float toFloat (Instruction i) = readMaybe i +toInt :: Instruction -> Maybe Int +toInt (Instruction i) = readMaybe i + |