diff options
| author | Eugen Wissner <belka@caraus.de> | 2024-12-20 15:32:20 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2024-12-20 15:32:20 +0100 |
| commit | a1c99103003bfd466c77a369fb3312f922f336d0 (patch) | |
| tree | 42dc2946a4183f63c90400b71d973023a5fe13aa /lib/Language/Elna | |
| parent | fbd08f27078f2e86c7e0ae65fd8d89658c0c34bd (diff) | |
| download | elna-a1c99103003bfd466c77a369fb3312f922f336d0.tar.gz | |
Add stage options
Diffstat (limited to 'lib/Language/Elna')
| -rw-r--r-- | lib/Language/Elna/CommandLine.hs | 48 | ||||
| -rw-r--r-- | lib/Language/Elna/Driver.hs | 37 | ||||
| -rw-r--r-- | lib/Language/Elna/Driver/CommandLine.hs | 69 |
3 files changed, 106 insertions, 48 deletions
diff --git a/lib/Language/Elna/CommandLine.hs b/lib/Language/Elna/CommandLine.hs deleted file mode 100644 index 19898e2..0000000 --- a/lib/Language/Elna/CommandLine.hs +++ /dev/null @@ -1,48 +0,0 @@ -{- This Source Code Form is subject to the terms of the Mozilla Public License, - v. 2.0. If a copy of the MPL was not distributed with this file, You can - obtain one at https://mozilla.org/MPL/2.0/. -} - -module Language.Elna.CommandLine - ( CommandLine(..) - , commandLine - , execParser - ) where - -import Options.Applicative - ( Parser - , ParserInfo(..) - , argument - , execParser - , fullDesc - , help - , helper - , info - , long - , metavar - , optional - , progDesc - , short - , str - , strOption - ) -import Control.Applicative ((<**>)) - -data CommandLine = CommandLine - { input :: FilePath - , output :: Maybe FilePath - } deriving (Eq, Show) - -parser :: Parser CommandLine -parser = CommandLine - <$> argument str inFile - <*> optional (strOption outFile) - where - inFile = metavar "INFILE" <> help "Input file." - outFile = long "output" - <> short 'o' - <> metavar "OUTFILE" - <> help "Output file." - -commandLine :: ParserInfo CommandLine -commandLine = info (parser <**> helper) - $ fullDesc <> progDesc "Elna compiler." diff --git a/lib/Language/Elna/Driver.hs b/lib/Language/Elna/Driver.hs new file mode 100644 index 0000000..9d13f59 --- /dev/null +++ b/lib/Language/Elna/Driver.hs @@ -0,0 +1,37 @@ +{- This Source Code Form is subject to the terms of the Mozilla Public License, + v. 2.0. If a copy of the MPL was not distributed with this file, You can + obtain one at https://mozilla.org/MPL/2.0/. -} + +module Language.Elna.Driver + ( Driver(..) + , IntermediateStage(..) + , drive + , execParser + ) where + +import Data.Maybe (fromMaybe) +import Language.Elna.Driver.CommandLine + ( CommandLine(..) + , IntermediateStage(..) + , commandLine + ) +import Options.Applicative (execParser) +import System.FilePath (replaceExtension, takeFileName) + +data Driver = Driver + { input :: FilePath + , output :: FilePath + , intermediateStage :: Maybe IntermediateStage + } deriving (Eq, Show) + +drive :: IO Driver +drive = rewrite <$> execParser commandLine + where + rewrite CommandLine{..} = + let defaultOutputName = replaceExtension (takeFileName input) "o" + outputName = fromMaybe defaultOutputName output + in Driver + { input = input + , output = outputName + , intermediateStage = intermediateStage + } diff --git a/lib/Language/Elna/Driver/CommandLine.hs b/lib/Language/Elna/Driver/CommandLine.hs new file mode 100644 index 0000000..1bd5f14 --- /dev/null +++ b/lib/Language/Elna/Driver/CommandLine.hs @@ -0,0 +1,69 @@ +{- This Source Code Form is subject to the terms of the Mozilla Public License, + v. 2.0. If a copy of the MPL was not distributed with this file, You can + obtain one at https://mozilla.org/MPL/2.0/. -} + +module Language.Elna.Driver.CommandLine + ( CommandLine(..) + , IntermediateStage(..) + , commandLine + ) where + +import Options.Applicative + ( Parser + , ParserInfo(..) + , argument + , flag' + , fullDesc + , help + , helper + , info + , long + , metavar + , optional + , progDesc + , short + , str + , strOption + ) +import Control.Applicative (Alternative(..), (<**>)) + +data IntermediateStage + = ParseStage + | ValidateStage + | CodeGenStage + deriving (Eq, Show) + +data CommandLine = CommandLine + { input :: FilePath + , output :: Maybe FilePath + , intermediateStage :: Maybe IntermediateStage + } deriving (Eq, Show) + +intermediateStageP :: Parser IntermediateStage +intermediateStageP + = flag' ParseStage parseStageP + <|> flag' ValidateStage validateStageP + <|> flag' CodeGenStage codeGenStageP + where + parseStageP = long "parse" + <> help "Run the lexer and parser, but stop before assembly generation" + validateStageP = long "validate" + <> help "Run through the semantic analysis stage, stopping before TAC generation" + codeGenStageP = long "codegen" + <> help "Perform lexing, parsing, and assembly generation, but stop before code emission" + +commandLineP :: Parser CommandLine +commandLineP = CommandLine + <$> argument str inFile + <*> optional (strOption outFile) + <*> optional intermediateStageP + where + inFile = metavar "INFILE" <> help "Input file." + outFile = long "output" + <> short 'o' + <> metavar "OUTFILE" + <> help "Output file." + +commandLine :: ParserInfo CommandLine +commandLine = info (commandLineP <**> helper) + $ fullDesc <> progDesc "Elna compiler." |
