From 2b0b62b44a87536597050c525322c7bcc745bdb2 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 3 Jun 2020 22:57:10 +0200 Subject: Added polynom solver from computorv1, Added matrix multipilcation --- src/Main.hs | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 src/Main.hs (limited to 'src/Main.hs') diff --git a/src/Main.hs b/src/Main.hs new file mode 100644 index 0000000..1f99b39 --- /dev/null +++ b/src/Main.hs @@ -0,0 +1,61 @@ +import Data.Char +import qualified Data.Map as M +import System.IO + +import Expr as E +import Parser.Core +import Parser.Statement + + +main :: IO () +main = promptLoop (Context M.empty M.empty) + +promptLoop :: Context -> IO () +promptLoop context = do + putStr "> " + hFlush stdout + eof <- isEOF + if eof + then return () + else do line <- getLine + if line /= "exit" + then loop line context >>= promptLoop + else return () + +loop :: String -> Context -> IO Context +loop input context = + do + case runParserStrict statementP (filter (not . isSpace) input) of + Left err -> putStrLn ("Error parsing: " ++ err) >> return context + Right s -> Main.eval s context + + + +eval :: Statement -> Context -> IO Context + +eval (Evaluation e) c = do case E.eval e c of + Just evaluated -> putStrLn $ show evaluated + Nothing -> putStrLn "Error: couldn't evaluate expression" + return c + +eval (VariableDeclaration name value) (Context vars funcs) = + case E.eval value context of + Just e -> do putStrLn $ show e + return $ Context (M.insert name e vars) funcs + Nothing -> putStrLn "Error: couldn't evaluate expression" >> return context + where context = Context vars funcs + +eval (FunctionDeclaration name argName e) (Context vars funcs) = + -- case evalIgnore e context argName of + -- Just e -> do putStrLn $ show e + -- return $ Context vars (M.insert name (argName, e) funcs) + -- Nothing -> putStrLn "Error: couldn't evaluate expression" >> return context + -- + -- where context = Context vars funcs + return $ Context vars (M.insert name (argName, e) funcs) + +eval _ c = return c +-- eval (PolynomEvaluation left right) c = do l <- eval left -- count number of unknoewn +-- r <- eval right + + -- cgit