4 $original_input_structure='';
6 $input_structure .= '// '.$_;
7 $original_input_structure .= $_;
10 $original_input_structure =~ tr/ \t\n\r\f/ /;
11 $original_input_structure =~ tr/ //s;
13 if ($original_input_structure =~ /^struct (\w+) ?\{ ?(.*)\}\;? ?$/) {
17 die "illegal struct, must match 'struct name { type var; /*comment*/ ...};': $original_input_structure";
20 # split off a part 'type var[,var...];' with a possible C-comment '/* ... */'
21 while ($decl =~ /^ ?(\w+) ([\w \,]+)\; ?((\/\*.*?\*\/)?)(.*)$/) {
26 while ($member =~ /^(\w+) ?\, ?(.*)$/) {
29 push @COMMENTS,$comment;
31 $comment='/* see above */';
35 if ($member !~ /^\w+$/) {
36 die "illegal struct, must match 'struct name { type var; /*comment*/ ...};': $input_structure";
39 push @MEMBERS,$member;
40 push @COMMENTS,$comment;
43 if ($decl !~ /^ ?$/) {
44 die "illegal struct, must match 'struct name { type var; /*comment*/ ...};': $input_structure";
47 #$STRUCTURE='teststruct';
48 $STRUCTURE_UC=uc(${STRUCTURE});
49 #@TYPES=('ex','ex','ex');
50 #@MEMBERS=('q10','q20','q21');
53 my ($template,$conj)=@_;
57 for ($N=1; $N<=$#MEMBERS+1; $N++) {
59 $MEMBER=$MEMBERS[$N-1];
60 $COMMENT=$COMMENTS[$N-1];
61 $res .= eval('"' . $template . '"');
62 $TYPE=''; # to avoid main::TYPE used only once warning
63 $MEMBER=''; # same as above
64 $COMMENT=''; # same as above
65 if ($N!=$#MEMBERS+1) {
72 $number_of_members=$#MEMBERS+1;
73 $constructor_arglist=generate('ex tmp_${MEMBER}',', ');
74 $member_access_functions=generate(' const ex & ${MEMBER}(void) { return m_${MEMBER}; }',"\n");
75 $op_access_indices_decl=generate(' static unsigned op_${MEMBER};',"\n");
76 $op_access_indices_def=generate('unsigned ${STRUCTURE}::op_${MEMBER}=${N}-1;',"\n");
77 $members=generate(' ex m_${MEMBER}; ${COMMENT}',"\n");
78 $copy_statements=generate(' m_${MEMBER}=other.m_${MEMBER};',"\n");
79 $constructor_statements=generate('m_${MEMBER}(tmp_${MEMBER})',', ');
80 $let_op_statements=generate(
82 ' return m_${MEMBER};'."\n".
85 $temporary_arglist=generate('tmp_${MEMBER}',', ');
86 $expand_statements=generate(' ex tmp_${MEMBER}=m_${MEMBER}.expand(options);',"\n");
87 $has_statements=generate(' if (m_${MEMBER}.has(other)) return true;',"\n");
88 $eval_statements=generate(
89 ' ex tmp_${MEMBER}=m_${MEMBER}.eval(level-1);'."\n".
90 ' all_are_trivially_equal = all_are_trivially_equal &&'."\n".
91 ' are_ex_trivially_equal(tmp_${MEMBER},m_${MEMBER});',
93 $evalf_statements=generate(
94 ' ex tmp_${MEMBER}=m_${MEMBER}.evalf(level-1);'."\n".
95 ' all_are_trivially_equal = all_are_trivially_equal &&'."\n".
96 ' are_ex_trivially_equal(tmp_${MEMBER},m_${MEMBER});',
98 $normal_statements=generate(
99 ' ex tmp_${MEMBER}=m_${MEMBER}.normal(level-1);'."\n".
100 ' all_are_trivially_equal = all_are_trivially_equal &&'."\n".
101 ' are_ex_trivially_equal(tmp_${MEMBER},m_${MEMBER});',
103 $diff_statements=generate(' ex tmp_${MEMBER}=m_${MEMBER}.diff(s);',"\n");
104 $subs_statements=generate(
105 ' ex tmp_${MEMBER}=m_${MEMBER}.subs(ls,lr);'."\n".
106 ' all_are_trivially_equal = all_are_trivially_equal &&'."\n".
107 ' are_ex_trivially_equal(tmp_${MEMBER},m_${MEMBER});',
109 $compare_statements=generate(
110 ' cmpval=m_${MEMBER}.compare(o.m_${MEMBER});'."\n".
111 ' if (cmpval!=0) return cmpval;',
113 $is_equal_statements=generate(' if (!m_${MEMBER}.is_equal(o.m_${MEMBER})) return false;',"\n");
114 $types_ok_statements=generate(
115 '#ifndef SKIP_TYPE_CHECK_FOR_${TYPE}'."\n".
116 ' if (!is_ex_exactly_of_type(m_${MEMBER},${TYPE})) return false;'."\n".
117 '#endif // ndef SKIP_TYPE_CHECK_FOR_${TYPE}',"\n");
119 $interface=<<END_OF_INTERFACE;
120 /** \@file ${STRUCTURE}.h
122 * Definition of GiNaC's user defined structure ${STRUCTURE}. */
125 * This file was generated automatically by structure.pl.
126 * Please do not modify it directly, edit the perl script instead!
128 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
130 * This program is free software; you can redistribute it and/or modify
131 * it under the terms of the GNU General Public License as published by
132 * the Free Software Foundation; either version 2 of the License, or
133 * (at your option) any later version.
135 * This program is distributed in the hope that it will be useful,
136 * but WITHOUT ANY WARRANTY; without even the implied warranty of
137 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138 * GNU General Public License for more details.
140 * You should have received a copy of the GNU General Public License
141 * along with this program; if not, write to the Free Software
142 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
145 // structure.pl input:
148 #ifndef __GINAC_${STRUCTURE_UC}_H__
149 #define __GINAC_${STRUCTURE_UC}_H__
151 #include "structure.h"
155 class ${STRUCTURE} : public structure
159 // default ctor, dtor, copy ctor assignment operator and helpers
163 ${STRUCTURE}(${STRUCTURE} const & other);
164 ${STRUCTURE} const & operator=(${STRUCTURE} const & other);
166 void copy(${STRUCTURE} const & other);
167 void destroy(bool call_parent);
171 ${STRUCTURE}(${constructor_arglist});
173 // functions overriding virtual functions from bases classes
175 basic * duplicate() const;
176 void print(const print_context & c, unsigned level = 0) const;
179 ex expand(unsigned options=0) const;
180 bool has(const ex & other) const;
181 ex eval(int level=0) const;
182 ex evalf(int level=0) const;
183 ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
184 ex diff(const symbol & s) const;
185 ex subs(const lst & ls, const lst & lr) const;
187 int compare_same_type(const basic & other) const;
188 bool is_equal_same_type(const basic & other) const;
189 unsigned return_type(void) const;
191 // new virtual functions which can be overridden by derived classes
194 // non-virtual functions in this class
196 ${member_access_functions}
197 bool types_ok(void) const;
203 ${op_access_indices_decl}
208 extern const ${STRUCTURE} some_${STRUCTURE};
209 extern const type_info & typeid_${STRUCTURE};
210 extern const unsigned tinfo_${STRUCTURE};
214 #define ex_to_${STRUCTURE}(X) (static_cast<${STRUCTURE} const &>(*(X).bp))
218 #endif // ndef _${STRUCTURE_UC}_H_
222 $implementation=<<END_OF_IMPLEMENTATION;
223 /** \@file ${STRUCTURE}.cpp
225 * Implementation of GiNaC's user defined structure ${STRUCTURE}. */
228 * This file was generated automatically by STRUCTURE.pl.
229 * Please do not modify it directly, edit the perl script instead!
231 * GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
233 * This program is free software; you can redistribute it and/or modify
234 * it under the terms of the GNU General Public License as published by
235 * the Free Software Foundation; either version 2 of the License, or
236 * (at your option) any later version.
238 * This program is distributed in the hope that it will be useful,
239 * but WITHOUT ANY WARRANTY; without even the implied warranty of
240 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
241 * GNU General Public License for more details.
243 * You should have received a copy of the GNU General Public License
244 * along with this program; if not, write to the Free Software
245 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
248 // structure.pl input:
253 #include "${STRUCTURE}.h"
259 // default ctor, destructor, copy ctor assignment operator and helpers
264 ${STRUCTURE}::${STRUCTURE}()
266 debugmsg("${STRUCTURE} default ctor",LOGLEVEL_CONSTRUCT);
267 tinfo_key=tinfo_${STRUCTURE};
270 ${STRUCTURE}::~${STRUCTURE}()
272 debugmsg("${STRUCTURE} destructor",LOGLEVEL_DESTRUCT);
276 ${STRUCTURE}::${STRUCTURE}(${STRUCTURE} const & other)
278 debugmsg("${STRUCTURE} copy ctor",LOGLEVEL_CONSTRUCT);
282 ${STRUCTURE} const & ${STRUCTURE}::operator=(${STRUCTURE} const & other)
284 debugmsg("${STRUCTURE} operator=",LOGLEVEL_ASSIGNMENT);
285 if (this != &other) {
294 void ${STRUCTURE}::copy(${STRUCTURE} const & other)
296 structure::copy(other);
300 void ${STRUCTURE}::destroy(bool call_parent)
302 if (call_parent) structure::destroy(call_parent);
311 ${STRUCTURE}::${STRUCTURE}(${constructor_arglist})
312 : ${constructor_statements}
314 debugmsg("${STRUCTURE} ctor from children", LOGLEVEL_CONSTRUCT);
315 tinfo_key=tinfo_${STRUCTURE};
319 // functions overriding virtual functions from bases classes
324 basic * ${STRUCTURE}::duplicate() const
326 debugmsg("${STRUCTURE} duplicate",LOGLEVEL_DUPLICATE);
327 return new ${STRUCTURE}(*this);
330 void ${STRUCTURE}::print(const print_context & c, unsigned level) const
332 debugmsg("${STRUCTURE} print", LOGLEVEL_PRINT);
333 c.s << class_name() << "()";
336 int ${STRUCTURE}::nops() const
338 return ${number_of_members};
341 ex & ${STRUCTURE}::let_op(int i)
344 GINAC_ASSERT(i<nops());
349 errormsg("${STRUCTURE}::let_op(): should not reach this point");
350 return *new ex(fail());
353 ex ${STRUCTURE}::expand(unsigned options) const
355 bool all_are_trivially_equal=true;
357 if (all_are_trivially_equal) {
360 return ${STRUCTURE}(${temporary_arglist});
363 // a ${STRUCTURE} 'has' an expression if it is this expression itself or a child 'has' it
365 bool ${STRUCTURE}::has(const ex & other) const
367 GINAC_ASSERT(other.bp!=0);
368 if (is_equal(*other.bp)) return true;
373 ex ${STRUCTURE}::eval(int level) const
378 bool all_are_trivially_equal=true;
380 if (all_are_trivially_equal) {
383 return ${STRUCTURE}(${temporary_arglist});
386 ex ${STRUCTURE}::evalf(int level) const
391 bool all_are_trivially_equal=true;
393 if (all_are_trivially_equal) {
396 return ${STRUCTURE}(${temporary_arglist});
399 /** Implementation of ex::normal() for ${STRUCTURE}s. It normalizes the arguments
400 * and replaces the ${STRUCTURE} by a temporary symbol.
401 * \@see ex::normal */
402 ex ${STRUCTURE}::normal(lst &sym_lst, lst &repl_lst, int level) const
405 return basic::normal(sym_lst,repl_lst,level);
407 bool all_are_trivially_equal=true;
409 if (all_are_trivially_equal) {
410 return basic::normal(sym_lst,repl_lst,level);
412 ex n=${STRUCTURE}(${temporary_arglist});
413 return n.bp->basic::normal(sym_lst,repl_lst,level);
416 /** ${STRUCTURE}::diff() differentiates the children.
417 there is no need to check for triavially equal, since diff usually
418 does not return itself unevaluated. */
419 ex ${STRUCTURE}::diff(const symbol & s) const
422 return ${STRUCTURE}(${temporary_arglist});
425 ex ${STRUCTURE}::subs(const lst & ls, const lst & lr) const
427 bool all_are_trivially_equal=true;
429 if (all_are_trivially_equal) {
432 return ${STRUCTURE}(${temporary_arglist});
437 int ${STRUCTURE}::compare_same_type(const basic & other) const
439 GINAC_ASSERT(is_of_type(other,${STRUCTURE}));
440 ${STRUCTURE} const & o=static_cast<${STRUCTURE} const &>
441 (const_cast<basic &>(other));
443 ${compare_statements}
447 bool ${STRUCTURE}::is_equal_same_type(const basic & other) const
449 GINAC_ASSERT(is_of_type(other,${STRUCTURE}));
450 ${STRUCTURE} const & o=static_cast<${STRUCTURE} const &>
451 (const_cast<basic &>(other));
452 ${is_equal_statements}
456 unsigned ${STRUCTURE}::return_type(void) const
458 return return_types::noncommutative_composite;
462 // new virtual functions which can be overridden by derived classes
468 // non-virtual functions in this class
473 #define SKIP_TYPE_CHECK_FOR_ex
474 // this is a hack since there is no meaningful
475 // is_ex_exactly_of_type(...,ex) macro definition
477 bool ${STRUCTURE}::types_ok(void) const
479 ${types_ok_statements}
484 // static member variables
487 ${op_access_indices_def}
493 const ${STRUCTURE} some_${STRUCTURE};
494 const type_info & typeid_${STRUCTURE}=typeid(some_${STRUCTURE});
495 const unsigned tinfo_${STRUCTURE}=structure::register_new("${STRUCTURE}");
499 END_OF_IMPLEMENTATION
501 print "Creating interface file ${STRUCTURE}.h...";
502 open OUT,">${STRUCTURE}.h" or die "cannot open ${STRUCTURE}.h";
503 print OUT $interface;
507 print "Creating implementation file ${STRUCTURE}.cpp...";
508 open OUT,">${STRUCTURE}.cpp" or die "cannot open ${STRUCTURE}.cpp";
509 print OUT $implementation;