Skip to content

Commit

Permalink
update Interpreter to make "testable"
Browse files Browse the repository at this point in the history
  • Loading branch information
evermake committed Aug 23, 2024
1 parent 0c9d00e commit 6961c4d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 25 deletions.
34 changes: 9 additions & 25 deletions app/Interpreter.hs
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
module Main where

import Control.Monad.Foil (emptyNameMap, emptyScope)
import HM.Eval
import HM.Parser.Par
import HM.Syntax (toExpClosed)
import HM.Typecheck
import HM.Interpret
import System.Exit

data Result
= Success String -- Output of evaluation.
| Failure Int String -- Exit code and error message.

main :: IO ()
main = do
sourceCode <- getContents
case run sourceCode of
case interpret sourceCode of
Success output -> putStrLn output
Failure code msg -> do
putStrLn msg
exitWith (ExitFailure code)
Failure errorKind errorMsg -> do
putStrLn errorMsg
exitWith (ExitFailure (errorCode errorKind))

run :: String -> Result
run input =

case toExpClosed <$> pExp tokens of
Left err -> Failure 1 ("Parsing error: " ++ err)
Right e -> case inferType emptyNameMap e of
Left err -> Failure 2 ("Typechecking error: " ++ err)
Right _type -> case eval emptyScope e of
Left err -> Failure 3 ("Evaluation error: " ++ err)
Right outExp -> Success (show outExp)
where
tokens = myLexer input
errorCode :: ErrorKind -> Int
errorCode ParsingError = 1
errorCode TypecheckingError = 2
errorCode EvaluationError = 3
1 change: 1 addition & 0 deletions free-foil-hm.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ custom-setup
library
exposed-modules:
HM.Eval
HM.Interpret
HM.Parser.Abs
HM.Parser.Lex
HM.Parser.Par
Expand Down
28 changes: 28 additions & 0 deletions src/HM/Interpret.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module HM.Interpret where

import Control.Monad.Foil (emptyNameMap, emptyScope)
import HM.Eval
import HM.Parser.Par
import HM.Syntax (toExpClosed)
import HM.Typecheck

data Result
= Success String -- Output of evaluation.
| Failure ErrorKind String -- Error kind with message.

data ErrorKind
= ParsingError
| TypecheckingError
| EvaluationError

interpret :: String -> Result
interpret input =
case toExpClosed <$> pExp tokens of
Left err -> Failure ParsingError ("Parsing error: " ++ err)
Right e -> case inferType emptyNameMap e of
Left err -> Failure TypecheckingError ("Typechecking error: " ++ err)
Right _type -> case eval emptyScope e of
Left err -> Failure EvaluationError ("Evaluation error: " ++ err)
Right outExp -> Success (show outExp)
where
tokens = myLexer input

0 comments on commit 6961c4d

Please sign in to comment.