Semantics for RISC code

This commit is contained in:
elvis
2024-12-03 17:18:42 +01:00
parent efa6ed21c9
commit 08a8d07422
20 changed files with 771 additions and 86 deletions

View File

@ -6,6 +6,10 @@
(name testingImpParser)
(libraries miniImp))
(test
(name testingRISC)
(libraries miniImp))
(test
(name testingFun)
(libraries miniFun))
@ -16,4 +20,4 @@
(test
(name testingTypeFunParser)
(libraries miniFun))
(libraries miniFun))

View File

@ -0,0 +1,8 @@
Identity program: 1
Factorial program: 3628800
Hailstone sequence's lenght program: 351
Sum multiples of 3 and 5 program: 35565945
Rand program: true
Fibonacci program: 4807526976
Miller-Rabin primality test program 1: 0
Miller-Rabin primality test program 2: 1

128
test/testingRISC.ml Normal file
View File

@ -0,0 +1,128 @@
open MiniImp
let compute x i =
Lexing.from_string x |>
Parser.prg Lexer.lex |>
CfgImp.convert_io i |>
CfgRISC.convert |>
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);
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);