Commit 551bfff0 authored by Thierry Martinez's avatar Thierry Martinez

Allows comma inside the arguments of a command with a single argument (typically, add_influence).

parent 4b00a512
......@@ -295,14 +295,3 @@ inputs(
list_enumeration(PositiveInputList, PositiveInputEnum),
list_enumeration(NegativeInputList, NegativeInputEnum),
!.
list_enumeration([], '_') :-
!.
list_enumeration([H], H) :-
H \= (_, _),
!.
list_enumeration([H | List], (H, Enum)) :-
list_enumeration(List, Enum).
:- module(
toplevel,
[
execute_command/1,
toplevel/0,
% Commands
quit/0,
% Public API
toplevel/0,
command/1,
prompt/1
execute_command/1,
prompt/1,
set_prompt/1
]).
toplevel :-
loop.
:- devdoc('\\section{Commands}').
loop :-
quit :-
biocham_command,
doc('quits the interpreter.'),
halt.
:- devdoc('\\section{Public API}').
toplevel :-
devdoc('
executes the toplevel loop.
'),
repeat,
\+ read_execute_print.
read_execute_print :-
set_counter(warnings, 0),
read_command(Command),
command(Command) :-
devdoc('
executes the given command.
It can be used to execute commands in the code while using (a part of)
the Biocham syntax.
'),
begin_command,
functor(Command, Functor, Arity),
(
Command = end_of_file
(
predicate_info(Functor/Arity, ArgumentTypes, yes, _)
->
Command0 = Command
;
between(1, Arity, Arity0),
predicate_info(Functor/Arity0, ArgumentTypes, variantargs, _)
->
Command =.. [Functor | Arguments],
PrefixLength is Arity0 - 1,
length(Prefix, PrefixLength),
append(Prefix, Tail, Arguments),
append(Prefix, [Tail], NewArguments),
Command0 =.. [Functor | NewArguments]
;
predicate_info(Functor/1, ArgumentTypes, yes, _)
->
Command =.. [Functor | Arguments],
merge_arguments(Arguments, Argument),
Command0 =.. [Functor, Argument]
)
->
nl,
quit
check_types(Command0, ArgumentTypes, NewCommand),
NewCommand
;
execute_command(Command)
reaction_predicate(Command)
->
command(add_reaction(Command))
;
influence_predicate(Command)
->
command(add_influence(Command))
;
throw(error(unknown_command(Functor/Arity)))
).
execute_command(Command) :-
devdoc('
executes the given command and prints an error message if it fails.
The predicate itself always succeeds.
'),
catch(
(
command(Command)
->
true
;
throw(error(prolog_failure))
),
Exception,
print_message(error, Exception)
).
:- dynamic(prompt/1).
prompt('biocham: ').
prompt('biocham: ') :-
devdoc('unifies the argument with the current prompt.').
set_prompt(NewPrompt) :-
devdoc('changes the prompt.'),
retract(prompt(_OldPrompt)),
asserta(prompt(NewPrompt)).
:- devdoc('\\section{Private predicates}').
read_execute_print :-
set_counter(warnings, 0),
read_command(Command),
(
Command = end_of_file
->
nl,
quit
;
execute_command(Command)
).
read_command(Command) :-
prompt(Prompt),
read_history('', '', [], Prompt, Command, VariableNames),
name_variables_and_anonymous(Command, VariableNames).
execute_command(Command) :-
catch(
(
command(Command)
->
true
;
throw(error(prolog_failure))
),
Exception,
print_message(error, Exception)
).
prolog:message(error(unknown_command(Command))) -->
['Unknown command: ~p'-[Command]].
......@@ -102,42 +171,7 @@ check_types(Command, ArgumentTypes, NewCommand) :-
NewCommand =.. [Functor | NewArguments].
command(Command) :-
begin_command,
functor(Command, Functor, Arity),
(
(
predicate_info(Functor/Arity, ArgumentTypes, yes, _)
->
Command0 = Command
;
between(1, Arity, Arity0),
predicate_info(Functor/Arity0, ArgumentTypes, variantargs, _)
->
Command =.. [Functor | Arguments],
PrefixLength is Arity0 - 1,
length(Prefix, PrefixLength),
append(Prefix, Tail, Arguments),
append(Prefix, [Tail], NewArguments),
Command0 =.. [Functor | NewArguments]
)
->
check_types(Command0, ArgumentTypes, NewCommand),
NewCommand
;
reaction_predicate(Command)
->
command(add_reaction(Command))
;
influence_predicate(Command)
->
command(add_influence(Command))
;
throw(error(unknown_command(Functor/Arity)))
).
quit :-
biocham_command,
doc('quits the interpreter.'),
halt.
merge_arguments(Arguments, Argument) :-
maplist(term_to_atom, Arguments, ArgumentAtoms),
atomic_list_concat(ArgumentAtoms, ',', ArgumentSequence),
term_to_atom(Argument, ArgumentSequence).
......@@ -4,7 +4,7 @@
name_variables_and_anonymous/2,
camel_case/2,
set_to_list/2,
enumeration_to_list/2,
list_enumeration/2,
equals_to_list/2,
list_to_equals/2,
list/1,
......@@ -75,18 +75,20 @@ camel_case_chars_tail([Head | Chars], [Head | CamelChars]) :-
set_to_list({ Enum }, List) :-
!,
enumeration_to_list(Enum, List).
list_enumeration(List, Enum).
set_to_list(Item, [Item]).
enumeration_to_list((A, B), List) :-
!,
enumeration_to_list(A, LA),
append(LA, LB, List),
enumeration_to_list(B, LB).
list_enumeration([], '_') :-
!.
list_enumeration([H], H) :-
H \= (_, _),
!.
enumeration_to_list(Item, [Item]).
list_enumeration([H | List], (H, Enum)) :-
list_enumeration(List, Enum).
equals_to_list(A = B, List) :-
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment