aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-09 16:20:28 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-09 16:20:28 +0100
commitddff2e34a8f5e7fdb4a9e67d4df1edfa393ab3b7 (patch)
tree0ef7eddfeadb0c7201bdc0fd3851350239229b06 /src
parentc27e85f27b8db206da23b813358ea44e302f2419 (diff)
downloadcomputorv1-ddff2e34a8f5e7fdb4a9e67d4df1edfa393ab3b7.tar.gz
computorv1-ddff2e34a8f5e7fdb4a9e67d4df1edfa393ab3b7.tar.bz2
computorv1-ddff2e34a8f5e7fdb4a9e67d4df1edfa393ab3b7.zip
Refactoring file structure
Diffstat (limited to 'src')
-rw-r--r--src/main.hs45
-rw-r--r--src/parser.hs78
2 files changed, 123 insertions, 0 deletions
diff --git a/src/main.hs b/src/main.hs
new file mode 100644
index 0000000..c327d2b
--- /dev/null
+++ b/src/main.hs
@@ -0,0 +1,45 @@
+import System.Environment
+import Data.Char
+
+data TokenType = Number | Add | Sub | Mul | Exp | Equal deriving (Show)
+data Token = Token TokenType Float deriving (Show)
+
+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
diff --git a/src/parser.hs b/src/parser.hs
new file mode 100644
index 0000000..7929c26
--- /dev/null
+++ b/src/parser.hs
@@ -0,0 +1,78 @@
+module Parser where
+
+import Control.Applicative
+import Control.Monad
+import Data.Char
+import Numeric.Natural
+
+
+newtype Parser a = Parser (String -> Maybe (a, String))
+
+parse :: Parser a -> String -> Maybe (a, String)
+parse (Parser p) input = p input
+
+instance Functor Parser where
+ -- fmap :: (a -> b) -> Parser a -> Parser b
+ fmap f (Parser p) = Parser new_p
+ where new_p s = do
+ (x, s') <- p s
+ return (f x, s')
+
+instance Applicative Parser where
+ -- pure :: a -> Parser a
+ pure x = Parser (\s -> Just (x, s))
+ -- (<*>) :: Parser (a -> b) -> Parser a -> Parser b
+ (Parser p1) <*> (Parser p2) = Parser new_p
+ where new_p s = do
+ (f, s') <- p1 s
+ (x, s'') <- p2 s'
+ return (f x, s'')
+
+instance Alternative Parser where
+ -- empty :: Parser a
+ empty = Parser (\_ -> Nothing)
+ -- (<|>) :: Parser a -> Parser a -> Parser a
+ (Parser p1) <|> (Parser p2) = Parser new_p
+ where new_p s = p1 s <|> p2 s
+
+
+charP :: Char -> Parser Char
+charP x = Parser p
+ where p "" = Nothing
+ p (c:cs) = if c == x then Just (c, cs)
+ else Nothing
+
+digitsP :: Parser String
+digitsP = Parser (\s -> Just $ span isDigit s)
+
+sepBy :: Parser a -> Parser b -> Parser [a]
+sepBy x sep = ((:) <$> x <*> many (sep *> x)) <|> pure []
+
+
+-- 1 * X^0 + 2 * X^1 + 1 * 3 * X^2 = 0
+data Equation = Equation { left :: Polynomial, right :: Polynomial } deriving (Show)
+type Polynomial = [Term]
+data Term = Term { coefficient :: Float, exponent :: Natural } deriving (Show)
+
+coefficientP :: Parser Float
+coefficientP = read <$> (floatP <|> digitsP)
+ where floatP = (\i _ f -> (i ++ "." ++ f))
+ <$> digitsP <*> charP '.' <*> digitsP
+
+exponentP :: Parser Natural
+exponentP = read <$> digitsP
+
+termP :: Parser Term
+termP = (\coef _ exp -> Term coef exp)
+ <$> coefficientP
+ <*> (charP '*' *> charP 'X' *> charP '^')
+ <*> exponentP
+
+polynomialP :: Parser Polynomial
+polynomialP = sepBy termP (charP '+')
+
+equationP :: Parser Equation
+equationP = (\l _ r -> Equation l r)
+ <$> polynomialP
+ <*> charP '='
+ <*> polynomialP