Skip to content
Snippets Groups Projects
Commit c29081a8 authored by pm3g19's avatar pm3g19
Browse files

First fully-working version of interpreter

parent cd95d0a2
No related branches found
No related tags found
No related merge requests found
module CSV where
import System.IO
import Data.List
readCSV :: FilePath -> IO [[String]]
readCSV fname = do
......@@ -17,3 +18,25 @@ split p l = case span p l of
([], _) -> []
(match, []) -> [match]
(match, _:rem') -> match:split p rem'
print2DList :: [[String]] -> IO ()
print2DList = putStrLn.toCSVString
toCSVString :: [[String]] -> String
--F: use the function lines!
toCSVString list = let lines = map (',' `join`) list in '\n' `join` lines
join :: a -> [[a]] -> [a]
join _ [] = []
join a l = foldr1 (\s1 s2 -> s1 ++ a:s2) l
sort2DListLex :: [[String]] -> [[String]]
sort2DListLex = sort
stripWhitespace = stripTrailingWhitespace.dropWhile (==' ')
stripTrailingWhitespace (' ':xs) = let remainder = stripTrailingWhitespace xs in
if null remainder then [] else ' ':remainder
stripTrailingWhitespace (x:xs) = x : stripTrailingWhitespace xs
stripTrailingWhitespace [] = []
\ No newline at end of file
......@@ -9,7 +9,6 @@ import Debug.Trace
parse :: String -> Program
parse = parseSource.alexScanTokens
--outline
main = interpret "sampleprogram.txt"
......@@ -19,9 +18,9 @@ interpret sourceFName = do
source <- readFile sourceFName
let program = parse source -- main abstract syntax tree
(env, mainExpr) <- prepare program
print $ "Main expression " ++ show mainExpr
--print $ "Main expression " ++ show mainExpr
let finalOutput = eval env mainExpr
print $ "Final outupt: " ++ show finalOutput
--print $ "Final outupt: " ++ show finalOutput
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
......@@ -44,10 +43,7 @@ showFinal = (print2DList.sort2DListLex.setTo2DList)
setTo2DList :: Expr -> [[String]]
setTo2DList (Set records) = map (map (\(String s) -> s).(\(Record list) -> list)) records
sort2DListLex = id -- TODO change this
print2DList :: [[String]] -> IO ()
print2DList = notImplemented
--------------------------------------------
loadInputFile :: SymbolName -> IO Expr
loadInputFile name = do
......@@ -58,11 +54,5 @@ loadInputFile name = do
where
toRecord stringList = Record $ map ((String).stripWhitespace) stringList
stripWhitespace = stripTrailingWhitespace.dropWhile (==' ')
stripTrailingWhitespace (' ':xs) = let remainder = stripTrailingWhitespace xs in
if null remainder then [] else ' ':remainder
stripTrailingWhitespace (x:xs) = x : stripTrailingWhitespace xs
stripTrailingWhitespace [] = []
......@@ -4,4 +4,4 @@ type Row = [String]
-- | Takes in a list of rows and prints them in lexicographical order
print2DListLex :: [Row] -> IO()
print2DListLex rows = returnIO $ sort rows
print2DListLex rows = return $ sort rows
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment