diff options
| author | Bruce Hill <bruce@bruce-hill.com> | 2024-02-11 15:31:30 -0500 |
|---|---|---|
| committer | Bruce Hill <bruce@bruce-hill.com> | 2024-02-11 15:31:30 -0500 |
| commit | 37a0493b59cfa5dfb21c1f5c39b13b301770e126 (patch) | |
| tree | 5dca24bf8e9f226f72bb90702e8d33e3a8b6aa1e | |
| parent | 23898b76180b3bbcc7e482492f2af0c59e3efe12 (diff) | |
String literals as cords
| -rw-r--r-- | ast.c | 2 | ||||
| -rw-r--r-- | ast.h | 2 | ||||
| -rw-r--r-- | compile.c | 16 | ||||
| -rw-r--r-- | nextlang.h | 4 | ||||
| -rw-r--r-- | parse.c | 4 |
5 files changed, 15 insertions, 13 deletions
@@ -90,7 +90,7 @@ CORD ast_to_cord(ast_t *ast) T(Int, "(\x1b[35m%ld\x1b[m, precision=\x1b[35m%ld\x1b[m)", data.i, data.precision) T(Num, "(\x1b[35m%ld\x1b[m, precision=\x1b[35m%ld\x1b[m)", data.n, data.precision) T(Char, "(\x1b[35m'%c'\x1b[m)", data.c) - T(StringLiteral, "\x1b[35m\"%s\"\x1b[m", data.str) + T(StringLiteral, "\x1b[35m\"%r\"\x1b[m", data.cord) T(StringJoin, "(%r)", ast_list_to_cord(data.children)) T(Declare, "(var=%s, value=%r)", ast_to_cord(data.var), ast_to_cord(data.value)) T(Assign, "(targets=%r, values=%r)", ast_list_to_cord(data.targets), ast_list_to_cord(data.values)) @@ -140,7 +140,7 @@ struct ast_s { char c; } Char; struct { - const char *str; + CORD cord; } StringLiteral; struct { ast_list_t *children; @@ -99,12 +99,14 @@ CORD compile(ast_t *ast) errx(1, "unimplemented binop"); } case StringLiteral: { - const char *str = Match(ast, StringLiteral)->str; - if (!str || strlen(str) == 0) + CORD literal = Match(ast, StringLiteral)->cord; + if (literal == CORD_EMPTY) return "(CORD)CORD_EMPTY"; CORD code = "\""; - for (; *str; ++str) { - switch (*str) { + CORD_pos i; + CORD_FOR(i, literal) { + int c = CORD_pos_fetch(i); + switch (c) { case '\\': code = CORD_cat(code, "\\\\"); break; case '"': code = CORD_cat(code, "\\\""); break; case '\a': code = CORD_cat(code, "\\a"); break; @@ -114,10 +116,10 @@ CORD compile(ast_t *ast) case '\t': code = CORD_cat(code, "\\t"); break; case '\v': code = CORD_cat(code, "\\v"); break; default: { - if (isprint(*str)) - code = CORD_cat_char(code, *str); + if (isprint(c)) + code = CORD_cat_char(code, c); else - CORD_sprintf(&code, "%r\\x%02X", *str); + CORD_sprintf(&code, "%r\\x%02X", c); break; } } @@ -37,8 +37,8 @@ int32_t: CORD_asprintf("%d", x), int64_t: CORD_asprintf("%ld", x), \ double: CORD_asprintf("%g", x), float: CORD_asprintf("%g", x), \ CORD: x, \ - char*: x, \ - char: CORD_cat_char(NULL, x), \ + char*: ({ char *__str = x; __str && __str[0] ? __str : CORD_EMPTY;}), \ + char: CORD_cat_char(CORD_EMPTY, x), \ default: "???") #define __heap(x) (__typeof(x)*)memcpy(GC_MALLOC(sizeof(x)), (__typeof(x)[1]){x}, sizeof(x)) #define __stack(x) (&(__typeof(x)){x}) @@ -915,7 +915,7 @@ PARSER(parse_string) { for (; pos < ctx->file->text + ctx->file->len && *pos != close_quote; ++pos) { if (*pos == start_interp) { if (chunk) { - ast_t *literal = NewAST(ctx->file, chunk_start, pos, StringLiteral, .str=CORD_to_const_char_star(chunk)); + ast_t *literal = NewAST(ctx->file, chunk_start, pos, StringLiteral, .cord=chunk); chunks = new(ast_list_t, .ast=literal, .next=chunks); chunk = NULL; } @@ -958,7 +958,7 @@ PARSER(parse_string) { } if (chunk) { - ast_t *literal = NewAST(ctx->file, chunk_start, pos, StringLiteral, .str=CORD_to_const_char_star(chunk)); + ast_t *literal = NewAST(ctx->file, chunk_start, pos, StringLiteral, .cord=chunk); chunks = new(ast_list_t, .ast=literal, .next=chunks); chunk = NULL; } |
