diff --git a/src/BC/Eval.hs b/src/BC/Eval.hs index f30b5c1..2d052c7 100644 --- a/src/BC/Eval.hs +++ b/src/BC/Eval.hs @@ -22,6 +22,7 @@ treeEval (x@(BOp _):xy) [] nums = treeEval xy [x] nums handleOp :: [Value] -> [Value] -> [Value] -> Value handleOp expr (op:ops) (op1:(op2:nums)) = treeEval expr ops ((evalOp op op1 op2):nums) +handleOp expr ((BOp op):ops) _ = BErr ("Not enough arguments to operation " ++ op) evalOp :: Value -> Value -> Value -> Value evalOp (BOp "*") (BInt x) (BInt y) = BInt $ x * y diff --git a/src/BC/Prompt.hs b/src/BC/Prompt.hs index bc893bd..b2117ab 100644 --- a/src/BC/Prompt.hs +++ b/src/BC/Prompt.hs @@ -24,11 +24,28 @@ output out = else putStrLn $ returnStr ++ show (eval res) +printStatus :: String -> IO () +printStatus str = + let res = parse str + in if length res == 1 && isErr (res !! 0) + then return () + else + let evald = eval res + out = show evald + in if isErr evald || length out == 0 + then return () + else let str = " \x1b[33m=> " ++ out ++ "\x1b[0m" + in putStr (str ++ repeat '\b' (length str - 9)) + where repeat str 0 = "" + repeat str n = (str:repeat str (n-1)) + + readline :: IO (Maybe String) readline = do putStr promptStr read' "" where read' acc = do + printStatus acc c <- getChar case c of '\EOT' -> return Nothing @@ -52,8 +69,8 @@ prompt :: IO () prompt = do input <- readline case input of - Nothing -> putStrLn "Bye!" - Just "quit" -> putStrLn "Bye!" + Nothing -> putStrLn "\nBye!" + Just "quit" -> putStrLn "\nBye!" Just str -> do putStrLn "" output str