aboutsummaryrefslogtreecommitdiff
path: root/src/main.hs
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-09 19:51:28 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-09 19:51:28 +0100
commit3bfad391d39d2c23cc01ca4c5ef5faa28575a346 (patch)
treee3c435df6dc31072f09bd3eabafb66a874dd3aef /src/main.hs
parentddff2e34a8f5e7fdb4a9e67d4df1edfa393ab3b7 (diff)
downloadcomputorv1-3bfad391d39d2c23cc01ca4c5ef5faa28575a346.tar.gz
computorv1-3bfad391d39d2c23cc01ca4c5ef5faa28575a346.tar.bz2
computorv1-3bfad391d39d2c23cc01ca4c5ef5faa28575a346.zip
Argument Checking, equation evaluation
Diffstat (limited to 'src/main.hs')
-rw-r--r--src/main.hs67
1 files changed, 27 insertions, 40 deletions
diff --git a/src/main.hs b/src/main.hs
index c327d2b..4b365a2 100644
--- a/src/main.hs
+++ b/src/main.hs
@@ -1,45 +1,32 @@
import System.Environment
-import Data.Char
+import Data.List
-data TokenType = Number | Add | Sub | Mul | Exp | Equal deriving (Show)
-data Token = Token TokenType Float deriving (Show)
+import Parser
+import Equation
+main :: IO ()
main = do
args <- getArgs
- -- putStr $ show args
- let l = lexer $ (head args)
- putStrLn $ show l
-
-
-lexer :: String -> [Token]
-lexer "" = []
-lexer (c:rest)
- | c == ' ' = lexer rest
- | isDigit c = (Token Number (read (isolateFloat (c:rest)) :: Float)) : lexer (afterFloat (c:rest))
- | c == '+' = (Token Add 0.0) : lexer rest
- | c == '-' = (Token Sub 0.0) : lexer rest
- | c == '*' = (Token Mul 0.0) : lexer rest
- | c == '^' = (Token Exp 0.0) : lexer rest
- | c == '=' = (Token Equal 0.0) : lexer rest
-
- where isolateFloat :: String -> String
- isolateFloat "" = ""
- isolateFloat (c:cs)
- | isDigit c = c : isolateFloat cs
- | c == '.' = c : isolateFloat cs
- | otherwise = ""
-
- afterFloat :: String -> String
- afterFloat "" = ""
- afterFloat (c:cs)
- | isDigit c = afterFloat cs
- | c == '.' = afterFloat cs
- | otherwise = (c:cs)
-
-
--- parse :: Lexing -> SyntaxTree
--- parse s = 2
---
---
--- eval :: SyntaxTree -> Maybe [Float]
--- eval _ = 0.0
+ checkArgs args
+ equ <- checkParsing (head args)
+ let reduced = reduce equ
+ putStrLn $ "Reduced From: " ++ show reduced
+ putSolutions (left reduced)
+
+
+checkArgs :: [String] -> IO ()
+checkArgs args
+ | length args == 0 = fail "Usage ./computor equation"
+ | length args > 1 = fail "Too many arguments"
+ | otherwise = return ()
+
+checkParsing :: String -> IO Equation
+checkParsing input = case parse Parser.equationP input
+ of Nothing -> fail "Couldnt parse equation"
+ Just (equ, "") -> return equ
+ Just (_, s) -> fail "Couldnt parse equation yo"
+
+putSolutions :: Polynomial -> IO ()
+putSolutions p
+ | degree p > 2 = fail "The polynomial degree is strictly greater then 2, can't solve."
+ | otherwise = putStr $ intercalate "\n" (map show (solve p))