File: source.op

package info (click to toggle)
mercury 0.9-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 18,488 kB
  • ctags: 9,800
  • sloc: objc: 146,680; ansic: 51,418; sh: 6,436; lisp: 1,567; cpp: 1,040; perl: 854; makefile: 450; asm: 232; awk: 203; exp: 32; fortran: 3; csh: 1
file content (425 lines) | stat: -rw-r--r-- 14,010 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
%------------------------------------------------------------------------------%
% Copyright (C) 1999 INRIA/INSA.
% 
% Author : Erwan Jahier <jahier@irisa.fr>
%
% This file implements the source scenario.


opium_scenario(
	name		: source,
	files		: [source],
	scenarios	: [],
	message		:
" Scenario source provides commands to retrieve and display Mercury source \
(and intermediate) code.\
"
	).


%------------------------------------------------------------------------------%
opium_command(
	name		: listing,
	arg_list	: [Module, ProcOrType, Listing],
	arg_type_list	: [is_atom_or_string, is_mercury_proc_or_type, 
				is_list_or_var],
	abbrev		: ls,
	interface	: button,
	command_type	: opium,
	implementation	: listing_Op,
	parameters	: [],
	message		:
"Retrieves the source code of a Mercury procedure or a Mercury type \
ProcOrType defined in the module Module and unifies it in Listing. \
Module can be either a library or a user defined module.\
\n\
Note: to be able to retrieve library procedures or types with \
listing/3, you need to have the source of the Mercury library somewhere \
and you need \
to make sure that the environment variable LIB_MERCURY has been set correctly \
to the path of the Mercury library in the sh script Opium-M. \
"
	).

% use the default library for library modules and the current dir for the user
% modules.
listing_Op(Module, Name, Listing) :-
	listing2(Module, Name, Listing, list, source).
%------------------------------------------------------------------------------%


%------------------------------------------------------------------------------%
opium_command(
	name		: listing,
	arg_list	: [Module, PredOrFuncOrType],
	arg_type_list	: [is_atom_or_string, is_mercury_proc_or_type],
	abbrev		: ls,
	interface	: button,
	command_type	: opium,
	implementation	: listing_Op,
	parameters	: [],
	message		:
"listing/2 is the same command as listing/3 except it prints the listing \
of the code on the standard output.\n\
\n\
Example:\n\
1 - listing(foo, bar/3, List) unify List with the list of terms defining \
the Mercury predicate bar/3. \n\
2 - listing(\"~/dir/foo\", bar) will display all the predicates bar/n defined \
in the module foo which is in \"~/dir/\" directory. \n\
3 - listing(io, io__read/3) will display the source of the Mercury library \
predicate io__read/3 defined in the Mercury module io.\n\
"
	).

listing_Op(Module, Name) :-
	listing2(Module, Name, _, stdo, source).
%------------------------------------------------------------------------------%


