module Lib
    ( interpreterMain,
      tokenizerMain
    ) where

import Type
import Lexer
import Syntax
import RunEnv

import TypeCheck
import Eval

import qualified Text.Parsec
import qualified Data.Map as M
import Data.IORef

tokenizerMain :: IO ()
tokenizerMain = do
  str <- getContents
  case Text.Parsec.runParser tokenizer () "stdin" str of
    Left err -> print err
    Right tks -> mapM_ (print . snd) tks


interpreterMain :: IO ()
interpreterMain = do
  str <- getContents
  case tokenizeAndParse programP () () "stdint" str of
    Left err -> print err
    Right stmts -> do
      putStrLn "---- Parse Result"
      mapM_ print stmts
      putStrLn "---- Static Check"
      cRoot <- IOEnvRef <$> newIORef (Env M.empty Nothing)
      cResult <- unwrapIOEnv $ runRunT cRoot $ mapM_ typeStmt stmts
      case cResult of
        Left err -> print err
        Right _ -> do
          putStrLn "---- Eval"
          eRoot <- EvalIORef <$> newIORef (Env M.empty Nothing)
          eResult <- unwrapEvalIO $ runRunT eRoot $ mapM_ evalStmt stmts
          case eResult of
            Left err -> print err
            Right _ -> putStrLn "Ok"
