aboutsummaryrefslogtreecommitdiff
path: root/src/naming.c
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2025-08-17 15:27:34 -0400
committerBruce Hill <bruce@bruce-hill.com>2025-08-17 15:27:34 -0400
commit4188d627c6869cb6b443ae3b6dece40486f4a039 (patch)
tree018d309e84cd752c93a3b55770fcd979e0d35c54 /src/naming.c
parent82466a2f9507ad6991c5a275d2be97691ef58db6 (diff)
Bugfix: added support for Tomo identifiers that are C keywords
Diffstat (limited to 'src/naming.c')
-rw-r--r--src/naming.c72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/naming.c b/src/naming.c
new file mode 100644
index 00000000..abe87569
--- /dev/null
+++ b/src/naming.c
@@ -0,0 +1,72 @@
+// Logic for converting user's Tomo names into valid C identifiers
+
+#include <ctype.h>
+#include <string.h>
+#include <sys/stat.h>
+
+#include "environment.h"
+#include "stdlib/paths.h"
+#include "stdlib/print.h"
+#include "stdlib/text.h"
+
+static const char *c_keywords[] = { // Maintain sorted order:
+ "_Alignas", "_Alignof", "_Atomic", "_BitInt", "_Bool", "_Complex", "_Decimal128", "_Decimal32", "_Decimal64", "_Generic",
+ "_Imaginary", "_Noreturn", "_Static_assert", "_Thread_local",
+ "alignas", "alignof", "auto", "bool", "break", "case", "char", "const", "constexpr", "continue", "default", "do", "double",
+ "else", "enum", "extern", "false", "float", "for", "goto", "if", "inline", "int", "long", "nullptr", "register", "restrict",
+ "return", "short", "signed", "sizeof", "static", "static_assert", "struct", "switch", "thread_local", "true", "typedef",
+ "typeof", "typeof_unqual", "union", "unsigned", "void", "volatile", "while",
+};
+
+static CONSTFUNC bool is_keyword(const char *word, size_t len) {
+ int64_t lo = 0, hi = sizeof(c_keywords)/sizeof(c_keywords[0])-1;
+ while (lo <= hi) {
+ int64_t mid = (lo + hi) / 2;
+ int32_t cmp = strncmp(word, c_keywords[mid], len);
+ if (cmp == 0)
+ return true;
+ else if (cmp > 0)
+ lo = mid + 1;
+ else if (cmp < 0)
+ hi = mid - 1;
+ }
+ return false;
+}
+
+public Text_t valid_c_name(const char *name)
+{
+ size_t len = strlen(name);
+ size_t trailing_underscores = 0;
+ while (trailing_underscores < len && name[len-1-trailing_underscores] == '_')
+ trailing_underscores += 1;
+ if (is_keyword(name, len-trailing_underscores)) {
+ return Texts(Text$from_str(name), Text("_"));
+ }
+ return Text$from_str(name);
+}
+
+public Text_t CONSTFUNC namespace_name(env_t *env, namespace_t *ns, Text_t name)
+{
+ for (; ns; ns = ns->parent)
+ name = Texts(ns->name, "$", name);
+ if (env->id_suffix.length > 0)
+ name = Texts(name, env->id_suffix);
+ return name;
+}
+
+public Text_t get_id_suffix(const char *filename)
+{
+ assert(filename);
+ Path_t path = Path$from_str(filename);
+ Path_t build_dir = Path$sibling(path, Text(".build"));
+ if (mkdir(Path$as_c_string(build_dir), 0755) != 0) {
+ if (!Path$is_directory(build_dir, true))
+ err(1, "Could not make .build directory");
+ }
+ Path_t id_file = Path$child(build_dir, Texts(Path$base_name(path), Text$from_str(".id")));
+ OptionalText_t id = Path$read(id_file);
+ if (id.length < 0) err(1, "Could not read ID file: ", id_file);
+ return Texts(Text("$"), id);
+}
+
+// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0