toplevel.pl 5.76 KB
Newer Older
Thierry Martinez's avatar
Thierry Martinez committed
1 2 3
:- module(
  toplevel,
  [
4
    % Commands
Thierry Martinez's avatar
Thierry Martinez committed
5
    quit/0,
6 7
    % Public API
    toplevel/0,
Thierry Martinez's avatar
Thierry Martinez committed
8
    command/1,
9 10
    execute_command/1,
    prompt/1,
11 12 13 14
    set_prompt/1,
    option/4,
    set_option/2,
    set_options/1
Thierry Martinez's avatar
Thierry Martinez committed
15 16
  ]).

Thierry Martinez's avatar
Thierry Martinez committed
17

18
:- devdoc('\\section{Commands}').
Thierry Martinez's avatar
Thierry Martinez committed
19 20


21 22 23 24 25 26 27 28 29 30 31 32 33
quit :-
  biocham_command,
  doc('quits the interpreter.'),
  halt.


:- devdoc('\\section{Public API}').


toplevel :-
  devdoc('
    executes the toplevel loop.
  '),
Thierry Martinez's avatar
Thierry Martinez committed
34 35 36
  repeat,
  \+ read_execute_print.

Thierry Martinez's avatar
Thierry Martinez committed
37

38 39 40 41 42 43 44
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,
45 46 47
  retractall(local_option(_, _)),
  extract_options(Command, CommandWithoutOptions, Options),
  functor(CommandWithoutOptions, Functor, Arity),
Thierry Martinez's avatar
Thierry Martinez committed
48
  (
49 50 51 52
    Functor/Arity = option/0
  ->
    set_options(Options)
  ;
53
    (
54
      predicate_info(Functor/Arity, ArgumentTypes, CommandOptions, yes, _)
55
    ->
56
      Command0 = CommandWithoutOptions
57 58
    ;
      between(1, Arity, Arity0),
59 60 61
      predicate_info(
        Functor/Arity0, ArgumentTypes, CommandOptions, variantargs, _
      )
62
    ->
63
      CommandWithoutOptions =.. [Functor | Arguments],
64 65 66 67 68 69
      PrefixLength is Arity0 - 1,
      length(Prefix, PrefixLength),
      append(Prefix, Tail, Arguments),
      append(Prefix, [Tail], NewArguments),
      Command0 =.. [Functor | NewArguments]
    ;
70
      predicate_info(Functor/1, ArgumentTypes, CommandOptions, yes, _)
71
    ->
72
      CommandWithoutOptions =.. [Functor | Arguments],
73 74 75
      merge_arguments(Arguments, Argument),
      Command0 =.. [Functor, Argument]
    )
Thierry Martinez's avatar
Thierry Martinez committed
76
  ->
77
    check_types(Command0, ArgumentTypes, NewCommand),
78
    check_options(Options, CommandOptions),
79
    NewCommand
Thierry Martinez's avatar
Thierry Martinez committed
80
  ;
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    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)
Thierry Martinez's avatar
Thierry Martinez committed
108 109
  ).

Thierry Martinez's avatar
Thierry Martinez committed
110 111 112

:- dynamic(prompt/1).

Thierry Martinez's avatar
Thierry Martinez committed
113

114 115
prompt('biocham: ') :-
  devdoc('unifies the argument with the current prompt.').
Thierry Martinez's avatar
Thierry Martinez committed
116

Thierry Martinez's avatar
Thierry Martinez committed
117

Thierry Martinez's avatar
Thierry Martinez committed
118
set_prompt(NewPrompt) :-
119
  devdoc('changes the prompt.'),
Thierry Martinez's avatar
Thierry Martinez committed
120 121 122
  retract(prompt(_OldPrompt)),
  asserta(prompt(NewPrompt)).

Thierry Martinez's avatar
Thierry Martinez committed
123

124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
option(Name, _Type, Value, _Doc) :-
  devdoc('defines an option for the command and gets its value.'),
  (
    local_option(Name, Value)
  ->
    true
  ;
    item([kind: option, key: Name, item: option(Name: Value)])
  ->
    true
  ;
    throw(error(option_required(Name), option))
  ).


