Compleating assignment for interpreter, modified grammars, fixed tests

This commit is contained in:
elvis
2024-11-16 15:40:00 +01:00
parent 40055899c9
commit 9e599cc018
24 changed files with 593 additions and 1238 deletions

View File

@ -11,7 +11,12 @@ let program =
)
;;
Printf.printf "Identity program: %d\n" (reduce program 1)
Printf.printf "Identity program: ";
match reduce 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 *)
@ -28,10 +33,12 @@ let program =
)
;;
try
Printf.printf "y not defined program: %d\n" (reduce program 100)
with AbsentAssignment s ->
Printf.printf "y not defined program: %s\n" s
Printf.printf "y not defined program: ";
match reduce 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
;;
(* -------------------------------------------------------------------------- *)
@ -54,9 +61,14 @@ let program =
)
;;
Printf.printf "Factorial program: %d\n" (reduce program 10)
Printf.printf "Factorial program: ";
match reduce 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 =
@ -80,7 +92,11 @@ let program =
)
;;
Printf.printf "Hailstone sequence's lenght program: %d\n" (reduce program 77031)
Printf.printf "Hailstone sequence's lenght program: ";
match reduce 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
;;
(* -------------------------------------------------------------------------- *)
@ -106,7 +122,11 @@ let program =
)
;;
Printf.printf "Sum multiples of 3 and 5 program: %d\n" (reduce program 12345)
Printf.printf "Sum multiples of 3 and 5 program: ";
match reduce 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
;;
(* -------------------------------------------------------------------------- *)
@ -119,7 +139,11 @@ let program =
)
;;
Printf.printf "Rand program: %b\n" ((reduce program 10) < 10)
Printf.printf "Rand program: ";
match reduce 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
;;
(* -------------------------------------------------------------------------- *)
@ -149,7 +173,11 @@ let program =
)
;;
Printf.printf "Fibonacci program: %d\n" (reduce program 48)
Printf.printf "Fibonacci program: ";
match reduce 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
;;
(* -------------------------------------------------------------------------- *)
@ -216,8 +244,16 @@ let program =
;;
(* should return 0 because prime *)
Printf.printf "Miller-Rabin primality test program: %d\n" (reduce program 179424673)
Printf.printf "Miller-Rabin primality test program 1: ";
match reduce 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: %d\n" (reduce program 179424675)
Printf.printf "Miller-Rabin primality test program 2: ";
match reduce 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
;;