Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
C
comp2212-cw-2021
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
PLC
comp2212-cw-2021
Commits
1cbf18b2
Commit
1cbf18b2
authored
4 years ago
by
pm3g19
Browse files
Options
Downloads
Patches
Plain Diff
Solves problem 2
parent
7583ee9c
No related branches found
No related tags found
No related merge requests found
Changes
6
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
Eval.hs
+8
-7
8 additions, 7 deletions
Eval.hs
Interpreter.hs
+2
-2
2 additions, 2 deletions
Interpreter.hs
Lexer.x
+98
-46
98 additions, 46 deletions
Lexer.x
Parser.hs
+447
-204
447 additions, 204 deletions
Parser.hs
Types.hs
+1
-1
1 addition, 1 deletion
Types.hs
solutions/pr2.cql
+2
-1
2 additions, 1 deletion
solutions/pr2.cql
with
558 additions
and
261 deletions
Eval.hs
+
8
−
7
View file @
1cbf18b2
...
@@ -19,7 +19,7 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
...
@@ -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
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
Map
->
Set
(
map
(
\
record
->
FuncCall
lambda
[]
[
record
])
records
)
Map
->
Set
(
map
(
\
record
->
eval'
$
FuncCall
lambda
[]
[
record
])
records
)
where
where
(
Set
records
:
_
)
=
inputSets
(
Set
records
:
_
)
=
inputSets
(
lambda
:
_
)
=
args
(
lambda
:
_
)
=
args
...
@@ -31,12 +31,12 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
...
@@ -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
]
Set
$
[
x
`
concatRecord
`
y
|
x
<-
l1
,
y
<-
l2
]
RecordIndex
->
let
(
Record
recordData
:
Int
index
:
_
)
=
(
map
eval'
args
)
in
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
where
(
Record
recordData
:
indexes
)
=
args
(
Record
recordData
:
indexes
)
=
map
eval'
args
indexesRaw
=
map
(
\
(
Int
i
)
->
i
)
indexes
indexesRaw
=
map
(
\
(
Int
i
)
->
i
-
1
)
indexes
filteredList
=
map
(
recordData
!!
)
indexesRaw
filteredList
=
map
(
recordData
!!
)
indexesRaw
...
@@ -49,7 +49,7 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
...
@@ -49,7 +49,7 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
Contains
->
case
args
of
Contains
->
case
args
of
(
mainString
:
containsWhat
:
_
)
->
case
(
eval'
mainString
,
eval'
containsWhat
)
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
Plus
->
let
(
e1
:
e2
:
_
)
=
args
in
case
(
eval'
e1
,
eval'
e2
)
of
(
String
a
,
String
b
)
->
String
(
a
++
b
)
(
String
a
,
String
b
)
->
String
(
a
++
b
)
...
@@ -87,4 +87,5 @@ evalFull = eval
...
@@ -87,4 +87,5 @@ evalFull = eval
--evalFull _ e = e
--evalFull _ e = e
--TODO implement properly
--TODO implement properly
concatRecord
(
Record
r1
)
(
Record
r2
)
=
Record
(
r1
++
r2
)
concatRecord
(
Record
r1
)
(
Record
r2
)
=
Record
(
r1
++
r2
)
beginsWith
=
notImplemented
\ No newline at end of file
sublist
=
notImplemented
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Interpreter.hs
+
2
−
2
View file @
1cbf18b2
...
@@ -16,7 +16,7 @@ main = do
...
@@ -16,7 +16,7 @@ main = do
args
<-
getArgs
args
<-
getArgs
case
args
of
case
args
of
(
srcname
:
_
)
->
interpret
srcname
(
srcname
:
_
)
->
interpret
srcname
_
->
interpret
"solutions/pr
1
.cql"
_
->
interpret
"solutions/pr
2
.cql"
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
...
@@ -47,7 +47,7 @@ showFinal :: Expr -> IO ()
...
@@ -47,7 +47,7 @@ showFinal :: Expr -> IO ()
showFinal
=
(
print2DList
.
sort2DListLex
.
setTo2DList
)
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
)
=
traceShow
records
$
map
(
map
(
\
(
String
s
)
->
s
)
.
(
\
(
Record
list
)
->
list
))
records
--------------------------------------------
--------------------------------------------
...
...
This diff is collapsed.
Click to expand it.
Lexer.x
+
98
−
46
View file @
1cbf18b2
...
@@ -3,61 +3,113 @@ module Lexer where
...
@@ -3,61 +3,113 @@ module Lexer where
import Data.List
import Data.List
}
}
%wrapper "
basic
"
%wrapper "
posn
"
$digit = 0-9
$digit = 0-9
$posDigit = 1-9
$alpha = [a-zA-Z]
$alpha = [a-zA-Z]
$lower = [a-z]
$lower = [a-z]
$upper = [A-Z]
$upper = [A-Z]
--\\map(\r -> r[1,2,3])
tokens :-
tokens :-
$white+ ;
$white+ ;
filter {\s -> TokenFilter }
\#.* ;
true {\s -> TokenTrue }
contains {\p s -> TokenContains p }
false {\s -> TokenFalse }
isEmpty {\p s -> TokenIsEmpty p }
\.in {\s -> TokenInSet }
filter {\p s -> TokenFilter p }
\.out {\s -> TokenOutSet }
true {\p s -> TokenTrue p }
\[ {\s -> TokenLeftSqBracket }
false {\p s -> TokenFalse p }
\] {\s -> TokenRightSqBracket }
\.in {\p s -> TokenInSet p }
"->" {\s -> TokenArrow }
\.out {\p s -> TokenOutSet p }
"==" {\s -> TokenisEqual }
\[ {\p s -> TokenLeftSqBracket p }
"/=" {\s -> TokenisNotEqual }
\] {\p s -> TokenRightSqBracket p }
\( {\s -> TokenLeftBracket }
"->" {\p s -> TokenArrow p }
\) {\s -> TokenRightBracket }
"==" {\p s -> TokenisEqual p }
\; {\s -> TokenSemiCol }
"/=" {\p s -> TokenisNotEqual p }
\\ {\s -> TokenLambda }
"+" {\p s -> TokenPlus p }
\, {\s -> TokenComma }
\( {\p s -> TokenLeftBracket p }
\. {\s -> TokenFullStop }
\) {\p s -> TokenRightBracket p }
x {\s -> TokenXProduct }
\: {\p s -> TokenCol p }
$lower [$lower $digit \_ \']* {\s -> TokenVarName s }
\; {\p s -> TokenSemiCol p }
$upper[$alpha]* {\s -> TokenSetName s }
\\ {\p s -> TokenLambda p }
$digit+ {\s -> TokenNat (read s) }
\, {\p s -> TokenComma p }
\"[$alpha $digit]+\" {\s -> ((TokenString).init.tail) s }
\. {\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:
--token type:
data Token =
data Token =
TokenFilter |
TokenFilter AlexPosn |
TokenSetName String |
TokenIsEmpty AlexPosn |
TokenFunc |
TokenContains AlexPosn |
TokenNat Int |
TokenSetName AlexPosn String |
TokenVarName String |
TokenNat AlexPosn Int |
TokenTrue |
TokenPosNat AlexPosn Int |
TokenFalse |
TokenVarName AlexPosn String |
TokenString String |
TokenTrue AlexPosn |
TokenLeftSqBracket |
TokenFalse AlexPosn |
TokenRightSqBracket |
TokenString AlexPosn String |
TokenArrow |
TokenLeftSqBracket AlexPosn |
TokenisEqual |
TokenRightSqBracket AlexPosn |
TokenisNotEqual |
TokenArrow AlexPosn |
TokenLeftBracket |
TokenisEqual AlexPosn |
TokenRightBracket |
TokenisNotEqual AlexPosn |
TokenSemiCol |
TokenPlus AlexPosn |
TokenLambda |
TokenLeftBracket AlexPosn |
TokenComma |
TokenRightBracket AlexPosn |
TokenFullStop |
TokenSemiCol AlexPosn |
TokenInSet |
TokenCol AlexPosn |
TokenXProduct |
TokenLambda AlexPosn |
TokenOutSet
TokenComma AlexPosn |
TokenFullStop AlexPosn |
TokenInSet AlexPosn |
TokenXProduct AlexPosn |
TokenXXProduct AlexPosn |
TokenOutSet AlexPosn |
TokenMap AlexPosn
deriving (Eq, Show)
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.
Click to expand it.
Parser.hs
+
447
−
204
View file @
1cbf18b2
This diff is collapsed.
Click to expand it.
Types.hs
+
1
−
1
View file @
1cbf18b2
...
@@ -11,7 +11,7 @@ data PredefFunc = XProduct | XXProduct | IsEqual | IsNotEqual | Plus --operators
...
@@ -11,7 +11,7 @@ data PredefFunc = XProduct | XXProduct | IsEqual | IsNotEqual | Plus --operators
|
Map
|
Filter
|
Map
|
Filter
|
RecordIndex
-- [] operator
|
RecordIndex
-- [] operator
|
RecordSelect
|
RecordSelect
|
IsEmpty
|
NotEmpty
|
Contains
|
IsSubString
-- string functions
|
IsEmpty
|
NotEmpty
|
Contains
-- string functions
deriving
(
Show
,
Eq
)
deriving
(
Show
,
Eq
)
...
...
This diff is collapsed.
Click to expand it.
solutions/pr2.cql
+
2
−
1
View file @
1cbf18b2
...
@@ -2,4 +2,5 @@
...
@@ -2,4 +2,5 @@
A:3
A:3
.out
.out
filter((r) -> isSubstring(r[1], r[2]));
filter( \(r) -> r[1] == r[2]);
\ No newline at end of file
map (\(r) -> r[3,1]);
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment