crafting-interpreters-hs/app/Main.hs
2025-12-30 05:43:01 +03:00

27 lines
712 B
Haskell

import Lox.Scanner
import Lox.Parser
import Lox.Interpreter
import System.IO
import System.Environment
run :: String -> IO ()
run source = do
let tokensMaybe = scanTokensFromSource source
case tokensMaybe of
Left UnexpectedCharacterError -> putStrLn "Unexpected character"
Right tokens -> do
let stmtMaybe = parse tokens
case stmtMaybe of
Left (SyntaxError s) -> putStrLn s
Right statements -> runStatements statements
repl :: IO ()
repl = putStr ">> " >> hFlush stdout >> getLine >>= run
main :: IO ()
main = getArgs >>= fs
fs :: [String] -> IO ()
fs [] = repl
fs [s] = readFile s >>= run
fs _ = putStrLn "Usage: lox [file]"