diff --git a/CSV.hs b/CSV.hs index 43f486d4191d639b4c39ea35e5cf62c2f1de7ae5..490690394c489885145b01eed7f8048d22eda22a 100644 --- a/CSV.hs +++ b/CSV.hs @@ -1,6 +1,7 @@ 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 diff --git a/Interpreter.hs b/Interpreter.hs index 058a4f2ac0c534bb4fe9b59da7d2d7200c8ae769..8eafcddfd0c91ca276492d04636f80acf7c88e5b 100644 --- a/Interpreter.hs +++ b/Interpreter.hs @@ -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 [] = [] diff --git a/Print2DListLex.hs b/Print2DListLex.hs index 0d853565c691e55f286a611c6fbfee63d609ba2e..e4e3f9fd3799ee6dc30a212306a7bf4f636c1c46 100644 --- a/Print2DListLex.hs +++ b/Print2DListLex.hs @@ -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