d9d3b3c30507eb77798ed57bdf254ce25dd39e7a
[pspp-builds.git] / src / expressions / operations.h.pl
1 do 'generate.pl';
2
3 sub generate_output {
4     print "#include <stdlib.h>\n";
5     print "#include \"bool.h\"\n\n";
6
7     print "typedef enum";
8     print "  {\n";
9     my (@atoms);
10     foreach my $type (@types) {
11         next if $type->{ROLE} eq 'fixed';
12         push (@atoms, "OP_$type->{NAME}");
13     }
14     print_operations ('atom', 1, \@atoms);
15     print_operations ('function', "OP_atom_last + 1", \@funcs);
16     print_operations ('operator', "OP_function_last + 1", \@opers);
17     print_range ("OP_composite", "OP_function_first", "OP_operator_last");
18     print ",\n\n";
19     print_range ("OP", "OP_atom_first", "OP_composite_last");
20     print "\n  }\n";
21     print "operation_type, atom_type;\n";
22
23     print_predicate ('is_operation', 'OP');
24     print_predicate ("is_$_", "OP_$_")
25         foreach qw (atom composite function operator);
26 }
27
28 sub print_operations {
29     my ($type, $first, $names) = @_;
30     print "    /* \u$type types. */\n";
31     print "    $names->[0] = $first,\n";
32     print "    $_,\n" foreach @$names[1...$#{$names}];
33     print_range ("OP_$type", $names->[0], $names->[$#{$names}]);
34     print ",\n\n";
35 }
36
37 sub print_range {
38     my ($prefix, $first, $last) = @_;
39     print "    ${prefix}_first = $first,\n";
40     print "    ${prefix}_last = $last,\n";
41     print "    ${prefix}_cnt = ${prefix}_last - ${prefix}_first + 1";
42 }
43
44 sub print_predicate {
45     my ($function, $category) = @_;
46     my ($assertion) = "";
47
48     print "\nstatic inline bool\n";
49     print "$function (operation_type op)\n";
50     print "{\n";
51     print "  assert (is_operation (op));\n" if $function ne 'is_operation';
52     print "  return op >= ${category}_first && op <= ${category}_last;\n";
53     print "}\n";
54 }