-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a8c2613
commit 5873206
Showing
9 changed files
with
278 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,3 +57,5 @@ tests: | |
- -with-rtsopts=-N | ||
dependencies: | ||
- lvtc | ||
- tasty | ||
- tasty-hunit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
{- | ||
-- EPITECH PROJECT, 2023 | ||
-- Leviator compiler | ||
-- File description: | ||
-- Expression | ||
-} | ||
|
||
module Expression ( | ||
Expression (..), | ||
parseExpresion, | ||
) where | ||
|
||
import Parser | ||
import Control.Applicative | ||
import ParseUtil | ||
|
||
data Expression = Function String | Alias String | Comment String | ||
|
||
instance Show Expression where | ||
show (Function str) = "F:`" ++ str ++ "`" | ||
show (Alias str) = "A:`" ++ str ++ "`" | ||
show (Comment str) = "C:`" ++ str ++ "`" | ||
|
||
instance Eq Expression where | ||
(==) (Function str1) (Function str2) = str1 == str2 | ||
(==) (Alias str1) (Alias str2) = str1 == str2 | ||
(==) (Comment str1) (Comment str2) = str1 == str2 | ||
(==) _ _ = False | ||
|
||
parseFunction :: Parser Expression | ||
parseFunction = Function <$> ((++) <$> (parseString "fn " <|> parseString "export fn") <*> parseAllCharUntil "\n};\n") | ||
|
||
parseAlias :: Parser Expression | ||
parseAlias = Alias <$> ((++) <$> parseString "alias " <*> parseAllCharUntil ";\n") | ||
|
||
parseComment :: Parser Expression | ||
parseComment = Comment <$> ((++) <$> parseString "//" <*> parseAllCharUntil "\n") | ||
|
||
parseExpresion :: Parser Expression | ||
parseExpresion = parseAlias <|> parseFunction <|> parseComment |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{- | ||
-- EPITECH PROJECT, 2023 | ||
-- Leviator compiler | ||
-- File description: | ||
-- ParseUtil | ||
-} | ||
|
||
module ParseUtil ( | ||
parseChar, | ||
parseAnyChar, | ||
parseUInt, | ||
parseSign, | ||
parseInt, | ||
parseString, | ||
parseBetween, | ||
parseAfter, | ||
parseBefore, | ||
parseAllCharUntil, | ||
) where | ||
|
||
import Parser | ||
import Data.Int (Int32) | ||
import Control.Applicative | ||
|
||
parseChar :: Char -> Parser Char | ||
parseChar c = Parser f | ||
where | ||
f [] = Nothing | ||
f (x:xs) | x == c = Just (x, xs) | ||
| otherwise = Nothing | ||
|
||
parseAnyChar :: String -> Parser Char | ||
parseAnyChar str = Parser f | ||
where | ||
f [] = Nothing | ||
f (x:xs) | x `elem` str = Just (x, xs) | ||
| otherwise = Nothing | ||
|
||
parseString :: String -> Parser String | ||
parseString value = Parser f | ||
where | ||
f s | take (length value) s == value = Just (value, drop (length value) s) | ||
| otherwise = Nothing | ||
|
||
parseUInt :: Parser Int32 | ||
parseUInt = read <$> some (parseAnyChar "0123456789") | ||
|
||
parseSign :: Parser Int32 | ||
parseSign = f <$> many (parseAnyChar "-+") | ||
where | ||
f s | even (length (filter (== '-') s)) = 1 | ||
| otherwise = -1 | ||
|
||
parseInt :: Parser Int32 | ||
parseInt = (*) <$> parseSign <*> parseUInt | ||
|
||
parseBetween :: Parser a -> Parser b -> Parser c -> Parser c | ||
parseBetween open close parser = open *> parser <* close | ||
|
||
parseAfter :: Parser a -> Parser b -> Parser b | ||
parseAfter open parser = open *> parser | ||
|
||
parseBefore :: Parser a -> Parser b -> Parser a | ||
parseBefore parser close = parser <* close | ||
|
||
parseAllCharUntil :: String -> Parser String | ||
parseAllCharUntil str = Parser f | ||
where | ||
f [] = empty | ||
f (x:xs) = case runParser (parseString str) (x:xs) of | ||
Nothing -> runParser ((x :) <$> parseAllCharUntil str) xs | ||
Just (y, ys) -> Just (y, ys) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
{- | ||
-- EPITECH PROJECT, 2023 | ||
-- Leviator compiler | ||
-- File description: | ||
-- Parser | ||
-} | ||
|
||
module Parser ( | ||
Parser (..), | ||
) where | ||
|
||
import Control.Applicative | ||
|
||
data Parser a = Parser { | ||
runParser :: String -> Maybe (a, String) | ||
} | ||
|
||
instance Functor Parser where | ||
fmap fct parser = Parser f | ||
where | ||
f str = case runParser parser str of | ||
Just (x, xs) -> Just (fct x, xs) | ||
Nothing -> Nothing | ||
|
||
instance Applicative Parser where | ||
pure x = Parser f | ||
where | ||
f str = Just (x, str) | ||
p1 <*> p2 = Parser f | ||
where | ||
f str = case runParser p1 str of | ||
Just (x, xs) -> | ||
case runParser p2 xs of | ||
Just (y, ys) -> Just (x y, ys) | ||
Nothing -> Nothing | ||
Nothing -> Nothing | ||
|
||
instance Alternative Parser where | ||
empty = Parser f | ||
where | ||
f _ = Nothing | ||
p1 <|> p2 = Parser f | ||
where | ||
f str = case runParser p1 str of | ||
Just (x, xs) -> Just (x, xs) | ||
Nothing -> runParser p2 str | ||
|
||
instance Monad Parser where | ||
parser >>= fct = Parser f | ||
where | ||
f str = case runParser parser str of | ||
Just (x, xs) -> runParser (fct x) xs | ||
Nothing -> Nothing | ||
return = pure |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters