Interpreter.hs 1.78 KB
Newer Older
1
2
import Types
import Debug
3
import Eval
pm3g19's avatar
pm3g19 committed
4
import System.IO
pm3g19's avatar
pm3g19 committed
5
import ReadCSV
6

7
8
parse :: String -> Program
parse = notImplemented
9

10
11
--outline

pm3g19's avatar
pm3g19 committed
12
13
14
interpret :: FilePath -> IO () -- the main function, takes in file name, prints out result
interpret sourceFName = do
    source <- readFile sourceFName
15
16
17
18
19
20
21
22
    let program = parse source -- main abstract syntax tree
    (env, mainExpr) <- prepare program
    let output = eval env mainExpr
    let finalOutput = evalFinal output
    showFinal finalOutput

prepare :: Program -> IO (Environment, Expr) -- takes in the AST for the program, reads the csv files and prepares the environment based on the CSV files
prepare (inputSets,instructions) = do
pm3g19's avatar
pm3g19 committed
23
    csvData <- loadInputFiles inputSets ::IO [Expr]
24
25
26
27
    let env = zip inputSets csvData :: Environment
    let mainExpr = Control (head csvData) instructions
    return (env, mainExpr)
    
pm3g19's avatar
pm3g19 committed
28
29
    where
        loadInputFiles = mapM loadInputFile
30
31
32


evalFinal :: Expr -> Expr -- whnf set to ready set
pm3g19's avatar
pm3g19 committed
33
evalFinal = evalFull []
34
35

showFinal :: Expr -> IO ()
pm3g19's avatar
pm3g19 committed
36
37
38
39
40
41
42
showFinal = (print2DList.sort2DListLex.setTo2DList)

setTo2DList :: Expr -> [[String]]
setTo2DList = notImplemented

sort2DListLex = id -- TODO change this
print2DList = notImplemented
43
44

--------------------------------------------
45
46
47
48
49
50
51
loadInputFile :: SymbolName -> IO Expr
loadInputFile name = do
    rawData <- readCSV (name ++ ".csv") :: IO [[String]]
    let output = Set $ map toRecord rawData
    return output

    where
pm3g19's avatar
pm3g19 committed
52
        toRecord stringList = Record $ map ((String).stripWhitespace) stringList
pm3g19's avatar
pm3g19 committed
53

pm3g19's avatar
pm3g19 committed
54
stripWhitespace = stripTrailingWhitespace.dropWhile (==' ')
55

pm3g19's avatar
pm3g19 committed
56
57
stripTrailingWhitespace (' ':xs) = let remainder = stripTrailingWhitespace xs in
    if null remainder then [] else ' ':remainder
58

pm3g19's avatar
pm3g19 committed
59
60
stripTrailingWhitespace (x:xs) = x : stripTrailingWhitespace xs
stripTrailingWhitespace [] = []
pm3g19's avatar
pm3g19 committed
61