7 our ($default_output_file) = $0;
8 $default_output_file =~ s/\.pl//;
13 # Initialize type system.
29 # Parses the command line.
31 # Initializes $input_file, $output_file.
33 GetOptions ("i|input=s" => \$input_file,
34 "o|output=s" => \$output_file,
35 "h|help" => sub { usage (); })
38 $input_file = "operations.def" if !defined $input_file;
39 $output_file = $default_output_file if !defined $output_file;
41 open (INPUT, "<$input_file") or die "$input_file: open: $!\n";
42 open (OUTPUT, ">$output_file") or die "$output_file: create: $!\n";
49 $0, for generating $default_output_file from definitions
50 usage: generate.pl [-i INPUT] [-o OUTPUT] [-h]
51 -i INPUT input file containing definitions (default: operations.def)
52 -o OUTPUT output file (default: $default_output_file)
53 -h display this help message
63 # Defines all our types.
65 # Initializes %type, @types.
67 # Common user-visible types used throughout evaluation trees.
68 init_type ('number', 'any', C_TYPE => 'double',
69 ATOM => 'number', MANGLE => 'n', HUMAN_NAME => 'num',
70 STACK => 'ns', MISSING_VALUE => 'SYSMIS');
71 init_type ('string', 'any', C_TYPE => 'struct substring',
72 ATOM => 'string', MANGLE => 's', HUMAN_NAME => 'string',
73 STACK => 'ss', MISSING_VALUE => 'empty_string');
74 init_type ('boolean', 'any', C_TYPE => 'double',
75 ATOM => 'number', MANGLE => 'n', HUMAN_NAME => 'boolean',
76 STACK => 'ns', MISSING_VALUE => 'SYSMIS');
79 init_type ('format', 'atom');
80 init_type ('ni_format', 'leaf', C_TYPE => 'const struct fmt_spec *',
81 ATOM => 'format', MANGLE => 'f',
82 HUMAN_NAME => 'num_input_format');
83 init_type ('no_format', 'leaf', C_TYPE => 'const struct fmt_spec *',
84 ATOM => 'format', MANGLE => 'f',
85 HUMAN_NAME => 'num_output_format');
88 init_type ('integer', 'leaf', C_TYPE => 'int',
89 ATOM => 'integer', MANGLE => 'n', HUMAN_NAME => 'integer');
90 init_type ('pos_int', 'leaf', C_TYPE => 'int',
91 ATOM => 'integer', MANGLE => 'n',
92 HUMAN_NAME => 'positive_integer_constant');
95 init_type ('variable', 'atom');
96 init_type ('num_var', 'leaf', C_TYPE => 'const struct variable *',
97 ATOM => 'variable', MANGLE => 'Vn',
98 HUMAN_NAME => 'num_variable');
99 init_type ('str_var', 'leaf', C_TYPE => 'const struct variable *',
100 ATOM => 'variable', MANGLE => 'Vs',
101 HUMAN_NAME => 'string_variable');
104 init_type ('vector', 'leaf', C_TYPE => 'const struct vector *',
105 ATOM => 'vector', MANGLE => 'v', HUMAN_NAME => 'vector');
108 init_type ('expression', 'fixed', C_TYPE => 'struct expression *',
110 init_type ('case', 'fixed', C_TYPE => 'const struct ccase *',
112 init_type ('case_idx', 'fixed', C_TYPE => 'size_t',
113 FIXED_VALUE => 'case_idx');
114 init_type ('dataset', 'fixed', C_TYPE => 'struct dataset *',
115 FIXED_VALUE => 'ds');
117 # One of these is emitted at the end of each expression as a sentinel
118 # that tells expr_evaluate() to return the value on the stack.
119 init_type ('return_number', 'atom');
120 init_type ('return_string', 'atom');
122 # Used only for debugging purposes.
123 init_type ('operation', 'atom');
126 # init_type has 2 required arguments:
130 # `$name' is the type's name in operations.def.
132 # `OP_$name' is the terminal's type in operations.h.
134 # `expr_allocate_$name()' allocates a node of the given type.
136 # ROLE: How the type may be used:
138 # "any": Usable as operands and function arguments, and
139 # function and operator results.
141 # "leaf": Usable as operands and function arguments, but
142 # not function arguments or results. (Thus, they appear
143 # only in leaf nodes in the parse type.)
145 # "fixed": Not allowed either as an operand or argument
146 # type or a result type. Used only as auxiliary data.
148 # "atom": Not allowed anywhere; just adds the name to
151 # All types except those with "atom" as their role also require:
153 # C_TYPE: The C type that represents this abstract type.
155 # Types with "any" or "leaf" role require:
159 # `$atom' is the `struct operation_data' member name.
161 # get_$atom_name() obtains the corresponding data from a
164 # MANGLE: Short string for name mangling. Use identical strings
165 # if two types should not be overloaded.
167 # HUMAN_NAME: Name for a type when we describe it to the user.
169 # Types with role "any" require:
171 # STACK: Name of the local variable in expr_evaluate(), used for
172 # maintaining the stack for this type.
174 # MISSING_VALUE: Expression used for the missing value of this
177 # Types with role "fixed" require:
179 # FIXED_VALUE: Expression used for the value of this type.
181 my ($name, $role, %rest) = @_;
182 my ($type) = $type{"\U$name"} = {NAME => $name, ROLE => $role, %rest};
184 my (@need_keys) = qw (NAME ROLE);
185 if ($role eq 'any') {
186 push (@need_keys, qw (C_TYPE ATOM MANGLE HUMAN_NAME STACK MISSING_VALUE));
187 } elsif ($role eq 'leaf') {
188 push (@need_keys, qw (C_TYPE ATOM MANGLE HUMAN_NAME));
189 } elsif ($role eq 'fixed') {
190 push (@need_keys, qw (C_TYPE FIXED_VALUE));
191 } elsif ($role eq 'atom') {
193 die "no role `$role'";
197 $have_keys{$_} = 1 foreach keys %$type;
198 for my $key (@need_keys) {
199 defined $type->{$key} or die "$name lacks $key";
200 delete $have_keys{$key};
202 scalar (keys (%have_keys)) == 0
203 or die "$name has superfluous key(s) " . join (', ', keys (%have_keys));
205 push (@types, $type);
210 # Returns the C type of the given type as a string designed to be
211 # prepended to a variable name to produce a declaration. (That won't
212 # work in general but it works well enough for our types.)
215 my ($c_type) = $type->{C_TYPE};
216 defined $c_type or die;
218 # Append a space unless (typically) $c_type ends in `*'.
219 $c_type .= ' ' if $c_type =~ /\w$/;
226 # Parses the entire input.
228 # Initializes %ops, @funcs, @opers.
232 while ($toktype ne 'eof') {
235 $op{OPTIMIZABLE} = 1;
236 $op{UNIMPLEMENTED} = 0;
240 if (match ('extension')) {
242 } elsif (match ('no_opt')) {
243 $op{OPTIMIZABLE} = 0;
244 } elsif (match ('absorb_miss')) {
245 $op{ABSORB_MISS} = 1;
246 } elsif (match ('perm_only')) {
253 $op{RETURNS} = parse_type () || $type{NUMBER};
254 die "$op{RETURNS} is not a valid return type"
255 if !any ($op{RETURNS}, @type{qw (NUMBER STRING BOOLEAN)});
257 $op{CATEGORY} = $token;
258 if (!any ($op{CATEGORY}, qw (operator function))) {
259 die "`operator' or `function' expected at `$token'";
263 my ($name) = force ("id");
265 die "function name may not contain underscore"
266 if $op{CATEGORY} eq 'function' && $name =~ /_/;
267 die "operator name may not contain period"
268 if $op{CATEGORY} eq 'operator' && $name =~ /\./;
270 if (my ($prefix, $suffix) = $name =~ /^(.*)\.(\d+)$/) {
272 $op{MIN_VALID} = $suffix;
273 $op{ABSORB_MISS} = 1;
279 while (!match (')')) {
280 my ($arg) = parse_arg ();
281 push (@{$op{ARGS}}, $arg);
282 if (defined ($arg->{IDX})) {
284 die "array must be last argument";
292 for my $arg (@{$op{ARGS}}) {
293 next if !defined $arg->{CONDITION};
294 my ($any_arg) = join ('|', map ($_->{NAME}, @{$op{ARGS}}));
295 $arg->{CONDITION} =~ s/\b($any_arg)\b/arg_$1/g;
298 my ($opname) = "OP_$op{NAME}";
300 if ($op{CATEGORY} eq 'function') {
301 my ($mangle) = join ('', map ($_->{TYPE}{MANGLE}, @{$op{ARGS}}));
302 $op{MANGLE} = $mangle;
303 $opname .= "_$mangle";
305 $op{OPNAME} = $opname;
307 if ($op{MIN_VALID}) {
308 my ($array_arg) = array_arg (\%op);
309 die "can't have minimum valid count without array arg"
310 if !defined $array_arg;
311 die "minimum valid count allowed only with double array"
312 if $array_arg->{TYPE} ne $type{NUMBER};
313 die "can't have minimum valid count if array has multiplication factor"
314 if $array_arg->{TIMES} != 1;
317 while ($toktype eq 'id') {
318 my ($type) = parse_type () or die "parse error";
319 die "`$type->{NAME}' is not allowed as auxiliary data"
320 unless $type->{ROLE} eq 'leaf' || $type->{ROLE} eq 'fixed';
321 my ($name) = force ("id");
322 push (@{$op{AUX}}, {TYPE => $type, NAME => $name});
326 if ($op{OPTIMIZABLE}) {
327 die "random variate functions must be marked `no_opt'"
328 if $op{NAME} =~ /^RV\./;
329 for my $aux (@{$op{AUX}}) {
330 if (any ($aux->{TYPE}, @type{qw (CASE CASE_IDX)})) {
331 die "operators with $aux->{TYPE} aux data must be "
337 if ($op{RETURNS} eq $type{STRING} && !defined ($op{ABSORB_MISS})) {
339 for my $arg (@{$op{ARGS}}) {
340 if (any ($arg->{TYPE}, @type{qw (NUMBER BOOLEAN)})) {
341 die "$op{NAME} returns string and has double or bool "
342 . "argument, but is not marked ABSORB_MISS";
344 if (defined $arg->{CONDITION}) {
345 die "$op{NAME} returns string but has argument with condition";
350 if ($toktype eq 'block') {
351 $op{BLOCK} = force ('block');
352 } elsif ($toktype eq 'expression') {
353 if ($token eq 'unimplemented') {
354 $op{UNIMPLEMENTED} = 1;
356 $op{EXPRESSION} = $token;
360 die "block or expression expected";
363 die "duplicate operation name $opname" if defined $ops{$opname};
364 $ops{$opname} = \%op;
365 if ($op{CATEGORY} eq 'function') {
366 push (@funcs, $opname);
368 push (@opers, $opname);
373 @funcs = sort {$ops{$a}->{NAME} cmp $ops{$b}->{NAME}
375 $ops{$a}->{OPNAME} cmp $ops{$b}->{OPNAME}}
377 @opers = sort {$ops{$a}->{NAME} cmp $ops{$b}->{NAME}} @opers;
378 our (@order) = (@funcs, @opers);
381 # Reads the next token into $token, $toktype.
385 return if defined ($toktype) && $toktype eq 'eof';
386 $toktype = 'id', $token = $1, return
387 if $line =~ /\G([a-zA-Z_][a-zA-Z_.0-9]*)/gc;
388 $toktype = 'int', $token = $1, return if $line =~ /\G([0-9]+)/gc;
389 $toktype = 'punct', $token = $1, return if $line =~ /\G([][(),*;.])/gc;
390 if ($line =~ /\G=/gc) {
391 $toktype = "expression";
393 $token = accumulate_balanced (';');
394 } elsif ($line =~ /\G\{/gc) {
396 $token = accumulate_balanced ('}');
399 die "bad character `" . substr ($line, pos $line, 1) . "' in input";
403 # Skip whitespace, then return the remainder of the line.
406 die "unexpected end of file" if !defined ($line);
409 last if pos ($line) < length ($line);
411 $token = $toktype = 'eof', return if !defined ($line);
413 return substr ($line, pos ($line));
416 # accumulate_balanced($chars)
418 # Accumulates input until a character in $chars is encountered, except
419 # that balanced pairs of (), [], or {} cause $chars to be ignored.
421 # Returns the input read.
422 sub accumulate_balanced {
428 my ($start) = pos ($line);
429 if ($line =~ /\G([^][(){};,]*)([][(){};,])/gc) {
430 $s .= substr ($line, $start, pos ($line) - $start - 1)
431 if pos ($line) > $start;
432 my ($last) = substr ($line, pos ($line) - 1, 1);
433 if ($last =~ /[[({]/) {
436 } elsif ($last =~ /[])}]/) {
440 } elsif (index ($end, $last) >= 0) {
443 die "unbalanced parentheses";
445 } elsif (index ($end, $last) >= 0) {
452 $s .= substr ($line, pos ($line)) . "\n";
458 # Reads the next line from INPUT into $line.
462 if (defined ($line)) {
469 # If the current token is an identifier that names a type,
470 # returns the type and skips to the next token.
471 # Otherwise, returns undef.
473 if ($toktype eq 'id') {
474 foreach my $type (values (%type)) {
475 get_token (), return $type
476 if defined ($type->{NAME}) && $type->{NAME} eq $token;
484 # Makes sure that $toktype equals $type, reads the next token, and
485 # returns the previous $token.
488 die "parse error at `$token' expecting $type"
489 if $type ne $toktype;
497 # If $token equals $tok, reads the next token and returns true.
498 # Otherwise, returns false.
501 if ($token eq $tok) {
511 # If $token equals $tok, reads the next token.
512 # Otherwise, flags an error in the input.
515 die "parse error at `$token' expecting `$tok'" if !match ($tok);
518 # Parses and returns a function argument.
521 $arg{TYPE} = parse_type () || $type{NUMBER};
522 die "argument name expected at `$token'" if $toktype ne 'id';
525 if (lookahead () =~ /^[[,)]/) {
528 die "only double and string arrays supported"
529 if !any ($arg{TYPE}, @type{qw (NUMBER STRING)});
530 $arg{IDX} = force ('id');
532 $arg{TIMES} = force ('int');
533 die "multiplication factor must be positive"
541 $arg{CONDITION} = $arg{NAME} . ' ' . accumulate_balanced (',)');
551 # Prints the output file header.
555 Generated from $input_file by generate.pl.
561 # Prints the output file trailer.
576 # any($target, @list)
578 # Returns true if $target appears in @list,
581 $_ eq $_[0] and return 1 foreach @_[1...$#_];
585 # make_sysmis_decl($op, $min_valid_src)
587 # Returns a declaration for a boolean variable called `force_sysmis',
588 # which will be true when operation $op should be system-missing.
589 # Returns undef if there are no such circumstances.
591 # If $op has a minimum number of valid arguments, $min_valid_src
592 # should be an an expression that evaluates to the minimum number of
593 # valid arguments for $op.
594 sub make_sysmis_decl {
595 my ($op, $min_valid_src) = @_;
597 if (!$op->{ABSORB_MISS}) {
598 for my $arg (@{$op->{ARGS}}) {
599 my ($arg_name) = "arg_$arg->{NAME}";
600 if (!defined $arg->{IDX}) {
601 if (any ($arg->{TYPE}, @type{qw (NUMBER BOOLEAN)})) {
602 push (@sysmis_cond, "!is_valid ($arg_name)");
604 } elsif ($arg->{TYPE} eq $type{NUMBER}) {
605 my ($a) = "$arg_name";
606 my ($n) = "arg_$arg->{IDX}";
607 push (@sysmis_cond, "count_valid ($a, $n) < $n");
610 } elsif (defined $op->{MIN_VALID}) {
611 my ($args) = $op->{ARGS};
612 my ($arg) = ${$args}[$#{$args}];
613 my ($a) = "arg_$arg->{NAME}";
614 my ($n) = "arg_$arg->{IDX}";
615 push (@sysmis_cond, "count_valid ($a, $n) < $min_valid_src");
617 for my $arg (@{$op->{ARGS}}) {
618 push (@sysmis_cond, "!($arg->{CONDITION})")
619 if defined $arg->{CONDITION};
621 return "bool force_sysmis = " . join (' || ', @sysmis_cond)
628 # If $op has an array argument, return it.
629 # Otherwise, returns undef.
632 my ($args) = $op->{ARGS};
634 my ($last_arg) = $args->[@$args - 1];
635 return $last_arg if defined $last_arg->{IDX};