Skip to content

Commit

Permalink
Merge pull request #13 from X-R-G-B/lvtc-cli
Browse files Browse the repository at this point in the history
Add cli for compilation
  • Loading branch information
Saverio976 authored Jan 13, 2024
2 parents 71414d1 + c3fd7a1 commit fe30d5f
Show file tree
Hide file tree
Showing 12 changed files with 278 additions and 57 deletions.
80 changes: 80 additions & 0 deletions lvtc/app/Args.hs
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
66 changes: 13 additions & 53 deletions lvtc/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,61 +7,21 @@

module Main (main) where

import Expression (parseExpresion, parseAllExpression)
import Parser (runParser)
import Alias (proceedAlias)
import ParseLvt (parseInstruction, parseInstructions, parseFuncDeclaration)
import WatLike (aSTToWatLike)
import AST
import System.Environment (getArgs)

test1 :: String
test1 = "if (a)\n{\nb(0);\n};\n"
import Args (Args (..), parseArgs, Action (..), printHelp)
import Run (run)
import Version (printVersion)

test2 :: String
test2 = part1 ++ part2
where
part1 = "@Int a = 0;\n @Int c = b(a);\n"
part2 = " if (c)\n {\n d(a);\n };\n"
dispatchArgs :: Args -> IO ()
dispatchArgs (Args Run fPath oFile) = run (Args Run fPath oFile)
dispatchArgs (Args ShowHelp _ _) = printHelp
dispatchArgs (Args ShowVersion _ _) = printVersion
-- dispatchArgs (Args New _ _) =

test3 :: String
test3 = "alias abc def;\n"

test4 :: String
test4 = "// this is a comment\n"

test5 :: String
test5 = "@Int a = 4 + 5;\n"

test6 :: String
test6 = "@Int a = 3 + 4 * 2 / ( 1 - 5 );\n"

text :: String
text = aliasInt ++ aliasRetValue ++ funcMain
where
aliasInt = "alias int Int;\n"
aliasRetValue = "alias retValue 0;\n"
funcMain = "fn main () -> int \n{\n <- retValue;\n};\n"

test8 :: String
test8 = "fn abc(a: Int) -> Int\n{\n <- a;\n};\n"

test7 :: [FuncDeclaration]
test7 =
[
(
("add", [("a", "Int"), ("b", "Int")], "Int"),
[Return (FuncValue ("+", [Var "a", Var "b"]))]
)
]
dispatchIfOk :: Either Args String -> IO ()
dispatchIfOk (Left args) = dispatchArgs args
dispatchIfOk (Right str) = print str

main :: IO ()
main =
print (runParser parseInstruction test1)
>> print (runParser parseInstructions test2)
>> print (runParser parseExpresion test3)
>> print (runParser parseExpresion test4)
>> print (runParser parseInstruction test5)
>> print (runParser parseInstruction test6)
>> print (runParser (proceedAlias <$> parseAllExpression) text)
>> print (runParser parseFuncDeclaration test8)
>> print (aSTToWatLike test7)
main = getArgs >>= parseArgs >>= dispatchIfOk
83 changes: 83 additions & 0 deletions lvtc/app/Run.hs
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

View workflow job for this annotation

GitHub Actions / release-macos

Defined but not used: ‘folder’

Check warning on line 41 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / release-linux

Defined but not used: ‘folder’

Check warning on line 41 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / release-windows

Defined but not used: `folder'

Check warning on line 41 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / compil-macos

Defined but not used: ‘folder’

Check warning on line 41 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / compil-windows

Defined but not used: `folder'

Check warning on line 41 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / compil-linux

Defined but not used: ‘folder’
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

View workflow job for this annotation

GitHub Actions / release-macos

Pattern match is redundant

Check warning on line 69 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / release-linux

Pattern match is redundant

Check warning on line 69 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / release-windows

Pattern match is redundant

Check warning on line 69 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / compil-macos

Pattern match is redundant

Check warning on line 69 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / compil-windows

Pattern match is redundant

Check warning on line 69 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / compil-linux

Pattern match is redundant

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

View workflow job for this annotation

GitHub Actions / release-macos

Defined but not used: ‘oFile’

Check warning on line 77 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / release-linux

Defined but not used: ‘oFile’

Check warning on line 77 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / release-windows

Defined but not used: `oFile'

Check warning on line 77 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / compil-macos

Defined but not used: ‘oFile’

Check warning on line 77 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / compil-windows

Defined but not used: `oFile'

Check warning on line 77 in lvtc/app/Run.hs

View workflow job for this annotation

GitHub Actions / compil-linux

Defined but not used: ‘oFile’
transformed >>= print
where
expressions = listAllFiles fPath >>= getFilesExpression
funcs = expressions >>= getAllFunc
transformed = transformToWatLike (checkAst funcs)
run _ = fail "Invalid option called"
16 changes: 16 additions & 0 deletions lvtc/app/Version.hs
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
7 changes: 7 additions & 0 deletions lvtc/lvtc.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ library
Builtins
Expression
Lib
LvtLibVersion
ParseLvt
Parser
ParseUtil
Expand All @@ -51,6 +52,9 @@ library
executable lvtc-exe
main-is: Main.hs
other-modules:
Args
Run
Version
Paths_lvtc
autogen-modules:
Paths_lvtc
Expand All @@ -59,6 +63,8 @@ executable lvtc-exe
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
build-depends:
base >=4.7 && <5
, directory
, filepath
, lvtc
default-language: Haskell2010

Expand All @@ -79,6 +85,7 @@ test-suite lvtc-test
build-depends:
base >=4.7 && <5
, lvtc
, process
, tasty
, tasty-hunit
default-language: Haskell2010
3 changes: 3 additions & 0 deletions lvtc/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ executables:
- -with-rtsopts=-N
dependencies:
- lvtc
- directory
- filepath

tests:
lvtc-test:
Expand All @@ -59,3 +61,4 @@ tests:
- lvtc
- tasty
- tasty-hunit
- process
33 changes: 33 additions & 0 deletions lvtc/src/LvtLibVersion.hs
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
13 changes: 9 additions & 4 deletions lvtc/src/ParseLvt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ parseFuncVar = Parser f
parseFuncVars :: Parser [Var]
parseFuncVars =
parseChar '(' *>
some
many
(parseFuncVar
<* (parseString "," <|> parseString " ," <|> parseString ", ")
<|> parseFuncVar)
Expand All @@ -343,7 +343,7 @@ parseFuncName = Parser f
_notVar -> Nothing

parseFuncType :: Parser Type
parseFuncType =
parseFuncType =
(parseString " -> "
<|> parseString "-> "
<|> parseString "->") *> parseType <* parseString "\n{\n"
Expand All @@ -355,9 +355,14 @@ parseFuncPrototype =
<*> parseFuncVars
<*> parseFuncType

parseFuncDeclaration :: Parser FuncDeclaration
parseFuncDeclaration =
parseFuncDeclaration' :: Parser FuncDeclaration
parseFuncDeclaration' =
(,)
<$> parseFuncPrototype
<*> parseInstructions
<* parseString "};\n"

parseFuncDeclaration :: Parser FuncDeclaration
parseFuncDeclaration = Parser f
where
f str = runParser parseFuncDeclaration' (lexeme str)
2 changes: 2 additions & 0 deletions lvtc/stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ packages:
extra-deps:
- tasty-1.4.2.2
- tasty-hunit-0.10.1
- directory-1.3.8.2
- process-1.6.18.0

# Override default flag values for local packages and extra-deps
# flags: {}
Expand Down
Loading

0 comments on commit fe30d5f

Please sign in to comment.