diff options
Diffstat (limited to 'src/Main.hs')
-rw-r--r-- | src/Main.hs | 89 |
1 files changed, 53 insertions, 36 deletions
diff --git a/src/Main.hs b/src/Main.hs index f643320..352a8cc 100644 --- a/src/Main.hs +++ b/src/Main.hs @@ -19,51 +19,68 @@ import Language.Fiddle.Compiler.Stage0 import Language.Fiddle.GenericTree (GenericSyntaxTree (..), ToGenericSyntaxTree (toGenericSyntaxTree), alterGenericSyntaxTree) import qualified Language.Fiddle.Parser import qualified Language.Fiddle.Tokenizer +import Options.Applicative import qualified System.Environment as System import System.Exit (exitWith) -phases res = - importResolutionPhase res >>> expansionPhase >>> consistencyCheckPhase +compilationPipeline = + importResolutionPhase >>> expansionPhase >>> consistencyCheckPhase + +newtype GlobalFlags + = GlobalFlags + { flagsInputFile :: String + } + +parseGlobalFlags :: Parser GlobalFlags +parseGlobalFlags = + GlobalFlags + <$> argument str (metavar "INPUT" <> help "Input file") main :: IO () main = do - argv <- System.getArgs - let opts = ImportResolutionOptions ["."] + (globalFlags, compilationPipelineAction) <- + execParser $ + info + ( ( (,) + <$> parseGlobalFlags + <*> execCompilationPipelineWithCmdline compilationPipeline + ) + <**> helper + ) + ( fullDesc + <> progDesc "Compile Fiddle Files" + <> header "fiddlec - A compiler for fiddle files" + ) - case argv of - [filePath] -> do - text <- Data.Text.IO.readFile filePath - let maybeParsedAst = compile_ $ toStage0 filePath text >>= toStage1 + let filePath = flagsInputFile globalFlags + text <- Data.Text.IO.readFile filePath - case maybeParsedAst of - (priorDiags, Just ast) -> do - ((priorDiags ++) -> diags, ma) <- - execCompilationPipeline (phases opts) ast - ec <- - case ma of - Just ast -> do - putStrLn $ - BL.unpack $ - encode $ - alterGenericSyntaxTree cleanupIdentifiers $ - toGenericSyntaxTree $ - fmap - (const (Nothing :: Maybe Value)) - ast - return ExitSuccess - Nothing -> do - putStrLn "\x1b[1;31mCompilation Failed\x1b[0m" - return (ExitFailure 1) + let maybeParsedAst = compile_ $ toStage0 filePath text >>= toStage1 + case maybeParsedAst of + (priorDiags, Just ast) -> do + ((priorDiags ++) -> diags, ma) <- compilationPipelineAction ast + ec <- + case ma of + Just ast -> do + putStrLn $ + BL.unpack $ + encode $ + alterGenericSyntaxTree cleanupIdentifiers $ + toGenericSyntaxTree $ + fmap + (const (Nothing :: Maybe Value)) + ast + return ExitSuccess + Nothing -> do + putStrLn "\x1b[1;31mCompilation Failed\x1b[0m" + return (ExitFailure 1) - forM_ diags printDiagnostic - exitWith ec - (diags, _) -> do - putStrLn "\x1b[1;31mParsing Failed\x1b[0m" - forM_ diags printDiagnostic - exitWith (ExitFailure 1) - _ -> do - putStrLn "Wrong Args" - exitWith (ExitFailure 2) + forM_ diags printDiagnostic + exitWith ec + (diags, _) -> do + putStrLn "\x1b[1;31mParsing Failed\x1b[0m" + forM_ diags printDiagnostic + exitWith (ExitFailure 1) cleanupIdentifiers :: GenericSyntaxTree Identity a -> Maybe (GenericSyntaxTree Identity a) cleanupIdentifiers (SyntaxTreeObject _ _ _ tr) |