]> git.draconx.ca Git - cdecl99.git/blob - test/gen-typegen.awk
Replace typegen.sh with a new and improved script.
[cdecl99.git] / test / gen-typegen.awk
1 #!/bin/awk -f
2 #
3 # Copyright © 2021 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 "\t*s = (struct cdecl_declspec) { .next = next, .type = type };"
41   print "\treturn s;\n}\n"
42
43   print "static inline struct cdecl_declspec *"
44   print "gen_raw_typespecs(unsigned rngval)\n{"
45   print "\tswitch (rngval) {"
46
47   for (i = 0; i < count; i++) {
48     print "\tcase " i ":"
49     $0 = specs[i]
50
51     for (j = 1; j <= NF; j++) {
52       prefix = j == 1 ? "return" : "";
53
54       printf "\t\t%6s gen_raw_typespec_(%s,\n", prefix, $j
55     }
56     printf "\t\t%25sNULL%.*s;\n", "", NF, "))))))))"
57   }
58   print "\tdefault:\n\t\tassert(0);\n\t}"
59   print "}\n"
60   print "enum { GEN_TOTAL_TYPES = " count " };"
61 }