v1 perplexed
This commit is contained in:
57
app/Main.hs
Normal file
57
app/Main.hs
Normal file
@@ -0,0 +1,57 @@
|
||||
module Main where
|
||||
|
||||
import System.Environment (getArgs)
|
||||
import System.Exit (exitFailure, exitSuccess)
|
||||
import System.IO (hPutStrLn, stderr)
|
||||
|
||||
import FWL.Parser (parseFile)
|
||||
import FWL.Pretty (prettyProgram)
|
||||
import FWL.Check (checkProgram)
|
||||
import FWL.Compile (compileToJson, compileProgram)
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
args <- getArgs
|
||||
case args of
|
||||
["check", fp] -> runCheck fp
|
||||
["compile", fp] -> runCompile fp
|
||||
["pretty", fp] -> runPretty fp
|
||||
_ -> do
|
||||
putStrLn "Usage: fwlc <command> <file.fwl>"
|
||||
putStrLn " check <file> -- parse and static-check"
|
||||
putStrLn " compile <file> -- emit nftables JSON to stdout"
|
||||
putStrLn " pretty <file> -- parse and re-print"
|
||||
exitFailure
|
||||
|
||||
runCheck :: FilePath -> IO ()
|
||||
runCheck fp = do
|
||||
result <- parseFile fp
|
||||
case result of
|
||||
Left err -> hPutStrLn stderr ("Parse error:\n" ++ show err) >> exitFailure
|
||||
Right prog -> do
|
||||
let errs = checkProgram prog
|
||||
if null errs
|
||||
then putStrLn "OK" >> exitSuccess
|
||||
else do
|
||||
mapM_ (hPutStrLn stderr . show) errs
|
||||
exitFailure
|
||||
|
||||
runCompile :: FilePath -> IO ()
|
||||
runCompile fp = do
|
||||
result <- parseFile fp
|
||||
case result of
|
||||
Left err -> hPutStrLn stderr ("Parse error:\n" ++ show err) >> exitFailure
|
||||
Right prog -> do
|
||||
let errs = checkProgram prog
|
||||
if null errs
|
||||
then putStrLn (compileToJson prog)
|
||||
else do
|
||||
mapM_ (hPutStrLn stderr . ("Check error: " ++) . show) errs
|
||||
exitFailure
|
||||
|
||||
runPretty :: FilePath -> IO ()
|
||||
runPretty fp = do
|
||||
result <- parseFile fp
|
||||
case result of
|
||||
Left err -> hPutStrLn stderr ("Parse error:\n" ++ show err) >> exitFailure
|
||||
Right prog -> putStr (prettyProgram prog)
|
||||
Reference in New Issue
Block a user