diff options
| author | aarne <aarne@cs.chalmers.se> | 2008-05-20 11:47:44 +0000 |
|---|---|---|
| committer | aarne <aarne@cs.chalmers.se> | 2008-05-20 11:47:44 +0000 |
| commit | 31bf84122b21efb444aa8d055472e166ffb90783 (patch) | |
| tree | 1f051909336f1534346bcccde8dda59beab02f64 /src-2.9/Transfer/InterpreterAPI.hs | |
| parent | 74f048dcf41de3540778de54dfa7541fa5b39c46 (diff) | |
moved all old source code to src-2.9 ; src will be for GF 3 development
Diffstat (limited to 'src-2.9/Transfer/InterpreterAPI.hs')
| -rw-r--r-- | src-2.9/Transfer/InterpreterAPI.hs | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/src-2.9/Transfer/InterpreterAPI.hs b/src-2.9/Transfer/InterpreterAPI.hs new file mode 100644 index 000000000..2fe04e8f3 --- /dev/null +++ b/src-2.9/Transfer/InterpreterAPI.hs @@ -0,0 +1,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 |
