~jojo/Carth

ref: 98ab37e59ce10d5f25d2a91dffb651a2452e9860 Carth/app/Main.hs -rw-r--r-- 717 bytes
98ab37e5 — Johan Johansson Wrap ident in newtype, parse defs, parse program 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{-# LANGUAGE LambdaCase #-}

module Main where

import Control.Monad
import Data.Functor
import Lib

import System.Environment
import System.Exit

main :: IO ()
main = do
  args <- getArgs
  case args of
    file:[] -> interpretFile file
    _ -> usage

interpretFile :: FilePath -> IO ()
interpretFile file = parseFile file <&> interpret >>= \case
  Left err -> do
    putStrLn "Interpretation error"
    putStrLn err
    exitFailure
  Right () -> pure ()

parseFile :: String -> IO Program
parseFile file = readFile file <&> parse file >>= \case
  Left err -> do
    putStrLn "Syntax error"
    print err
    exitFailure
  Right pgm -> print pgm >> pure pgm

usage = putStrLn "Usage: carth SRC-FILE" >> exitFailure