]> git.draconx.ca Git - cdecl99.git/blob - src/gen-specstr.awk
Generate specifier strings directly from cdecl.h
[cdecl99.git] / src / gen-specstr.awk
1 #!/bin/awk -f
2 #
3 # Copyright © 2021 Nick Bowler
4 #
5 # Generate a function to return the C keyword corresponding to a specifier
6 # type as a string, for internal use by the output routines.
7 #
8 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
9 # This is free software: you are free to do what the fuck you want to.
10 # There is NO WARRANTY, to the extent permitted by law.
11
12 END {
13   print "/*"
14   if (FILENAME) {
15     print " * Automatically generated by gen-specstr.awk from " FILENAME
16   } else {
17     print " * Automatically generated by gen-specstr.awk"
18   }
19   print " * Do not edit."
20   print " */"
21 }
22
23 BEGIN {
24   kinds["TYPE"] = kinds["STOR"] = kinds["QUAL"] = kinds["FUNC"] = 1
25   underscore["BOOL"] = underscore["COMPLEX"] = underscore["IMAGINARY"] = 1
26 }
27
28 $1 ~ /^CDECL_/ {
29   sub(/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ_].*/, "", $1)
30
31   split($1, parts, "_")
32   if (parts[2] in kinds) {
33     if (parts[3] == "IDENT") {
34       s = ""
35     } else if (parts[3] in underscore) {
36       s = "_" substr(parts[3], 1, 1) tolower(substr(parts[3], 2))
37     } else {
38       s = tolower(parts[3])
39     }
40     specs[$1] = s
41   }
42 }
43
44 END {
45   print "static inline const char *spec_string(unsigned type)\n{"
46   print "\tswitch (type) {"
47
48   for (s in specs) {
49     print "\tcase " s ": return \"" specs[s] "\";"
50   }
51
52   print "\t}"
53   print "\n\tassert(0);"
54   print "}"
55 }