Commit 048a566d authored by pm3g19's avatar pm3g19
Browse files

[NOCOMPILE] Started working on interpreter.

parent 113cd751
module Debug where
notImplemented = error "Not implemented yet"
eval :: ResultEnvironment -> Expr -> Expr --only focus on one expression
type Environment = ResultEnvironment
findVar :: Environment -> SymbolName -> Expr
findVar = notImplemented
addVar :: Environment -> SymbolName -> Expr -> Environment
eval env expr = let eval' = eval env in case expr of -- evaluates expression to weak-head normal form (i.e. top level data structure is not a FuncCall)
FuncCall func inputSets args -> case func of
(PredefFunc f) -> case f of
(Filter) -> let (Set inputRecords,predicate) = (eval' $ head inputSets, eval' $ head args) in
Set $ filter ((==Boolean True).eval')(map (\r -> FuncCall predicate [] [r]) inputRecords) -- result is weak-head normal form
--Set $ map (eval env) (map (\r -> FuncCall predicate [] [r]) inputRecords) # don't evaluate last step
(IsEqual) -> let (e1:e2:_) = args in
Boolean (eval' e1 == eval' e2)
(RecordIndex) -> let (Record recordData, Int index) = (eval' $ args !! 0, eval' $ args !! 1) in
recordData !! index
--implement later
--(Map) -> Set $ (map (\r -> FuncCall predicate [] [r]) inputRecords)
(FuncDef setParams argParams body) -> eval new_env body
where
newEnv = foldl (\env entry@(name, expr) -> addVar env name expr) env --adds entries to environment
-- TODO FIx
(Var name) -> findVar env name
_ -> expr
import Types
import Debug
type Control = (ResultEnvironment, [Expr])
type ResultEnvironment = [(SymbolName, Expr)] -- (result of last calculation, other bindings e.g. let statements)
prepare :: Program -> IO Control -- takes in the AST for the program, reads the csv files and prepares the environment based on the CSV files
prepare (inputSets,instructions) = zipM (environment, return instructions)
where environment = fmap (zip inputSets) $ loadInputFiles inputSets
---eval :: Control -> Expr -- fully evaluates the program, returning a set
zipM (a,b) = do
a' <- a
b' <- b
return (a',b')
--pairmap f s = (s, fs)
--------------------------------------------
programStep :: Control -> Control -- updates environment and list
programStep = notImplemented
--------------------------------------------
loadInputFiles :: [String] -> IO [[String]]
loadInputFiles = mapM readCSV
readCSV :: FilePath -> IO [[String]]
readCSV _ = error ""
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment