Skip to content

Commit

Permalink
add 'Interpreter' with basic infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
ccntrq committed Jan 8, 2018
1 parent fe4ab13 commit 56c8237
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/Interpreter.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
module Interpreter (interpret) where

import Object
import Stmt
import Expr

import Control.Monad.Except
import Control.Monad.State


data InterpreterState = InterpreterState deriving (Show)

data InterpreterError = InterpreterError String deriving (Show)

type Interpreter a = ExceptT InterpreterError (StateT InterpreterState IO) a

interpret :: [Stmt] -> IO ()
interpret stmts = do
_ <- runInterpreter InterpreterState $ interpretStmts stmts
return ()

runInterpreter :: InterpreterState -> Interpreter a -> IO (Either InterpreterError a)
runInterpreter st i = evalStateT (runExceptT i) st


interpretStmts :: [Stmt] -> Interpreter ()
interpretStmts [] = return ()
interpretStmts (s:stmts) = execute s >> interpretStmts stmts

execute :: Stmt -> Interpreter ()
execute (Expression expr) = evaluate expr >>= liftIO . print >> return ()
execute _ = error "fuck"


evaluate :: Expr -> Interpreter Object
evaluate (Literal obj) = return obj
evaluate _ = error "fuck"

0 comments on commit 56c8237

Please sign in to comment.