listing2(Module, Name, Listing, Where, FileType) :-
    pathname(Module, ModulePath, ModuleName),
    ( ModulePath \= "" ->
        ( FileType = hlds ->
            (  
		concat_string([ModulePath, ModuleName], FullModuleName),
                exists(FullModuleName) 
	    ->
                listing_module(ModulePath, ModuleName, Name, Listing, 
			Where)
            ;
		concat_string([ModulePath, ModuleName], FullModuleName),
		append_strings(FullModuleName2, ".hlds_dump.99-final", 
			FullModuleName),
                concat_string([ 
                    "Unable to find the file %w in the directory %w. \n"
		    "You need to compile the module %w.m with" 
	            " \"-dfinal\"option.\n"
			], Msg),
		printf(help, Msg, [ModuleName, ModulePath, FullModuleName2])
            )
        ;
            % FileType = source
            (
                concat_string([ModulePath, ModuleName, ".m"], Module_m),
                exists(Module_m)
            ->
		( is_mercury_library_module(ModuleName) ->
			concat_string([ModuleName, "__", Name], 
				Name2Str),
			atom_string(Name2, Name2Str)
		;
			Name2 = Name
		),
                listing_module(ModulePath, ModuleName, Name2, Listing, 
			Where)
            ;
                concat_string([ 
                    "Unable to find the module %w in the directory %w. \n"
		    "You need to compile %w%w.m with \"-dfinal\" option.\n"
			], Msg),
		printf(help, Msg, [ModuleName, ModulePath, ModulePath, 
			ModuleName])
            )
        )
    ;
        % ModulePath == "" 
        ( FileType = hlds ->
            ( 
                append_strings(ModuleName2, ".hlds_dump.99-final", ModuleName),
                is_mercury_library_module(ModuleName2)
            ->
                getenv("LIB_MERCURY", PathStr),
                concat_string([PathStr, ModuleName], FileName),
                ( exists(FileName) ->
                    listing_module(PathStr, ModuleName, Name, Listing, 
			Where)
                ;
 		    append_strings(ModuleName2, ".hlds_dump.99-final", 
			ModuleName),
                    concat_string([ 
                    "Unable to find the file %w in the directory %w. \n"
		    "You need to compile the module %w%w.m with"
		    "\"-dfinal\" option.\n"
			], Msg),
		     printf(help, Msg, [ModuleName, PathStr, PathStr, 
			ModuleName2])
                )
            ;
                % is not a Mercury procedure.
                ( exists(ModuleName) ->
                    listing_module("", ModuleName, Name, Listing, Where)
                ;
		    append_strings(ModuleName2, ".hlds_dump.99-final", 
			ModuleName),
                    concat_string([ 
                    "Unable to find the module %w in the current directory. \n"
		    "You need to compile %w.m with \"-dfinal\" option.\n"
			], Msg),
		   printf(help, Msg, [ModuleName, ModuleName2])
                )
            )
        ;
            % FileType = source
            ( 
                is_mercury_library_module(ModuleName)
            ->
                getenv("LIB_MERCURY", PathStr),
                concat_string([PathStr, ModuleName, ".m"], FileName),
		concat_string([ModuleName, "__", Name], Name2Str),
		atom_string(Name2, Name2Str),
                ( exists(FileName) ->
                    listing_module(PathStr, ModuleName, Name2, Listing, 
			Where)
                ;
                    printf(help, 
                    "Unable to find the module %w in the directory %w. \n", 
                    [ModuleName, PathStr])
                )
            ;
                % is not a Mercury procedure.
                ( 
                    concat_string([ModuleName, ".m"], Module_m),
                    exists(Module_m)
                ->
                    listing_module("", ModuleName, Name, Listing, Where)
                ;
                    printf(help, 
                    "Unable to find the module %w in the current directory.\n", 
                    [ModuleName])
                )
            )
        )
    ).


listing_module(PathStr, ModuleStr, P/A, Listing, Where) :-
	integer_atom(A, AAtom),
	atom_string(AAtom, AStr),
	atom_string(P, PStr),
	concat_string(["listing ", PathStr, ModuleStr, " ", PStr, 
		" ", AStr], Command), 
	sh("rm -rf listing_output; touch listing_output"),
	printf("%w.\n%b", command),
	sh(Command),
	( Where = list ->
		% Put the terms in the list Listing
		read_listing(Listing)
	;
		% Where = stdo
		% Display the procedure on the standard output.	
		Listing = [],
		sh("cat listing_output")
	).

listing_module(PathStr, ModuleStr, P, Listing, Where) :-
	atom_string(P, PStr),
	concat_string(["listing ", PathStr, ModuleStr, " ", PStr], 
		Command),
	sh("rm -rf listing_output; touch listing_output"),
	printf("%w.\n%b", Command),
	sh(Command),
	( Where = list ->
		% Put the terms in the list Listing
		read_listing(Listing)
	;
		% Where = stdo
		% Display the procedure on the standard output.		
		Listing = [],
		sh("cat listing_output")
	).


% Output in Listing the list of the read terms in the file "listing_output".
read_listing(Listing) :-
	open("listing_output", read, stream),
	read_all([], Listing2),
	close(stream),
	reverse(Listing2, Listing).

read_all(ListingIn , ListingOut) :-
	read_mercury_term(stream, Term),
	(
		Term = end_of_file,
		ListingOut = ListingIn,
		!
	;
		read_all([Term | ListingIn], ListingOut)
	).
	
%------------------------------------------------------------------------------%
opium_command(
	name		: listing_hlds,
	arg_list	: [Module, PredOrFuncOrType, Listing],
	arg_type_list	: [is_atom_or_string, is_mercury_proc_or_type, 
				is_list_or_var],
	abbrev		: _,
	interface	: hidden,
	command_type	: opium,
	implementation	: listing_hlds_Op,
	parameters	: [],
	message		:
"See listing_hlds/2."
	).

listing_hlds_Op(Module, Name, Listing) :-
	pathname(Module, ModulePath, ModuleName),
	concat_string([ModulePath, ModuleName, ".hlds_dump.99-final"], Module2),
	listing2(Module2, Name, Listing, list, hlds).

%------------------------------------------------------------------------------%
opium_command(
	name		: listing_hlds,
	arg_list	: [Module, PredOrFuncOrType],
	arg_type_list	: [is_atom_or_string, is_mercury_proc_or_type],
	abbrev		: _,
	interface	: hidden,
	command_type	: opium,
	implementation	: listing_hlds_Op,
	parameters	: [],
	message		:

"listing_hlds/2 and listing_hlds/3 are the same commands as listing/2 and \
listing/3 except they will list the HLDS procedures instead of the source code. \
To be able to list such HLDS code, you need to compile your module with \
\"-dfinal\" option.\
"
	).

