From d4b0e89483d1d158e13f45eacdff21ff9b0ccaf3 Mon Sep 17 00:00:00 2001 From: p9malino26 <pm3g19@soton.ac.uk> Date: Fri, 23 Apr 2021 21:40:29 +0100 Subject: [PATCH] Added support for cartesian product --- A.csv | 3 +++ B.csv | 3 +++ Eval.hs | 12 ++++++++---- Interpreter.hs | 8 +++++++- Lexer.x | 4 +++- Parser.y | 3 +++ 6 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 A.csv create mode 100644 B.csv diff --git a/A.csv b/A.csv new file mode 100644 index 0000000..3c46149 --- /dev/null +++ b/A.csv @@ -0,0 +1,3 @@ +patryk,malinowski +maram, jones +jeffrey,sylvester diff --git a/B.csv b/B.csv new file mode 100644 index 0000000..bf47bb3 --- /dev/null +++ b/B.csv @@ -0,0 +1,3 @@ +jian,shi +andrew,sogokon +julian,rathke \ No newline at end of file diff --git a/Eval.hs b/Eval.hs index 7f9a370..7d41bec 100644 --- a/Eval.hs +++ b/Eval.hs @@ -17,13 +17,16 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o FuncCall func inputSets args -> case func of (PredefFunc f) -> case f of Filter -> let (Set inputRecords,predicate) = (eval' $ head inputSets, eval' $ head args) in - Set $ filter ((==Boolean True).(\r -> eval' $ FuncCall predicate [] [r])) $ map eval' inputRecords + Set $ filter ((==Boolean True).(\r -> eval' $ FuncCall predicate [] [r])) $ map eval' inputRecords IsEqual -> let (e1:e2:_) = args in -- TODO not sufficent. - Boolean (eval' e1 == eval' e2) + Boolean (eval' e1 == eval' e2) RecordIndex -> let (Record recordData:Int index:_) = (map eval' args) in - recordData !! index + recordData !! index + + XProduct -> debug (show env) $ let ((Set l1): (Set l2):_) = map eval' inputSets in + Set $ [ x `concatRecord` y | x <- l1, y <- l2] --implement later --(Map) -> Set $ (map (\r -> FuncCall predicate [] [r]) inputRecords) @@ -48,4 +51,5 @@ evalControl1 env (Control last (currentExpr:exprs)) = let output = eval env curr evalFull = eval --evalFull env (Set xs) = Set (map (eval env) xs) -- evaluates expression fully (not just weak head normal form) --evalFull _ e = e ---TODO implement properly \ No newline at end of file +--TODO implement properly +concatRecord (Record r1) (Record r2) = Record (r1 ++ r2) \ No newline at end of file diff --git a/Interpreter.hs b/Interpreter.hs index 8eafcdd..97a5a6b 100644 --- a/Interpreter.hs +++ b/Interpreter.hs @@ -2,6 +2,7 @@ import Types import Debug import Eval import System.IO +import System.Environment import CSV import Lexer import Parser @@ -11,7 +12,12 @@ parse :: String -> Program parse = parseSource.alexScanTokens --outline -main = interpret "sampleprogram.txt" +main = do + args <- getArgs + case args of + (srcname:_) -> interpret srcname + _ -> interpret "q1.txt" + interpret :: FilePath -> IO () -- the main function, takes in file name, prints out result interpret sourceFName = do diff --git a/Lexer.x b/Lexer.x index 8f37a6a..407bb44 100644 --- a/Lexer.x +++ b/Lexer.x @@ -26,8 +26,9 @@ false {\s -> TokenFalse } \) {\s -> TokenRightBracket } \; {\s -> TokenSemiCol } \\ {\s -> TokenLambda } ---\, {\s -> TokenComma } +\, {\s -> TokenComma } \. {\s -> TokenFullStop } +x {\s -> TokenXProduct } $lower [$lower $digit \_ \']* {\s -> TokenVarName s } $upper[$alpha]* {\s -> TokenSetName s } $digit+ {\s -> TokenNat (read s) } @@ -56,6 +57,7 @@ data Token = TokenComma | TokenFullStop | TokenInSet | + TokenXProduct | TokenOutSet deriving (Eq, Show) } diff --git a/Parser.y b/Parser.y index 8be3046..8875f80 100644 --- a/Parser.y +++ b/Parser.y @@ -29,6 +29,7 @@ import Types '\\' { TokenLambda } ',' { TokenComma } '.' { TokenFullStop } + x { TokenXProduct } %right "->" %left "/=" "==" ';' @@ -49,6 +50,8 @@ SetFuncCalls : SetFuncCall {[$1]} SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [Var $3] [$6]} | filter '('Func')' {FuncCall (PredefFunc Filter) [] [$3]} + | SetName x SetName {FuncCall (PredefFunc XProduct) (map Var [$1, $3]) []} + Func : '\\' '(' VarNames ')' "->" Expr {FuncDef [] $3 $6} -- GitLab