perga/app/Main.hs

31 lines
760 B
Haskell
Raw Normal View History

2024-10-05 13:31:09 -07:00
module Main where
2024-11-17 18:33:14 -08:00
import Control.Monad (void)
import qualified Data.Map as M
2024-11-15 18:39:44 -08:00
import qualified Data.Text.IO as T
2024-11-17 18:33:14 -08:00
import Eval (Env)
2024-10-05 16:04:13 -07:00
import Parser
2024-11-15 18:39:44 -08:00
import Repl
import System.Environment
2024-10-05 16:04:13 -07:00
import System.IO
2024-11-11 14:34:55 -08:00
2024-10-05 13:31:09 -07:00
main :: IO ()
2024-10-05 16:04:13 -07:00
main = do
2024-11-15 18:39:44 -08:00
args <- getArgs
case args of
2024-11-17 18:33:14 -08:00
[] -> void repl
2024-11-15 18:39:44 -08:00
[file] -> handleFile file
_ -> putStrLn "usage './perga' for repl and './perga <filename>' to get input from a file"
2024-11-17 18:33:14 -08:00
dumpEnv :: Env -> IO ()
dumpEnv = void . M.traverseWithKey ((putStrLn .) . showEnvEntry)
2024-11-15 18:39:44 -08:00
handleFile :: String -> IO ()
handleFile fileName =
do
fileH <- openFile fileName ReadMode
input <- T.hGetContents fileH
2024-11-17 18:33:14 -08:00
case parseProgram input of
2024-11-15 18:39:44 -08:00
Left err -> putStrLn err
2024-11-17 18:33:14 -08:00
Right env -> dumpEnv env