mirror of
https://github.com/Mercury-Language/mercury.git
synced 2025-12-16 06:14:59 +00:00
Estimated hours taken: 6 If calling from the internal debugger, use readline input for the interactive term browser and interactive queries. browser/browse.m: Change some if-then-elses to switches, which will help catch errors if a new functor is added to the debugger type. browser/parse.m: browser/util.m: Return a string from util__trace_getline/4 rather than a list of chars, which saves converting from a string to a list of chars and then back again. browser/util.m: Add a version of util__trace_getline that also takes I/O stream arguments. Pass these arguments to MR_trace_getline. browser/declarative_oracle.m: Call util__trace_getline/4 to do input via readline (if available). Improve error handling. browser/interactive_query.m: Call util__trace_getline to get user input, instead of standard library predicates. runtime/mercury_init.h: runtime/mercury_wrapper.c: runtime/mercury_wrapper.h: trace/mercury_trace_internal.c: trace/mercury_trace_internal.h: Add two I/O stream arguments to MR_trace_getline.
110 lines
3.0 KiB
Mathematica
110 lines
3.0 KiB
Mathematica
%---------------------------------------------------------------------------%
|
|
% Copyright (C) 1998-1999 The University of Melbourne.
|
|
% This file may only be copied under the terms of the GNU Library General
|
|
% Public License - see the file COPYING.LIB in the Mercury distribution.
|
|
%---------------------------------------------------------------------------%
|
|
|
|
:- module util.
|
|
|
|
:- interface.
|
|
|
|
:- import_module list, string, io.
|
|
|
|
% Get user input via the same method used by the internal
|
|
% debugger.
|
|
:- pred util__trace_getline(string, io__result(string), io__state,
|
|
io__state).
|
|
:- mode util__trace_getline(in, out, di, uo) is det.
|
|
|
|
:- pred util__trace_getline(string, io__result(string), io__input_stream,
|
|
io__output_stream, io__state, io__state).
|
|
:- mode util__trace_getline(in, out, in, in, di, uo) is det.
|
|
|
|
:- pred util__zip_with(pred(T1, T2, T3), list(T1), list(T2), list(T3)).
|
|
:- mode util__zip_with(pred(in, in, out) is det, in, in, out) is det.
|
|
|
|
% Apply predicate to argument repeatedly until the result
|
|
% remains the same.
|
|
:- pred util__limit(pred(list(T), list(T)), list(T), list(T)).
|
|
:- mode util__limit(pred(in,out) is det, in, out) is det.
|
|
|
|
%---------------------------------------------------------------------------%
|
|
:- implementation.
|
|
|
|
:- import_module int, require.
|
|
|
|
util__trace_getline(Prompt, Result) -->
|
|
io__input_stream(MdbIn),
|
|
io__output_stream(MdbOut),
|
|
util__trace_getline(Prompt, Result, MdbIn, MdbOut).
|
|
|
|
:- pragma promise_pure(util__trace_getline/6).
|
|
|
|
util__trace_getline(Prompt, Result, MdbIn, MdbOut) -->
|
|
{
|
|
impure call_trace_getline(MdbIn, MdbOut, Prompt, Line)
|
|
->
|
|
Result = ok(Line)
|
|
;
|
|
Result = eof
|
|
}.
|
|
|
|
:- impure pred call_trace_getline(input_stream, output_stream, string, string).
|
|
:- mode call_trace_getline(in, in, in, out) is semidet.
|
|
|
|
:- pragma c_header_code("
|
|
#include ""mercury_wrapper.h""
|
|
#include ""mercury_string.h""
|
|
#include ""mercury_trace_base.h""
|
|
#include ""mercury_trace_internal.h""
|
|
").
|
|
|
|
:- pragma c_code(call_trace_getline(MdbIn::in, MdbOut::in, Prompt::in,
|
|
Line::out),
|
|
[will_not_call_mercury],
|
|
"
|
|
char *line;
|
|
char *mercury_string;
|
|
MercuryFile *mdb_in = (MercuryFile *) MdbIn;
|
|
MercuryFile *mdb_out = (MercuryFile *) MdbOut;
|
|
|
|
if (MR_address_of_trace_getline != NULL) {
|
|
line = (*MR_address_of_trace_getline)((char *) Prompt,
|
|
mdb_in->file, mdb_out->file);
|
|
} else {
|
|
MR_tracing_not_enabled();
|
|
/* not reached */
|
|
}
|
|
|
|
if (line == NULL) {
|
|
SUCCESS_INDICATOR = FALSE;
|
|
} else {
|
|
make_aligned_string_copy(mercury_string, line);
|
|
free(line);
|
|
Line = (String) mercury_string;
|
|
SUCCESS_INDICATOR = TRUE;
|
|
}
|
|
"
|
|
).
|
|
|
|
util__zip_with(Pred, XXs, YYs, Zipped) :-
|
|
( (XXs = [], YYs = []) ->
|
|
Zipped = []
|
|
; (XXs = [X|Xs], YYs = [Y|Ys]) ->
|
|
Pred(X,Y,PXY),
|
|
Zipped = [PXY|Rest],
|
|
util__zip_with(Pred, Xs, Ys, Rest)
|
|
;
|
|
error("zip_with: list arguments are of unequal length")
|
|
).
|
|
|
|
util__limit(Pred, Xs, Ys) :-
|
|
Pred(Xs, Zs),
|
|
( Xs = Zs ->
|
|
Ys = Zs
|
|
;
|
|
util__limit(Pred, Zs, Ys)
|
|
).
|
|
|
|
%---------------------------------------------------------------------------%
|