Fix main, added extra functionality, added extra tests in main. TODO: add real unit tests
This commit is contained in:
121
bin/main.ml
121
bin/main.ml
@ -15,16 +15,125 @@ then print_endline "true"
|
|||||||
else print_endline "false"
|
else print_endline "false"
|
||||||
|
|
||||||
*)
|
*)
|
||||||
|
|
||||||
open Lang.MiniImp
|
open Lang.MiniImp
|
||||||
|
|
||||||
let program1 =
|
(* -------------------------------------------------------------------------- *)
|
||||||
|
(* Identity program *)
|
||||||
|
let program =
|
||||||
Main
|
Main
|
||||||
("a",
|
("a",
|
||||||
"b",
|
"b",
|
||||||
(Sequence
|
(Assignment ("b", (Variable "a")))
|
||||||
((Assignment "x" (Integer 1)),
|
)
|
||||||
(Assignment "b" (Plus (Plus (Variable "a") (Variable "x")) (Variable "y"))))
|
;;
|
||||||
|
|
||||||
|
Printf.printf "%d\n" (reduce 100 program)
|
||||||
|
|
||||||
|
(* -------------------------------------------------------------------------- *)
|
||||||
|
(* y not defined program *)
|
||||||
|
let program =
|
||||||
|
Main
|
||||||
|
("a",
|
||||||
|
"b",
|
||||||
|
(Sequence (
|
||||||
|
(Assignment ("x", (Integer 1))),
|
||||||
|
(Assignment ("b",
|
||||||
|
(Plus ((Plus (Variable "a", Variable "x")), (Variable "y")))))
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
Printf.printf "%d\n" (result program1 1)
|
)
|
||||||
|
;;
|
||||||
|
|
||||||
|
try
|
||||||
|
Printf.printf "%d\n" (reduce 100 program)
|
||||||
|
with AbsentAssignment s ->
|
||||||
|
Printf.printf "%s\n" s
|
||||||
|
;;
|
||||||
|
|
||||||
|
(* -------------------------------------------------------------------------- *)
|
||||||
|
(* factorial program *)
|
||||||
|
let program =
|
||||||
|
Main
|
||||||
|
("a",
|
||||||
|
"b",
|
||||||
|
(Sequence (
|
||||||
|
(Assignment ("b", (Integer 1))),
|
||||||
|
(For (
|
||||||
|
(Assignment ("i", (Integer 1))),
|
||||||
|
(BCmpLessEq (Variable "i", Variable "a")),
|
||||||
|
(Assignment ("i", (Plus ((Variable "i"), (Integer 1))))),
|
||||||
|
(Assignment ("b", (Times (Variable "b", Variable "i"))))
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
;;
|
||||||
|
|
||||||
|
Printf.printf "%d\n" (reduce 10 program)
|
||||||
|
;;
|
||||||
|
|
||||||
|
(* -------------------------------------------------------------------------- *)
|
||||||
|
(* hailstone sequence lenght program *)
|
||||||
|
let program =
|
||||||
|
Main
|
||||||
|
("a",
|
||||||
|
"b",
|
||||||
|
(Sequence (
|
||||||
|
(Assignment ("b", (Integer 1))),
|
||||||
|
(While (
|
||||||
|
(BNot (BCmp ((Variable "a"), (Integer 1)))),
|
||||||
|
(Sequence (
|
||||||
|
(Assignment ("b", (Plus (Variable "b", Integer 1)))),
|
||||||
|
(If (
|
||||||
|
(BCmp (Modulo (Variable "a", Integer 2), Integer 1)),
|
||||||
|
(Assignment ("a", Plus (Times (Integer 3, Variable "a"), Integer 1))),
|
||||||
|
(Assignment ("a", Division (Variable "a", Integer 2)))
|
||||||
|
))
|
||||||
|
))
|
||||||
|
))
|
||||||
|
))
|
||||||
|
)
|
||||||
|
;;
|
||||||
|
|
||||||
|
Printf.printf "%d\n" (reduce 77031 program)
|
||||||
|
;;
|
||||||
|
|
||||||
|
(* -------------------------------------------------------------------------- *)
|
||||||
|
(* sum multiples of 3 and 5 program *)
|
||||||
|
let program =
|
||||||
|
Main
|
||||||
|
("a",
|
||||||
|
"b",
|
||||||
|
(Sequence (
|
||||||
|
(Assignment ("b", (Integer 0))),
|
||||||
|
(For (
|
||||||
|
(Assignment ("i", Integer 0)),
|
||||||
|
(BCmpLess (Variable "i", Variable "a")),
|
||||||
|
(Assignment ("i", Plus (Variable "i", Integer 1))),
|
||||||
|
(If (
|
||||||
|
(BOr ((BCmp (Modulo (Variable "i", Integer 3), Integer 0)),
|
||||||
|
(BCmp (Modulo (Variable "i", Integer 5), Integer 0)))),
|
||||||
|
(Assignment ("b", Plus (Variable "b", Variable "i"))),
|
||||||
|
(Skip)
|
||||||
|
))
|
||||||
|
))
|
||||||
|
))
|
||||||
|
)
|
||||||
|
;;
|
||||||
|
|
||||||
|
Printf.printf "%d\n" (reduce 12345 program)
|
||||||
|
;;
|
||||||
|
|
||||||
|
(* -------------------------------------------------------------------------- *)
|
||||||
|
(* rand program *)
|
||||||
|
let program =
|
||||||
|
Main
|
||||||
|
("a",
|
||||||
|
"b",
|
||||||
|
(Assignment ("b", Rand (Variable "a")))
|
||||||
|
)
|
||||||
|
;;
|
||||||
|
|
||||||
|
Printf.printf "%d\n" (reduce 10 program)
|
||||||
|
;;
|
||||||
|
|||||||
@ -8,17 +8,27 @@ and c_exp =
|
|||||||
| Sequence of c_exp * c_exp (* c; c *)
|
| Sequence of c_exp * c_exp (* c; c *)
|
||||||
| If of b_exp * c_exp * c_exp (* if b then c else 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 *)
|
| 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 =
|
and b_exp =
|
||||||
Boolean of bool (* v *)
|
Boolean of bool (* v *)
|
||||||
| BAnd of b_exp * b_exp (* b and b *)
|
| BAnd of b_exp * b_exp (* b and b *)
|
||||||
|
| BOr of b_exp * b_exp (* b or b *)
|
||||||
| BNot of b_exp (* not b *)
|
| BNot of b_exp (* not b *)
|
||||||
|
| BCmp of a_exp * a_exp (* a = a *)
|
||||||
| BCmpLess 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 =
|
and a_exp =
|
||||||
Variable of variable (* x *)
|
Variable of variable (* x *)
|
||||||
| Integer of int (* n *)
|
| Integer of int (* n *)
|
||||||
| Plus of a_exp * a_exp (* a + a *)
|
| Plus of a_exp * a_exp (* a + a *)
|
||||||
| Minus of a_exp * a_exp (* a - a *)
|
| Minus of a_exp * a_exp (* a - a *)
|
||||||
| Times 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 AbsentAssignment of string
|
||||||
|
exception DivisionByZero of string
|
||||||
|
module Utility = Utility;;
|
||||||
|
|
||||||
|
Random.self_init ()
|
||||||
|
|
||||||
let rec evaluate (mem: memory) (command: c_exp) =
|
let rec evaluate (mem: memory) (command: c_exp) =
|
||||||
match command with
|
match command with
|
||||||
@ -52,6 +66,17 @@ let rec evaluate (mem: memory) (command: c_exp) =
|
|||||||
else
|
else
|
||||||
mem
|
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) =
|
and evaluate_a (mem: memory) (exp_a: a_exp) =
|
||||||
match exp_a with
|
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
|
let exp_a2val = evaluate_a mem exp_a2 in
|
||||||
exp_a1val * exp_a2val
|
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) =
|
and evaluate_b (mem: memory) (exp_b: b_exp) =
|
||||||
match exp_b with
|
match exp_b with
|
||||||
Boolean b -> b
|
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
|
let exp_b2val = evaluate_b mem exp_b2 in
|
||||||
exp_b1val && exp_b2val
|
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) -> (
|
| BNot (exp_b) -> (
|
||||||
not (evaluate_b mem 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) -> (
|
| BCmpLess (exp_a1, exp_a2) -> (
|
||||||
let exp_a1val = evaluate_a mem exp_a1 in
|
let exp_a1val = evaluate_a mem exp_a1 in
|
||||||
let exp_a2val = evaluate_a mem exp_a2 in
|
let exp_a2val = evaluate_a mem exp_a2 in
|
||||||
exp_a1val < exp_a2val
|
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) =
|
let reduce (iin : int) (program: p_exp) =
|
||||||
|
|||||||
@ -8,17 +8,27 @@ and c_exp =
|
|||||||
| Sequence of c_exp * c_exp (* c; c *)
|
| Sequence of c_exp * c_exp (* c; c *)
|
||||||
| If of b_exp * c_exp * c_exp (* if b then c else 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 *)
|
| 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 =
|
and b_exp =
|
||||||
Boolean of bool (* v *)
|
Boolean of bool (* v *)
|
||||||
| BAnd of b_exp * b_exp (* b and b *)
|
| BAnd of b_exp * b_exp (* b and b *)
|
||||||
|
| BOr of b_exp * b_exp (* b or b *)
|
||||||
| BNot of b_exp (* not b *)
|
| BNot of b_exp (* not b *)
|
||||||
|
| BCmp of a_exp * a_exp (* a = a *)
|
||||||
| BCmpLess 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 =
|
and a_exp =
|
||||||
Variable of variable (* x *)
|
Variable of variable (* x *)
|
||||||
| Integer of int (* n *)
|
| Integer of int (* n *)
|
||||||
| Plus of a_exp * a_exp (* a + a *)
|
| Plus of a_exp * a_exp (* a + a *)
|
||||||
| Minus of a_exp * a_exp (* a - a *)
|
| Minus of a_exp * a_exp (* a - a *)
|
||||||
| Times 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