open MiniImp let get_result x = Lexing.from_string x |> Parser.prg Lexer.lex |> Semantics.reduce (* -------------------------------------------------------------------------- *) (* Identity program *) let program = "def main with input a output b as b := a" ;; Printf.printf "Identity program: "; match get_result program 1 with Ok d -> Printf.printf "%d\n" d | Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg | Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg ;; (* -------------------------------------------------------------------------- *) (* y not defined program *) let program = "def main with input a output b as x := 1; b := a + x + y" ;; Printf.printf "y not defined program: "; match get_result program 100 with Ok d -> Printf.printf "error: %d\n" d | Error `AbsentAssignment msg -> Printf.printf "%s\n" msg | Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg ;; (* -------------------------------------------------------------------------- *) (* Factorial program *) let program = "def main with input a output b as b := 1; for (i := 1, i <= a, i := i + 1) do b := b * i; " ;; Printf.printf "Factorial program: "; match get_result program 10 with Ok d -> Printf.printf "%d\n" d | Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg | Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg ;; (* -------------------------------------------------------------------------- *) (* Hailstone sequence's lenght program *) let program = "def main with input a output b as b := 1; while not a == 1 do ( b := b + 1; if ((a % 2) == 1) then a := 3 * a + 1 else a := a / 2 ) " ;; Printf.printf "Hailstone sequence's lenght program: "; match get_result program 77031 with Ok d -> Printf.printf "%d\n" d | Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg | Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg ;; (* -------------------------------------------------------------------------- *) (* Sum multiples of 3 and 5 program *) let program = "def main with input a output b as b := 0; for (i := 0, i <= a, i := i+1) do if (i % 3 == 0 || i % 5 == 0) then b := b + i; else skip; " ;; Printf.printf "Sum multiples of 3 and 5 program: "; match get_result program 12345 with Ok d -> Printf.printf "%d\n" d | Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg | Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg ;; (* -------------------------------------------------------------------------- *) (* Rand program *) let program = "def main with input a output b as b := rand(a)" ;; Printf.printf "Rand program: "; match get_result program 10 with Ok d -> Printf.printf "%b\n" (d < 10) | Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg | Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg ;; (* -------------------------------------------------------------------------- *) (* Fibonacci program *) let program = "def main with input n output fnext as fnow := 0; fnext := 1; while (n > 1) do ( tmp := fnow + fnext; fnow := fnext; fnext := tmp; n := n - 1; ) " ;; Printf.printf "Fibonacci program: "; match get_result program 48 with Ok d -> Printf.printf "%d\n" d | Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg | Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg ;; (* -------------------------------------------------------------------------- *) (* Miller-Rabin primality test program *) let program = "def main with input n output result as if (n % 2) == 0 then result := 1 else ( result := 0; s := 0; while (0 == ((n - 1) / (2 ^ s)) % 2) do ( s := s + 1 ); d := ((n - 1) / 2 ^ s); for (i := 20, i > 0, i := i - 1) do ( a := rand(n - 4) + 2; x := powmod(a, d, n); for (j := 0, j < s, j := j+1) do ( y := powmod(x, 2, n); if (y == 1 && (not x == 1) && (not x == n - 1)) then result := 1; else skip; x := y; ); if not y == 1 then result := 1; else skip; ) ) " ;; (* should return 0 because prime *) Printf.printf "Miller-Rabin primality test program 1: "; match get_result program 179424673 with Ok d -> Printf.printf "%d\n" d | Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg | Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg ;; (* should return 1 because not prime *) Printf.printf "Miller-Rabin primality test program 2: "; match get_result program 179424675 with Ok d -> Printf.printf "%d\n" d | Error `AbsentAssignment msg -> Printf.printf "error -> %s\n" msg | Error `DivisionByZero msg -> Printf.printf "error -> %s\n" msg ;;