2 * Introduction to Gentran::
3 * Functions for Gentran::
4 * Gentran Mode Switches::
5 * Gentran Option Variables::
6 * Gentran Evaluation Forms::
9 @node Introduction to Gentran, Functions for Gentran, Package gentran, Package gentran
10 @section Introduction to Gentran
12 Original Authors Barbara Gates and Paul Wang
14 Gentran is a powerful generator of foreign language code. Currently it
15 can generate FORTRAN, 'C', and RATFOR code from Maxima language code.
16 Gentran can translate mathematical expressions, iteration loops,
17 conditional branching statements, data type information, function
18 definitions, matrtices and arrays, and more.
20 @node Functions for Gentran, Gentran Mode Switches, Introduction to Gentran, Package gentran
21 @section Functions for Gentran
23 @deffn {Function} gentran (@var{stmt1}, @var{stmt2}, ... , @var{stmtn} , @var{[f1, f2, ... , fm]})
24 Translates each stmt into formatted code in the target language. A
25 substantial subset of expressions and statements in the Maxima
26 programming language can be translated directly into numerical code. The
27 @strong{gentran} command translates Maxima statements or procedure
28 definitions into code in the target language (@strong{gentranlang:}
29 fortran, c, or ratfor). Expressions may optionally be given to Maxima
30 for evaluation prior to translation.
32 @emph{stmt1, stmt2, ... , stmtn} is a sequence of one or more
33 statements, each of which is any Maxima user level expression, (simple,
34 group, or block) statement, or procedure definition that can be
35 translated into the target language.
37 @emph{[f1, f2, ... , fm]} is an optional list of output files to which
38 translated output will be written. They can be any of the following:
40 @emph{@strong{string}} = the name of an output file in quotes
42 @strong{true} (no quotes) = the terminal
44 @strong{false} = the current output file(s)
46 @strong{all} = all files currently open for output by gentran
48 If the files are not open they will be opened; if they are open, output
49 will be appended to them. Filenames are given as quoted strings. If the
50 optional variable @strong{genoutpath} (string, including the final /)
51 default @strong{false} is set, it will be prepended to the output file
52 names. If the output file list is omitted, output will be written to the
53 current output, generally the terminal. @strong{gentran} returns (a list
54 of) the name(s) of file(s) to which code was written.
57 @deffn {Function} gentranout (@var{f1}, @var{f2}, ... , @var{fn})
59 Gentran maintains a list of files currently open for output by gentran
60 commands only. gentranout inserts each file name represented by
61 @emph{f1, f2,... , fn} into that list and opens each one for output. It
62 also resets the current output file(s) to include all files in
63 @emph{f1, f2, ... , fn}. gentranout returns the list of files represented by
64 @emph{f1, f2, ... , fn}; i.e., the current output file(s) after the
65 command has been executed.
68 @deffn {Function} gentranshut (@var{f1}, @var{f2}, ... , @var{fn})
70 gentranshut creates a list of file names from @emph{f1, f2, ... , fn},
71 deletes each from the output file list, and closes the corresponding
72 files. If (all of) the current output file(s) are closed, then the
73 current output file is reset to the terminal. gentranshut returns (a
74 list of) the current output file(s) after the command has been executed.
75 @strong{gentranshut}(@strong{all}) will close all gentran output files.
78 @deffn {Function} gentranpush (@var{f1}, @var{f2}, ... , @var{fn})
80 gentranpush pushes the file list onto the output stack. Each file in the
81 list that is not already open for output is opened at this time. The
82 current output file is reset to this new element on the top of the
86 @deffn {Function} gentranpop (@var{f1}, @var{f2}, ... , @var{fn})
88 gentranpop deletes the top-most occurrence of the single element
89 containing the file name(s) represented by @emph{f1, f2, ... , fn} from
90 the output stack. Files whose names have been completely removed from
91 the output stack are closed. The current output file is reset to the
92 (new) element on the top of the output stack. gentranpop returns the
93 current output file(s) after this command has been executed.
96 @deffn {Function} gentranin (@var{f1}, @var{f2}, ... , @var{fn}, [@var{f1},@var{f2}, ... , @var{fm}])
98 gentranin processes mixed-language template files consisting of active
99 parts (delimited by <<@dots{}>>) containing Maxima statements, including
100 calls to gentran, and passive parts, assumed to contain statements in
101 the target language (including comments), which are transcribed
102 verbatim. Input files are processed sequentially and the results
103 appended to the output. The presence of >> in passive parts of the file
104 (except for in comments) is interpreted as an end-of-file and terminates
105 processing of that file. The optional list of output files @emph{[f1,f2,
106 ... , fm]} each receive a copy of the entire output. All filespecs are
107 quoted strings. Input files may be given as (quoted string) filenames,
108 which will be located by Maxima @strong{file_search}. The optional
109 variable @strong{geninpath} (default @strong{false} ) must be a
110 @emph{list} of quoted strings describing the paths to be searched for
111 the input files. If it is set, that list replaces the standard Maxima
114 Active parts may contain any number of Maxima expressions and
115 statements. They are not copied directly to the output. Instead, they
116 are given to Maxima for evaluation. All output generated by each
117 evaluation is sent to the output file(s). Returned values are only
118 printed on the terminal. Active parts will most likely contain calls to
119 gentran to generate code. This means that the result of processing a
120 template file will be the original template file with all active parts
121 replaced by generated code. If @emph{[f1, f2, ... , fm]} is not
122 supplied, then generated code is simply written to the current output
123 file(s). However, if it is given, then the current output file is
124 temporarily overridden. Generated code is written to each file
125 represented by @emph{f1, f2, ... , fn} for this command only. Files which
126 were open prior to the call to gentranin will remain open after the
127 call, and files which did not exist prior to the call will be created,
128 opened, written to, and closed. The output file stack will be exactly
129 the same both before and after the call. gentranin returns (to the
130 terminal) the name(s) of (all) file(s) written to by this command.
133 @deffn {Function} gentraninshut ()
135 A cleanup function to close input files in case where gentranin hung due
136 to error in template.
139 @deffn {Function} tempvar (@var{type})
141 Generates temporary variable names by concatenating @strong{tempvarname}
142 (default @strong{'t}) with sequence numbers. If @emph{type} is
143 non-false, @emph{e.g.} "real*8" the corresponding type is assigned to
144 the variable in the gentran symbol table, which may be used to generate
145 declarations depending on the setting of the @strong{gendecs} flag. It
146 is the users responsibility to make sure temporary variable names do not
147 conflict with the main program.
150 @deffn {Function} markvar (@var{vname})
152 markvar "marks" variable name @emph{vname} to indicate that it currently
153 holds a significant value.
156 @deffn {Function} unmarkvar (@var{vname})
158 unmarkvar "unmarks" variable name @emph{vname} to indicate that it no
159 longer holds a significant value.
162 @deffn {Function} markedvarp (@var{vname})
164 markedvarp tests whether the variable name @emph{vname} is currently
168 @deffn {Function} gendecs (@var{name})
170 The gendecs command can be called any time the gendecs flag is switched
171 off to retrieve all type declarations from Gentran's symbol table for
172 the given subprogram name (or the "current" subprogram if false is given
176 @deffn {Function} gentran_on (@var{sw})
178 Turns on the mode switch @emph{sw}.
181 @deffn {Function} gentran_off (@var{sw})
183 Turns the given switch, @emph{sw}, off.
186 @node Gentran Mode Switches, Gentran Option Variables, Functions for Gentran, Package gentran
187 @section Gentran Mode Switches
189 @defvr {Option variable} fortran
190 @defvrx {Option variable} ratfor
191 @defvrx {Option variable} c
194 These mode switches change the default mode of Maxima from evaluation to
195 translation. They can be turned on and off with the gentran commands
196 gentran_on and gentran_off. Each time a new Maxima session is started
197 up, the system is in evaluation mode. It prints a prompt on the user's
198 terminal screen and waits for an expression or statement to be entered.
199 It then proceeds to evaluate the expression, prints a new prompt, and
200 waits for the user to enter another expression or statement. This mode
201 can be changed to translation mode by turning on either the fortran,
202 ratfor or c switches. After one of these switches is turned on and until
203 it is turned off, every expression or statement entered by the user is
204 translated into the corresponding language just as if it had been given
205 as an argument to the gentran command. Each of the special functions
206 that can be used from within a call to gentran can be used at the top
207 level until the switch is turned off.
210 @defvr {Option variable} gendecs
213 When the gendecs switch is turned on, gentran generates type
214 declarations whenever possible. When gendecs is switched off, type
215 declarations are not generated. Instead, type information is stored in
216 gentran's symbol table but is not retrieved in the form of declarations
217 unless and until either the gendecs command is called or the gendecs
218 flag is switched back. @strong{Note}: Generated declarations may often
219 be placed in an inappropriate place (@emph{e.g.} in the middle of
220 executable fortran code). Therefore the gendecs flag is turned off
221 during processing of templates by @strong{gentranin}.
224 @node Gentran Option Variables, Gentran Evaluation Forms, Gentran Mode Switches, Package gentran
225 @section Gentran Option Variables
227 @defvr {Option variable} gentranlang
230 Selects the target numerical language. Currently, gentranlang must be
231 fortran, ratfor, or c. Note that symbols entered in Maxima are
232 case-sensitive. gentranlang should not be set to FORTRAN, RATFOR or C.
236 @defvr {Option variable} fortlinelen
239 Maximum number of characters printed on each line of generated FORTRAN
244 @defvr {Option variable} minfortlinelen
247 Minimum number of characters printed on each line of generated FORTRAN
252 @defvr {Option variable} fortcurrind
255 Number of blank spaces printed at the beginning of each line of
256 generated FORTRAN code (after column 6).
260 @defvr {Option variable} ratlinelen
263 Maximum number of characters printed on each line of generated Ratfor
268 @defvr {Option variable} clinelen
271 Maximum number of characters printed on each line of generated 'C' code.
275 @defvr {Option variable} minclinelen
278 Minimum number of characters printed on each line of generated 'C' code.
282 @defvr {Option variable} ccurind
285 Number of blank spaces printed at the beginning of each line of
290 @defvr {Option variable} tablen
293 Number of blank spaces printed for each new level of indentation.
294 (Automatic indentation can be turned off by setting this variable to 0.)
298 @defvr {Option variable} genfloat
301 When set to true (or any non-false value), causes integers in generated
302 numerical code to be converted to floating point numbers, except in the
303 following places: array subscripts, exponents, and initial, final, and
304 step values in do-loops. An exception (for compatibility with Macsyma
305 2.4) is that numbers in exponentials (with base %e only) are
306 double-floated even when genfloat is false.
310 @defvr {Option variable} dblfloat
311 Default: @strong{false} If dblfloat is set to
312 true, floating point numbers in gentran output in implementations (such
313 as Windows Maxima under CLISP) in which float and double-float are the
314 same will be printed as *.d0. In implementations in which float and
315 double-float are different, floats will be coerced to double-float
316 before being printed.
320 @defvr {Option variable} gentranseg
321 Default: @strong{true}
325 @defvr {Option variable} maxexpprintlen
328 When @strong{gentranseg} is true (or any non-false value), causes
329 Gentran to "segment" large expressions into subexpressions of manageable
330 size. The segmentation facility generates a sequence of assignment
331 statements, each of which assigns a subexpression to an automatically
332 generated temporary variable name. This sequence is generated in such a
333 way that temporary variables are re-used as soon as possible, thereby
334 keeping the number of automatically generated variables to a minimum.
335 The maximum allowable expression size can be controlled by setting the
336 @strong{maxexpprintlen} variable to the maximum number of characters
337 allowed in an expression printed in the target numerical language
338 (excluding spaces and other whitespace characters automatically printed
339 by the formatter). When the segmentation routine generates temporary
340 variables, it places type declarations in the symbol table for those
341 variables if possible. It uses the following rules to determine their
344 1. If the type of the variable to which the large expression is being
345 assigned is already known (i.e., has been declared by the user via a
346 TYPE form), then the temporary variables will be declared to be of that
347 same type. 2. If the global variable @strong{tempvartype} has a
348 non-false value, then the temporary variables are declared to be of that
349 type. 3. Otherwise, the variables are not declared unless
350 @strong{implicit} has been set to @strong{true}.
354 @defvr {Option variable} gentranopt
355 Default: @strong{false}
357 When set to true (or any non-false value), causes Gentran to replace
358 each block of straightline code by an optimized sequence of assignments
359 obtained from the Maxima optimize command. (The optimize command takes
360 an expression and replaces common subexpressions by temporary variable
361 names. It returns the resulting assignment statement, preceded by
362 common-subexpression-to-temporary-variable assignments.
366 @defvr {Option variable} tempvarname
369 Name used as the prefix when generating temporary variable names.
373 @defvr {Option variable} optimvarname
377 to generate temporary file names produced by the optimizer when
378 @strong{gentranopt} is @strong{true}. When both gentranseg and
379 gentranopt are true, the optimizer generates temporary file names using
380 @strong{optimvarname} while the segmentation routine uses
381 @strong{tempvarname} preventing conflict.
385 @defvr {Option variable} tempvarnum
388 Number appended onto tempvarname to create a temporary variable name. If
389 the temporary variable name resulting from appending tempvarnum onto the
390 end of tempvarname has already been generated and still holds a useful
391 value or has a different type than requested, then the number is
392 incremented until one is found that was not previously generated or does
393 not still hold a significant value or a different type.
397 @defvr {Option variable} tempvartype
398 Default: @strong{false}
400 Target language variable type (e.g., INTEGER, REAL*8, FLOAT, etc.) used
401 as a default for automatically generated variables whose type cannot be
402 determined otherwise. If tempvartype is false, then generated temporary
403 variables whose type cannot be determined are not automatically
408 @defvr {Option variable} implicit
409 Default: @strong{false}
411 If implicit is set to @strong{true} temporary variables are assigned
412 their implicit type according to Fortran rules based on the initial
413 letter of the name. If gendecs is on, this results in printed type
418 @defvr {Option variable} gentranparser
419 Default: @strong{false}
421 If gentranparser is set to @strong{true} Maxima forms input to gentran
422 will be parsed and an error will be produced if an expression cannot be
423 translated. Otherwise, untranslatable expressions may generate anomalous
424 output, sometimes containing explicit calls to Maxima functions.
428 @defvr {Option variable} genstmtno
431 Number used when a statement number must be generated. Note: it is the
432 user's responsibility to make sure this number will not clash with
433 statement numbers in template files.
437 @defvr {Option variable} genstmtincr
440 number by which genstmtno is incremented each time a new statement
444 @defvr {Option variable} usefortcomplex
445 Default: @strong{false}
447 If usefortcomplex is true, real numbers in expressions declared to be
448 complex by @emph{type(complex,@dots{})} will be printed in Fortran
449 complex number format @emph{(realpart,0.0)}. This is a purely syntactic
450 device and does not carry out any complex calculations.
454 @node Gentran Evaluation Forms, , Gentran Option Variables, Package gentran
455 @section Gentran Evaluation Forms
457 The following special functions can be included in Maxima statements
458 which are to be translated by the gentran command to indicate that they
459 are to be partially or fully evaluated by Maxima before being translated
460 into numerical code. Note that these functions have the described effect
461 only when supplied in arguments to the gentran command.
463 @deffn {Function} eval (@var{exp})
465 Where @emph{exp} is any Maxima expression or statement which, after
466 evaluation by Maxima, results in an expression that can be translated by
467 gentran into the target language. When eval is called from an argument
468 that is to be translated, it tells gentran to give the expression to
469 Maxima for evaluation first, and then to translate the result of that
473 @deffn {Function} rsetq (@var{var}, @var{exp})
475 Where @emph{var} is any Maxima variable, matrix or array element, and
476 @emph{exp} is any Maxima expression which, after evaluation by Maxima
477 results in an expression that can be translated by Gentran into the
478 target language. This is equivalent to VAR : EVAL(EXP) ;
481 @deffn {Function} lsetq (@emph{var}, @emph{exp})
483 Where @emph{var} is any Maxima user level matrix or array element with
484 indices which, after evaluation by Maxima, will result in expressions
485 that can be translated by Gentran, and @emph{exp} is any Maxima user
486 level expression that can be translated into the target language. This
487 is equivalent to @code{var[eval(s1), eval(s2), ...]: exp} where @var{s1}, @var{s2}, ...
491 @deffn {Function} lrsetq (@emph{var}, @emph{exp})
493 Where @emph{var} is any Maxima matrix or array element with indices
494 which, after evaluation by Maxima, will result in expressions that can
495 be translated by Gentran; and @emph{exp} is any user level expression
496 which, after evaluation, will result in an expression that can be
497 translated by Gentran into the target language. This is equivalent to
498 @code{var[eval(s1), eval(s2), ...]: eval(exp);}.
501 @deffn {Function} type (@emph{type,v1@dots{}vn})
503 Places information in the gentran symbol table to assign @emph{type} to
504 variables @emph{v1@dots{}vn}. This may result in type declarations
505 printed by gentran depending on the setting of gendecs. @strong{type}
506 must be called from within gentran and does not evaluate its arguments
507 unless @strong{eval}() is used.
511 @deffn {Function} literal (@emph{arg1, arg2, ... , argn})
513 where arg1, arg2, ... , argn is an argument list containing one or more
514 arg's, each of which either is, or evaluates to, an atom. The atoms
515 @emph{tab} and @emph{cr} have special meanings. arg's are not evaluated
516 unless given as arguments to eval. This function call is replaced by the
517 character sequence resulting from concatenation of the given atoms.
518 Double quotes are stripped from all string type arg's, and each
519 occurrence of the reserved atom @emph{tab} or @emph{cr} is replaced by a
520 tab to the current level of indentation, or an end-of-line character.