diff --git a/app/Main.hs b/app/Main.hs index b255e27..f303cfc 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -1,4 +1,41 @@ module Main where +import Text.Megaparsec.Error (errorBundlePretty) + +import System.Environment +import System.IO +import System.Exit +import qualified Data.Text.IO as T +import Control.Monad ((<=<)) + +import Parse (parseExpr) +import Type (infer) +import CompChez (comp) +import Eval (eval) + main :: IO () -main = putTextLn "mjau" +main = getArgs >>= \case + [] -> processFile "stdin" stdin + xs -> mapM_ (\file -> processFile file =<< openFile file ReadMode) xs + +putStyle :: String -> IO () -- bold blue :) +putStyle s = putStrLn ("\x1b[38;5;87m\x1b[1m=>> " ++ s ++ "\x1b[0m") + +processFile :: String -> Handle -> IO () +processFile f h = do + contents <- T.hGetContents h + case parseExpr f contents of + Left e -> putStrLn (errorBundlePretty e) + Right ast -> do + putStyle "Parse Successful" + print ast + putStyle "Type Checking / Inferring" + case infer [] ast of + Nothing -> putStrLn "=> Type error" >> exitWith (ExitFailure 1) + Just t -> print t + putStyle "Compiling to Chez" + putStrLn (comp ast) + putStyle "Evaluating" + print (eval [] ast) + + diff --git a/example/fib b/example/fib new file mode 100644 index 0000000..731d8be --- /dev/null +++ b/example/fib @@ -0,0 +1,6 @@ +let fib : Int -> Int = fix \f.\x. + if 2 > x + then 1 + else f (x-1) + f (x-2) + +in fib 10 diff --git a/gecco.cabal b/gecco.cabal index 03edd7a..9c6ded0 100644 --- a/gecco.cabal +++ b/gecco.cabal @@ -13,6 +13,8 @@ executable gecco build-depends: base , gecco + , megaparsec + , text hs-source-dirs: app default-language: GHC2021 default-extensions: LambdaCase @@ -20,9 +22,7 @@ executable gecco library - exposed-modules: Core - , Val - , Syn + exposed-modules: Syn , Eval , Type , Parse diff --git a/src/Core.hs b/src/Core.hs deleted file mode 100644 index 1c2f512..0000000 --- a/src/Core.hs +++ /dev/null @@ -1,4 +0,0 @@ -module Core where - -test :: String -test = "mjau" diff --git a/src/Eval.hs b/src/Eval.hs index 47db79f..3d23d82 100644 --- a/src/Eval.hs +++ b/src/Eval.hs @@ -1,7 +1,20 @@ module Eval where import Syn -import Val + +data Val + = I Int + | B Bool + | U + | Clos (Val -> Val) + +instance Show Val where + show (I i) = "I " ++ show i + show (B b) = "B " ++ show b + show U = "U" + show (Clos _) = "" + +type Ctx = [Val] eval :: Ctx -> Syn -> Val eval ctx (Var i) = ctx !! i diff --git a/src/Parse.hs b/src/Parse.hs index bd8a85c..a0a458c 100644 --- a/src/Parse.hs +++ b/src/Parse.hs @@ -25,8 +25,8 @@ instance ShowErrorComponent Text where skipSpace :: Parser () skipSpace = L.space space1 (L.skipLineComment "--") (L.skipBlockComment "{-" "-}") -parseExpr :: Text -> Either (ParseErrorBundle Text Text) Syn -parseExpr = parse (parser []) "" +parseExpr :: String -> Text -> Either (ParseErrorBundle Text Text) Syn +parseExpr = parse (parser []) parser :: Ctx -> Parser Syn parser ctx = do diff --git a/src/TestProg.hs b/src/TestProg.hs index d80f212..be67501 100644 --- a/src/TestProg.hs +++ b/src/TestProg.hs @@ -1,7 +1,6 @@ module TestProg where import Syn -import Val import Eval fib :: Syn diff --git a/src/Val.hs b/src/Val.hs deleted file mode 100644 index a40bb1f..0000000 --- a/src/Val.hs +++ /dev/null @@ -1,15 +0,0 @@ -module Val where - -data Val - = I Int - | B Bool - | U - | Clos (Val -> Val) - -instance Show Val where - show (I i) = "I " ++ show i - show (B b) = "B " ++ show b - show U = "U" - show (Clos _) = "" - -type Ctx = [Val]