Sanchayan Maity
997a0d11f8
This is for the fourth chapter of Crafting Interpreters. https://craftinginterpreters.com/scanning.html This is only a first pass. Still need to test complex expressions. Basic scanning/parsing works. See test.
73 lines
1.5 KiB
Haskell
73 lines
1.5 KiB
Haskell
module Main where
|
|
|
|
import Data.Text.IO qualified as TextIO
|
|
import GHC.IO.Encoding (setLocaleEncoding)
|
|
import GHC.IO.Encoding.UTF8 (utf8)
|
|
import MyLib (runLox)
|
|
import Options.Applicative
|
|
( Alternative ((<|>)),
|
|
Parser,
|
|
ParserInfo,
|
|
execParser,
|
|
flag',
|
|
fullDesc,
|
|
header,
|
|
help,
|
|
helper,
|
|
info,
|
|
long,
|
|
metavar,
|
|
progDesc,
|
|
short,
|
|
strOption,
|
|
(<**>),
|
|
)
|
|
import Options.Applicative.Builder (value)
|
|
|
|
data CmdLineInput
|
|
= FileInput FilePath
|
|
| StdInput
|
|
|
|
fileInput :: Parser CmdLineInput
|
|
fileInput =
|
|
FileInput
|
|
<$> strOption
|
|
( long "file"
|
|
<> short 'f'
|
|
<> metavar "FILENAME"
|
|
<> value "lox-input.lox"
|
|
<> help "Input file with Lox code"
|
|
)
|
|
|
|
stdInput :: Parser CmdLineInput
|
|
stdInput =
|
|
flag'
|
|
StdInput
|
|
( long "stdin"
|
|
<> help "Read from stdin"
|
|
)
|
|
|
|
cmdLineInput :: Parser CmdLineInput
|
|
cmdLineInput = fileInput <|> stdInput
|
|
|
|
handleCmdLineInput :: CmdLineInput -> IO ()
|
|
handleCmdLineInput = \case
|
|
FileInput file -> do
|
|
source <- TextIO.readFile file
|
|
runLox source >>= print
|
|
StdInput -> print "We do not handle stdin yet"
|
|
|
|
main :: IO ()
|
|
main = do
|
|
setLocaleEncoding utf8
|
|
handleCmdLineInput =<< execParser opts
|
|
where
|
|
opts :: ParserInfo CmdLineInput
|
|
opts =
|
|
info
|
|
(cmdLineInput <**> helper)
|
|
( fullDesc
|
|
<> progDesc "Interpreter for LOX language from Crafting Interpreters"
|
|
<> header "Interpreter for LOX language from Crafting Interpreters"
|
|
)
|