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
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
PLC
comp2212-cw-2021
Commits
e04ef58f
Commit
e04ef58f
authored
4 years ago
by
pm3g19
Browse files
Options
Downloads
Patches
Plain Diff
Improved error checking
parent
84040589
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
Eval.hs
+12
-5
12 additions, 5 deletions
Eval.hs
Interpreter.hs
+11
-5
11 additions, 5 deletions
Interpreter.hs
with
23 additions
and
10 deletions
Eval.hs
+
12
−
5
View file @
e04ef58f
...
...
@@ -28,7 +28,7 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
IsEqual
->
case
args'
of
-- TODO not sufficent.
[
e1
,
e2
]
->
Boolean
(
e1
==
e2
)
_
->
"isEqual argument error"
_
->
error
"isEqual argument error"
XProduct
->
case
inputSets'
of
[(
Set
l1
),
(
Set
l2
)]
->
Set
$
[
x
`
concatRecord
`
y
|
x
<-
l1
,
y
<-
l2
]
...
...
@@ -38,16 +38,19 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
[
Boolean
e1
,
Boolean
e2
]
->
Boolean
(
e1
`
op
`
e2
)
_
->
error
"Boolean function takes in two arguments."
RecordIndex
->
eval'
$
let
(
Record
recordData
:
Int
index
:
_
)
=
args'
in
recordData
!!
(
index
-
1
)
RecordIndex
->
eval'
$
case
args'
of
[
Record
recordData
,
Int
index
]
-
>
recordData
`
listAtIndex
`
index
--TODO CBH
RecordSelect
->
case
args'
of
(
Record
recordData
:
indexes
)
->
Record
filteredList
where
indexesRaw
=
map
checkInt
indexes
filteredList
=
map
(
eval'
.
(
recordData
!!
))
indexesRaw
checkInt
(
Int
i
)
=
i
-
1
--filteredList = map (eval'.(recordData !!)) indexesRaw
filteredList
=
map
(
eval'
.
listAtIndex
recordData
)
indexesRaw
checkInt
(
Int
i
)
=
i
checkInt
_
=
error
"Arguments to index record must be integers"
{-numberedElems = zip [1..] recordData :: [(Int, Expr)]
filtered = filter ((`elem` indexesRaw).fst) numberedElems :: [(Int, Expr)]
filteredList = map snd filtered :: [Expr]-}
...
...
@@ -93,9 +96,13 @@ eval env expr = let (eval', evalFull') = (eval env, evalFull env) in case expr o
let
(
newEnv
,
newControl
)
=
evalControl1
env
control
in
eval
newEnv
newControl
(
Record
exprs
)
->
Record
$
map
eval'
exprs
(
Record
exprs
)
->
Record
$
map
eval'
exprs
_
->
expr
listAtIndex
::
[
a
]
->
Int
->
a
listAtIndex
recordData
i
|
i
>
length
recordData
=
error
$
"Index "
++
show
i
++
" too large."
|
otherwise
=
recordData
!!
(
i
-
1
)
evalControl1
::
Environment
->
Expr
->
(
Environment
,
Expr
)
evalControl1
env
(
Control
last
(
currentExpr
:
exprs
))
=
(
newEnv
,
Control
newLast
exprs
)
...
...
This diff is collapsed.
Click to expand it.
Interpreter.hs
+
11
−
5
View file @
e04ef58f
...
...
@@ -7,18 +7,24 @@ import CSV
import
Lexer
import
Parser
import
Debug.Trace
import
Control.Exception
printErr
s
=
hPutStrLn
stderr
(
"[ERROR] "
++
s
)
parse
::
String
->
Program
parse
=
parseSource
.
(
\
x
->
let
o
=
alexScanTokens
x
in
traceShowId
o
)
parse
=
parseSource
.
alexScanTokens
--outline
main
=
do
args
<-
getArgs
case
args
of
(
srcname
:
_
)
->
interpret
srcname
_
->
interpret
"solutions/pr4.cql"
--
_
->
interpret
"extra-problems/pr3.cql"
--
_
->
interpret
"sampleprogram.txt"
--("default":_) -> catch (interpret "/home/patryk/dev/plc/extra-problems/ex1.cql") (errorFunc)
(
srcname
:
_
)
->
catch
(
interpret
srcname
)
(
errorFunc
)
--
_
->
printErr
"No source file specified."
_
->
catch
(
interpret
"/home/patryk/dev/plc/extra-problems/ex1.cql"
)
(
errorFunc
)
errorFunc
::
ErrorCall
->
IO
()
errorFunc
=
printErr
.
show
interpret
::
FilePath
->
IO
()
-- the main function, takes in file name, prints out result
...
...
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