X-Git-Url: https://ruin.nu/git/?p=proglang.git;a=blobdiff_plain;f=Interpret.hs;h=5afcefb10b28cd6ac4a390e71958499b5cfdc956;hp=1938e55308263f3076063e629d8e12d998f17f21;hb=cdcd3b92ee3145e646634d428b02118238d47f33;hpb=08d98a82fabc381a8b101387f39ae5752cfeb314 diff --git a/Interpret.hs b/Interpret.hs index 1938e55..5afcefb 100644 --- a/Interpret.hs +++ b/Interpret.hs @@ -1,4 +1,4 @@ -module Interpret (eval, execute,addFunction, emptyState, Value(..), State(..)) where +module Interpret (interpret, eval, execute,addFunction, emptyState, Value(..), State(..)) where import Abssyntax import Control.Monad.State hiding (State) @@ -21,6 +21,12 @@ type Function = ([Decl],[Stm]) data State = State {variables::Variables,functions::(Map Ident Function),ret::(MVar Value)} +interpret :: [Func] -> [Stm] -> IO () +interpret fun st = do + mv <- newEmptyMVar + runStateT (do mapM Interpret.addFunction fun; mapM execute st) emptyState{ret=mv} + return () + --eval :: (MonadState Variables m) => Exp -> m Value eval :: Exp -> StateT State IO Value eval (EBool b) = return (VBool b) @@ -52,21 +58,13 @@ eval (EFunc i as) = do vs <- mapM eval as state <- get (ds,ss) <- lookup i $ functions state - modify (\s -> s{variables=[empty]}) - addParams vs ds + let m = foldr (\((Decl t i),v) m -> insert i v m) empty $ zip ds vs + in modify (\s -> s{variables=[m]}) mapM_ execute ss `catchError` (\_ -> return ()) - put state v <- lift $ takeMVar $ ret state + put state return v -addParams :: [Value] -> [Decl] -> StateT State IO () -addParams [] [] = return () -addParams (v:vs) ((Decl t i):ds) = do - state <- get - let (m:ms) = variables state in modify (\s -> s{variables=insert i v m:ms }) - addParams vs ds - - getWord :: IO String getWord = do c <- getChar