diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-09-01 17:54:33 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-09-01 17:54:33 -0400 |
commit | ca34590d9968d36feb1061b9e275d0f85f09863a (patch) | |
tree | 3c74ad17a4b9bea54a3b29000f40cffd705fc1bf | |
parent | cc7d494226f41d76208bd2a0613a26435247cc87 (diff) |
trying to get unicode idents to work
-rw-r--r-- | base_cgen.c | 4 | ||||
-rw-r--r-- | identifiers.c | 35 | ||||
-rw-r--r-- | out.c | 6 | ||||
-rw-r--r-- | out.h | 4 | ||||
-rw-r--r-- | test.toc | 5 | ||||
-rw-r--r-- | util/err.c | 1 |
6 files changed, 43 insertions, 12 deletions
diff --git a/base_cgen.c b/base_cgen.c index 125146b..b13012c 100644 --- a/base_cgen.c +++ b/base_cgen.c @@ -99,7 +99,9 @@ static bool cgen_ident(CGenerator *g, Identifier i, Location *where) { } } cgen_indent(g); - fprint_ident(cgen_writing_to(g), i); + fprint_ident(stdout, i); + printf("\n"); + fprint_ident_ascii(cgen_writing_to(g), i); return true; } diff --git a/identifiers.c b/identifiers.c index b7a2b3c..93c7eb6 100644 --- a/identifiers.c +++ b/identifiers.c @@ -72,6 +72,17 @@ static void idents_create(Identifiers *ids) { ids->root = ident_new(ids, NULL, 0); /* create root tree */ } +#if CHAR_MIN < 0 +#define ident_char_to_uchar(c) ((c) < 0 ? (256 + (c)) : (c)) +#else +#define ident_char_to_uchar(c) (c) +#endif + +#if CHAR_MIN < 0 +#define ident_uchar_to_char(c) ((c) > 127 ? ((c) - 256) : (c)) +#else +#define ident_uchar_to_char(c) (c) +#endif /* moves s to the char after the identifier */ /* inserts if does not exist. reads until non-ident char is found. */ @@ -82,10 +93,11 @@ static Identifier ident_insert(Identifiers *ids, char **s) { if (!isident(**s)) { return tree; } - int c = (**s) - CHAR_MIN; + int c = ident_char_to_uchar(**s); assert(c >= 0 && c <= 255); unsigned char c_low = (unsigned char)(c & 0xf); unsigned char c_high = (unsigned char)(c >> 4); + printf("inserting %d as %d = %d, %d\n", **s, c, c_low, c_high); if (!tree->children[c_low]) { tree->children[c_low] = ident_new(ids, tree, c_low); } @@ -106,10 +118,27 @@ static void fprint_ident(FILE *out, Identifier id) { fprint_ident(out, id->parent->parent); /* to go up one character, we need to go to the grandparent */ int c_low = id->parent->index_in_parent; int c_high = id->index_in_parent; - int c = c_low + (c_high << 4) + CHAR_MIN; + int c = ident_uchar_to_char(c_low + (c_high << 4)); fputc(c, out); } +static void fprint_ident_ascii(FILE *out, Identifier id) { + assert(id); + if (id->parent == NULL) return; /* at root */ + fprint_ident(out, id->parent->parent); /* to go up one character, we need to go to the grandparent */ + int c_low = id->parent->index_in_parent; + int c_high = id->index_in_parent; + int c = c_low + (c_high << 4); + printf("Got %d as %d, %d\n", c, c_low, c_high); + if (c > 127) { + puts("x thing"); + fprintf(out, "x__%x",c); + } else { + printf("single char %d\n",c); + fputc(ident_uchar_to_char(c), out); + } +} + /* NULL = no such identifier */ static Identifier ident_get(Identifiers *ids, const char *s) { IdentTree *tree = ids->root; @@ -137,7 +166,7 @@ static char *ident_to_str(Identifier i) { str--; unsigned char c_high = i->index_in_parent; unsigned char c_low = i->parent->index_in_parent; - char c = (char)(CHAR_MIN + (int)(c_low + (c_high << 4))); + char c = (char)ident_uchar_to_char((int)c_low + ((int)c_high << 4)); *str = c; i = i->parent->parent; /* go to grandparent (prev char) */ } @@ -1,10 +1,10 @@ #include "out.h" /* toc */ -void foo(void) { +void Äx__a8(void) { } -void main__(void) { - foo(); +void main(void) { + Äx__a8(); } int main(void) { @@ -1,4 +1,4 @@ #include <stddef.h> #include <stdint.h> -void foo(void); -void main__(void); +void Äx__a8(void); +void main(void); @@ -1,5 +1,6 @@ -foo @= fn() { +Ĩ @= fn() { }; main @= fn() { - foo(); + Ĩ(); + }; @@ -52,7 +52,6 @@ static void warn_print_header_(LineNo line) { } static void err_print_footer_(const char *context) { - err_fprint("\n\there --> "); const char *end = strchr(context, '\n'); int has_newline = end != NULL; if (!has_newline) |