elipl/type_check_file.pl
Kacper Marzecki 971af134c4 checkpoint
2025-05-29 19:13:51 +02:00

49 lines
2.0 KiB
Prolog
Executable File

#!/usr/bin/env swipl
:- initialization(main, main).
:- consult(log).
:- consult(parser).
:- consult(types).
main(Args) :-
log:set_verbosity(1), % Default verbosity, can be adjusted
( Args = [FilePath | VerbosityArgs] ->
( VerbosityArgs = [VerbosityArg | _], atom_string(VerbosityAtom, VerbosityArg), atom_number(VerbosityAtom, VLevel) ->
log:set_verbosity(VLevel),
format(user_error, 'Verbosity set to ~w from command line.~n', [VLevel])
; format(user_error, 'Using default verbosity 1. Add a number (0-2) after filepath to change.~n', [])
),
type_check_file(FilePath)
; writeln(user_error, 'Usage: type_check_file.pl <filepath> [verbosity_level]'),
halt(1)
).
type_check_file(FilePath) :-
( exists_file(FilePath) ->
read_file_to_string(FilePath, CodeString, []),
format(user_output, "--- Type Checking File: ~w ---~n", [FilePath]),
format(user_output, "Code:~n~s~n~n", [CodeString]),
( parser:parse(CodeString, AST) ->
format(user_output, "Parsed AST: ~w~n", [AST]),
types:initial_env(EmptyEnv),
( catch(types:infer_type(AST, EmptyEnv, InferredType), Error, (
log:log(error, caught_error_in_type_check_file(Error)),
log:explain_error(Error, Explanation),
format(user_output, "Type Inference Error: ~w~n", [Explanation]),
InferredType = error(Error) % Represent error
)) -> true
; InferredType = 'inference_failed_silently_in_type_check_file'
),
format(user_output, "~n--- Inferred Type for the whole expression ---~n~w~n~n", [InferredType]),
( InferredType = error(_) ; InferredType = 'inference_failed_silently_in_type_check_file' ->
halt(1)
; halt(0)
)
; format(user_error, "Parse FAILED for file: ~w~n", [FilePath]),
halt(1)
)
; format(user_error, "File not found: ~w~n", [FilePath]),
halt(1)
).