Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
U
uva
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
Taddeüs Kroes
uva
Commits
feedb9d1
Commit
feedb9d1
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
funclang series4: Improved assigmment 10.
parent
1f71a51d
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
funclang-taddeus/series4/ass10.ml
+40
-52
40 additions, 52 deletions
funclang-taddeus/series4/ass10.ml
with
40 additions
and
52 deletions
funclang-taddeus/series4/ass10.ml
+
40
−
52
View file @
feedb9d1
type
arithOp
=
Plus
of
int
*
int
|
Minus
of
int
*
int
|
Times
of
int
*
int
|
Divide
of
int
*
int
|
Modulo
of
int
*
int
type
relOp
=
EQ
of
int
*
int
|
NEQ
of
int
*
int
|
LT
of
int
*
int
|
LTE
of
int
*
int
|
GT
of
int
*
int
|
GTE
of
int
*
int
type
logicOp
=
AND
of
bool
*
bool
|
OR
of
bool
*
bool
type
binOp
=
ArithOp
of
arithOp
|
RelOp
of
relOp
|
LogicOp
of
logicOp
type
monOp
=
UnaryMinus
of
int
|
Negation
of
bool
type
const
=
BoolConst
of
bool
|
IntConst
of
int
type
arithOp
=
Plus
|
Minus
|
Times
|
Divide
|
Modulo
type
relOp
=
Eq
|
Neq
|
Lt
|
Lte
|
Gt
|
Gte
type
logicOp
=
And
|
Or
type
binOp
=
ArithOp
of
arithOp
|
RelOp
of
relOp
|
LogicOp
of
logicOp
type
monOp
=
UnaryMinus
|
Negation
type
const
=
BoolConst
of
bool
|
IntConst
of
int
type
expr
=
Enclosure
of
expr
|
BinOp
of
expr
*
binOp
*
expr
...
...
@@ -40,27 +14,23 @@ type expr =
let
rec
eval_expr
=
let
eval_binOp
=
let
eval_arithOp
=
function
Plus
(
a
,
b
)
->
"("
^
(
string_of_int
a
)
^
" + "
^
(
string_of_int
b
)
|
Minus
(
a
,
b
)
->
"("
^
(
string_of_int
a
)
^
" - "
^
(
string_of_int
b
)
^
")"
|
Times
(
a
,
b
)
->
"("
^
(
string_of_int
a
)
^
" * "
^
(
string_of_int
b
)
^
")"
|
Divide
(
a
,
b
)
->
"("
^
(
string_of_int
a
)
^
" / "
^
(
string_of_int
b
)
^
")"
|
Modulo
(
a
,
b
)
->
"("
^
(
string_of_int
a
)
^
" mod "
^
(
string_of_int
b
)
^
")"
Plus
->
"+"
|
Minus
->
"-"
|
Times
->
"*"
|
Divide
->
"/"
|
Modulo
->
"mod"
in
let
eval_relOp
=
function
E
Q
(
a
,
b
)
->
(
string_of_int
a
)
^
" = "
^
(
string_of_int
b
)
|
N
EQ
(
a
,
b
)
->
(
string_of_int
a
)
^
" != "
^
(
string_of_int
b
)
|
L
T
(
a
,
b
)
->
(
string_of_int
a
)
^
" < "
^
(
string_of_int
b
)
|
L
TE
(
a
,
b
)
->
(
string_of_int
a
)
^
" <= "
^
(
string_of_int
b
)
|
G
T
(
a
,
b
)
->
(
string_of_int
a
)
^
" > "
^
(
string_of_int
b
)
|
G
TE
(
a
,
b
)
->
(
string_of_int
a
)
^
" >= "
^
(
string_of_int
b
)
E
q
->
"="
|
N
eq
->
"!="
|
L
t
->
"<"
|
L
te
->
"<="
|
G
t
->
">"
|
G
te
->
">="
in
let
eval_logicOp
=
function
A
ND
(
a
,
b
)
->
(
string_of_bool
a
)
^
" && "
^
(
string_of_bool
b
)
|
O
R
(
a
,
b
)
->
(
string_of_bool
a
)
^
" || "
^
(
string_of_bool
b
)
A
nd
->
"&&"
|
O
r
->
"||"
in
function
ArithOp
op
->
eval_arithOp
(
op
)
...
...
@@ -68,8 +38,8 @@ let rec eval_expr =
|
LogicOp
op
->
eval_logicOp
(
op
)
in
let
eval_monOp
=
function
UnaryMinus
i
->
"-"
^
string_of_int
i
|
Negation
b
->
"!"
^
string_of_bool
b
UnaryMinus
->
"-"
|
Negation
->
"!"
in
let
eval_const
=
function
IntConst
i
->
string_of_int
i
...
...
@@ -77,7 +47,25 @@ let rec eval_expr =
in
function
Enclosure
e
->
"("
^
eval_expr
(
e
)
^
")"
|
BinOp
(
e1
,
op
,
e2
)
->
eval_expr
(
e1
)
^
eval_binOp
(
op
)
^
eval_expr
(
e2
)
|
BinOp
(
e1
,
op
,
e2
)
->
eval_expr
(
e1
)
^
" "
^
eval_binOp
(
op
)
^
" "
^
eval_expr
(
e2
)
|
MonOp
(
op
,
e
)
->
eval_monOp
(
op
)
^
eval_expr
(
e
)
|
Id
id
->
id
|
Const
c
->
eval_const
(
c
)
;;
(* (a) *)
print_endline
(
eval_expr
(
Enclosure
(
Id
"a"
)));;
(* -a *)
print_endline
(
eval_expr
(
MonOp
(
UnaryMinus
,
Id
"a"
)));;
(* a - b *)
print_endline
(
eval_expr
(
BinOp
(
Id
"a"
,
ArithOp
Minus
,
Id
"b"
)));;
(* a * (-b + 1) *)
let
a_times
b
=
BinOp
(
Id
"a"
,
ArithOp
Times
,
b
)
in
let
uminus
a
=
MonOp
(
UnaryMinus
,
a
)
in
let
plus
a
b
=
BinOp
(
a
,
ArithOp
Plus
,
b
)
in
let
one
=
Const
(
IntConst
1
)
in
print_endline
(
eval_expr
(
a_times
(
Enclosure
(
plus
(
uminus
(
Id
"b"
))
one
))));;
(* a = b && c *)
let
b_and_c
=
BinOp
(
Id
"b"
,
LogicOp
And
,
Id
"c"
)
in
print_endline
(
eval_expr
(
BinOp
(
Id
"a"
,
RelOp
Eq
,
b_and_c
)));;
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