]> ruin.nu Git - proglang.git/blobdiff - Interpret.hs
compiler seems to be working
[proglang.git] / Interpret.hs
index 1938e55308263f3076063e629d8e12d998f17f21..5afcefb10b28cd6ac4a390e71958499b5cfdc956 100644 (file)
@@ -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