----------------------------------------------------------------------------
-- |
-- Module      :  CSPM.Interpreter
-- Copyright   :  (c) Fontaine 2008
-- License     :  BSD
-- 
-- Maintainer  :  Fontaine@cs.uni-duesseldorf.de
-- Stability   :  experimental
-- Portability :  GHC-only
--
-- An API for the Interpreter.
--
----------------------------------------------------------------------------

module CSPM.Interpreter
(
   runFile
  ,evalTest
  ,getAllEvents
  ,prepareAST
  ,runInterpreter
  ,module CSPM.Interpreter.Types
  ,module CSPM.Interpreter.Bindings
  ,module CSPM.Interpreter.CoreInstances
)
where

import CSPM.Interpreter.Types
import CSPM.Interpreter.CoreInstances ()
import CSPM.Interpreter.Bindings
import CSPM.Interpreter.Eval
import CSPM.Interpreter.Hash (hs)
import CSPM.Interpreter.PrepareAST
import CSPM.Interpreter.Test.CLI

import Language.CSPM.AST as AST

import Data.IntMap as IntMap

-- | Run the interpreter for a given module and top-level identifier.
runInterpreter :: AST.LModule -> AST.UniqueIdent -> IO (Process,Env)
runInterpreter ast entry = do
  initEnv <- initialEnvirionment
  let
    env = processDeclList (hs "TopLevelEnvirionment") initEnv
          $ AST.moduleDecls $ unLabel ast
    val :: Value
    val = (IntMap.!) (getLetBindings env) $ AST.uniqueIdentId entry
  case val of
    VProcess x -> return (x,env)
    _ -> throwTypingError "entrypoint is not a CSPM-process" (Just $ AST.bindingLoc entry)
           $  Just val