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 module CSV where
import System.IO import System.IO
import Data.List
readCSV :: FilePath -> IO [[String]] readCSV :: FilePath -> IO [[String]]
readCSV fname = do readCSV fname = do
...@@ -17,3 +18,25 @@ split p l = case span p l of ...@@ -17,3 +18,25 @@ split p l = case span p l of
([], _) -> [] ([], _) -> []
(match, []) -> [match] (match, []) -> [match]
(match, _:rem') -> match:split p rem' (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 ...@@ -9,7 +9,6 @@ import Debug.Trace
parse :: String -> Program parse :: String -> Program
parse = parseSource.alexScanTokens parse = parseSource.alexScanTokens
--outline --outline
main = interpret "sampleprogram.txt" main = interpret "sampleprogram.txt"
...@@ -19,9 +18,9 @@ interpret sourceFName = do ...@@ -19,9 +18,9 @@ interpret sourceFName = do
source <- readFile sourceFName source <- readFile sourceFName
let program = parse source -- main abstract syntax tree let program = parse source -- main abstract syntax tree
(env, mainExpr) <- prepare program (env, mainExpr) <- prepare program
print $ "Main expression " ++ show mainExpr --print $ "Main expression " ++ show mainExpr
let finalOutput = eval env mainExpr let finalOutput = eval env mainExpr
print $ "Final outupt: " ++ show finalOutput --print $ "Final outupt: " ++ show finalOutput
showFinal 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 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) ...@@ -44,10 +43,7 @@ showFinal = (print2DList.sort2DListLex.setTo2DList)
setTo2DList :: Expr -> [[String]] setTo2DList :: Expr -> [[String]]
setTo2DList (Set records) = map (map (\(String s) -> s).(\(Record list) -> list)) records 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 :: SymbolName -> IO Expr
loadInputFile name = do loadInputFile name = do
...@@ -58,11 +54,5 @@ loadInputFile name = do ...@@ -58,11 +54,5 @@ loadInputFile name = do
where where
toRecord stringList = Record $ map ((String).stripWhitespace) stringList 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] ...@@ -4,4 +4,4 @@ type Row = [String]
-- | Takes in a list of rows and prints them in lexicographical order -- | Takes in a list of rows and prints them in lexicographical order
print2DListLex :: [Row] -> IO() 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