diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-03-16 16:27:39 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-03-16 16:27:39 +0100 |
| commit | 9a4cf15fc0e724e6bc93c6530b47ca45836da5ba (patch) | |
| tree | bb1c91857d632be1e61ad92d2ce69243cc710741 /src/main.hs | |
| parent | 8c8f6155f1b05230c271059c52a503211aec872b (diff) | |
| download | computorv2-9a4cf15fc0e724e6bc93c6530b47ca45836da5ba.tar.gz computorv2-9a4cf15fc0e724e6bc93c6530b47ca45836da5ba.tar.bz2 computorv2-9a4cf15fc0e724e6bc93c6530b47ca45836da5ba.zip | |
variable and function context between prompts
Diffstat (limited to 'src/main.hs')
| -rw-r--r-- | src/main.hs | 31 |
1 files changed, 24 insertions, 7 deletions
diff --git a/src/main.hs b/src/main.hs index 34a190a..569ba28 100644 --- a/src/main.hs +++ b/src/main.hs @@ -2,17 +2,26 @@ import System.IO import Statement import Parser.Statement import Parser.Core +import Statement +import Assignment +import Expr +import Evaluation + +main = promptLoop [] -main = do +promptLoop :: Context -> IO () +promptLoop context = do line <- prompt - loop line + if line /= "exit" + then loop line context >>= promptLoop + else return () -loop :: String -> IO () -loop "exit" = return () -loop line = do s <- parseIO line - putStrLn $ show s - main +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 "> " @@ -23,3 +32,11 @@ 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 |
