aboutsummaryrefslogtreecommitdiff
path: root/src/main.hs
blob: 569ba2855ee66f597e86d74933d2dc9d3dfd1ce4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import System.IO
import Statement
import Parser.Statement
import Parser.Core
import Statement
import Assignment
import Expr
import Evaluation


main = promptLoop []

promptLoop :: Context -> IO ()
promptLoop context = do
    line <- prompt
    if line /= "exit"
       then loop line context >>= promptLoop
       else return ()

loop :: String -> Context -> IO Context
loop line context = do s <- parseIO line
                       context <- printStatement s context
                       putStrLn $ show context
                       return context

prompt :: IO String
prompt = do putStr "> "
            hFlush stdout
            getLine

parseIO :: String -> IO Statement
parseIO input = case parseStrict statementP input of
                  Nothing -> fail "Couldn't parse input"
                  Just s  -> return s

printStatement :: Statement -> Context -> IO Context
printStatement (SAssignment a) context = do putStrLn $ show a
                                            return $ update context a
printStatement (SExpr e) context = do putStrLn evalStr
                                      return context
    where evalStr = case eval context e of Nothing -> "Couldn't evaluate expression"
                                           Just a  -> show a