From d3113b4c382b23dc8a822fa8b27d2b86037079da Mon Sep 17 00:00:00 2001 From: Finn Barber Date: Sun, 15 Sep 2024 11:34:01 +0100 Subject: [PATCH] Fix symbols with characters not allowed in C names --- src/cognac.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/cognac.c b/src/cognac.c index 657ad58..15b550c 100755 --- a/src/cognac.c +++ b/src/cognac.c @@ -38,6 +38,8 @@ bool debug = false; bool gc_test = false; bool noinline = false; +char* sanitize(char*); + static bool is_prelude(module_t* mod) { return mod == &prelude1 || mod == &prelude2; @@ -724,7 +726,7 @@ const char* c_literal(lit_t* literal) char s[strlen(literal->string) + strlen(prefix) + 1]; s[0] = '\0'; strcat(s, prefix); - strcat(s, literal->string); + strcat(s, sanitize((char*)literal->string)); return strdup(s); } return literal->string; // TODO @@ -899,7 +901,7 @@ void to_c(module_t* mod) { for (int i = 0 ; i < sizeof(builtin_symbols) / sizeof(builtin_symbols[0]) ; ++i) if (!strcmp(builtin_symbols[i], syms->text)) goto next; - fprintf(c_source, "const SYMBOL SYM%s = \"%s\";\n", syms->text, syms->text); + fprintf(c_source, "const SYMBOL SYM%s = \"%s\";\n", sanitize(syms->text), syms->text); next:; } if (mod->symbols) fputc('\n', c_source);