open MiniImp let compute x i = Lexing.from_string x |> Parser.prg Lexer.lex |> CfgImp.convert_io i |> CfgRISC.convert |> LiveVariables.compute_live_variables |> LiveVariables.optimize_cfg |> LiveVariables.compute_cfg |> ReduceRegisters.reduce_registers 4 |> RISC.convert |> RISCSemantics.reduce (* -------------------------------------------------------------------------- *) (* Identity program *) let program = "def main with input a output b as b := a" ;; Printf.printf "Identity program: "; Printf.printf "%d\n" (compute program 1) ;; (* -------------------------------------------------------------------------- *) (* 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: "; Printf.printf "%d\n" (compute program 10) (* -------------------------------------------------------------------------- *) (* 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: "; Printf.printf "%d\n" (compute program 77031) (* -------------------------------------------------------------------------- *) (* 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: "; Printf.printf "%d\n" (compute program 12345) (* -------------------------------------------------------------------------- *) (* Rand program *) let program = "def main with input a output b as b := rand(a)" ;; Printf.printf "Rand program: "; Printf.printf "%b\n" ((compute program 10) < 10) (* -------------------------------------------------------------------------- *) (* 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: "; Printf.printf "%d\n" (compute program 48) (* -------------------------------------------------------------------------- *) (* 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); y := 0; 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: "; Printf.printf "%d\n" (compute program 179424673); (* should return 1 because not prime *) Printf.printf "Miller-Rabin primality test program 2: "; Printf.printf "%d\n" (compute program 179424675);