Fix main, added extra functionality, added extra tests in main. TODO: add real unit tests

This commit is contained in:
elvis
2024-10-05 18:40:45 +02:00
parent c1e513d3dc
commit 92fa995807
5 changed files with 230 additions and 34 deletions

View File

@ -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)
;;

View File

@ -1,24 +1,34 @@
type variable = string type variable = string
type p_exp = 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 = and c_exp =
Skip Skip
| Assignment of variable * a_exp (* x := a *) | Assignment of variable * a_exp (* x := a *)
| 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 *)
| BNot of b_exp (* not b *) | BOr of b_exp * b_exp (* b or b *)
| BCmpLess of a_exp * a_exp (* a < a *) | 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 = 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) =

View File

@ -1,24 +1,34 @@
type variable = string type variable = string
type p_exp = 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 = and c_exp =
Skip Skip
| Assignment of variable * a_exp (* x := a *) | Assignment of variable * a_exp (* x := a *)
| 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 *)
| BNot of b_exp (* not b *) | BOr of b_exp * b_exp (* b or b *)
| BCmpLess of a_exp * a_exp (* a < a *) | 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 = 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
View 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
View File

@ -0,0 +1 @@
val pow : int -> int -> int