-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
Add cli for compilation
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
{- | ||
-- EPITECH PROJECT, 2023 | ||
-- Leviator compiler | ||
-- File description: | ||
-- Args | ||
-} | ||
|
||
|
||
module Args | ||
( | ||
Action(..), | ||
Args(..), | ||
parseArgs, | ||
printHelp | ||
) where | ||
|
||
import System.Directory (getCurrentDirectory) | ||
|
||
data Action = ShowHelp | ShowVersion | Run | ||
|
||
data Args = Args { | ||
action :: Action, | ||
folderPath :: String, | ||
outFile :: String | ||
} | ||
|
||
parseArgs' :: [String] -> Args -> Either Args String | ||
parseArgs' [] args = | ||
Left args | ||
parseArgs' ("--help":xs) args = | ||
parseArgs' xs (args {action = ShowHelp}) | ||
parseArgs' ("-h":xs) args = | ||
parseArgs' xs (args {action = ShowHelp}) | ||
parseArgs' ("--version":xs) args = | ||
parseArgs' xs (args {action = ShowVersion}) | ||
parseArgs' ("-v":xs) args = | ||
parseArgs' xs (args {action = ShowVersion}) | ||
parseArgs' ("-o":x:xs) args = | ||
parseArgs' xs (args {outFile = x}) | ||
parseArgs' ["-o"] _ = | ||
Right "Missing argument for -o" | ||
parseArgs' (('-':xs):_) _ = | ||
Right ("Unknown option: " ++ xs) | ||
parseArgs' (x:xs) args = | ||
parseArgs' xs (args {action = Run, folderPath = x}) | ||
|
||
parseArgs :: [String] -> IO (Either Args String) | ||
parseArgs args = | ||
getCurrentDirectory >>= \path -> | ||
return (parseArgs' args (Args { | ||
action = Run, folderPath = path, outFile = "out.wasm" | ||
})) | ||
|
||
hLine1 :: String | ||
hLine1 = "Usage: lvtc [OPTION] [FOLDER]\n" | ||
hLine2 :: String | ||
hLine2 = "\n" | ||
hLine3 :: String | ||
hLine3 = "Compile Leviator source code to WebAssembly\n" | ||
hLine4 :: String | ||
hLine4 = "" | ||
hLine5 :: String | ||
hLine5 = "Options:\n" | ||
hLine6 :: String | ||
hLine6 = "\t-h, --help\n\t\tDisplay this help and exit\n" | ||
hLine7 :: String | ||
hLine7 = "\t-v, --version\n\t\tOutput version information and exit\n" | ||
hLine8 :: String | ||
hLine8 = "\t-o FILE\n\t\tWrite WebAssembly to FILE\n" | ||
hLine9 :: String | ||
hLine9 = part1 ++ part2 | ||
where | ||
part1 = "\tFOLDER\n\t\tTake all Leviator" | ||
part2 = " source code recursively from FOLDER\n" | ||
|
||
printHelp :: IO () | ||
printHelp = | ||
putStr hLine1 >> putStr hLine2 >> putStr hLine3 >> putStr hLine4 | ||
>> putStr hLine5 >> putStr hLine6 >> putStr hLine7 >> putStr hLine8 | ||
>> putStr hLine9 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
{- | ||
-- EPITECH PROJECT, 2023 | ||
-- Leviator compiler | ||
-- File description: | ||
-- Run | ||
-} | ||
|
||
module Run | ||
( | ||
run | ||
) where | ||
|
||
import Expression (parseAllExpression, Expression (..)) | ||
import Alias (proceedAlias) | ||
import ParseLvt (parseFuncDeclaration) | ||
import WatLike (aSTToWatLike, FuncDeclare) | ||
import Parser (runParser) | ||
import AST (FuncDeclaration) | ||
import Args | ||
|
||
import System.Directory (listDirectory) | ||
import System.FilePath (joinPath) | ||
import Data.List (isSuffixOf) | ||
|
||
getExpressionFromFile :: FilePath -> IO [Expression] | ||
getExpressionFromFile path = | ||
readFile path | ||
>>= (\str -> | ||
case runParser (proceedAlias <$> parseAllExpression) str of | ||
Nothing -> fail "Invalid expression" | ||
Just (expression, _) -> return expression) | ||
|
||
getFilesExpression :: [FilePath] -> IO [Expression] | ||
getFilesExpression (file:files) = | ||
getExpressionFromFile file | ||
>>= (\expFile -> getFilesExpression files | ||
>>= (\expFiles -> return (expFile ++ expFiles))) | ||
getFilesExpression [] = return [] | ||
|
||
selectGoodFiles :: FilePath -> [FilePath] -> IO [FilePath] | ||
selectGoodFiles folder [] = return [] | ||
Check warning on line 41 in lvtc/app/Run.hs
|
||
selectGoodFiles folder (file:files) | ||
| ".lvt" `isSuffixOf` trueFile = | ||
putStrLn ("- " ++ trueFile) | ||
>> selectGoodFiles folder files | ||
>>= (\others -> return (trueFile : others)) | ||
| otherwise = selectGoodFiles folder files | ||
where | ||
trueFile = joinPath [folder, file] | ||
|
||
listAllFiles :: FilePath -> IO [FilePath] | ||
listAllFiles path = listDirectory path >>= selectGoodFiles path | ||
|
||
getAllFunc :: [Expression] -> IO [FuncDeclaration] | ||
getAllFunc [] = return [] | ||
getAllFunc ((Expression.Function str):expressions) = | ||
case runParser parseFuncDeclaration str of | ||
Nothing -> fail ("Parser Error: `" ++ str ++ "`") | ||
Just (func, _) -> | ||
getAllFunc expressions >>= \funcs -> return (func:funcs) | ||
getAllFunc (_ : expressions) = getAllFunc expressions | ||
|
||
-- TODO: replace with the function of gui | ||
checkAst :: IO [FuncDeclaration] -> IO [FuncDeclaration] | ||
checkAst funcsIo = | ||
funcsIo | ||
>>= (\funcs -> case Just funcs of | ||
Just f -> return f | ||
Nothing -> fail "Invalid Code") | ||
Check warning on line 69 in lvtc/app/Run.hs
|
||
|
||
transformToWatLike :: IO [FuncDeclaration] -> IO [FuncDeclare] | ||
transformToWatLike funcsIo = | ||
funcsIo | ||
>>= (\funcs -> return (aSTToWatLike funcs)) | ||
|
||
run :: Args -> IO () | ||
run (Args Run fPath oFile) = putStrLn ("Compiling from: " ++ fPath) >> | ||
Check warning on line 77 in lvtc/app/Run.hs
|
||
transformed >>= print | ||
where | ||
expressions = listAllFiles fPath >>= getFilesExpression | ||
funcs = expressions >>= getAllFunc | ||
transformed = transformToWatLike (checkAst funcs) | ||
run _ = fail "Invalid option called" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{- | ||
-- EPITECH PROJECT, 2023 | ||
-- Leviator compiler | ||
-- File description: | ||
-- Run | ||
-} | ||
|
||
module Version | ||
( | ||
printVersion | ||
) where | ||
|
||
import LvtLibVersion | ||
|
||
printVersion :: IO () | ||
printVersion = putStrLn lvtLibVersion |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{- | ||
-- EPITECH PROJECT, 2023 | ||
-- Leviator compiler | ||
-- File description: | ||
-- LvtLibVersion | ||
-} | ||
|
||
module LvtLibVersion | ||
( | ||
lvtLibVersionPatch, | ||
lvtLibVersionMinor, | ||
lvtLibVersionMajor, | ||
lvtLibVersion | ||
) | ||
where | ||
|
||
lvtLibVersionPatch :: Int | ||
lvtLibVersionPatch = 0 | ||
|
||
lvtLibVersionMinor :: Int | ||
lvtLibVersionMinor = 0 | ||
|
||
lvtLibVersionMajor :: Int | ||
lvtLibVersionMajor = 0 | ||
|
||
|
||
lvtLibVersion :: String | ||
lvtLibVersion = fullVersion | ||
where | ||
fMaj = show lvtLibVersionMajor | ||
fMin = show lvtLibVersionMinor | ||
fPat = show lvtLibVersionPatch | ||
fullVersion = fMaj ++ "." ++ fMin ++ "." ++ fPat |