summaryrefslogtreecommitdiff
path: root/src/Transfer/InterpreterAPI.hs
blob: 2fe04e8f373f33ebb320374ab765238061f0ce59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module Transfer.InterpreterAPI (Env, builtin,
                                load, loadFile, 
                                evaluateString, evaluateExp
                               ) where

import Transfer.Core.Abs
import Transfer.Core.Lex
import Transfer.Core.Par
import Transfer.Core.Print
import Transfer.Interpreter
import Transfer.ErrM

-- | Read a transfer module in core format from a string.
load :: Monad m => 
        String -- ^ Input source name, for error messages.
     -> String -- ^ Module contents.
     -> m Env
load n s = case pModule (myLexer s) of
              Bad e -> fail $ "Parse error in " ++ n ++ ": " ++ e
              Ok  m -> return $ addModuleEnv builtin m

-- | Read a transfer module in core format from a file.
--   Fails in the IO monad if there is a problem loading the file.
loadFile :: FilePath -> IO Env
loadFile f = readFile f >>= load f

-- | Read a transfer expression from a string and evaluate it.
--   Returns the result as a string.
evaluateString :: Monad m => Env -> String -> m String
evaluateString env s = 
    case pExp (myLexer s) of
        Bad e -> fail $ "Parse error: " ++ e
        Ok  e -> do
                 let v = eval env e
                 return $ printValue v

-- | Evaluate an expression in the given environment.
evaluateExp :: Env -> Exp -> Exp
evaluateExp env exp = valueToExp $ eval env exp