From 8ee2545b05ea326917675c0eebd6a9f0b263a204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Arroyo=20Calle?= Date: Fri, 20 Aug 2021 14:00:32 +0200 Subject: [PATCH 1/7] Add a -f switch to load alternative initialization files --- src/toplevel.pl | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/src/toplevel.pl b/src/toplevel.pl index e45de8d7..cdfd52ef 100644 --- a/src/toplevel.pl +++ b/src/toplevel.pl @@ -11,14 +11,17 @@ :- use_module(library('$atts')). load_scryerrc :- - ( '$home_directory'(HomeDir) -> - append(HomeDir, "/.scryerrc", ScryerrcFile), - ( file_exists(ScryerrcFile) -> - atom_chars(ScryerrcFileAtom, ScryerrcFile), - catch(use_module(ScryerrcFileAtom), E, print_exception(E)) - ; true - ) - ; true + ( '$home_directory'(HomeDir) -> + append(HomeDir, "./scryerrc", ScryerrcFile), + ( load_init_file(ScryerrcFile) -> true ; true) + ; true + ). + +load_init_file(ScryerrcFile) :- + ( file_exists(ScryerrcFile) -> + atom_chars(ScryerrcFileAtom, ScryerrcFile), + catch(use_module(ScryerrcFileAtom), E, print_exception(E)) + ; true ). :- dynamic(argv/1). @@ -31,7 +34,6 @@ load_scryerrc :- ; asserta('$toplevel':argv([])), Args = Args0 ), - load_scryerrc, delegate_task(Args, []), repl. '$repl'(_) :- @@ -46,29 +48,33 @@ delegate_task([], Goals0) :- reverse(Goals0, Goals), run_goals(Goals), repl. + delegate_task([Arg0|Args], Goals0) :- - ( member(Arg0, ["-h", "--help"]) -> print_help - ; member(Arg0, ["-v", "--version"]) -> print_version + ( member(Arg0, ["-h", "--help"]) -> print_help(Args, ArgsOut) + ; member(Arg0, ["-v", "--version"]) -> print_version(Args, ArgsOut) ; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0) + ; member(Arg0, ["-f"]) -> init_file(Args, ArgsOut) ; atom_chars(Mod, Arg0), catch(use_module(Mod), E, print_exception(E)) ), - delegate_task(Args, Goals0). + delegate_task(ArgsOut, Goals0). -print_help :- +print_help(Args, Args) :- write('Usage: scryer-prolog [OPTIONS] [FILES] [-- ARGUMENTS]'), nl, nl, write('Options:'), nl, - write(' -h, --help '), + write(' -h, --help '), write('Display this message'), nl, - write(' -v, --version '), + write(' -v, --version '), write('Print version information and exit'), nl, - write(' -g, --goal GOAL '), + write(' -g, --goal GOAL '), write('Run the query GOAL'), nl, + write(' -f FILE '), + write('Run the provided initialization file instead of the default (~/.scryerrc)'),nl, % write(' '), halt. -print_version :- +print_version(Args, Args) :- '$scryer_prolog_version'(Version), write(Version), nl, halt. @@ -82,6 +88,14 @@ gather_goal(Type, Args0, Goals) :- Gs =.. [Type, Gs1], delegate_task(Args, [Gs|Goals]). +init_file(Args0, Args) :- + length(Args0, N), + ( N < 1 -> print_help, halt + ; true + ), + [File|Args] = Args0, + load_init_file(File). + arg_type(g). arg_type(t). arg_type(g(_)). From 80e228f236c04feb2087caf678b6d9c64b1100d4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Arroyo=20Calle?= Date: Fri, 20 Aug 2021 14:06:57 +0200 Subject: [PATCH 2/7] Clean the code --- src/toplevel.pl | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/toplevel.pl b/src/toplevel.pl index cdfd52ef..cb12410a 100644 --- a/src/toplevel.pl +++ b/src/toplevel.pl @@ -50,16 +50,16 @@ delegate_task([], Goals0) :- repl. delegate_task([Arg0|Args], Goals0) :- - ( member(Arg0, ["-h", "--help"]) -> print_help(Args, ArgsOut) - ; member(Arg0, ["-v", "--version"]) -> print_version(Args, ArgsOut) + ( member(Arg0, ["-h", "--help"]) -> print_help + ; member(Arg0, ["-v", "--version"]) -> print_version ; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0) - ; member(Arg0, ["-f"]) -> init_file(Args, ArgsOut) + ; member(Arg0, ["-f"]) -> init_file(Args, Goals0) ; atom_chars(Mod, Arg0), catch(use_module(Mod), E, print_exception(E)) ), - delegate_task(ArgsOut, Goals0). + delegate_task(Args, Goals0). -print_help(Args, Args) :- +print_help :- write('Usage: scryer-prolog [OPTIONS] [FILES] [-- ARGUMENTS]'), nl, nl, write('Options:'), nl, @@ -74,7 +74,7 @@ print_help(Args, Args) :- % write(' '), halt. -print_version(Args, Args) :- +print_version :- '$scryer_prolog_version'(Version), write(Version), nl, halt. @@ -88,13 +88,14 @@ gather_goal(Type, Args0, Goals) :- Gs =.. [Type, Gs1], delegate_task(Args, [Gs|Goals]). -init_file(Args0, Args) :- +init_file(Args0, Goals) :- length(Args0, N), ( N < 1 -> print_help, halt ; true ), [File|Args] = Args0, - load_init_file(File). + load_init_file(File), + delegate_task(Args, Goals). arg_type(g). arg_type(t). From dce0c43e2609219a42ba561a918ef3f5f8c6fa57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Arroyo=20Calle?= Date: Fri, 20 Aug 2021 14:15:16 +0200 Subject: [PATCH 3/7] Read scryerrc if goals specified but not -f specified --- src/toplevel.pl | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/toplevel.pl b/src/toplevel.pl index cb12410a..2dc7abf0 100644 --- a/src/toplevel.pl +++ b/src/toplevel.pl @@ -10,6 +10,8 @@ :- use_module(library('$project_atts')). :- use_module(library('$atts')). +:- dynamic(custom_init_file/0). + load_scryerrc :- ( '$home_directory'(HomeDir) -> append(HomeDir, "./scryerrc", ScryerrcFile), @@ -35,6 +37,7 @@ load_init_file(ScryerrcFile) :- Args = Args0 ), delegate_task(Args, []), + (\+ custom_init_file -> load_scryerrc ; true), repl. '$repl'(_) :- ( \+ argv(_) -> asserta('$toplevel':argv([])) @@ -46,6 +49,7 @@ load_init_file(ScryerrcFile) :- delegate_task([], []). delegate_task([], Goals0) :- reverse(Goals0, Goals), + (\+ custom_init_file -> load_scryerrc ; true), run_goals(Goals), repl. @@ -89,12 +93,14 @@ gather_goal(Type, Args0, Goals) :- delegate_task(Args, [Gs|Goals]). init_file(Args0, Goals) :- + \+ custom_init_file, length(Args0, N), ( N < 1 -> print_help, halt ; true ), [File|Args] = Args0, load_init_file(File), + asserta('custom_init_file'), delegate_task(Args, Goals). arg_type(g). From 3cd112b60fa2b03330790c5b6a394f18452866fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Arroyo=20Calle?= Date: Sat, 21 Aug 2021 17:53:20 +0200 Subject: [PATCH 4/7] PR feedback --- src/toplevel.pl | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/src/toplevel.pl b/src/toplevel.pl index 2dc7abf0..f47d145a 100644 --- a/src/toplevel.pl +++ b/src/toplevel.pl @@ -10,20 +10,17 @@ :- use_module(library('$project_atts')). :- use_module(library('$atts')). -:- dynamic(custom_init_file/0). +:- dynamic(disabled_init_file/0). load_scryerrc :- - ( '$home_directory'(HomeDir) -> - append(HomeDir, "./scryerrc", ScryerrcFile), - ( load_init_file(ScryerrcFile) -> true ; true) - ; true - ). - -load_init_file(ScryerrcFile) :- - ( file_exists(ScryerrcFile) -> - atom_chars(ScryerrcFileAtom, ScryerrcFile), - catch(use_module(ScryerrcFileAtom), E, print_exception(E)) - ; true + ( '$home_directory'(HomeDir) -> + append(HomeDir, "/.scryerrc", ScryerrcFile), + ( file_exists(ScryerrcFile) -> + atom_chars(ScryerrcFileAtom, ScryerrcFile), + catch(use_module(ScryerrcFileAtom), E, print_exception(E)) + ; true + ) + ; true ). :- dynamic(argv/1). @@ -37,7 +34,7 @@ load_init_file(ScryerrcFile) :- Args = Args0 ), delegate_task(Args, []), - (\+ custom_init_file -> load_scryerrc ; true), + (\+ disabled_init_file -> load_scryerrc ; true), repl. '$repl'(_) :- ( \+ argv(_) -> asserta('$toplevel':argv([])) @@ -49,7 +46,7 @@ load_init_file(ScryerrcFile) :- delegate_task([], []). delegate_task([], Goals0) :- reverse(Goals0, Goals), - (\+ custom_init_file -> load_scryerrc ; true), + (\+ disabled_init_file -> load_scryerrc ; true), run_goals(Goals), repl. @@ -57,7 +54,7 @@ delegate_task([Arg0|Args], Goals0) :- ( member(Arg0, ["-h", "--help"]) -> print_help ; member(Arg0, ["-v", "--version"]) -> print_version ; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0) - ; member(Arg0, ["-f"]) -> init_file(Args, Goals0) + ; member(Arg0, ["-f"]) -> init_file ; atom_chars(Mod, Arg0), catch(use_module(Mod), E, print_exception(E)) ), @@ -73,8 +70,8 @@ print_help :- write('Print version information and exit'), nl, write(' -g, --goal GOAL '), write('Run the query GOAL'), nl, - write(' -f FILE '), - write('Run the provided initialization file instead of the default (~/.scryerrc)'),nl, + write(' -f '), + write('Do not load initialization file (~/.scryerrc)'),nl, % write(' '), halt. @@ -92,16 +89,8 @@ gather_goal(Type, Args0, Goals) :- Gs =.. [Type, Gs1], delegate_task(Args, [Gs|Goals]). -init_file(Args0, Goals) :- - \+ custom_init_file, - length(Args0, N), - ( N < 1 -> print_help, halt - ; true - ), - [File|Args] = Args0, - load_init_file(File), - asserta('custom_init_file'), - delegate_task(Args, Goals). +init_file :- + asserta('disabled_init_file'). arg_type(g). arg_type(t). From 9391dd9d51184752b707de8599ce8c036c9be0e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Arroyo=20Calle?= Date: Sun, 22 Aug 2021 11:36:27 +0200 Subject: [PATCH 5/7] Rename to disable_init_file and change description --- src/toplevel.pl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/toplevel.pl b/src/toplevel.pl index f47d145a..aa763177 100644 --- a/src/toplevel.pl +++ b/src/toplevel.pl @@ -10,7 +10,7 @@ :- use_module(library('$project_atts')). :- use_module(library('$atts')). -:- dynamic(disabled_init_file/0). +:- dynamic(disable_init_file/0). load_scryerrc :- ( '$home_directory'(HomeDir) -> @@ -34,7 +34,7 @@ load_scryerrc :- Args = Args0 ), delegate_task(Args, []), - (\+ disabled_init_file -> load_scryerrc ; true), + (\+ disable_init_file -> load_scryerrc ; true), repl. '$repl'(_) :- ( \+ argv(_) -> asserta('$toplevel':argv([])) @@ -46,7 +46,7 @@ load_scryerrc :- delegate_task([], []). delegate_task([], Goals0) :- reverse(Goals0, Goals), - (\+ disabled_init_file -> load_scryerrc ; true), + (\+ disable_init_file -> load_scryerrc ; true), run_goals(Goals), repl. @@ -71,7 +71,7 @@ print_help :- write(' -g, --goal GOAL '), write('Run the query GOAL'), nl, write(' -f '), - write('Do not load initialization file (~/.scryerrc)'),nl, + write('Fast startup. Do not load initialization file (~/.scryerrc)'),nl, % write(' '), halt. @@ -90,7 +90,7 @@ gather_goal(Type, Args0, Goals) :- delegate_task(Args, [Gs|Goals]). init_file :- - asserta('disabled_init_file'). + asserta('disable_init_file'). arg_type(g). arg_type(t). From c30bd51b918c3717aebe0a1207959501f8593128 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Arroyo=20Calle?= Date: Sun, 22 Aug 2021 11:44:17 +0200 Subject: [PATCH 6/7] use consult instead of use_module --- src/toplevel.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/toplevel.pl b/src/toplevel.pl index aa763177..8ce69ed5 100644 --- a/src/toplevel.pl +++ b/src/toplevel.pl @@ -17,7 +17,7 @@ load_scryerrc :- append(HomeDir, "/.scryerrc", ScryerrcFile), ( file_exists(ScryerrcFile) -> atom_chars(ScryerrcFileAtom, ScryerrcFile), - catch(use_module(ScryerrcFileAtom), E, print_exception(E)) + catch(consult(ScryerrcFileAtom), E, print_exception(E)) ; true ) ; true @@ -56,7 +56,7 @@ delegate_task([Arg0|Args], Goals0) :- ; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0) ; member(Arg0, ["-f"]) -> init_file ; atom_chars(Mod, Arg0), - catch(use_module(Mod), E, print_exception(E)) + catch(consult(Mod), E, print_exception(E)) ), delegate_task(Args, Goals0). From 00d0502cbd111af9069699ace549942e11f7f975 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adri=C3=A1n=20Arroyo=20Calle?= Date: Mon, 23 Aug 2021 09:53:15 +0200 Subject: [PATCH 7/7] Rename init_file to disable_init_file --- src/toplevel.pl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/toplevel.pl b/src/toplevel.pl index 8ce69ed5..fdc5c18f 100644 --- a/src/toplevel.pl +++ b/src/toplevel.pl @@ -10,7 +10,7 @@ :- use_module(library('$project_atts')). :- use_module(library('$atts')). -:- dynamic(disable_init_file/0). +:- dynamic(disabled_init_file/0). load_scryerrc :- ( '$home_directory'(HomeDir) -> @@ -34,7 +34,7 @@ load_scryerrc :- Args = Args0 ), delegate_task(Args, []), - (\+ disable_init_file -> load_scryerrc ; true), + (\+ disabled_init_file -> load_scryerrc ; true), repl. '$repl'(_) :- ( \+ argv(_) -> asserta('$toplevel':argv([])) @@ -46,7 +46,7 @@ load_scryerrc :- delegate_task([], []). delegate_task([], Goals0) :- reverse(Goals0, Goals), - (\+ disable_init_file -> load_scryerrc ; true), + (\+ disabled_init_file -> load_scryerrc ; true), run_goals(Goals), repl. @@ -54,7 +54,7 @@ delegate_task([Arg0|Args], Goals0) :- ( member(Arg0, ["-h", "--help"]) -> print_help ; member(Arg0, ["-v", "--version"]) -> print_version ; member(Arg0, ["-g", "--goal"]) -> gather_goal(g, Args, Goals0) - ; member(Arg0, ["-f"]) -> init_file + ; member(Arg0, ["-f"]) -> disable_init_file ; atom_chars(Mod, Arg0), catch(consult(Mod), E, print_exception(E)) ), @@ -89,8 +89,8 @@ gather_goal(Type, Args0, Goals) :- Gs =.. [Type, Gs1], delegate_task(Args, [Gs|Goals]). -init_file :- - asserta('disable_init_file'). +disable_init_file :- + asserta('disabled_init_file'). arg_type(g). arg_type(t).