aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-02-11 15:31:30 -0500
committerBruce Hill <bruce@bruce-hill.com>2024-02-11 15:31:30 -0500
commit37a0493b59cfa5dfb21c1f5c39b13b301770e126 (patch)
tree5dca24bf8e9f226f72bb90702e8d33e3a8b6aa1e
parent23898b76180b3bbcc7e482492f2af0c59e3efe12 (diff)
String literals as cords
-rw-r--r--ast.c2
-rw-r--r--ast.h2
-rw-r--r--compile.c16
-rw-r--r--nextlang.h4
-rw-r--r--parse.c4
5 files changed, 15 insertions, 13 deletions
diff --git a/ast.c b/ast.c
index 2623a46b..17f46f2e 100644
--- a/ast.c
+++ b/ast.c
@@ -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))
diff --git a/ast.h b/ast.h
index 157d7af9..3e3a6327 100644
--- a/ast.h
+++ b/ast.h
@@ -140,7 +140,7 @@ struct ast_s {
char c;
} Char;
struct {
- const char *str;
+ CORD cord;
} StringLiteral;
struct {
ast_list_t *children;
diff --git a/compile.c b/compile.c
index 90741253..4444a135 100644
--- a/compile.c
+++ b/compile.c
@@ -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;
}
}
diff --git a/nextlang.h b/nextlang.h
index 8a87e777..8f74995f 100644
--- a/nextlang.h
+++ b/nextlang.h
@@ -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})
diff --git a/parse.c b/parse.c
index c24cd071..9abbd6e6 100644
--- a/parse.c
+++ b/parse.c
@@ -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;
}