]> git.draconx.ca Git - cdecl99.git/blobdiff - src/gen-specstr.awk
Generate specifier strings directly from cdecl.h
[cdecl99.git] / src / gen-specstr.awk
diff --git a/src/gen-specstr.awk b/src/gen-specstr.awk
new file mode 100755 (executable)
index 0000000..611b2a8
--- /dev/null
@@ -0,0 +1,55 @@
+#!/bin/awk -f
+#
+# Copyright © 2021 Nick Bowler
+#
+# Generate a function to return the C keyword corresponding to a specifier
+# type as a string, for internal use by the output routines.
+#
+# License WTFPL2: Do What The Fuck You Want To Public License, version 2.
+# This is free software: you are free to do what the fuck you want to.
+# There is NO WARRANTY, to the extent permitted by law.
+
+END {
+  print "/*"
+  if (FILENAME) {
+    print " * Automatically generated by gen-specstr.awk from " FILENAME
+  } else {
+    print " * Automatically generated by gen-specstr.awk"
+  }
+  print " * Do not edit."
+  print " */"
+}
+
+BEGIN {
+  kinds["TYPE"] = kinds["STOR"] = kinds["QUAL"] = kinds["FUNC"] = 1
+  underscore["BOOL"] = underscore["COMPLEX"] = underscore["IMAGINARY"] = 1
+}
+
+$1 ~ /^CDECL_/ {
+  sub(/[^ABCDEFGHIJKLMNOPQRSTUVWXYZ_].*/, "", $1)
+
+  split($1, parts, "_")
+  if (parts[2] in kinds) {
+    if (parts[3] == "IDENT") {
+      s = ""
+    } else if (parts[3] in underscore) {
+      s = "_" substr(parts[3], 1, 1) tolower(substr(parts[3], 2))
+    } else {
+      s = tolower(parts[3])
+    }
+    specs[$1] = s
+  }
+}
+
+END {
+  print "static inline const char *spec_string(unsigned type)\n{"
+  print "\tswitch (type) {"
+
+  for (s in specs) {
+    print "\tcase " s ": return \"" specs[s] "\";"
+  }
+
+  print "\t}"
+  print "\n\tassert(0);"
+  print "}"
+}