Refactoring, errors are not thrown anymore

This commit is contained in:
elvis
2024-10-26 01:47:30 +02:00
parent 8427133fd7
commit 3e4e1615d2
11 changed files with 413 additions and 339 deletions

View File

@ -8,14 +8,13 @@ let program =
Function
(["a"],
FunctionType ([IntegerType], IntegerType),
(Variable "x")
Plus (Variable "x", Integer 1)
)
;;
try
Printf.printf "Error absent assignment program: %b\n" (typecheck program)
with AbsentAssignment _ ->
Printf.printf "Error absent assignment program: error\n"
match typecheck program with
Error (`AbsentAssignment _) -> Printf.printf "Error absent assignment program: error (success)\n"
| _ -> Printf.printf "Error absent assignment program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Error wrong arity program *)
@ -28,10 +27,9 @@ let program =
)
;;
try
Printf.printf "Error wrong arity program 1: %b\n" (typecheck program)
with WrongTypeSpecification _ ->
Printf.printf "Error wrong arity program 1: error\n"
match typecheck program with
Error (`WrongTypeSpecification _) -> Printf.printf "Error wrong arity program 1: error (success)\n"
| _ -> Printf.printf "Error wrong arity program 1: failed\n"
let program =
LetFun
@ -43,11 +41,9 @@ let program =
)
;;
try
Printf.printf "Error wrong arity program 2: %b\n" (typecheck program)
with WrongArity _ ->
Printf.printf "Error wrong arity program 2: error\n"
match typecheck program with
Error (`WrongArity _) -> Printf.printf "Error wrong arity program 2: error (success)\n"
| _ -> Printf.printf "Error wrong arity program 2: failed\n"
(* -------------------------------------------------------------------------- *)
(* Error wrong return type program *)
@ -60,10 +56,9 @@ let program =
)
;;
try
Printf.printf "Error wrong return type program: %b\n" (typecheck program)
with WrongTypeSpecification _ ->
Printf.printf "Error wrong return type program: error\n"
match typecheck program with
Error (`WrongTypeSpecification _) -> Printf.printf "Error wrong return type program: error (success)\n"
| _ -> Printf.printf "Error wrong return type program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Error wrong specification program *)
@ -76,10 +71,9 @@ let program =
)
;;
try
Printf.printf "Error wrong specification program: %b\n" (typecheck program)
with WrongTypeSpecification _ ->
Printf.printf "Error wrong specification program: error\n"
match typecheck program with
Error (`WrongTypeSpecification _) -> Printf.printf "Error wrong specification program: error (success)\n"
| _ -> Printf.printf "Error wrong specification program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Error wrong input type program *)
@ -95,10 +89,9 @@ let program =
)
;;
try
Printf.printf "Error wrong input type program: %b\n" (typecheck program)
with WrongType _ ->
Printf.printf "Error wrong input type program: error\n"
match typecheck program with
Error (`WrongType _) -> Printf.printf "Error wrong input type program: error (success)\n"
| _ -> Printf.printf "Error wrong input type program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Error not a function program *)
@ -110,10 +103,9 @@ let program =
)
;;
try
Printf.printf "Error not a function program: %b\n" (typecheck program)
with WrongType _ ->
Printf.printf "Error not a function program: error\n"
match typecheck program with
Error (`WrongType _) -> Printf.printf "Error not a function program: error (success)\n"
| _ -> Printf.printf "Error not a function program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Error if branches with different types program *)
@ -126,10 +118,9 @@ let program =
)
;;
try
Printf.printf "Error if branches with different types program: %b\n" (typecheck program)
with WrongType _ ->
Printf.printf "Error if branches with different types program: error\n"
match typecheck program with
Error (`WrongType _) -> Printf.printf "Error if branches with different types program: error (success)\n"
| _ -> Printf.printf "Error if branches with different types program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Error if guard is not a boolean program *)
@ -142,11 +133,9 @@ let program =
)
;;
try
Printf.printf "Error if guard is not a boolean program: %b\n" (typecheck program)
with WrongType _ ->
Printf.printf "Error if guard is not a boolean program: error\n"
match typecheck program with
Error (`WrongType _) -> Printf.printf "Error if guard is not a boolean program: error (success)\n"
| _ -> Printf.printf "Error if guard is not a boolean program: failed\n"
(* -------------------------------------------------------------------------- *)
@ -159,7 +148,9 @@ let program =
)
;;
Printf.printf "Identity program: %b\n" (typecheck program)
match typecheck program with
Ok _ -> Printf.printf "Identity program: success\n"
| _ -> Printf.printf "Identity program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Constant program *)
@ -171,7 +162,9 @@ let program =
)
;;
Printf.printf "Constant program: %b\n" (typecheck program)
match typecheck program with
Ok _ -> Printf.printf "Constant program: success\n"
| _ -> Printf.printf "Constant program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Partial application of function program *)
@ -185,7 +178,9 @@ let program =
)
;;
Printf.printf "Partial application of function program: %b\n" (typecheck program)
match typecheck program with
Ok _ -> Printf.printf "Partial application of function program 1: success\n"
| _ -> Printf.printf "Partial application of function program 1: failed\n"
(* -------------------------------------------------------------------------- *)
(* Partial application of function program *)
@ -201,7 +196,9 @@ let program =
)
;;
Printf.printf "Partial application of function program: %b\n" (typecheck program)
match typecheck program with
Ok _ -> Printf.printf "Partial application of function program 2: success\n"
| _ -> Printf.printf "Partial application of function program 2: failed\n"
(* -------------------------------------------------------------------------- *)
(* Passing functions to functions program *)
@ -237,7 +234,9 @@ let program =
)
;;
Printf.printf "Passing functions to functions program: %b\n" (typecheck program)
match typecheck program with
Ok _ -> Printf.printf "Passing functions to functions program: success\n"
| _ -> Printf.printf "Passing functions to functions program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Recursive function program *)
@ -251,7 +250,9 @@ let program =
)
;;
Printf.printf "Recursive function program: %b\n" (typecheck program)
match typecheck program with
Ok _ -> Printf.printf "Recursive function program: success\n"
| _ -> Printf.printf "Recursive function program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Scope program *)
@ -263,7 +264,9 @@ let program =
)
;;
Printf.printf "Scope program: %b\n" (typecheck program)
match typecheck program with
Ok _ -> Printf.printf "Scope program: success\n"
| _ -> Printf.printf "Scope program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Factorial program *)
@ -277,8 +280,9 @@ let program =
)
;;
Printf.printf "Factorial program: %b\n" (typecheck program)
;;
match typecheck program with
Ok _ -> Printf.printf "Factorial program: success\n"
| _ -> Printf.printf "Factorial program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Hailstone sequence's lenght program *)
@ -301,8 +305,9 @@ let program =
)
;;
Printf.printf "Hailstone sequence's lenght program: %b\n" (typecheck program)
;;
match typecheck program with
Ok _ -> Printf.printf "Hailstone sequence's lenght program: success\n"
| _ -> Printf.printf "Hailstone sequence's lenght program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Sum multiples of 3 and 5 program *)
@ -323,8 +328,9 @@ let program =
)
;;
Printf.printf "Sum multiples of 3 and 5 program: %b\n" (typecheck program)
;;
match typecheck program with
Ok _ -> Printf.printf "Sum multiples of 3 and 5 program: success\n"
| _ -> Printf.printf "Sum multiples of 3 and 5 program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Rand program *)
@ -337,8 +343,9 @@ let program =
;;
Printf.printf "Rand program: %b\n" (typecheck program)
;;
match typecheck program with
Ok _ -> Printf.printf "Rand program: success\n"
| _ -> Printf.printf "Rand program: failed\n"
(* -------------------------------------------------------------------------- *)
(* Fibonacci program *)
@ -360,5 +367,6 @@ let program =
;;
Printf.printf "Fibonacci program: %b\n" (typecheck program)
;;
match typecheck program with
Ok _ -> Printf.printf "Fibonacci program: success\n"
| _ -> Printf.printf "Fibonacci program: failed\n"