diff --git a/bin/main.ml b/bin/main.ml index 33b1150..64ed0ff 100644 --- a/bin/main.ml +++ b/bin/main.ml @@ -15,16 +15,125 @@ then print_endline "true" else print_endline "false" *) - open Lang.MiniImp -let program1 = +(* -------------------------------------------------------------------------- *) +(* Identity program *) +let program = Main ("a", "b", - (Sequence - ((Assignment "x" (Integer 1)), - (Assignment "b" (Plus (Plus (Variable "a") (Variable "x")) (Variable "y")))) + (Assignment ("b", (Variable "a"))) + ) +;; + +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) +;; diff --git a/lib/miniImp.ml b/lib/miniImp.ml index e945c84..773834e 100644 --- a/lib/miniImp.ml +++ b/lib/miniImp.ml @@ -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) = diff --git a/lib/miniImp.mli b/lib/miniImp.mli index f31f72c..49cf20b 100644 --- a/lib/miniImp.mli +++ b/lib/miniImp.mli @@ -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) *) diff --git a/lib/utility.ml b/lib/utility.ml new file mode 100644 index 0000000..6b979ec --- /dev/null +++ b/lib/utility.ml @@ -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) diff --git a/lib/utility.mli b/lib/utility.mli new file mode 100644 index 0000000..d00d6c4 --- /dev/null +++ b/lib/utility.mli @@ -0,0 +1 @@ +val pow : int -> int -> int