Fix main, added extra functionality, added extra tests in main. TODO: add real unit tests
This commit is contained in:
@ -1,24 +1,34 @@
|
||||
type variable = string
|
||||
|
||||
type p_exp =
|
||||
Main of variable * variable * c_exp (* def main with input x output y as c *)
|
||||
Main of variable * variable * c_exp (* def main with input x output y as c *)
|
||||
and c_exp =
|
||||
Skip
|
||||
| Assignment of variable * a_exp (* x := a *)
|
||||
| Sequence of c_exp * c_exp (* c; c *)
|
||||
| If of b_exp * c_exp * c_exp (* if b then c else c *)
|
||||
| While of b_exp * c_exp (* while b do c *)
|
||||
| Assignment of variable * a_exp (* x := a *)
|
||||
| Sequence of c_exp * c_exp (* c; c *)
|
||||
| If of b_exp * c_exp * c_exp (* if b then c else c *)
|
||||
| While of b_exp * c_exp (* while b do c *)
|
||||
| For of c_exp * b_exp * c_exp * c_exp (* for c; b; c do c *)
|
||||
and b_exp =
|
||||
Boolean of bool (* v *)
|
||||
| BAnd of b_exp * b_exp (* b and b *)
|
||||
| BNot of b_exp (* not b *)
|
||||
| BCmpLess of a_exp * a_exp (* a < a *)
|
||||
Boolean of bool (* v *)
|
||||
| BAnd of b_exp * b_exp (* b and b *)
|
||||
| BOr of b_exp * b_exp (* b or b *)
|
||||
| BNot of b_exp (* not b *)
|
||||
| BCmp of a_exp * a_exp (* a = a *)
|
||||
| BCmpLess of a_exp * a_exp (* a < a *)
|
||||
| BCmpLessEq of a_exp * a_exp (* a <= a *)
|
||||
| BCmpGreater of a_exp * a_exp (* a > a *)
|
||||
| BCmpGreaterEq of a_exp * a_exp (* a >= a *)
|
||||
and a_exp =
|
||||
Variable of variable (* x *)
|
||||
| Integer of int (* n *)
|
||||
| Plus of a_exp * a_exp (* a + a *)
|
||||
| Minus of a_exp * a_exp (* a - a *)
|
||||
| Times of a_exp * a_exp (* a * a *)
|
||||
Variable of variable (* x *)
|
||||
| Integer of int (* n *)
|
||||
| Plus of a_exp * a_exp (* a + a *)
|
||||
| Minus of a_exp * a_exp (* a - a *)
|
||||
| Times of a_exp * a_exp (* a * a *)
|
||||
| Division of a_exp * a_exp (* a / a *)
|
||||
| Modulo of a_exp * a_exp (* a % a *)
|
||||
| Power of a_exp * a_exp (* a ^ a *)
|
||||
| Rand of a_exp (* rand(0, a) *)
|
||||
|
||||
|
||||
|
||||
@ -30,6 +40,10 @@ type memory = {
|
||||
|
||||
|
||||
exception AbsentAssignment of string
|
||||
exception DivisionByZero of string
|
||||
module Utility = Utility;;
|
||||
|
||||
Random.self_init ()
|
||||
|
||||
let rec evaluate (mem: memory) (command: c_exp) =
|
||||
match command with
|
||||
@ -52,6 +66,17 @@ let rec evaluate (mem: memory) (command: c_exp) =
|
||||
else
|
||||
mem
|
||||
)
|
||||
| For (exp_c1, exp_b, exp_c2, body_c) -> (
|
||||
let mem2 = evaluate mem exp_c1 in
|
||||
let rec f localmem =
|
||||
if (evaluate_b localmem exp_b)
|
||||
then f (
|
||||
let tmpmem = (evaluate localmem body_c) in
|
||||
(evaluate tmpmem exp_c2))
|
||||
else localmem
|
||||
in
|
||||
f mem2
|
||||
)
|
||||
|
||||
and evaluate_a (mem: memory) (exp_a: a_exp) =
|
||||
match exp_a with
|
||||
@ -76,6 +101,26 @@ and evaluate_a (mem: memory) (exp_a: a_exp) =
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
exp_a1val * exp_a2val
|
||||
)
|
||||
| Division (exp_a1, exp_a2) -> (
|
||||
let exp_a1val = evaluate_a mem exp_a1 in
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
try
|
||||
exp_a1val / exp_a2val
|
||||
with Division_by_zero -> raise (DivisionByZero "Dividing by zero")
|
||||
)
|
||||
| Modulo (exp_a1, exp_a2) -> (
|
||||
let exp_a1val = evaluate_a mem exp_a1 in
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
exp_a1val mod exp_a2val
|
||||
)
|
||||
| Power (exp_a1, exp_a2) -> (
|
||||
let exp_a1val = evaluate_a mem exp_a1 in
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
Utility.pow exp_a1val exp_a2val
|
||||
)
|
||||
| Rand (exp_a) -> (
|
||||
Random.int (evaluate_a mem exp_a)
|
||||
)
|
||||
and evaluate_b (mem: memory) (exp_b: b_exp) =
|
||||
match exp_b with
|
||||
Boolean b -> b
|
||||
@ -84,14 +129,39 @@ and evaluate_b (mem: memory) (exp_b: b_exp) =
|
||||
let exp_b2val = evaluate_b mem exp_b2 in
|
||||
exp_b1val && exp_b2val
|
||||
)
|
||||
| BOr (exp_b1, exp_b2) -> (
|
||||
let exp_b1val = evaluate_b mem exp_b1 in
|
||||
let exp_b2val = evaluate_b mem exp_b2 in
|
||||
exp_b1val || exp_b2val
|
||||
)
|
||||
| BNot (exp_b) -> (
|
||||
not (evaluate_b mem exp_b)
|
||||
)
|
||||
| BCmp (exp_a1, exp_a2) -> (
|
||||
let exp_a1val = evaluate_a mem exp_a1 in
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
exp_a1val = exp_a2val
|
||||
)
|
||||
| BCmpLess (exp_a1, exp_a2) -> (
|
||||
let exp_a1val = evaluate_a mem exp_a1 in
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
exp_a1val < exp_a2val
|
||||
)
|
||||
| BCmpLessEq (exp_a1, exp_a2) -> (
|
||||
let exp_a1val = evaluate_a mem exp_a1 in
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
exp_a1val <= exp_a2val
|
||||
)
|
||||
| BCmpGreater (exp_a1, exp_a2) -> (
|
||||
let exp_a1val = evaluate_a mem exp_a1 in
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
exp_a1val > exp_a2val
|
||||
)
|
||||
| BCmpGreaterEq (exp_a1, exp_a2) -> (
|
||||
let exp_a1val = evaluate_a mem exp_a1 in
|
||||
let exp_a2val = evaluate_a mem exp_a2 in
|
||||
exp_a1val >= exp_a2val
|
||||
)
|
||||
|
||||
|
||||
let reduce (iin : int) (program: p_exp) =
|
||||
|
||||
@ -1,24 +1,34 @@
|
||||
type variable = string
|
||||
|
||||
type p_exp =
|
||||
Main of variable * variable * c_exp (* def main with input x output y as c *)
|
||||
Main of variable * variable * c_exp (* def main with input x output y as c *)
|
||||
and c_exp =
|
||||
Skip
|
||||
| Assignment of variable * a_exp (* x := a *)
|
||||
| Sequence of c_exp * c_exp (* c; c *)
|
||||
| If of b_exp * c_exp * c_exp (* if b then c else c *)
|
||||
| While of b_exp * c_exp (* while b do c *)
|
||||
| Assignment of variable * a_exp (* x := a *)
|
||||
| Sequence of c_exp * c_exp (* c; c *)
|
||||
| If of b_exp * c_exp * c_exp (* if b then c else c *)
|
||||
| While of b_exp * c_exp (* while b do c *)
|
||||
| For of c_exp * b_exp * c_exp * c_exp (* for c; b; c do c *)
|
||||
and b_exp =
|
||||
Boolean of bool (* v *)
|
||||
| BAnd of b_exp * b_exp (* b and b *)
|
||||
| BNot of b_exp (* not b *)
|
||||
| BCmpLess of a_exp * a_exp (* a < a *)
|
||||
Boolean of bool (* v *)
|
||||
| BAnd of b_exp * b_exp (* b and b *)
|
||||
| BOr of b_exp * b_exp (* b or b *)
|
||||
| BNot of b_exp (* not b *)
|
||||
| BCmp of a_exp * a_exp (* a = a *)
|
||||
| BCmpLess of a_exp * a_exp (* a < a *)
|
||||
| BCmpLessEq of a_exp * a_exp (* a <= a *)
|
||||
| BCmpGreater of a_exp * a_exp (* a > a *)
|
||||
| BCmpGreaterEq of a_exp * a_exp (* a >= a *)
|
||||
and a_exp =
|
||||
Variable of variable (* x *)
|
||||
| Integer of int (* n *)
|
||||
| Plus of a_exp * a_exp (* a + a *)
|
||||
| Minus of a_exp * a_exp (* a - a *)
|
||||
| Times of a_exp * a_exp (* a * a *)
|
||||
Variable of variable (* x *)
|
||||
| Integer of int (* n *)
|
||||
| Plus of a_exp * a_exp (* a + a *)
|
||||
| Minus of a_exp * a_exp (* a - a *)
|
||||
| Times of a_exp * a_exp (* a * a *)
|
||||
| Division of a_exp * a_exp (* a / a *)
|
||||
| Modulo of a_exp * a_exp (* a % a *)
|
||||
| Power of a_exp * a_exp (* a ^ a *)
|
||||
| Rand of a_exp (* rand(0, a) *)
|
||||
|
||||
|
||||
|
||||
|
||||
6
lib/utility.ml
Normal file
6
lib/utility.ml
Normal file
@ -0,0 +1,6 @@
|
||||
let rec pow a = function
|
||||
| 0 -> 1
|
||||
| 1 -> a
|
||||
| n ->
|
||||
let b = pow a (n / 2) in
|
||||
b * b * (if n mod 2 = 0 then 1 else a)
|
||||
1
lib/utility.mli
Normal file
1
lib/utility.mli
Normal file
@ -0,0 +1 @@
|
||||
val pow : int -> int -> int
|
||||
Reference in New Issue
Block a user