set_option(Option, Value) :-
  (
    predicate_property(Head, visible),
    functor(Head, Functor, Arity),
    predicate_info(Functor/Arity, _, CommandOptions, BiochamCommand, _),
    BiochamCommand \= no,
    member(option(Option, Type, _Doc), CommandOptions)
  ->
    check_type_with_error(Type, Value, TypedValue),
    change_item([], option, Option, option(Option: TypedValue))
  ;
    throw(error(unknown_option(Option), set_option))
  ).


set_options(List) :-
  \+ (
    member(Option: Value, List),
    \+ (
      set_option(Option, Value)
    )
  ).


163 164 165
:- devdoc('\\section{Private predicates}').


166 167 168
:- dynamic(local_option/2).


169 170 171 172 173 174 175 176 177 178 179 180 181
read_execute_print :-
  set_counter(warnings, 0),
  read_command(Command),
  (
    Command = end_of_file
  ->
    nl,
    quit
  ;
    execute_command(Command)
  ).


Thierry Martinez's avatar
Thierry Martinez committed
182 183
read_command(Command) :-
  prompt(Prompt),
Thierry Martinez's avatar
Thierry Martinez committed
184 185
  read_history('', '', [], Prompt, Command, VariableNames),
  name_variables_and_anonymous(Command, VariableNames).
Thierry Martinez's avatar
Thierry Martinez committed
186

Thierry Martinez's avatar
Thierry Martinez committed
187

188 189
prolog:message(error(unknown_command(Command))) -->
  ['Unknown command: ~p'-[Command]].
Thierry Martinez's avatar
Thierry Martinez committed
190

191 192
prolog:message(error(prolog_failure)) -->
  ['Prolog failure'].
Thierry Martinez's avatar
Thierry Martinez committed
193

194 195
prolog:message(error(Exception)) -->
   ['Uncaught exception: ~p'-[Exception]].
Thierry Martinez's avatar
Thierry Martinez committed
196

Thierry Martinez's avatar
Thierry Martinez committed
197

Thierry Martinez's avatar
Thierry Martinez committed
198 199 200 201 202 203 204 205 206 207 208 209
check_types(Command, ArgumentTypes, NewCommand) :-
  Command =.. [Functor | Arguments],
  findall(
    NewArgument,
    (
      nth0(Index, Arguments, Argument),
      nth0(Index, ArgumentTypes, ArgumentType),
      (
        var(ArgumentType)
      ->
        NewArgument = Argument
      ;
210
        check_type_with_error(ArgumentType, Argument, NewArgument)
Thierry Martinez's avatar
Thierry Martinez committed
211 212 213 214 215 216 217
      )
    ),
    NewArguments
  ),
  NewCommand =.. [Functor | NewArguments].


218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
check_options(Options, CommandOptions) :-
  \+ (
    member(Option: UntypedValue, Options),
    \+ (
      (
        member(option(Option, Type, _Doc), CommandOptions)
      ->
        check_type_with_error(Type, UntypedValue, Value),
        assertz(local_option(Option, Value))
      ;
        throw(error(option_not_applicable(Option), check_options))
      )
    )
  ).


check_type_with_error(ArgumentType, Argument, NewArgument) :-
  catch(
    (
      check_type(ArgumentType, Argument, NewArgument)
    ->
      true
    ;
      throw(error(failure))
    ),
    error(Type),
    throw(error(invalid_type(Argument, Type)))
  ).

247 248 249 250
merge_arguments(Arguments, Argument) :-
  maplist(term_to_atom, Arguments, ArgumentAtoms),
  atomic_list_concat(ArgumentAtoms, ',', ArgumentSequence),
  term_to_atom(Argument, ArgumentSequence).
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272


extract_options(Command, CommandWithoutOptions, Options) :-
  Command =.. [Functor | Arguments],
  extract_options_arguments(Arguments, RealArguments, Options),
  CommandWithoutOptions =.. [Functor | RealArguments].


extract_options_arguments([], [], []).

extract_options_arguments(
  [OptionValue | Arguments], RealArguments, [OptionValue | Options]
) :-
  OptionValue = Option : _Value,
  Option \= library,
  !,
  extract_options_arguments(Arguments, RealArguments, Options).

extract_options_arguments(
  [Argument | Arguments], [Argument | RealArguments], Options
) :-
  extract_options_arguments(Arguments, RealArguments, Options).