From c29081a80883c44af0ee5dd531678c6e8a920a8d Mon Sep 17 00:00:00 2001
From: p9malino26 <pm3g19@soton.ac.uk>
Date: Mon, 19 Apr 2021 11:43:36 +0100
Subject: [PATCH] First fully-working version of interpreter

---
 CSV.hs            | 23 +++++++++++++++++++++++
 Interpreter.hs    | 14 ++------------
 Print2DListLex.hs |  2 +-
 3 files changed, 26 insertions(+), 13 deletions(-)

diff --git a/CSV.hs b/CSV.hs
index 43f486d..4906903 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 058a4f2..8eafcdd 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 0d85356..e4e3f9f 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
-- 
GitLab