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

Added support for cartesian product

parent 492e7639
No related branches found
No related tags found
No related merge requests found
A.csv 0 → 100644
patryk,malinowski
maram, jones
jeffrey,sylvester
B.csv 0 → 100644
jian,shi
andrew,sogokon
julian,rathke
\ No newline at end of file
...@@ -25,6 +25,9 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o ...@@ -25,6 +25,9 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
RecordIndex -> let (Record recordData:Int index:_) = (map eval' args) in 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 --implement later
--(Map) -> Set $ (map (\r -> FuncCall predicate [] [r]) inputRecords) --(Map) -> Set $ (map (\r -> FuncCall predicate [] [r]) inputRecords)
(FuncDef setParams argParams body) -> eval newEnv body (FuncDef setParams argParams body) -> eval newEnv body
...@@ -49,3 +52,4 @@ evalFull = eval ...@@ -49,3 +52,4 @@ evalFull = eval
--evalFull env (Set xs) = Set (map (eval env) xs) -- evaluates expression fully (not just weak head normal form) --evalFull env (Set xs) = Set (map (eval env) xs) -- evaluates expression fully (not just weak head normal form)
--evalFull _ e = e --evalFull _ e = e
--TODO implement properly --TODO implement properly
concatRecord (Record r1) (Record r2) = Record (r1 ++ r2)
\ No newline at end of file
...@@ -2,6 +2,7 @@ import Types ...@@ -2,6 +2,7 @@ import Types
import Debug import Debug
import Eval import Eval
import System.IO import System.IO
import System.Environment
import CSV import CSV
import Lexer import Lexer
import Parser import Parser
...@@ -11,7 +12,12 @@ parse :: String -> Program ...@@ -11,7 +12,12 @@ parse :: String -> Program
parse = parseSource.alexScanTokens parse = parseSource.alexScanTokens
--outline --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 :: FilePath -> IO () -- the main function, takes in file name, prints out result
interpret sourceFName = do interpret sourceFName = do
......
...@@ -26,8 +26,9 @@ false {\s -> TokenFalse } ...@@ -26,8 +26,9 @@ false {\s -> TokenFalse }
\) {\s -> TokenRightBracket } \) {\s -> TokenRightBracket }
\; {\s -> TokenSemiCol } \; {\s -> TokenSemiCol }
\\ {\s -> TokenLambda } \\ {\s -> TokenLambda }
--\, {\s -> TokenComma } \, {\s -> TokenComma }
\. {\s -> TokenFullStop } \. {\s -> TokenFullStop }
x {\s -> TokenXProduct }
$lower [$lower $digit \_ \']* {\s -> TokenVarName s } $lower [$lower $digit \_ \']* {\s -> TokenVarName s }
$upper[$alpha]* {\s -> TokenSetName s } $upper[$alpha]* {\s -> TokenSetName s }
$digit+ {\s -> TokenNat (read s) } $digit+ {\s -> TokenNat (read s) }
...@@ -56,6 +57,7 @@ data Token = ...@@ -56,6 +57,7 @@ data Token =
TokenComma | TokenComma |
TokenFullStop | TokenFullStop |
TokenInSet | TokenInSet |
TokenXProduct |
TokenOutSet TokenOutSet
deriving (Eq, Show) deriving (Eq, Show)
} }
...@@ -29,6 +29,7 @@ import Types ...@@ -29,6 +29,7 @@ import Types
'\\' { TokenLambda } '\\' { TokenLambda }
',' { TokenComma } ',' { TokenComma }
'.' { TokenFullStop } '.' { TokenFullStop }
x { TokenXProduct }
%right "->" %right "->"
%left "/=" "==" ';' %left "/=" "==" ';'
...@@ -49,6 +50,8 @@ SetFuncCalls : SetFuncCall {[$1]} ...@@ -49,6 +50,8 @@ SetFuncCalls : SetFuncCall {[$1]}
SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [Var $3] [$6]} SetFuncCall : filter '['SetName']' '('Func')' {FuncCall (PredefFunc Filter) [Var $3] [$6]}
| filter '('Func')' {FuncCall (PredefFunc Filter) [] [$3]} | filter '('Func')' {FuncCall (PredefFunc Filter) [] [$3]}
| SetName x SetName {FuncCall (PredefFunc XProduct) (map Var [$1, $3]) []}
Func : '\\' '(' VarNames ')' "->" Expr {FuncDef [] $3 $6} Func : '\\' '(' VarNames ')' "->" Expr {FuncDef [] $3 $6}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment