[main repl much better jupdike@gmail.com**20080208180014] { hunk ./Main.lhs 3 ----------------------------------------------------------------------- - hunk ./Main.lhs 6 ----------------------------------------------------------------------- - ----------------------------------------------------------------------- - - > import Support - > import IOSupport - > - hunk ./Main.lhs 8 +> import System.Console.Readline hunk ./Main.lhs 30 - hunk ./Main.lhs 31 - hunk ./Main.lhs 33 +> data Theme = Theme { prompt, helpString, leavingMessage :: String } + hunk ./Main.lhs 39 -> main = readTable "integrals" >>= \t -> -> putStrLn "for help type /help" >> -> commandLineInterpreter -> "> " commands evaluate (Var "x", t) +> isQuit ("exit") = True +> isQuit (':':'q':_) = True +> isQuit _ = False hunk ./Main.lhs 43 ----------------------------------------------------------------------- +> isHelp (':':'h':_) = True +> isHelp ("?") = True +> isHelp _ = False hunk ./Main.lhs 47 ----------------------------------------------------------------------- +> version = "0.0.2" +> startMessage = "calc "++version++"\n"++ +> "exit or :q or ^D to quit" ++"\n"++ +> "? or :h or :help for help" + +> main = do +> tab <- readTable "integrals" +> putStrLn startMessage +> loop (Theme {prompt=">>> ", leavingMessage="Leaving calc", helpString=help}) evaluate (Var "x", tab) + +> loop :: Theme -> (st -> String -> IO st) -> st -> IO () +> loop theme f st = do +> s <- readline (prompt theme) +> case s of +> Nothing -> putStrLn $ "\n" ++ (leavingMessage theme) +> Just str -> do +> if isHelp str +> then do putStrLn (helpString theme) +> loop theme f st +> else do +> if isQuit str +> then putStrLn (leavingMessage theme) +> else do +> myAddHistory str +> st' <- f st str +> loop theme f st' +> myAddHistory x = if x == "" then return () else addHistory x +> myPutStrLn x = if x == "" then return () else putStrLn x hunk ./Main.lhs 116 - - - - - - ----------------------------------------------------------------------- - hunk ./Main.lhs 118 ----------------------------------------------------------------------- - ----------------------------------------------------------------------- - hunk ./Main.lhs 121 ----------------------------------------------------------------------- - -Abarbeitung von Kommandos -Standardformen zu `expression'!!! - -Execution for commands -standardforms for 'expression' - ----------------------------------------------------------------------- - -> commands = [ -> (["/exit", "/quit"], \_ _ -> -> exit), -> (["?", "/help"], \s _ -> -> help >> return s)] - ----------------------------------------------------------------------- - ----------------------------------------------------------------------- - -> help = sequence (map putStr [ -> " enter expression\n", -> "' differentiate (or current expression)\n", -> "| integrate (or current expression)\n", -> "[/] replace by in (or current)\n", -> "lim ... \n", -> "st2 standard form 2\n", -> "st3 standard form 3 [n]\n", -> "? display this command summary\n", -> "/exit quit the system\n", -> "/help display this command summary\n", -> "/quit quit the system\n"]) - ----------------------------------------------------------------------- - -Hilfsfunktionen -Helper functions - ----------------------------------------------------------------------- +> help = unlines [ +> " enter expression", +> "' differentiate (or last entered expression)", +> "| integrate (or last entered expression)", +> "[/] replace by in (or last)", +> "lim ... ", +> "st2 standard form 2", +> "st3 standard form 3 of ordern [n]", +> "? or :help or :h display this command summary", +> "exit or :quit or :q or ^D exit the system"] hunk ./Main.lhs 134 ----------------------------------------------------------------------- - ----------------------------------------------------------------------- - hunk ./Main.lhs 140 ----------------------------------------------------------------------- - - addfile ./REPL.hs hunk ./REPL.hs 1 +module REPL + +where + +-- import Calc + +import System.Console.Readline +import System + +-- import Useful + + +{- +untag ('<':xs) = intag xs +untag (x:xs) = x : untag xs +untag [] = [] +intag ('>':xs) = untag xs +intag (x:xs) = intag xs +intag [] = [] +-} + +main = do + path <- checkPath "units.txt" + cnts <- readFile path + hs <- newHashes + errors <- mapM (\s -> run hs s) (lines cnts) + mapM_ myPutStrLn errors + putStrLn startMessage + loop (myF hs) -- (untag . calc) + + +prompt = ">>> " + +isQuit (':':'q':_) = True +isQuit _ = False + +-- isHelp (':':'h':_) = True +-- isHelp _ = False + +version = "0.2.1" +startMessage = "calc "++version++"\n"++ + ":q or ^D to quit" +leavingMessage = "Leaving calc" + +loop f = do + s <- readline prompt + case s of + Nothing -> putStrLn $ "\n" ++ leavingMessage + Just str -> do + if isQuit str + then putStrLn leavingMessage + else do + addHistory str + result <- f str + putStrLn result -- $ f str + loop f +myF hs x = do + c <- calc hs x + return $ untag c + +myPutStrLn x = if x == "" then return () else putStrLn $ untag x hunk ./g 8 -ghc -O2 --make Main.lhs +#ghc -O2 --make Main.lhs hunk ./g 10 -#ghc --make Parse.lhs +ghc --make Main.lhs }