module Calc where
import Stack (Stack)
import qualified Stack
import System.IO
data Cmd = Push Int
| Pop
| Add
deriving (Show, Read)
exec :: Cmd -> Stack Int -> Stack Int
exec (Push i) s = Stack.push i s
exec Pop s = snd $ Stack.pop s
exec Add s =
let (x, s1) = Stack.pop s in
let (y, s2) = Stack.pop s1 in
Stack.push (x + y) s2
calc :: Stack Int -> IO ()
calc s = do
putStrLn $ "stack: " ++ show s
putStr "> "
hFlush stdout
l <- getLine
if l == "exit" then return ()
else calc (exec (read l) s)
main = calc Stack.empty