public
Description: Haskell implemented JavaScript interpreter
Homepage:
Clone URL: git://github.com/motemen/jusk.git
jusk / Repl.hs
100644 45 lines (40 sloc) 1.414 kb
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
40
41
42
43
44
45
{-
Repl.hs
-}
 
module Repl where
import Control.Monad.Cont
import Control.Monad.State
import IO
 
import Parser
import ParserUtil
import Context
import Eval
import DataTypes
 
runReplWithTry :: Evaluate ()
runReplWithTry =
    do e <- callCC $ \cc -> do
                pushCont cc CThrow
                return Void
       case e of
            Void -> runRepl'
            e -> do liftIO $ ePutStrLn "uncaught exception:"
                     liftIO . ePutStrLn =<< toString e
                     runReplWithTry
 
runRepl' :: Evaluate ()
runRepl' =
    do line <- liftIO (putStr "js> " >> hFlush stdout >> getLine)
       value <- evalWithMoreInput line
       unless (isVoid value || isUndefined value)
              (do string <- toString value
                  liftIO $ putStrLn string
                  env <- getEnv
                  when (Debug `elem` envFlags env)
                       (liftIO $ putStrLn $ "-> " ++ inspect value))
       runRepl'
       where evalWithMoreInput input =
                 case parse input of
                      Left err | isErrorAtEnd input err
                                 -> do line <- liftIO (putStr "**> " >> hFlush stdout >> getLine)
                                       evalWithMoreInput $ input ++ "\n" ++ line
                      Left err | otherwise -> liftIO $ printParseError input err
                      Right program -> evalProgram program