1 module Main where
    2 
    3 import Ast
    4 import Parser
    5 import Env
    6 import Print
    7 import Eval
    8 import BasicNumber -- partain
    9 import IO --(isEOF,hFlush,stdout)
   10 
   11 ----------------------------------------------------------------------------
   12 
   13 -- command line prompt
   14 prompt :: String
   15 prompt = "-> "
   16 ----------------------------------------------------------------------------
   17 
   18 main :: IO ()
   19 main = cmdloop (initEnv [])
   20   where
   21     cmdloop env = do
   22         putStr prompt
   23         hFlush stdout
   24         l <- getLine
   25         if l == "exit" then 
   26            return ()
   27          else do
   28            let (res, nenv) = cmd_process (l, env)
   29            putStrLn res
   30            cmdloop nenv
   31 
   32 ----------------------------------------------------------------------------
   33 
   34 -- process the command line
   35 cmd_process :: (String, Env) -> (String, Env)
   36 cmd_process (c,e) = 
   37         case c of
   38         "env"   -> ((printEnv e e),e)
   39         "clear" -> ("",initEnv [])
   40         "?"    -> (printHelp, e)
   41         _      -> 
   42                 case ast of
   43                 (Set evar bexpr)     -> (printAst ast e, enterEnv evar bexpr e)
   44                 (EvalSet evar bexpr) -> (res, enterEnv evar rexpr e)
   45                                 where       rexpr = eval bexpr e
   46                                         res = printAst ast e ++
   47                                               (printBasicExp rexpr e)
   48                 (Eval bexpr)       -> (printBasicExp (eval bexpr e) e, e)
   49                 _                      -> (printAst ast e, e)
   50             where ast = parse c
   51 
   52 ----------------------------------------------------------------------------
   53 
   54 -- print the help menu
   55 printHelp :: String
   56 printHelp = "Commands:\n"++
   57             "clear        clear the environment\n"++
   58             "env          browse through the environment\n"++
   59             "exit         quit\n"++
   60             "?            display this help info\n"++
   61             "$var = 'expr augment the environment\n"++
   62             "$var = expr  eval and assign\n"++
   63             "expr         evaluate the expression\n\n"++ 
   64   "where expr could be any of the following:\n\n"++
   65   "EXP -> BINARY-EXP (EXP1,EXP2)\n"++
   66   "     | UNARY-EXP (EXP)\n"++
   67   "     | EXP1 OP EXP2\n"++
   68   "\n"++
   69   "BINARY-EXP -> add | sub | mul | div | equ\n"++
   70   "            | ne  | lt  | gt | lte | gte\n"++
   71   "\n"++
   72   "UNARY-EXP -> sqrt | real | rat \n"++
   73   "\n"++
   74   "VAR -> a variable name\n"
   75 ----------------------------------------------------------------------------