summaryrefslogtreecommitdiff
path: root/src/Main.hs
blob: 92e9a1d54a778ed4c9f6a074e6eda9895ec59f71 (plain) (blame)
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
36
module Main where

import Control.Monad (forM_)
import Control.Monad.Writer
import Data.Aeson (encode)
import qualified Data.ByteString.Lazy.Char8 as BL
import qualified Data.Text.IO
import GHC.IO.Exception (ExitCode (ExitFailure))
import Language.Fiddle.Ast
import Language.Fiddle.Compiler (coloredFormat, compile_, printDiagnostic)
import Language.Fiddle.Compiler.Stage0
import Language.Fiddle.GenericTree (ToGenericSyntaxTree (toGenericSyntaxTree))
import qualified Language.Fiddle.Parser
import qualified Language.Fiddle.Tokenizer
import qualified System.Environment as System
import System.Exit (exitWith)

main :: IO ()
main = do
  argv <- System.getArgs

  case argv of
    [filePath] -> do
      text <- Data.Text.IO.readFile filePath
      let (diags, ma) = compile_ $ toStage1 =<< toStage0 filePath text
      forM_ diags printDiagnostic
      case ma of
        Just ast -> do
          putStrLn "\x1b[1;32mCompilation Succeeded:\x1b[0m"
          putStrLn $ BL.unpack $ encode $ toGenericSyntaxTree ast
        Nothing -> do
          putStrLn "\x1b[1;31mCompilation Failed\x1b[0m"
          exitWith (ExitFailure 1)
    _ -> do
      putStrLn "Wrong Args"
      exitWith (ExitFailure 2)