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 |
|
409 print "\n void set_default_options (void) { init (); }\n"; |
|
410 |
|
411 for ($i = 0; $i < $opt_num; $i++) |
|
412 { |
|
413 if ($set_expr[$i]) |
|
414 { |
|
415 &emit_set_decl ($i); |
|
416 |
4049
|
417 print "\n { $optvar[$i] = $set_expr[$i]; reset = true; }\n"; |
3998
|
418 } |
|
419 elsif ($set_body[$i]) |
|
420 { |
|
421 &emit_set_decl ($i); |
|
422 |
|
423 $s = &substopt ($set_body[$i], $optvar[$i], $opt[$i], $type[$i]); |
|
424 chop ($s); |
|
425 $s =~ s/^/ /g; |
|
426 $s =~ s/\n/\n /g; |
4049
|
427 print "\n {\n$s\n reset = true;\n }\n"; |
3998
|
428 } |
|
429 elsif ($set_code[$i]) |
|
430 { |
|
431 $s = &substopt ($set_code[$i], $optvar[$i], $opt[$i], $type[$i]); |
|
432 chop ($s); |
|
433 $s =~ s/^ //g; |
|
434 $s =~ s/\n /\n/g; |
|
435 print "\n$s\n"; |
|
436 } |
|
437 } |
|
438 |
|
439 for ($i = 0; $i < $opt_num; $i++) |
|
440 { |
|
441 print " $type[$i] $opt[$i] (void) const\n { return $optvar[$i]; }\n\n"; |
|
442 } |
|
443 |
|
444 print "private:\n\n"; |
|
445 |
|
446 for ($i = 0; $i < $opt_num; $i++) |
|
447 { |
|
448 print " $type[$i] $optvar[$i];\n"; |
|
449 } |
|
450 |
4049
|
451 print "\nprotected:\n\n bool reset;\n};\n\n#endif\n"; |
3998
|
452 } |
|
453 |
|
454 sub emit_set_decl |
|
455 { |
|
456 local ($i) = @_; |
|
457 |
|
458 print " |
|
459 void set_$opt[$i] ($set_arg_type[$i] val)"; |
|
460 } |
|
461 |
|
462 sub emit_opt_handler_fcns |
|
463 { |
|
464 local ($i); |
4044
|
465 my $header = $INFILE; |
|
466 $header =~ s/[.]\w*$/.h/; # replace .in with .h |
|
467 $header =~ s|^.*/([^/]*)$|$1|; # strip directory part |
3998
|
468 |
|
469 print "// DO NOT EDIT!\n// Generated automatically from $INFILE.\n\n"; |
|
470 |
|
471 print "#ifdef HAVE_CONFIG_H |
|
472 #include <config.h> |
|
473 #endif |
|
474 |
|
475 #include <iomanip> |
|
476 #include <iostream> |
|
477 |
4044
|
478 #include \"$header\" |
|
479 |
3998
|
480 #include \"defun-dld.h\" |
|
481 #include \"pr-output.h\" |
|
482 |
4044
|
483 #include \"oct-obj.h\" |
|
484 #include \"utils.h\" |
|
485 #include \"pager.h\" |
|
486 |
3998
|
487 static ${class_name} ${static_object_name};\n\n"; |
|
488 |
|
489 &emit_struct_decl; |
|
490 |
|
491 &emit_struct_def; |
|
492 |
|
493 &emit_print_function; |
|
494 |
|
495 &emit_set_functions; |
|
496 |
|
497 &emit_show_function; |
|
498 |
|
499 &emit_options_function; |
|
500 } |
|
501 |
|
502 sub emit_struct_decl |
|
503 { |
|
504 local ($i); |
|
505 |
|
506 print "#define MAX_TOKENS $max_tokens\n\n"; |
|
507 |
|
508 print "struct ${struct_name}\n{\n"; |
|
509 |
|
510 print " const char *keyword;\n"; |
|
511 print " const char *kw_tok[MAX_TOKENS + 1];\n"; |
|
512 print " int min_len[MAX_TOKENS + 1];\n"; |
|
513 print " int min_toks_to_match;\n"; |
|
514 |
|
515 print "};\n\n"; |
|
516 } |
|
517 |
|
518 sub emit_struct_def |
|
519 { |
|
520 local ($i); |
|
521 |
|
522 print "#define NUM_OPTIONS $opt_num\n\n"; |
|
523 |
|
524 print "static ${struct_name} ${static_table_name} [] =\n{\n"; |
|
525 |
|
526 for ($i = 0; $i < $opt_num; $i++) |
|
527 { |
|
528 &emit_option_table_entry ($i, 0); |
|
529 |
|
530 if ($i < $opt_num - 1) |
|
531 { |
|
532 print "\n"; |
|
533 } |
|
534 } |
|
535 |
|
536 print "};\n\n"; |
|
537 } |
|
538 |
|
539 sub emit_option_table_entry |
|
540 { |
|
541 local ($i, $empty) = @_; |
|
542 |
|
543 local ($k); |
|
544 |
|
545 if ($empty) |
|
546 { |
|
547 print " { 0,\n"; |
|
548 } |
|
549 else |
|
550 { |
|
551 print " { \"$name[$i]\",\n"; |
|
552 } |
|
553 |
|
554 local ($n) = scalar $#{$kw_tok[$i]}; |
|
555 print " {"; |
|
556 for $k (0 .. $max_tokens) |
|
557 { |
|
558 if ($empty || $k > $n) |
|
559 { |
|
560 print " 0,"; |
|
561 } |
|
562 else |
|
563 { |
|
564 print " \"$kw_tok[$i][$k]\","; |
|
565 } |
|
566 } |
|
567 print " },\n"; |
|
568 |
|
569 print " {"; |
|
570 for $k (0 .. $max_tokens) |
|
571 { |
|
572 if ($empty || $k > $n) |
|
573 { |
|
574 print " 0,"; |
|
575 } |
|
576 else |
|
577 { |
|
578 print " $min_tok_len_to_match[$i][$k],"; |
|
579 } |
|
580 } |
|
581 print " }, $min_toks_to_match[$i], "; |
|
582 |
|
583 print "},\n"; |
|
584 } |
|
585 |
|
586 sub emit_print_function |
|
587 { |
|
588 local ($i); |
|
589 |
4047
|
590 ## XXX FIXME XXX -- determine the width of the table automatically. |
|
591 |
3998
|
592 print "static void |
|
593 print_${class_name} (std::ostream& os) |
|
594 { |
|
595 print_usage (\"$opt_fcn_name\", 1); |
|
596 |
|
597 os << \"\\n\" |
|
598 << \"Options for $CLASS include:\\n\\n\" |
4047
|
599 << \" keyword value\\n\" |
|
600 << \" ------- -----\\n\"; |
3998
|
601 |
|
602 $struct_name *list = $static_table_name;\n\n"; |
|
603 |
|
604 for ($i = 0; $i < $opt_num; $i++) |
|
605 { |
|
606 print " {\n os << \" \" |
4047
|
607 << std::setiosflags (std::ios::left) << std::setw (50) |
3998
|
608 << list[$i].keyword |
|
609 << std::resetiosflags (std::ios::left) |
|
610 << \" \";\n\n"; |
|
611 |
|
612 if ($type[$i] eq "double") |
|
613 { |
|
614 print " double val = $static_object_name.$opt[$i] ();\n\n"; |
|
615 print " os << val << \"\\n\";\n"; |
|
616 } |
|
617 elsif ($type[$i] eq "int") |
|
618 { |
|
619 print " int val = $static_object_name.$opt[$i] ();\n\n"; |
|
620 print " os << val << \"\\n\";\n"; |
|
621 } |
|
622 elsif ($type[$i] eq "std::string") |
|
623 { |
|
624 print " os << $static_object_name.$opt[$i] () << \"\\n\";\n"; |
|
625 } |
4044
|
626 elsif ($type[$i] eq "Array<int>") |
|
627 { |
|
628 print " Array<int> val = $static_object_name.$opt[$i] ();\n\n"; |
|
629 print " if (val.length () == 1) |
|
630 { |
|
631 os << val(0) << \"\\n\"; |
|
632 } |
|
633 else |
|
634 { |
|
635 os << \"\\n\\n\"; |
|
636 int len = val.length (); |
|
637 Matrix tmp (len, 1); |
|
638 for (int i = 0; i < len; i++) |
|
639 tmp(i,0) = val(i); |
|
640 octave_print_internal (os, tmp, false, 2); |
|
641 os << \"\\n\\n\"; |
|
642 }\n"; |
|
643 } |
3998
|
644 elsif ($type[$i] eq "Array<double>") |
|
645 { |
|
646 print " Array<double> val = $static_object_name.$opt[$i] ();\n\n"; |
|
647 print " if (val.length () == 1) |
|
648 { |
|
649 os << val(0) << \"\\n\"; |
|
650 } |
|
651 else |
|
652 { |
|
653 os << \"\\n\\n\"; |
|
654 Matrix tmp = Matrix (ColumnVector (val)); |
|
655 octave_print_internal (os, tmp, false, 2); |
|
656 os << \"\\n\\n\"; |
|
657 }\n"; |
|
658 } |
|
659 else |
|
660 { |
|
661 die ("unknown type $type[$i]"); |
|
662 } |
|
663 |
|
664 print " }\n\n"; |
|
665 } |
|
666 |
|
667 print " os << \"\\n\";\n}\n\n"; |
|
668 } |
|
669 |
|
670 sub emit_set_functions |
|
671 { |
|
672 print "static void |
|
673 set_${class_name} (const std::string& keyword, const octave_value& val) |
|
674 { |
|
675 $struct_name *list = $static_table_name;\n\n"; |
|
676 |
|
677 $iftok = "if"; |
|
678 |
|
679 for ($i = 0; $i < $opt_num; $i++) |
|
680 { |
|
681 $iftok = "else if" if ($i > 0); |
|
682 |
|
683 print " $iftok (keyword_almost_match (list[$i].kw_tok, list[$i].min_len, |
|
684 keyword, list[$i].min_toks_to_match, MAX_TOKENS)) |
|
685 {\n"; |
|
686 |
|
687 if ($type[$i] eq "double") |
|
688 { |
|
689 print " double tmp = val.double_value ();\n\n"; |
|
690 print " if (! error_state) |
|
691 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
692 } |
|
693 elsif ($type[$i] eq "int") |
|
694 { |
|
695 print " int tmp = val.int_value ();\n\n"; |
|
696 print " if (! error_state) |
|
697 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
698 } |
|
699 elsif ($type[$i] eq "std::string") |
|
700 { |
|
701 print " std::string tmp = val.string_value ();\n\n"; |
|
702 print " if (! error_state) |
|
703 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
704 } |
4044
|
705 elsif ($type[$i] eq "Array<int>") |
|
706 { |
|
707 print " Array<int> tmp = val.int_vector_value ();\n\n"; |
|
708 print " if (! error_state) |
|
709 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
710 } |
3998
|
711 elsif ($type[$i] eq "Array<double>") |
|
712 { |
|
713 print " Array<double> tmp = val.vector_value ();\n\n"; |
|
714 print " if (! error_state) |
|
715 $static_object_name.set_$opt[$i] (tmp);\n"; |
|
716 } |
|
717 else |
|
718 { |
|
719 die ("unknown type $type[$i]"); |
|
720 } |
|
721 |
|
722 print " }\n"; |
|
723 } |
|
724 |
|
725 print " else |
|
726 { |
|
727 warning (\"$opt_fcn_name: no match for `%s'\", keyword.c_str ()); |
|
728 } |
|
729 }\n\n"; |
|
730 } |
|
731 |
|
732 sub emit_show_function |
|
733 { |
|
734 local ($i, $iftok); |
|
735 |
|
736 print "static octave_value_list |
|
737 show_${class_name} (const std::string& keyword) |
|
738 { |
|
739 octave_value retval; |
|
740 |
|
741 $struct_name *list = $static_table_name;\n\n"; |
|
742 |
|
743 $iftok = "if"; |
|
744 |
|
745 for ($i = 0; $i < $opt_num; $i++) |
|
746 { |
|
747 $iftok = "else if" if ($i > 0); |
|
748 |
|
749 print " $iftok (keyword_almost_match (list[$i].kw_tok, list[$i].min_len, |
|
750 keyword, list[$i].min_toks_to_match, MAX_TOKENS)) |
|
751 {\n"; |
|
752 |
|
753 if ($type[$i] eq "double") |
|
754 { |
|
755 print " double val = $static_object_name.$opt[$i] ();\n\n"; |
|
756 print " retval = val;\n"; |
|
757 } |
|
758 elsif ($type[$i] eq "int") |
|
759 { |
|
760 print " int val = $static_object_name.$opt[$i] ();\n\n"; |
|
761 print " retval = static_cast<double> (val);\n"; |
|
762 } |
|
763 elsif ($type[$i] eq "std::string") |
|
764 { |
|
765 print " retval = $static_object_name.$opt[$i] ();\n"; |
|
766 } |
4044
|
767 elsif ($type[$i] eq "Array<int>") |
|
768 { |
|
769 print " Array<int> val = $static_object_name.$opt[$i] ();\n\n"; |
|
770 print " if (val.length () == 1) |
|
771 { |
|
772 retval = static_cast<double> (val(0)); |
|
773 } |
|
774 else |
|
775 { |
|
776 int len = val.length (); |
|
777 ColumnVector tmp (len); |
|
778 for (int i = 0; i < len; i++) |
|
779 tmp(i) = val(i); |
|
780 retval = tmp; |
|
781 }\n"; |
|
782 } |
3998
|
783 elsif ($type[$i] eq "Array<double>") |
|
784 { |
|
785 print " Array<double> val = $static_object_name.$opt[$i] ();\n\n"; |
|
786 print " if (val.length () == 1) |
|
787 { |
|
788 retval = val(0); |
|
789 } |
|
790 else |
|
791 { |
|
792 retval = ColumnVector (val); |
|
793 }\n"; |
|
794 } |
|
795 else |
|
796 { |
|
797 die ("unknown type $type[$i]"); |
|
798 } |
|
799 |
|
800 print " }\n"; |
|
801 } |
|
802 |
|
803 print " else |
|
804 { |
|
805 warning (\"$opt_fcn_name: no match for `%s'\", keyword.c_str ()); |
|
806 } |
|
807 |
|
808 return retval;\n}\n\n"; |
|
809 } |
|
810 |
|
811 sub emit_options_function |
|
812 { |
|
813 print "DEFUN_DLD ($opt_fcn_name, args, , |
|
814 \"-*- texinfo -*-\\n\\ |
|
815 \@deftypefn {Loadable Function} {} $opt_fcn_name (\@var{opt}, \@var{val})\\n\\ |
4050
|
816 $doc_string\\n\\ |
|
817 Options include\\n\\ |
|
818 \\n\\ |
|
819 \@table \@code\\n\\\n"; |
|
820 |
|
821 for ($i = 0; $i < $opt_num; $i++) |
|
822 { |
|
823 print "\@item \\\"$name[$i]\\\"\\n\\\n"; |
|
824 if ($doc_item[$i] ne "") |
|
825 { |
|
826 print "$doc_item[$i]"; |
|
827 } |
|
828 } |
|
829 |
|
830 print "\@end table\\n\\\n\@end deftypefn\") |
3998
|
831 { |
|
832 octave_value_list retval; |
|
833 |
|
834 int nargin = args.length (); |
|
835 |
|
836 if (nargin == 0) |
|
837 { |
|
838 print_${class_name} (octave_stdout); |
|
839 } |
|
840 else if (nargin == 1 || nargin == 2) |
|
841 { |
|
842 std::string keyword = args(0).string_value (); |
|
843 |
|
844 if (! error_state) |
|
845 { |
|
846 if (nargin == 1) |
|
847 retval = show_${class_name} (keyword); |
|
848 else |
|
849 set_${class_name} (keyword, args(1)); |
|
850 } |
|
851 else |
|
852 error (\"$opt_fcn_name: expecting keyword as first argument\"); |
|
853 } |
|
854 else |
|
855 print_usage (\"$opt_fcn_name\"); |
|
856 |
|
857 return retval; |
4035
|
858 }\n"; |
3998
|
859 } |
|
860 |
|
861 sub emit_options_debug |
|
862 { |
|
863 print "CLASS = \"$class\"\n"; |
|
864 |
|
865 for ($i = 0; $i < $opt_num; $i++) |
|
866 { |
|
867 $NAME = $name[$i]; |
|
868 ($OPT = $NAME) =~ s/\s+/_/g; |
|
869 $OPTVAR = "x_$OPT"; |
|
870 $TYPE = $type[$i]; |
|
871 print "\n"; |
|
872 print "OPTION\n"; |
|
873 print " NAME = \"$NAME\"\n"; |
|
874 print " TYPE = \"$TYPE\"\n"; |
|
875 if ($set_arg_type[$i]) |
|
876 { |
|
877 print eval ("\" SET_ARG_TYPE = \\\"$set_arg_type[$i]\\\"\"") . "\n"; |
|
878 } |
|
879 if ($init_value[$i]) |
|
880 { |
|
881 print " INIT_VALUE = \"$init_value[$i]\"\n"; |
|
882 } |
|
883 if ($init_body[$i]) |
|
884 { |
|
885 print " INIT_BODY\n"; |
|
886 print &substopt ($init_body[$i]); |
|
887 print " END_INIT_BODY\n"; |
|
888 } |
|
889 if ($set_expr[$i]) |
|
890 { |
|
891 print " SET_EXPR = \"$set_expr[$i]\"\n"; |
|
892 } |
|
893 if ($set_body[$i]) |
|
894 { |
|
895 print " SET_BODY\n"; |
|
896 print &substopt ($set_body[$i]); |
|
897 print " END_SET_BODY\n"; |
|
898 } |
|
899 if ($set_code[$i]) |
|
900 { |
|
901 print " SET_CODE\n"; |
|
902 print &substopt ($set_code[$i]); |
|
903 print " END_SET_CODE\n"; |
|
904 } |
|
905 print "END_OPTION\n"; |
|
906 } |
|
907 } |
|
908 |
|
909 sub substopt |
|
910 { |
|
911 local ($string, $OPTVAR, $OPT, $TYPE) = @_; |
|
912 |
|
913 $string =~ s/\$OPTVAR/$OPTVAR/g; |
|
914 $string =~ s/\$OPT/$OPT/g; |
|
915 $string =~ s/\$TYPE/$TYPE/g; |
|
916 |
|
917 $string; |
|
918 } |
|
919 |
|
920 sub print_assoc_array |
|
921 { |
|
922 local (%t) = @_; |
|
923 |
|
924 local ($k); |
|
925 |
|
926 foreach $k (keys (%t)) |
|
927 { |
|
928 print "$k: $t{$k}\n"; |
|
929 } |
|
930 } |
|
931 |
|
932 sub max |
|
933 { |
|
934 local ($max) = shift; |
|
935 |
|
936 foreach (@_) |
|
937 { |
|
938 $max = $_ if $max < $_; |
|
939 } |
|
940 |
|
941 $max; |
|
942 } |