You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
3.7 KiB
106 lines
3.7 KiB
open Lambda |
|
open Parser |
|
open Tools |
|
|
|
(* --------------------------------------- |
|
Prompt |
|
--------------------------------------- *) |
|
|
|
let welcome () = print_endline "Use 'help' for the list of commands." |
|
|
|
let tab = repeat_string "\t" |
|
|
|
let line = repeat_string "-" |
|
|
|
let commands_list () = |
|
print_endline ("Command" ^ tab 4 ^ "Description (shortcut)"); |
|
print_endline (line 70); |
|
print_endline ("exit" ^ tab 4 ^ "Exits the program"); |
|
print_endline ("display <lambda term>" ^ tab 2 ^ "Displays the written lambda with the pretty printer"); |
|
print_endline ("cbn -f <filename>" ^ tab 2 ^ "Eval the lambda term <filename> using call by name strategy"); |
|
print_endline ("cbv -f <filename>" ^ tab 2 ^ "Eval the lambda term <filename> using call by value strategy"); |
|
print_endline ("lo -f <filename>" ^ tab 2 ^ "Eval the lambda term <filename> using the lo reduction strategy") |
|
|
|
let prompt () = print_string "> " |
|
let last_command : string option ref = ref None |
|
|
|
(* --------------------------------------- |
|
Main function |
|
--------------------------------------- *) |
|
|
|
let _ = |
|
welcome (); |
|
while true do |
|
prompt (); |
|
let input = read_line () in |
|
begin match String.split_on_char ' ' input with |
|
| ["exit"] -> exit 0 |
|
| ["help"] -> commands_list () |
|
(*| _ as string_list -> |
|
let lexbuf = Lexing.from_string (String.concat " " string_list) in |
|
(try |
|
let t = termc Lexer.read lexbuf in |
|
(* print_endline (String.concat " " string_list); *) |
|
print_endline (string_of_term t) |
|
with _ -> print_endline "Syntax error. Please try again.")*) |
|
| "display"::string_list -> |
|
let lexbuf = Lexing.from_string (String.concat " " string_list) in |
|
(try |
|
let t = termc Lexer.read lexbuf in |
|
print_endline (string_of_term t) |
|
with _ -> print_endline "Syntax error. Please try again.") |
|
| "alpha"::string_list -> |
|
let lexbuf = Lexing.from_string (String.concat " " string_list) in |
|
(try |
|
let t = termc Lexer.read lexbuf in |
|
print_endline (string_of_term (alpha_conv t)) |
|
with _ -> print_endline "Syntax error. Please try again.") |
|
| ["cbn"; "-f"; filename] -> |
|
begin try |
|
let lexbuf = Lexing.from_channel (open_in filename) in |
|
let t = termc Lexer.read lexbuf in |
|
let result = cbn_eval t in |
|
print_endline (string_of_term result) |
|
with Sys_error f -> print_endline f |
|
end |
|
| ["cbv"; "-f"; filename] -> |
|
begin try |
|
let lexbuf = Lexing.from_channel (open_in filename) in |
|
let t = termc Lexer.read lexbuf in |
|
let result = cbv_eval t in |
|
print_endline (string_of_term result) |
|
with Sys_error f -> print_endline f |
|
end |
|
| ["lo"; "-f"; filename] -> |
|
begin try |
|
let lexbuf = Lexing.from_channel (open_in filename) in |
|
let t = termc Lexer.read lexbuf in |
|
let result = lo_eval t in |
|
print_endline (string_of_term result) |
|
with Sys_error f -> print_endline f |
|
end |
|
| "cbn"::string_list -> |
|
let lexbuf = Lexing.from_string (String.concat " " string_list) in |
|
(try |
|
let t = termc Lexer.read lexbuf in |
|
let result = cbn_eval t in |
|
print_endline (string_of_term result) |
|
with _ -> print_endline "Syntax error. Please try again.") |
|
| "cbv"::string_list -> |
|
let lexbuf = Lexing.from_string (String.concat " " string_list) in |
|
(try |
|
let t = termc Lexer.read lexbuf in |
|
let result = cbv_eval t in |
|
print_endline (string_of_term result) |
|
with _ -> print_endline "Syntax error. Please try again.") |
|
| "lo"::string_list -> |
|
let lexbuf = Lexing.from_string (String.concat " " string_list) in |
|
(try |
|
let t = termc Lexer.read lexbuf in |
|
let result = lo_eval t in |
|
print_endline (string_of_term result) |
|
with _ -> print_endline "Syntax error. Please try again.") |
|
| _ -> |
|
print_endline "Invalid command. Please type 'help' for the list of commands." |
|
end |
|
done |