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