Commit 1cbf18b2 authored by pm3g19's avatar pm3g19
Browse files

Solves problem 2

parent 7583ee9c
......@@ -19,7 +19,7 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
Filter -> let (Set inputRecords,predicate) = (eval' $ head inputSets, eval' $ head args) in
Set $ filter ((==Boolean True).(\r -> eval' $ FuncCall predicate [] [r])) $ map eval' inputRecords
Map -> Set (map (\record -> FuncCall lambda [] [record]) records)
Map -> Set (map (\record -> eval' $ FuncCall lambda [] [record]) records)
where
(Set records:_) = inputSets
(lambda:_) = args
......@@ -31,12 +31,12 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
Set $ [ x `concatRecord` y | x <- l1, y <- l2]
RecordIndex -> let (Record recordData:Int index:_) = (map eval' args) in
recordData !! index
recordData !! (index - 1)
RecordSelect -> Record filteredList
RecordSelect -> debug (show expr) $ Record filteredList
where
(Record recordData: indexes) = args
indexesRaw = map (\(Int i) -> i) indexes
(Record recordData: indexes) = map eval' args
indexesRaw = map (\(Int i) -> i - 1) indexes
filteredList = map (recordData!!) indexesRaw
......@@ -49,7 +49,7 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
Contains -> case args of
(mainString:containsWhat:_) -> case (eval' mainString , eval' containsWhat) of
(String a, String b) -> a `beginsWith` b
(String a, String b) -> b `sublist` a
Plus -> let (e1:e2:_) = args in case (eval' e1, eval' e2) of
(String a, String b) -> String (a ++ b)
......@@ -87,4 +87,5 @@ evalFull = eval
--evalFull _ e = e
--TODO implement properly
concatRecord (Record r1) (Record r2) = Record (r1 ++ r2)
beginsWith = notImplemented
\ No newline at end of file
sublist = notImplemented
\ No newline at end of file
......@@ -16,7 +16,7 @@ main = do
args <- getArgs
case args of
(srcname:_) -> interpret srcname
_ -> interpret "solutions/pr1.cql"
_ -> interpret "solutions/pr2.cql"
interpret :: FilePath -> IO () -- the main function, takes in file name, prints out result
......@@ -47,7 +47,7 @@ showFinal :: Expr -> IO ()
showFinal = (print2DList.sort2DListLex.setTo2DList)
setTo2DList :: Expr -> [[String]]
setTo2DList (Set records) = map (map (\(String s) -> s).(\(Record list) -> list)) records
setTo2DList (Set records) = traceShow records $ map (map (\(String s) -> s).(\(Record list) -> list)) records
--------------------------------------------
......
......@@ -3,61 +3,113 @@ module Lexer where
import Data.List
}
%wrapper "basic"
%wrapper "posn"
$digit = 0-9
$posDigit = 1-9
$alpha = [a-zA-Z]
$lower = [a-z]
$upper = [A-Z]
--\\map(\r -> r[1,2,3])
tokens :-
$white+ ;
filter {\s -> TokenFilter }
true {\s -> TokenTrue }
false {\s -> TokenFalse }
\.in {\s -> TokenInSet }
\.out {\s -> TokenOutSet }
\[ {\s -> TokenLeftSqBracket }
\] {\s -> TokenRightSqBracket }
"->" {\s -> TokenArrow }
"==" {\s -> TokenisEqual }
"/=" {\s -> TokenisNotEqual }
\( {\s -> TokenLeftBracket }
\) {\s -> TokenRightBracket }
\; {\s -> TokenSemiCol }
\\ {\s -> TokenLambda }
\, {\s -> TokenComma }
\. {\s -> TokenFullStop }
x {\s -> TokenXProduct }
$lower [$lower $digit \_ \']* {\s -> TokenVarName s }
$upper[$alpha]* {\s -> TokenSetName s }
$digit+ {\s -> TokenNat (read s) }
\"[$alpha $digit]+\" {\s -> ((TokenString).init.tail) s }
\#.* ;
contains {\p s -> TokenContains p }
isEmpty {\p s -> TokenIsEmpty p }
filter {\p s -> TokenFilter p }
true {\p s -> TokenTrue p }
false {\p s -> TokenFalse p }
\.in {\p s -> TokenInSet p }
\.out {\p s -> TokenOutSet p }
\[ {\p s -> TokenLeftSqBracket p }
\] {\p s -> TokenRightSqBracket p }
"->" {\p s -> TokenArrow p }
"==" {\p s -> TokenisEqual p }
"/=" {\p s -> TokenisNotEqual p }
"+" {\p s -> TokenPlus p }
\( {\p s -> TokenLeftBracket p }
\) {\p s -> TokenRightBracket p }
\: {\p s -> TokenCol p }
\; {\p s -> TokenSemiCol p }
\\ {\p s -> TokenLambda p }
\, {\p s -> TokenComma p }
\. {\p s -> TokenFullStop p }
x {\p s -> TokenXProduct p }
xx {\p s -> TokenXXProduct p }
map {\p s -> TokenMap p }
$lower [$lower $digit \_ \']* {\p s -> TokenVarName p s }
$upper[$alpha]* {\p s -> TokenSetName p s }
--$posDigit$digit* {\p s -> TokenPosNat p (read s) }
$digit+ {\p s -> TokenNat p (read s) }
\"[$alpha $digit]+\" {\p s -> TokenString p (init.tail $ s) }
{
--token type:
data Token =
TokenFilter |
TokenSetName String |
TokenFunc |
TokenNat Int |
TokenVarName String |
TokenTrue |
TokenFalse |
TokenString String |
TokenLeftSqBracket |
TokenRightSqBracket |
TokenArrow |
TokenisEqual |
TokenisNotEqual |
TokenLeftBracket |
TokenRightBracket |
TokenSemiCol |
TokenLambda |
TokenComma |
TokenFullStop |
TokenInSet |
TokenXProduct |
TokenOutSet
data Token =
TokenFilter AlexPosn |
TokenIsEmpty AlexPosn |
TokenContains AlexPosn |
TokenSetName AlexPosn String |
TokenNat AlexPosn Int |
TokenPosNat AlexPosn Int |
TokenVarName AlexPosn String |
TokenTrue AlexPosn |
TokenFalse AlexPosn |
TokenString AlexPosn String |
TokenLeftSqBracket AlexPosn |
TokenRightSqBracket AlexPosn |
TokenArrow AlexPosn |
TokenisEqual AlexPosn |
TokenisNotEqual AlexPosn |
TokenPlus AlexPosn |
TokenLeftBracket AlexPosn |
TokenRightBracket AlexPosn |
TokenSemiCol AlexPosn |
TokenCol AlexPosn |
TokenLambda AlexPosn |
TokenComma AlexPosn |
TokenFullStop AlexPosn |
TokenInSet AlexPosn |
TokenXProduct AlexPosn |
TokenXXProduct AlexPosn |
TokenOutSet AlexPosn |
TokenMap AlexPosn
deriving (Eq, Show)
}
pos :: Token -> AlexPosn
pos token = case token of
(TokenFilter p ) -> p
(TokenIsEmpty p ) -> p
(TokenContains p ) -> p
(TokenSetName p _) -> p
(TokenNat p _) -> p
(TokenPosNat p _) -> p
(TokenVarName p _) -> p
(TokenTrue p ) -> p
(TokenFalse p ) -> p
(TokenString p _) -> p
(TokenLeftSqBracket p ) -> p
(TokenRightSqBracket p ) -> p
(TokenArrow p ) -> p
(TokenisEqual p ) -> p
(TokenisNotEqual p ) -> p
(TokenPlus p ) -> p
(TokenLeftBracket p ) -> p
(TokenRightBracket p ) -> p
(TokenSemiCol p ) -> p
(TokenCol p ) -> p
(TokenLambda p ) -> p
(TokenComma p ) -> p
(TokenFullStop p ) -> p
(TokenInSet p ) -> p
(TokenXProduct p ) -> p
(TokenXXProduct p ) -> p
(TokenOutSet p ) -> p
(TokenMap p) -> p
}
\ No newline at end of file
This diff is collapsed.
......@@ -11,7 +11,7 @@ data PredefFunc = XProduct | XXProduct | IsEqual | IsNotEqual | Plus --operators
| Map | Filter
| RecordIndex -- [] operator
| RecordSelect
| IsEmpty | NotEmpty | Contains | IsSubString -- string functions
| IsEmpty | NotEmpty | Contains -- string functions
deriving (Show, Eq)
......
......@@ -2,4 +2,5 @@
A:3
.out
filter((r) -> isSubstring(r[1], r[2]));
\ No newline at end of file
filter( \(r) -> r[1] == r[2]);
map (\(r) -> r[3,1]);
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment