]> git.draconx.ca Git - cdecl99.git/blob - t/gen-typegen.awk
Port to use getline.h from dxcommon.
[cdecl99.git] / t / gen-typegen.awk
1 #!/bin/awk -f
2 #
3 # Copyright © 2021-2022, 2024 Nick Bowler
4 #
5 # Generate a mapping from a (random) integer to a list of type specifiers
6 # represented by struct cdecl_declspec.  Used internally by declgen to
7 # produce random valid declarations.
8 #
9 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
10 # This is free software: you are free to do what the fuck you want to.
11 # There is NO WARRANTY, to the extent permitted by law.
12
13 END {
14   print "/*"
15   if (FILENAME) {
16     print " * Automatically generated by gen-typegen.awk from " FILENAME
17   } else {
18     print " * Automatically generated by gen-typegen.awk"
19   }
20   print " * Do not edit."
21   print " */"
22 }
23
24 BEGIN {
25   count = 0
26 }
27
28 $0 ~ /^[abcdefghijklmnopqrstuvwxyz_]/ {
29   for (i = 1; i <= NF; i++) {
30     sub(/_/, "", $i)
31     $i = "CDECL_TYPE_" toupper($i)
32   }
33   specs[count++] = $0
34 }
35
36 END {
37   print "static inline struct cdecl_declspec *";
38   print "gen_raw_typespec_(unsigned type, struct cdecl_declspec *next)\n{";
39   print "\tstruct cdecl_declspec *s = malloc_nofail(sizeof *s);";
40   print "\ts->type = type;";
41   print "\ts->next = next;";
42   print "\ts->ident = 0;";
43   print "\treturn s;\n}\n"
44
45   print "static inline struct cdecl_declspec *"
46   print "gen_raw_typespecs(unsigned rngval)\n{"
47   print "\tswitch (rngval) {"
48
49   for (i = 0; i < count; i++) {
50     print "\tcase " i ":";
51
52     n = split(specs[i], parts);
53     for (j = 1; j <= n; j++) {
54       prefix = j == 1 ? "return" : "";
55
56       printf "\t\t%6s gen_raw_typespec_(%s,\n", prefix, parts[j];
57     }
58     printf "\t\t%25sNULL%s;\n", "", substr("))))))))", 1, n);
59   }
60   print "\tdefault:\n\t\tassert(0);\n\t}"
61   print "}\n"
62   print "enum { GEN_TOTAL_TYPES = " count " };"
63 }