listing_hlds_Op(Module, Name) :-
	pathname(Module, ModulePath, ModuleName),
	concat_string([ModulePath, ModuleName, ".hlds_dump.99-final"], Module2),
	listing2(Module2, Name, _, stdo, hlds).


%------------------------------------------------------------------------------%
opium_command(
	name		: listing_current_procedure,
	arg_list	: [],
	arg_type_list	: [],
	abbrev		: lcp,
	interface	: menu,
	command_type	: opium,
	implementation	: listing_current_procedure_Op,
	parameters	: [],
	message		:
"listing_current_procedure/0 prints the source code of the current procedure \
on the user window. If the current procedure is defined in a file that is not \
in the current directory, you need to specify the path of this file with \
listing_current_procedure/1.\
").


% :- pred listing_current_procedure.
% :- mode listing_current_procedure is semidet.
listing_current_procedure_Op :-
	current(name = Name and decl_module = Module),
	listing(Module, Name).


%------------------------------------------------------------------------------%
opium_command(
	name		: listing_current_procedure,
	arg_list	: [Path],
	arg_type_list	: [atom],
	abbrev		: lcp,
	interface	: menu,
	command_type	: opium,
	implementation	: listing_current_procedure_Op,
	parameters	: [],
	message		:
"listing_current_procedure/1 is the same as listing_current_procedure/0 \
except you specify the path of the module of the current procedure.\
").

% :- pred listing_current_procedure(atom).
% :- mode listing_current_procedure(in) is semidet.
listing_current_procedure_Op(Path) :-
	current(name=Name),
	current(decl_module=Module),
	listing(Path, Module, Name).


%------------------------------------------------------------------------------%
opium_type(
	name		: is_mercury_proc_or_type,
	implementation	: is_mercury_proc_or_type_Op,
	message		:
"Type which succeeds if its argument is of the form Name or \
Name/Arity, where Name is an atom and Arity an integer.\
").


is_mercury_proc_or_type_Op(Name/Arity) :-
	atom(Name),
	integer(Arity).

is_mercury_proc_or_type_Op(Name) :-
	atom(Name).


%------------------------------------------------------------------------------%
is_mercury_library_module("gc").                
is_mercury_library_module("array").                
is_mercury_library_module("int").                  
is_mercury_library_module("rbtree").
is_mercury_library_module("assoc_list").           
is_mercury_library_module("integer").              
is_mercury_library_module("relation").
is_mercury_library_module("bag").                  
is_mercury_library_module("io").                   
is_mercury_library_module("require").
is_mercury_library_module("benchmarking").         
is_mercury_library_module("lexer").                
is_mercury_library_module("set").
is_mercury_library_module("bimap").                
is_mercury_library_module("library").              
is_mercury_library_module("set_bbbtree").
is_mercury_library_module("bintree").              
is_mercury_library_module("list").                 
is_mercury_library_module("set_ordlist").
is_mercury_library_module("bintree_set").          
is_mercury_library_module("map").                  
is_mercury_library_module("set_unordlist").
is_mercury_library_module("bool").                 
is_mercury_library_module("math").                 
is_mercury_library_module("stack").
is_mercury_library_module("bt_array"). 
is_mercury_library_module("std_util").
is_mercury_library_module("builtin").              
is_mercury_library_module("multi_map").            
is_mercury_library_module("store").
is_mercury_library_module("char").                 
is_mercury_library_module("ops").                  
is_mercury_library_module("string").
is_mercury_library_module("debugger_interface").   
is_mercury_library_module("parser").  
is_mercury_library_module("dir").                  
is_mercury_library_module("pqueue").               
is_mercury_library_module("swi_lib").
is_mercury_library_module("eqvclass").             
is_mercury_library_module("private_builtin").      
is_mercury_library_module("term").
is_mercury_library_module("float").                
is_mercury_library_module("prolog").               
is_mercury_library_module("term_io").
is_mercury_library_module("getopt").               
is_mercury_library_module("queue").                
is_mercury_library_module("tree234").
is_mercury_library_module("graph").                
is_mercury_library_module("random").               
is_mercury_library_module("varset").
is_mercury_library_module("group").                
is_mercury_library_module("rational").


%------------------------------------------------------------------------------%
opium_type(
	name		: is_atom_or_string,
	implementation	: is_atom_or_string_Op,
	message		: 
"Type which succeed for an atom or a string."
	).

is_atom_or_string_Op(X) :- atom(X), !.
is_atom_or_string_Op(X) :- string(X).