added live updates

This commit is contained in:
2017-05-03 13:26:15 +02:00
parent d396dc0978
commit 2aa996e963
2 changed files with 20 additions and 2 deletions

View File

@@ -22,6 +22,7 @@ treeEval (x@(BOp _):xy) [] nums = treeEval xy [x] nums
handleOp :: [Value] -> [Value] -> [Value] -> Value handleOp :: [Value] -> [Value] -> [Value] -> Value
handleOp expr (op:ops) (op1:(op2:nums)) = handleOp expr (op:ops) (op1:(op2:nums)) =
treeEval expr ops ((evalOp op 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 :: Value -> Value -> Value -> Value
evalOp (BOp "*") (BInt x) (BInt y) = BInt $ x * y evalOp (BOp "*") (BInt x) (BInt y) = BInt $ x * y

View File

@@ -24,11 +24,28 @@ output out =
else putStrLn $ returnStr ++ show (eval res) 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 :: IO (Maybe String)
readline = do readline = do
putStr promptStr putStr promptStr
read' "" read' ""
where read' acc = do where read' acc = do
printStatus acc
c <- getChar c <- getChar
case c of case c of
'\EOT' -> return Nothing '\EOT' -> return Nothing
@@ -52,8 +69,8 @@ prompt :: IO ()
prompt = do prompt = do
input <- readline input <- readline
case input of case input of
Nothing -> putStrLn "Bye!" Nothing -> putStrLn "\nBye!"
Just "quit" -> putStrLn "Bye!" Just "quit" -> putStrLn "\nBye!"
Just str -> do Just str -> do
putStrLn "" putStrLn ""
output str output str