3998
|
1 #! /usr/bin/perl |
|
2 |
|
3 # Generate option handling code from a simpler input files for |
|
4 # Octave's functions like lsode, dassl, etc. |
|
5 |
3999
|
6 # TODO: |
|
7 # |
|
8 # * Improve default documentation and/or individual documentation |
|
9 # in data files. |
|
10 # |
|
11 # * Fix print/show code to display/return something more informative |
|
12 # for special values (for example, -1 ==> infinite in some cases). |
|
13 # Probably need more information in the data files for this. |
|
14 |
3998
|
15 # Input file format: |
|
16 # |
|
17 # CLASS = string |
|
18 # FCN_NAME = string |
4044
|
19 # INCLUDE = file |
3998
|
20 # DOC_STRING doc END_DOC_STRING |
|
21 # OPTION |
|
22 # NAME = string |
4050
|
23 # DOC_ITEM doc END_DOC_ITEM |
3998
|
24 # TYPE = string |
|
25 # SET_ARG_TYPE = string (optional, defaults to TYPE) |
|
26 # INIT_VALUE = string | INIT_BODY code END_INIT_BODY |
|
27 # SET_EXPR = string | SET_BODY code END_SET_BODY | SET_CODE code END_SET_CODE |
|
28 # END_OPTION |
|
29 # |
|
30 # END_* must appear at beginning of line (whitespace ignored). |
|
31 |
|
32 use Getopt::Long; |
|
33 |
|
34 $opt_emit_opt_class_header = 0; |
|
35 $opt_emit_opt_handler_fcns = 0; |
|
36 $opt_debug = 0; |
|
37 |
|
38 GetOptions ("opt-class-header" => \$opt_emit_opt_class_header, |
|
39 "opt-handler-fcns" => \$opt_emit_opt_handler_fcns, |
|
40 "debug" => \$opt_debug); |
|
41 |
|
42 if (@ARGV == 1) |
|
43 { |
|
44 $INFILE = shift @ARGV; |
|
45 open (INFILE) || die "unable to open input file $INFILE"; |
|
46 } |
|
47 else |
|
48 { |
|
49 die "usage: mk-opts.pl [options] FILE"; |
|
50 } |
|
51 |
|
52 $opt_num = 0; |
|
53 |
|
54 &parse_input; |
|
55 |
|
56 &process_data; |
|
57 |
|
58 FOO: |
|
59 { |
|
60 $opt_emit_opt_class_header && do { &emit_opt_class_header; last FOO; }; |
|
61 |
|
62 $opt_emit_opt_handler_fcns && do { &emit_opt_handler_fcns; last FOO; }; |
|
63 |
|
64 $opt_debug && do { &emit_options_debug; last FOO; }; |
|
65 } |
|
66 |
|
67 sub parse_input |
|
68 { |
|
69 local ($have_doc_string); |
|
70 |
|
71 while (<INFILE>) |
|
72 { |
|
73 next if (/^\s*$/); |
|
74 |
|
75 if (/^\s*OPTION\s*$/) |
|
76 { |
|
77 &parse_option_block; |
|
78 } |
|
79 elsif (/^\s*CLASS\s*=\s*"(\w+)"\s*$/) |
|
80 { |
|
81 die "duplicate CLASS" if ($class ne ""); |
|
82 $CLASS = $1; |
|
83 $class_name = "${CLASS}_options"; |
|
84 $struct_name = "${class_name}_struct"; |
|
85 $static_table_name = "${class_name}_table"; |
|
86 } |
|
87 elsif (/^\s*FCN_NAME\s*=\s*"(\w+)"\s*$/) |
|
88 { |
|
89 die "duplicate FCN_NAME" if ($fcn_name ne ""); |
|
90 $fcn_name = $1; |
|
91 } |
4044
|
92 elsif (/^\s*INCLUDE\s*=\s*"(\S+)"\s*$/) |
|
93 { |
|
94 $include = "${include}#include <$1>\n"; |
|
95 } |
3998
|
96 elsif (/^\s*DOC_STRING\s*$/) |
|
97 { |
|
98 die "duplicate DOC_STRING" if ($have_doc_string); |
|
99 &parse_doc_string; |
|
100 $have_doc_string = 1; |
|
101 } |
4044
|
102 else |
|
103 { |
|
104 die "mk-opts.pl: unknown command: $_\n" |
|
105 } |
3998
|
106 } |
|
107 } |
|
108 |
|
109 sub parse_option_block |
|
110 { |
4050
|
111 local ($have_doc_item, $have_init_body, $have_set_body, $have_set_code); |
3998
|
112 |
|
113 while (<INFILE>) |
|
114 { |
|
115 next if (/^\s*$/); |
|
116 |
|
117 die "missing END_OPTION" if (/^\s*OPTION\s*$/); |
|
118 |
|
119 last if (/^\s*END_OPTION\s*$/); |
|
120 |
|
121 if (/^\s*NAME\s*=\s*"(.*)"\s*$/) |
|
122 { |
|
123 die "duplicate NAME" if ($name[$opt_num] ne ""); |
|
124 $name[$opt_num] = $1; |
|
125 ($opt[$opt_num] = $name[$opt_num]) =~ s/\s+/_/g; |
|
126 $optvar[$opt_num] = "x_$opt[$opt_num]"; |
|
127 $kw_tok[$opt_num] = [ split (/\s+/, $name[$opt_num]) ]; |
|
128 $n_toks[$opt_num] = @{$kw_tok[$opt_num]}; |
|
129 } |
4050
|
130 elsif (/^\s*DOC_ITEM\s*$/) |
|
131 { |
|
132 die "duplicate DOC_ITEM" if ($have_doc_item); |
|
133 &parse_doc_item; |
|
134 $have_doc_item = 1; |
|
135 } |
3998
|
136 elsif (/^\s*TYPE\s*=\s*"(.*)"\s*$/) |
|
137 { |
|
138 die "duplicate TYPE" if ($type[$opt_num] ne ""); |
|
139 $type[$opt_num] = $1; |
|
140 } |
|
141 elsif (/^\s*SET_ARG_TYPE\s*=\s*"(.*)"\s*$/) |
|
142 { |
|
143 die "duplicate SET_ARG_TYPE" if ($set_arg_type[$opt_num] ne ""); |
|
144 $set_arg_type[$opt_num] = $1; |
|
145 } |
|
146 elsif (/^\s*INIT_VALUE\s*=\s*"(.*)"\s*$/) |
|
147 { |
|
148 die "duplicate INIT_VALUE" if ($init_value[$opt_num] ne ""); |
|
149 $init_value[$opt_num] = $1; |
|
150 } |
|
151 elsif (/^\s*SET_EXPR\s*=\s*"(.*)"\s*$/) |
|
152 { |
|
153 die "duplicate SET_EXPR" if ($set_expr[$opt_num] ne ""); |
|
154 $set_expr[$opt_num] = $1; |
|
155 } |
|
156 elsif (/^\s*INIT_BODY\s*$/) |
|
157 { |
|
158 die "duplicate INIT_BODY" if ($have_init_body); |
|
159 &parse_init_body; |
|
160 $have_init_body = 1; |
|
161 } |
|
162 elsif (/^\s*SET_BODY\s*$/) |
|
163 { |
|
164 die "duplicate SET_BODY" if ($have_set_body); |
|
165 &parse_set_body; |
|
166 $have_set_body = 1; |
|
167 } |
|
168 elsif (/^\s*SET_CODE\s*$/) |
|
169 { |
|
170 die "duplicate SET_CODE" if ($have_set_code); |
|
171 &parse_set_code; |
|
172 $have_set_code = 1; |
|
173 } |
|
174 } |
|
175 |
|
176 if ($set_arg_type[$opt_num] eq "") |
|
177 { |
|
178 $set_arg_type[$opt_num] = $type[$opt_num] |
|
179 } |
|
180 else |
|
181 { |
|
182 $set_arg_type[$opt_num] |
|
183 = &substopt ($set_arg_type[$opt_num], $optvar[$opt_num], |
|
184 $opt[$opt_num], $type[$opt_num]); |
|
185 } |
|
186 |
|
187 $opt_num++; |
|
188 } |
|
189 |
|
190 sub process_data |
|
191 { |
|
192 $max_tokens = &max (@n_toks); |
|
193 |
|
194 &get_min_match_len_info ($max_tokens); |
|
195 |
|
196 $fcn_name = lc ($CLASS) if ($fcn_name eq ""); |
|
197 |
|
198 $opt_fcn_name = "${fcn_name}_options" if ($opt_fcn_name eq ""); |
|
199 |
|
200 $static_object_name = "${fcn_name}_opts"; |
|
201 |
|
202 if ($doc_string eq "") |
|
203 { |
|
204 $doc_string = "When called with two arguments, this function\\n\\ |
|
205 allows you set options parameters for the function \@code{$fcn_name}.\\n\\ |
|
206 Given one argument, \@code{$opt_fcn_name} returns the value of the\\n\\ |
|
207 corresponding option. If no arguments are supplied, the names of all\\n\\ |
|
208 the available options and their current values are displayed.\\n\\\n"; |
|
209 } |
|
210 } |
|
211 |
|
212 sub get_min_match_len_info |
|
213 { |
|
214 local ($max_tokens) = @_; |
|
215 |
|
216 local ($i, $j, $k); |
|
217 |
|
218 for ($i = 0; $i < $opt_num; $i++) |
|
219 { |
|
220 for ($j = 0; $j < $max_tokens; $j++) |
|
221 { |
|
222 $min_tok_len_to_match[$i][$j] = 0; |
|
223 } |
|
224 |
|
225 $min_toks_to_match[$i] = 1; |
|
226 |
|
227 L1: for ($k = 0; $k < $opt_num; $k++) |
|
228 { |
|
229 local ($duplicate) = 1; |
|
230 |
|
231 if ($i != $k) |
|
232 { |
|
233 L2: for ($j = 0; $j < $max_tokens; $j++) |
|
234 { |
|
235 if ($j < $n_toks[$i]) |
|
236 { |
|
237 if ($kw_tok[$i][$j] eq $kw_tok[$k][$j]) |
|
238 { |
|
239 if ($min_tok_len_to_match[$i][$j] == 0) |
|
240 { |
|
241 $min_tok_len_to_match[$i][$j] = 1; |
|
242 } |
|
243 |
|
244 $min_toks_to_match[$i]++; |
|
245 } |
|
246 else |
|
247 { |
|
248 $duplicate = 0; |
|
249 |
|
250 if ($min_tok_len_to_match[$i][$j] == 0) |
|
251 { |
|
252 $min_tok_len_to_match[$i][$j] = 1; |
|
253 } |
|
254 |
|
255 local (@s) = split (//, $kw_tok[$i][$j]); |
|
256 local (@t) = split (//, $kw_tok[$k][$j]); |
|
257 |
|
258 local ($n, $ii); |
|
259 $n = scalar (@s); |
|
260 $n = scalar (@t) if (@t < $n); |
|
261 |
|
262 for ($ii = 0; $ii < $n; $ii++) |
|
263 { |
|
264 if ("$s[$ii]" eq "$t[$ii]") |
|
265 { |
|
266 if ($ii + 2 > $min_tok_len_to_match[$i][$j]) |
|
267 { |
|
268 $min_tok_len_to_match[$i][$j]++; |
|
269 } |
|
270 } |
|
271 else |
|
272 { |
|
273 last L2; |
|
274 } |
|
275 } |
|
276 |
|
277 last L1; |
|
278 } |
|
279 } |
|
280 else |
|
281 { |
|
282 die "ambiguous options \"$name[$i]\" and \"$name[$k]\"" if ($duplicate); |
|
283 } |
|
284 } |
|
285 } |
|
286 } |
|
287 } |
|
288 } |
|
289 |
|
290 sub parse_doc_string |
|
291 { |
|
292 while (<INFILE>) |
|
293 { |
|
294 last if (/^\s*END_DOC_STRING\s*$/); |
|
295 |
|
296 $doc_string .= $_; |
|
297 } |
|
298 |
|
299 $doc_string =~ s/\n/\\n\\\n/g; |
|
300 } |
|
301 |
4050
|
302 sub parse_doc_item |
|
303 { |
|
304 while (<INFILE>) |
|
305 { |
|
306 last if (/^\s*END_DOC_ITEM\s*$/); |
|
307 |
|
308 $doc_item[$opt_num] .= $_; |
|
309 } |
|
310 |
|
311 $doc_item[$opt_num] =~ s/\n/\\n\\\n/g; |
|
312 } |
|
313 |
3998
|
314 sub parse_init_body |
|
315 { |
|
316 while (<INFILE>) |
|
317 { |
|
318 last if (/^\s*END_INIT_BODY\s*$/); |
|
319 |
|
320 $init_body[$opt_num] .= $_; |
|
321 } |
|
322 } |
|
323 |
|
324 sub parse_set_body |
|
325 { |
|
326 while (<INFILE>) |
|
327 { |
|
328 last if (/^\s*END_SET_BODY\s*$/); |
|
329 |
|
330 $set_body[$opt_num] .= $_; |
|
331 } |
|
332 } |
|
333 |
|
334 sub parse_set_code |
|
335 { |
|
336 while (<INFILE>) |
|
337 { |
|
338 last if (/^\s*END_SET_CODE\s*$/); |
|
339 |
|
340 $set_code[$opt_num] .= $_; |
|
341 } |
|
342 } |
|
343 |
|
344 sub emit_opt_class_header |
|
345 { |
|
346 local ($i, $s); |
|
347 |
|
348 print "// DO NOT EDIT! |
|
349 // Generated automatically from $INFILE. |
|
350 |
|
351 #if !defined (octave_${class_name}_h) |
|
352 #define octave_${class_name}_h 1 |
|
353 |
|
354 #include <cfloat> |
|
355 #include <cmath> |
|
356 |
4044
|
357 ${include} |
|
358 |
3998
|
359 class |
|
360 ${class_name} |
|
361 { |
|
362 public: |
|
363 |
|
364 ${class_name} (void) { init (); } |
|
365 |
|
366 ${class_name} (const ${class_name}& opt) { copy (opt); } |
|
367 |
|
368 ${class_name}& operator = (const ${class_name}& opt) |
|
369 { |
|
370 if (this != &opt) |
|
371 copy (opt); |
|
372 |
|
373 return *this; |
|
374 } |
|
375 |
|
376 ~${class_name} (void) { }\n"; |
|
377 |
|
378 print "\n void init (void)\n {\n"; |
|
379 |
|
380 for ($i = 0; $i < $opt_num; $i++) |
|
381 { |
|
382 if ($init_value[$i]) |
|
383 { |
|
384 print " $optvar[$i] = $init_value[$i];\n"; |
|
385 } |
|
386 elsif ($init_body[$i]) |
|
387 { |
|
388 $s = &substopt ($init_body[$i], $optvar[$i], $opt[$i], $type[$i]); |
|
389 chop ($s); |
|
390 $s =~ s/^\s*/ /g; |
|
391 $s =~ s/\n\s*/\n /g; |
|
392 print "$s\n"; |
|
393 } |
|
394 } |
|
395 |
4049
|
396 print " reset = true; |
|
397 }\n"; |
3998
|
398 |
|
399 print "\n void copy (const ${class_name}& opt)\n {\n"; |
|
400 |
|
401 for ($i = 0; $i < $opt_num; $i++) |
|
402 { |
|
403 print " $optvar[$i] = opt.$optvar[$i];\n"; |
|
404 } |
|
405 |
4049
|
406 print " reset = opt.reset; |
|
407 }\n"; |
3998
|
408 |
4122
|
409 ## For backward compatibility and because set_options is probably |
|
410 ## a better name in some contexts: |
|
411 |
|
412 print "\n void set_options (const ${class_name}& opt) { copy (opt); }\n"; |
|
413 |
3998
|
414 print "\n void set_default_options (void) { init (); }\n"; |
|
415 |
|
416 for ($i = 0; $i < $opt_num; $i++) |
|
417 { |
|
418 if ($set_expr[$i]) |
|
419 { |
|
420 &emit_set_decl ($i); |
|
421 |
4049
|
422 print "\n { $optvar[$i] = $set_expr[$i]; reset = true; }\n"; |
3998
|
423 } |
|
424 elsif ($set_body[$i]) |
|
425 { |
|
426 &emit_set_decl ($i); |
|
427 |
|
428 $s = &substopt ($set_body[$i], $optvar[$i], $opt[$i], $type[$i]); |
|
429 chop ($s); |
|
430 $s =~ s/^/ /g; |
|
431 $s =~ s/\n/\n /g; |
4049
|
432 print "\n {\n$s\n reset = true;\n }\n"; |
3998
|
433 } |
|
434 elsif ($set_code[$i]) |
|
435 { |
|
436 $s = &substopt ($set_code[$i], $optvar[$i], $opt[$i], $type[$i]); |
|
437 chop ($s); |
|
438 $s =~ s/^ //g; |
|
439 $s =~ s/\n /\n/g; |
|
440 print "\n$s\n"; |
|
441 } |
|
442 } |
|
443 |
|
444 for ($i = 0; $i < $opt_num; $i++) |
|
445 { |
|
446 print " $type[$i] $opt[$i] (void) const\n { return $optvar[$i]; }\n\n"; |
|
447 } |
|
448 |
|
449 print "private:\n\n"; |
|
450 |
|
451 for ($i = 0; $i < $opt_num; $i++) |
|
452 { |
|
453 print " $type[$i] $optvar[$i];\n"; |
|
454 } |
|
455 |
4049
|
456 print "\nprotected:\n\n bool reset;\n};\n\n#endif\n"; |
3998
|
457 } |
|
458 |
|
459 sub emit_set_decl |
|
460 { |
|
461 local ($i) = @_; |
|
462 |
|
463 print " |
|
464 void set_$opt[$i] ($set_arg_type[$i] val)"; |
|
465 } |
|
466 |
|
467 sub emit_opt_handler_fcns |
|
468 { |
|
469 local ($i); |
4044
|
470 my $header = $INFILE; |
|
471 $header =~ s/[.]\w*$/.h/; # replace .in with .h |
|
472 $header =~ s|^.*/([^/]*)$|$1|; # strip directory part |
3998
|
473 |
|
474 print "// DO NOT EDIT!\n// Generated automatically from $INFILE.\n\n"; |
|
475 |
|
476 print "#ifdef HAVE_CONFIG_H |
|
477 #include <config.h> |
|
478 #endif |
|
479 |
|
480 #include <iomanip> |
|
481 #include <iostream> |
|
482 |
4044
|
483 #include \"$header\" |
|
484 |
3998
|
485 #include \"defun-dld.h\" |
|
486 #include \"pr-output.h\" |
|
487 |
4044
|
488 #include \"oct-obj.h\" |
|
489 #include \"utils.h\" |
|
490 #include \"pager.h\" |
|
491 |
3998
|
492 static ${class_name} ${static_object_name};\n\n"; |
|
493 |
|
494 &emit_struct_decl; |
|
495 |
|
496 &emit_struct_def; |
|
497 |
|
498 &emit_print_function; |
|
499 |
|
500 &emit_set_functions; |
|
501 |
|
502 &emit_show_function; |
|
503 |
|
504 &emit_options_function; |
|
505 } |
|
506 |
|
507 sub emit_struct_decl |
|
508 { |
|
509 local ($i); |
|
510 |
|
511 print "#define MAX_TOKENS $max_tokens\n\n"; |
|
512 |
|
513 print "struct ${struct_name}\n{\n"; |
|
514 |
|
515 print " const char *keyword;\n"; |
|
516 print " const char *kw_tok[MAX_TOKENS + 1];\n"; |
|
517 print " int min_len[MAX_TOKENS + 1];\n"; |
|
518 print " int min_toks_to_match;\n"; |
|
519 |
|
520 print "};\n\n"; |
|
521 } |
|
522 |
|
523 sub emit_struct_def |
|
524 { |
|
525 local ($i); |
|
526 |
|
527 print "#define NUM_OPTIONS $opt_num\n\n"; |
|
528 |
|
529 print "static ${struct_name} ${static_table_name} [] =\n{\n"; |
|
530 |
|
531 for ($i = 0; $i < $opt_num; $i++) |
|
532 { |
|
533 &emit_option_table_entry ($i, 0); |
|
534 |
|
535 if ($i < $opt_num - 1) |
|
536 { |
|
537 print "\n"; |
|
538 } |
|
539 } |
|
540 |
|
541 print "};\n\n"; |
|
542 } |
|
543 |
|
544 sub emit_option_table_entry |
|
545 { |
|
546 local ($i, $empty) = @_; |
|
547 |
|
548 local ($k); |
|
549 |
|
550 if ($empty) |
|
551 { |
|
552 print " { 0,\n"; |
|
553 } |
|
554 else |
|
555 { |
|
556 print " { \"$name[$i]\",\n"; |
|
557 } |
|
558 |
|
559 local ($n) = scalar $#{$kw_tok[$i]}; |
|
560 print " {"; |
|
561 for $k (0 .. $max_tokens) |
|
562 { |
|
563 if ($empty || $k > $n) |
|
564 { |
|
565 print " 0,"; |
|
566 } |
|
567 else |
|
568 { |
|
569 print " \"$kw_tok[$i][$k]\","; |
|
570 } |
|
571 } |
|
572 print " },\n"; |
|
573 |
|
574 print " {"; |
|
575 for $k (0 .. $max_tokens) |
|
576 { |
|
577 if ($empty || $k > $n) |
|
578 { |
|
579 print " 0,"; |
|
580 } |
|
581 else |
|
582 { |
|
583 print " $min_tok_len_to_match[$i][$k],"; |
|
584 } |
|
585 } |
|
586 print " }, $min_toks_to_match[$i], "; |
|
587 |
|
588 print "},\n"; |
|
589 } |
|
590 |
|
591 sub emit_print_function |
|
592 { |
|
593 local ($i); |
|
594 |
4047
|
595 ## XXX FIXME XXX -- determine the width of the table automatically. |
|
596 |
3998
|
597 print "static void |
|
598 print_${class_name} (std::ostream& os) |
|
599 { |
|
600 print_usage (\"$opt_fcn_name\", 1); |
|
601 |
|
602 os << \"\\n\" |
|
603 << \"Options for $CLASS include:\\n\\n\" |
4047
|
604 << \" keyword value\\n\" |
|
605 << \" ------- -----\\n\"; |
3998
|
606 |
|
607 $struct_name *list = $static_table_name;\n\n"; |
|
608 |
|
609 for ($i = 0; $i < $opt_num; $i++) |
|
610 { |
|
611 print " {\n os << \" \" |
4047
|
612 << std::setiosflags (std::ios::left) << std::setw (50) |
3998
|
613 << list[$i].keyword |
|
614 << std::resetiosflags (std::ios::left) |
|
615 << \" \";\n\n"; |
|
616 |
|
617 if ($type[$i] eq "double") |
|
618 { |
|
619 print " double val = $static_object_name.$opt[$i] ();\n\n"; |
|
620 print " os << val << \"\\n\";\n"; |
|
621 } |
|
622 elsif ($type[$i] eq "int") |
|
623 { |
|
624 print " int val = $static_object_name.$opt[$i] ();\n\n"; |
|
625 print " os << val << \"\\n\";\n"; |
|
626 } |
|
627 elsif ($type[$i] eq "std::string") |
|
628 { |
|
629 print " os << $static_object_name.$opt[$i] () << \"\\n\";\n"; |
|
630 } |
4044
|
631 elsif ($type[$i] eq "Array<int>") |
|
632 { |
|
633 print " Array<int> val = $static_object_name.$opt[$i] ();\n\n"; |
|
634 print " if (val.length () == 1) |
|
635 { |
|
636 os << val(0) << \"\\n\"; |
|
637 } |
|
638 else |
|
639 { |
|
640 os << \"\\n\\n\"; |
|
641 int len = val.length (); |
|
642 Matrix tmp (len, 1); |
|
643 for (int i = 0; i < len; i++) |
|
644 tmp(i,0) = val(i); |
|
645 octave_print_internal (os, tmp, false, 2); |
|
646 os << \"\\n\\n\"; |
|
647 }\n"; |
|
648 } |
3998
|
649 elsif ($type[$i] eq "Array<double>") |
|
650 { |
|
651 print " Array<double> val = $static_object_name.$opt[$i] ();\n\n"; |
|
652 print " if (val.length () == 1) |
|
653 { |
|
654 os << val(0) << \"\\n\"; |
|
655 } |
|
656 else |
|
657 { |
|
658 os << \"\\n\\n\"; |
|
659 Matrix tmp = Matrix (ColumnVector (val)); |
|
660 octave_print_internal (os, tmp, false, 2); |
|
661 os << \"\\n\\n\"; |
|
662 }\n"; |
|
663 } |
|
664 else |
|
665 { |
|
666 die ("unknown type $type[$i]"); |
|
667 } |
|
668 |
|
669 print " }\n\n"; |
|
670 } |
|
671 |
|
672 print " os << \"\\n\";\n}\n\n"; |
|
673 } |
|
674 |
|
675 sub emit_set_functions |
|
676 { |
|
677 print "static void |
|
678 set_${class_name} (const std::string& keyword, const octave_value& val) |
|
679 { |
|
680 $struct_name *list = $static_table_name;\n\n"; |
|
681 |
|
682 $iftok = "if"; |
|
683 |
|
684 for ($i = 0; $i < $opt_num; $i++) |
|
685 { |
|
686 $iftok = "else if" if ($i > 0); |
|
687 |
|
688 print " $iftok (keyword_almost_match (list[$i].kw_tok, list[$i].min_len, |
|
689 keyword, list[$i].min_toks_to_match, MAX_TOKENS)) |
|
690 {\n"; |
|
691 |
|
692 if ($type[$i] eq "double") |
|
693 { |
|
694 print " double tmp = val.double_value ();\n\n"; |
|
695 print " if (! error_state) |
|
696 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
697 } |
|
698 elsif ($type[$i] eq "int") |
|
699 { |
|
700 print " int tmp = val.int_value ();\n\n"; |
|
701 print " if (! error_state) |
|
702 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
703 } |
|
704 elsif ($type[$i] eq "std::string") |
|
705 { |
|
706 print " std::string tmp = val.string_value ();\n\n"; |
|
707 print " if (! error_state) |
|
708 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
709 } |
4044
|
710 elsif ($type[$i] eq "Array<int>") |
|
711 { |
|
712 print " Array<int> tmp = val.int_vector_value ();\n\n"; |
|
713 print " if (! error_state) |
|
714 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
715 } |
3998
|
716 elsif ($type[$i] eq "Array<double>") |
|
717 { |
|
718 print " Array<double> tmp = val.vector_value ();\n\n"; |
|
719 print " if (! error_state) |
|
720 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
721 } |
|
722 else |
|
723 { |
|
724 die ("unknown type $type[$i]"); |
|
725 } |
|
726 |
|
727 print " }\n"; |
|
728 } |
|
729 |
|
730 print " else |
|
731 { |
|
732 warning (\"$opt_fcn_name: no match for `%s'\", keyword.c_str ()); |
|
733 } |
|
734 }\n\n"; |
|
735 } |
|
736 |
|
737 sub emit_show_function |
|
738 { |
|
739 local ($i, $iftok); |
|
740 |
|
741 print "static octave_value_list |
|
742 show_${class_name} (const std::string& keyword) |
|
743 { |
|
744 octave_value retval; |
|
745 |
|
746 $struct_name *list = $static_table_name;\n\n"; |
|
747 |
|
748 $iftok = "if"; |
|
749 |
|
750 for ($i = 0; $i < $opt_num; $i++) |
|
751 { |
|
752 $iftok = "else if" if ($i > 0); |
|
753 |
|
754 print " $iftok (keyword_almost_match (list[$i].kw_tok, list[$i].min_len, |
|
755 keyword, list[$i].min_toks_to_match, MAX_TOKENS)) |
|
756 {\n"; |
|
757 |
|
758 if ($type[$i] eq "double") |
|
759 { |
|
760 print " double val = $static_object_name.$opt[$i] ();\n\n"; |
|
761 print " retval = val;\n"; |
|
762 } |
|
763 elsif ($type[$i] eq "int") |
|
764 { |
|
765 print " int val = $static_object_name.$opt[$i] ();\n\n"; |
|
766 print " retval = static_cast<double> (val);\n"; |
|
767 } |
|
768 elsif ($type[$i] eq "std::string") |
|
769 { |
|
770 print " retval = $static_object_name.$opt[$i] ();\n"; |
|
771 } |
4044
|
772 elsif ($type[$i] eq "Array<int>") |
|
773 { |
|
774 print " Array<int> val = $static_object_name.$opt[$i] ();\n\n"; |
|
775 print " if (val.length () == 1) |
|
776 { |
|
777 retval = static_cast<double> (val(0)); |
|
778 } |
|
779 else |
|
780 { |
|
781 int len = val.length (); |
|
782 ColumnVector tmp (len); |
|
783 for (int i = 0; i < len; i++) |
|
784 tmp(i) = val(i); |
|
785 retval = tmp; |
|
786 }\n"; |
|
787 } |
3998
|
788 elsif ($type[$i] eq "Array<double>") |
|
789 { |
|
790 print " Array<double> val = $static_object_name.$opt[$i] ();\n\n"; |
|
791 print " if (val.length () == 1) |
|
792 { |
|
793 retval = val(0); |
|
794 } |
|
795 else |
|
796 { |
|
797 retval = ColumnVector (val); |
|
798 }\n"; |
|
799 } |
|
800 else |
|
801 { |
|
802 die ("unknown type $type[$i]"); |
|
803 } |
|
804 |
|
805 print " }\n"; |
|
806 } |
|
807 |
|
808 print " else |
|
809 { |
|
810 warning (\"$opt_fcn_name: no match for `%s'\", keyword.c_str ()); |
|
811 } |
|
812 |
|
813 return retval;\n}\n\n"; |
|
814 } |
|
815 |
|
816 sub emit_options_function |
|
817 { |
|
818 print "DEFUN_DLD ($opt_fcn_name, args, , |
|
819 \"-*- texinfo -*-\\n\\ |
|
820 \@deftypefn {Loadable Function} {} $opt_fcn_name (\@var{opt}, \@var{val})\\n\\ |
4050
|
821 $doc_string\\n\\ |
|
822 Options include\\n\\ |
|
823 \\n\\ |
|
824 \@table \@code\\n\\\n"; |
|
825 |
|
826 for ($i = 0; $i < $opt_num; $i++) |
|
827 { |
|
828 print "\@item \\\"$name[$i]\\\"\\n\\\n"; |
|
829 if ($doc_item[$i] ne "") |
|
830 { |
|
831 print "$doc_item[$i]"; |
|
832 } |
|
833 } |
|
834 |
|
835 print "\@end table\\n\\\n\@end deftypefn\") |
3998
|
836 { |
|
837 octave_value_list retval; |
|
838 |
|
839 int nargin = args.length (); |
|
840 |
|
841 if (nargin == 0) |
|
842 { |
|
843 print_${class_name} (octave_stdout); |
|
844 } |
|
845 else if (nargin == 1 || nargin == 2) |
|
846 { |
|
847 std::string keyword = args(0).string_value (); |
|
848 |
|
849 if (! error_state) |
|
850 { |
|
851 if (nargin == 1) |
|
852 retval = show_${class_name} (keyword); |
|
853 else |
|
854 set_${class_name} (keyword, args(1)); |
|
855 } |
|
856 else |
|
857 error (\"$opt_fcn_name: expecting keyword as first argument\"); |
|
858 } |
|
859 else |
|
860 print_usage (\"$opt_fcn_name\"); |
|
861 |
|
862 return retval; |
4035
|
863 }\n"; |
3998
|
864 } |
|
865 |
|
866 sub emit_options_debug |
|
867 { |
|
868 print "CLASS = \"$class\"\n"; |
|
869 |
|
870 for ($i = 0; $i < $opt_num; $i++) |
|
871 { |
|
872 $NAME = $name[$i]; |
|
873 ($OPT = $NAME) =~ s/\s+/_/g; |
|
874 $OPTVAR = "x_$OPT"; |
|
875 $TYPE = $type[$i]; |
|
876 print "\n"; |
|
877 print "OPTION\n"; |
|
878 print " NAME = \"$NAME\"\n"; |
|
879 print " TYPE = \"$TYPE\"\n"; |
|
880 if ($set_arg_type[$i]) |
|
881 { |
|
882 print eval ("\" SET_ARG_TYPE = \\\"$set_arg_type[$i]\\\"\"") . "\n"; |
|
883 } |
|
884 if ($init_value[$i]) |
|
885 { |
|
886 print " INIT_VALUE = \"$init_value[$i]\"\n"; |
|
887 } |
|
888 if ($init_body[$i]) |
|
889 { |
|
890 print " INIT_BODY\n"; |
|
891 print &substopt ($init_body[$i]); |
|
892 print " END_INIT_BODY\n"; |
|
893 } |
|
894 if ($set_expr[$i]) |
|
895 { |
|
896 print " SET_EXPR = \"$set_expr[$i]\"\n"; |
|
897 } |
|
898 if ($set_body[$i]) |
|
899 { |
|
900 print " SET_BODY\n"; |
|
901 print &substopt ($set_body[$i]); |
|
902 print " END_SET_BODY\n"; |
|
903 } |
|
904 if ($set_code[$i]) |
|
905 { |
|
906 print " SET_CODE\n"; |
|
907 print &substopt ($set_code[$i]); |
|
908 print " END_SET_CODE\n"; |
|
909 } |
|
910 print "END_OPTION\n"; |
|
911 } |
|
912 } |
|
913 |
|
914 sub substopt |
|
915 { |
|
916 local ($string, $OPTVAR, $OPT, $TYPE) = @_; |
|
917 |
|
918 $string =~ s/\$OPTVAR/$OPTVAR/g; |
|
919 $string =~ s/\$OPT/$OPT/g; |
|
920 $string =~ s/\$TYPE/$TYPE/g; |
|
921 |
|
922 $string; |
|
923 } |
|
924 |
|
925 sub print_assoc_array |
|
926 { |
|
927 local (%t) = @_; |
|
928 |
|
929 local ($k); |
|
930 |
|
931 foreach $k (keys (%t)) |
|
932 { |
|
933 print "$k: $t{$k}\n"; |
|
934 } |
|
935 } |
|
936 |
|
937 sub max |
|
938 { |
|
939 local ($max) = shift; |
|
940 |
|
941 foreach (@_) |
|
942 { |
|
943 $max = $_ if $max < $_; |
|
944 } |
|
945 |
|
946 $max; |
|
947 } |