File: pce_cxx_headers.pl

package info (click to toggle)
swi-prolog-packages 5.0.1-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 50,688 kB
  • ctags: 25,904
  • sloc: ansic: 195,096; perl: 91,425; cpp: 7,660; sh: 3,046; makefile: 2,750; yacc: 843; awk: 14; sed: 12
file content (212 lines) | stat: -rw-r--r-- 6,325 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
/*  $Id: pce_cxx_headers.pl,v 1.6 2002/02/01 15:04:49 jan Exp $

    Part of XPCE --- The SWI-Prolog GUI toolkit

    Author:        Jan Wielemaker and Anjo Anjewierden
    E-mail:        jan@swi.psy.uva.nl
    WWW:           http://www.swi.psy.uva.nl/projects/xpce/
    Copyright (C): 1985-2002, University of Amsterdam

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

    As a special exception, if you link this library with other files,
    compiled with a Free Software compiler, to produce an executable, this
    library does not by itself cause the resulting executable to be covered
    by the GNU General Public License. This exception does not however
    invalidate any other reasons why the executable file might be covered by
    the GNU General Public License.
*/

:- module(pce_cpp_header,
	  [ generate_cpp_class_header_file/0
	  ]).
:- use_module(library(pce)).
:- require([ apply/2
	   , between/3
	   , concat/3
	   , concat_atom/2
	   , forall/2
	   , ignore/1
	   , is_list/1
	   , memberchk/2
	   , nth1/3
	   , once/1
	   ]).

itf_max_arg(9).

cpp_header_dir(Dir) :-
	get(string('%s/include/pce', @pce?home), value, Dir).

generate_cpp_class_header_file :-
	new(X, chain),
	send(@classes, for_all, if(@arg2?creator == built_in,
				   message(X, append, @arg1))),
	send(X, delete, class),
	ignore(send(X, delete, call)),	% just make sure
	send(X, delete, object),
	send(X, delete, variable),
	send(X, delete, chain),
	send(X, delete, pce),
	send(X, delete, string),
	send(X, sort),
	send(X, for_all, message(@prolog, cpp_class, @arg1)).

cpp_class(ClassName) :-
	cpp_header_dir(Dir),
	cpp_class_name(ClassName, CppName),
	concat_atom([Dir, /, CppName, '.h'], File),
	new(@output, file(File)),
	send(@output, open, write),
	xpce_file_header(CppName),
	generate_class_def(ClassName),
	generate_as(ClassName),
	xpce_file_footer(CppName),
	send(@output, close),
	free(@output).

xpce_file_header(ClassName) :-
	output('/*  $Id'),		% break to fool CVS
	output('$\n\n'),
	output('    Part of XPCE\n'),
	output('    Designed and implemented by Anjo Anjewierden and Jan Wielemaker\n'),
	output('    E-mail: jan@swi.psy.uva.nl\n\n'),
	output('    Copyright (C) 1993-1997 University of Amsterdam. All rights reserved.\n*/\n\n'),
	output('#ifndef _PCE_%s_H\n', ClassName?upcase),
	output('#define _PCE_%s_H\n\n', ClassName?upcase).

xpce_file_footer(ClassName) :-
	output('#endif /*!_PCE_%s_H*/\n', ClassName?upcase).
	

generate_as(ClassName) :-
	get(@pce, convert, ClassName, class, Class),
	get(Class, name, Name),
	once(cpp_class_name(Name, CppName)),
	output('inline Pce%s\nAs%s(PceArg a)\n', [CppName, CppName]),
	output('{ return *((Pce%s*) &a);\n}\n\n', CppName).


generate_class_def(ClassName) :-
	get(@pce, convert, ClassName, class, Class),
	get(Class, send_method, initialise, InitMethod),
	get(Class, name, Name),
	once(cpp_class_name(Name, CppName)),
	output('PceExternalClass(Class%s);\n', CppName),
	output('class Pce%s :public PceObject\n{\npublic:\n', CppName),
	get(InitMethod, types, Types),
	argument_range(Types, Low, High),
	forall(between(Low, High, Arity),
	       constructor(InitMethod, CppName, Arity)),
	output('};\n\n').

constructor(InitMethod, CppName, N) :-
	arg_names(InitMethod, 1, N, [], ArgNames),
	output('  Pce%s(', CppName),
	forall(between(1, N, Arg),
	       constructor_iarg(ArgNames, Arg)),
	output(') :\n    PceObject(Class%s', CppName),
	forall(between(1, N, Arg),
	       constructor_oarg(ArgNames, Arg)),
	output(')\n  {\n  }\n').

arg_names(_, I, N, _, []) :-
	I =:= N + 1, !.
arg_names(InitMethod, I, N, D, [TheName|R]) :-
	get(InitMethod, argument_type, I, Type),
	get(Type, argument_name, Name),
	cpp_arg_name(Name, ArgName),
	unique_name(ArgName, D, TheName),
	NI is I + 1,
	arg_names(InitMethod, NI, N, [TheName|D], R).

cpp_arg_name(@nil, a).			% unnamed xpce argument
cpp_arg_name(return, ret).		% avoid C-keywords
cpp_arg_name(class, cl).
cpp_arg_name(int, i).
cpp_arg_name(char, chr).
cpp_arg_name(then, thn).
cpp_arg_name(else, els).
cpp_arg_name(default, def).
cpp_arg_name(X, X).			% ok-to-use name

unique_name(Name, D, Name) :-
	\+ memberchk(Name, D), !.
unique_name(Name, D, Unique) :-
	between(2, 100, N),
	atom_concat(Name, N, Unique),
	\+ memberchk(Unique, D).

constructor_iarg(ArgNames, Arg) :-
	(   Arg \== 1
	->  output(', ')
	;   true
	),
	nth1(Arg, ArgNames, Name),
	output('PceArg %s', Name).

constructor_oarg(ArgNames, Arg) :-
	nth1(Arg, ArgNames, Name),
	output(', %s', Name).
	    
argument_range(Types, Low, High) :-
	get(Types, size, Size),
	(   Size == 0
	->  Low = 0,
	    High = 0
	;   get(Types, element, Size, Last),
	    (   get(Last, vector, @on)
	    ->	VLow is Size - 1,
		itf_max_arg(High)
	    ;	VLow is Size,
		High = Size
	    ),
	    (	between(1, VLow, DLow),
		get(Types, element, DLow, Type),
		get(Type, check, @default, _)
	    ->  Low is DLow - 1
	    ;	Low is VLow
	    )
	).

	    
		 /*******************************
		 *	      OUTPUT		*
		 *******************************/

output(Fmt) :-
	output(Fmt, []).
output(Fmt, Atom) :-
	\+ is_list(Atom), !,
	output(Fmt, [Atom]).
output(Fmt, Args) :-
	apply(send(@output, format, Fmt), Args).

cpp_class_name(*,	'Times').
cpp_class_name(-,	'Minus').
cpp_class_name(+, 	'Plus').
cpp_class_name(/,	'Divide').
cpp_class_name(:=,	'Binding').
cpp_class_name(<,	'Less').
cpp_class_name(=,	'Equation').
cpp_class_name(=<,	'LessEqual').
cpp_class_name(==,	'Equal').
cpp_class_name(>,	'Greater').
cpp_class_name(>=,	'GreaterEqual').
cpp_class_name(?,	'Obtain').
cpp_class_name(\==,	'NonEqual').
cpp_class_name(@=,	'NameObject').
cpp_class_name(Name, CppName) :-
	get(Name, capitalise, CppName).