Compleating assignment for interpreter, modified grammars, fixed tests
This commit is contained in:
@ -5,5 +5,5 @@ 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: 0
|
||||
Miller-Rabin primality test program: 1
|
||||
Miller-Rabin primality test program 1: 0
|
||||
Miller-Rabin primality test program 2: 1
|
||||
|
||||
@ -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
|
||||
;;
|
||||
|
||||
@ -5,5 +5,5 @@ 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: 0
|
||||
Miller-Rabin primality test program: 1
|
||||
Miller-Rabin primality test program 1: 0
|
||||
Miller-Rabin primality test program 2: 1
|
||||
|
||||
@ -6,123 +6,161 @@ let get_result x =
|
||||
(* -------------------------------------------------------------------------- *)
|
||||
(* Identity program *)
|
||||
let program =
|
||||
"main a b {b := a}"
|
||||
"def main with input a output b as b := a"
|
||||
;;
|
||||
|
||||
Printf.printf "Identity program: %d\n" (get_result program 1);;
|
||||
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 =
|
||||
"main a b {x := 1; b := a + x + y}"
|
||||
"def main with input a output b as x := 1; b := a + x + y"
|
||||
;;
|
||||
|
||||
try
|
||||
Printf.printf "y not defined program: %d\n" (get_result program 100)
|
||||
with Types.AbsentAssignment s ->
|
||||
Printf.printf "y not defined program: %s\n" s
|
||||
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 =
|
||||
"main a b {
|
||||
"def main with input a output b as
|
||||
b := 1;
|
||||
for (i := 1, i <= a, i := i + 1) do {
|
||||
for (i := 1, i <= a, i := i + 1) do
|
||||
b := b * i;
|
||||
}
|
||||
}
|
||||
"
|
||||
;;
|
||||
|
||||
Printf.printf "Factorial program: %d\n" (get_result program 10)
|
||||
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 =
|
||||
"main a b {
|
||||
"def main with input a output b as
|
||||
b := 1;
|
||||
while (not a == 1) do {
|
||||
while not a == 1 do (
|
||||
b := b + 1;
|
||||
if ((a % 2) == 1) a := 3 * a + 1 else a := a / 2
|
||||
}
|
||||
}"
|
||||
if ((a % 2) == 1) then a := 3 * a + 1 else a := a / 2
|
||||
)
|
||||
"
|
||||
;;
|
||||
|
||||
Printf.printf "Hailstone sequence's lenght program: %d\n" (get_result program 77031)
|
||||
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 =
|
||||
"main a b {
|
||||
"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) {b := b + i} else {skip}
|
||||
}
|
||||
}"
|
||||
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: %d\n" (get_result program 12345)
|
||||
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 =
|
||||
"main a b {b := rand(a)}"
|
||||
"def main with input a output b as b := rand(a)"
|
||||
;;
|
||||
|
||||
Printf.printf "Rand program: %b\n" ((get_result program 10) < 10)
|
||||
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 =
|
||||
"main n fnext {
|
||||
"def main with input n output fnext as
|
||||
fnow := 0;
|
||||
fnext := 1;
|
||||
while (n > 1) do {
|
||||
while (n > 1) do (
|
||||
tmp := fnow + fnext;
|
||||
fnow := fnext;
|
||||
fnext := tmp;
|
||||
n := n - 1
|
||||
}
|
||||
}"
|
||||
n := n - 1;
|
||||
)
|
||||
"
|
||||
;;
|
||||
|
||||
Printf.printf "Fibonacci program: %d\n" (get_result program 48)
|
||||
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 =
|
||||
"main n result {
|
||||
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))
|
||||
{result := 1}
|
||||
else
|
||||
{skip};
|
||||
x := y
|
||||
};
|
||||
if (not y == 1) {result := 1} else {skip}
|
||||
}
|
||||
}"
|
||||
"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: %d\n" (get_result program 179424673)
|
||||
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: %d\n" (get_result program 179424675)
|
||||
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
|
||||
;;
|
||||
|
||||
Reference in New Issue
Block a user