From 34c562e88e4c7b779bc3374019c0124f68d9125f Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Fri, 13 Sep 2024 14:23:24 -0400 Subject: [PATCH] Move cord helper functions into their own file --- Makefile | 2 +- ast.c | 1 + ast.h | 1 + builtins/array.h | 1 - builtins/bool.c | 1 - builtins/bool.h | 1 - builtins/c_string.c | 1 - builtins/c_string.h | 1 - builtins/channel.c | 1 - builtins/channel.h | 1 - builtins/files.c | 1 + builtins/functions.h | 1 - builtins/integers.c | 1 - builtins/integers.h | 1 - builtins/memory.h | 1 - builtins/nums.c | 1 - builtins/nums.h | 1 - builtins/optionals.h | 1 - builtins/path.h | 1 - builtins/pointer.c | 1 - builtins/pointer.h | 1 - builtins/shell.h | 1 - builtins/thread.h | 1 - builtins/tomo.h | 1 - builtins/util.c | 63 --------------------------------------- builtins/util.h | 8 ----- compile.c | 1 + cordhelpers.c | 70 ++++++++++++++++++++++++++++++++++++++++++++ cordhelpers.h | 14 +++++++++ enums.c | 1 + environment.c | 1 + parse.c | 1 + structs.c | 1 + tomo.c | 1 + typecheck.c | 1 + types.c | 1 + 36 files changed, 96 insertions(+), 92 deletions(-) create mode 100644 cordhelpers.c create mode 100644 cordhelpers.h diff --git a/Makefile b/Makefile index 1ec338e..820de54 100644 --- a/Makefile +++ b/Makefile @@ -36,7 +36,7 @@ TESTS=$(patsubst %.tm,%.tm.testresult,$(wildcard test/*.tm)) all: libtomo.so tomo -tomo: tomo.o $(BUILTIN_OBJS) ast.o parse.o environment.o types.o typecheck.o structs.o enums.o compile.o repl.o +tomo: tomo.o $(BUILTIN_OBJS) ast.o parse.o environment.o types.o typecheck.o structs.o enums.o compile.o repl.o cordhelpers.o @echo $(CC) $(CFLAGS_PLACEHOLDER) $(LDFLAGS) $^ $(LDLIBS) -o $@ @$(CC) $(CFLAGS) $(CWARN) $(LDFLAGS) $^ $(LDLIBS) -o $@ diff --git a/ast.c b/ast.c index dc2ff13..aa8b1b5 100644 --- a/ast.c +++ b/ast.c @@ -7,6 +7,7 @@ #include "ast.h" #include "builtins/integers.h" #include "builtins/text.h" +#include "cordhelpers.h" static const char *OP_NAMES[] = { [BINOP_UNKNOWN]="unknown", diff --git a/ast.h b/ast.h index 868b823..198e3d8 100644 --- a/ast.h +++ b/ast.h @@ -2,6 +2,7 @@ // Logic defining ASTs (abstract syntax trees) to represent code +#include #include #include #include diff --git a/builtins/array.h b/builtins/array.h index 91272f9..478cd77 100644 --- a/builtins/array.h +++ b/builtins/array.h @@ -3,7 +3,6 @@ // Functions that operate on arrays #include -#include #include "datatypes.h" #include "functions.h" diff --git a/builtins/bool.c b/builtins/bool.c index 7759cf3..14406c5 100644 --- a/builtins/bool.c +++ b/builtins/bool.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include diff --git a/builtins/bool.h b/builtins/bool.h index d6e5307..98b2ac0 100644 --- a/builtins/bool.h +++ b/builtins/bool.h @@ -2,7 +2,6 @@ // Boolean functions/type info -#include #include #include diff --git a/builtins/c_string.c b/builtins/c_string.c index ab9c606..392565a 100644 --- a/builtins/c_string.c +++ b/builtins/c_string.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include diff --git a/builtins/c_string.h b/builtins/c_string.h index e596bf4..d4c1caa 100644 --- a/builtins/c_string.h +++ b/builtins/c_string.h @@ -2,7 +2,6 @@ // Type info and methods for CString datatype, which represents C's `char*` -#include #include #include diff --git a/builtins/channel.c b/builtins/channel.c index 5b046ef..ffe9318 100644 --- a/builtins/channel.c +++ b/builtins/channel.c @@ -3,7 +3,6 @@ #include #include #include -#include #include #include #include diff --git a/builtins/channel.h b/builtins/channel.h index bb74da8..8deb056 100644 --- a/builtins/channel.h +++ b/builtins/channel.h @@ -3,7 +3,6 @@ // Functions that operate on channels (thread-safe arrays) #include -#include #include "datatypes.h" #include "types.h" diff --git a/builtins/files.c b/builtins/files.c index 6ff3f98..4a4220e 100644 --- a/builtins/files.c +++ b/builtins/files.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include diff --git a/builtins/functions.h b/builtins/functions.h index 16e1cd2..3077e08 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -2,7 +2,6 @@ // Built-in functions -#include #include #include diff --git a/builtins/integers.c b/builtins/integers.c index cbdf6f5..65ac5c8 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -1,7 +1,6 @@ // Integer type infos and methods #include #include -#include #include #include #include diff --git a/builtins/integers.h b/builtins/integers.h index 641d4e2..c0fceb3 100644 --- a/builtins/integers.h +++ b/builtins/integers.h @@ -2,7 +2,6 @@ // Integer type infos and methods -#include #include #include #include diff --git a/builtins/memory.h b/builtins/memory.h index 9bde461..701ea68 100644 --- a/builtins/memory.h +++ b/builtins/memory.h @@ -2,7 +2,6 @@ // Type info and methods for "Memory" opaque type -#include #include #include diff --git a/builtins/nums.c b/builtins/nums.c index cfa1419..c54e8fc 100644 --- a/builtins/nums.c +++ b/builtins/nums.c @@ -2,7 +2,6 @@ #include #include -#include #include #include #include diff --git a/builtins/nums.h b/builtins/nums.h index 51f65c5..78f32c1 100644 --- a/builtins/nums.h +++ b/builtins/nums.h @@ -2,7 +2,6 @@ // Type infos and methods for Nums (floating point) -#include #include #include #include diff --git a/builtins/optionals.h b/builtins/optionals.h index 5f0a407..e37d534 100644 --- a/builtins/optionals.h +++ b/builtins/optionals.h @@ -2,7 +2,6 @@ // Optional types -#include #include #include diff --git a/builtins/path.h b/builtins/path.h index 64a1d72..e0d8525 100644 --- a/builtins/path.h +++ b/builtins/path.h @@ -2,7 +2,6 @@ // A lang for filesystem paths -#include #include #include diff --git a/builtins/pointer.c b/builtins/pointer.c index 9067ebd..7113f83 100644 --- a/builtins/pointer.c +++ b/builtins/pointer.c @@ -2,7 +2,6 @@ #include #include #include -#include #include #include #include diff --git a/builtins/pointer.h b/builtins/pointer.h index c3c622c..faa9531 100644 --- a/builtins/pointer.h +++ b/builtins/pointer.h @@ -2,7 +2,6 @@ // Type infos and methods for Pointer types -#include #include #include diff --git a/builtins/shell.h b/builtins/shell.h index cf102c2..48c59ab 100644 --- a/builtins/shell.h +++ b/builtins/shell.h @@ -2,7 +2,6 @@ // A lang for Shell Command Language -#include #include #include diff --git a/builtins/thread.h b/builtins/thread.h index 2e3298c..5209167 100644 --- a/builtins/thread.h +++ b/builtins/thread.h @@ -4,7 +4,6 @@ #include #include -#include #include "datatypes.h" #include "types.h" diff --git a/builtins/tomo.h b/builtins/tomo.h index b3a60f9..b487f09 100644 --- a/builtins/tomo.h +++ b/builtins/tomo.h @@ -4,7 +4,6 @@ // import #include -#include #include #include #include diff --git a/builtins/util.c b/builtins/util.c index 20f60c0..5cff113 100644 --- a/builtins/util.c +++ b/builtins/util.c @@ -1,7 +1,6 @@ // Built-in utility functions #include #include -#include #include #include #include @@ -59,66 +58,4 @@ public char *mangle(const char *name) return mangled; } -__attribute__((format(printf, 1, 2))) -public CORD CORD_asprintf(CORD fmt, ...) -{ - va_list args; - va_start(args, fmt); - CORD c = NULL; - CORD_vsprintf(&c, fmt, args); - va_end(args); - return c; -} - -public CORD CORD_quoted(CORD str) -{ - CORD quoted = "\""; - CORD_pos i; -#pragma GCC diagnostic ignored "-Wsign-conversion" - CORD_FOR(i, str) { - char c = CORD_pos_fetch(i); - switch (c) { - case '\a': quoted = CORD_cat(quoted, "\\a"); break; - case '\b': quoted = CORD_cat(quoted, "\\b"); break; - case '\x1b': quoted = CORD_cat(quoted, "\\e"); break; - case '\f': quoted = CORD_cat(quoted, "\\f"); break; - case '\n': quoted = CORD_cat(quoted, "\\n"); break; - case '\r': quoted = CORD_cat(quoted, "\\r"); break; - case '\t': quoted = CORD_cat(quoted, "\\t"); break; - case '\v': quoted = CORD_cat(quoted, "\\v"); break; - case '"': quoted = CORD_cat(quoted, "\\\""); break; - case '\\': quoted = CORD_cat(quoted, "\\\\"); break; - case '\x00' ... '\x06': case '\x0E' ... '\x1A': - case '\x1C' ... '\x1F': case '\x7F' ... '\x7F': - CORD_sprintf("ed, "%r\\x%02X", quoted, c); - break; - default: quoted = CORD_cat_char(quoted, c); break; - } - } - quoted = CORD_cat_char(quoted, '"'); - return quoted; -} - -public CORD CORD_replace(CORD c, CORD to_replace, CORD replacement) -{ - size_t len = CORD_len(c); - size_t replaced_len = CORD_len(to_replace); - size_t pos = 0; - CORD ret = CORD_EMPTY; - while (pos < len) { - size_t found = CORD_str(c, pos, to_replace); - if (found == CORD_NOT_FOUND) { - if (pos < len) - ret = CORD_cat(ret, CORD_substr(c, pos, len)); - return ret; - } - if (found > pos) - ret = CORD_cat(ret, CORD_substr(c, pos, found-pos)); - ret = CORD_cat(ret, replacement); - pos = found + replaced_len; - } - return ret; -} - - // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/builtins/util.h b/builtins/util.h index 568e2cf..a497fcf 100644 --- a/builtins/util.h +++ b/builtins/util.h @@ -4,7 +4,6 @@ #include #include -#include #include #include #include @@ -37,9 +36,6 @@ extern bool USE_COLOR; -#define CORD_appendf(cord, fmt, ...) CORD_sprintf(cord, "%r" fmt, *(cord) __VA_OPT__(,) __VA_ARGS__) -#define CORD_all(...) CORD_catn(sizeof((CORD[]){__VA_ARGS__})/sizeof(CORD), __VA_ARGS__) - #define REVERSE_LIST(list) do { \ __typeof(list) _prev = NULL; \ __typeof(list) _next = NULL; \ @@ -66,9 +62,5 @@ extern bool USE_COLOR; __attribute__((format(printf, 1, 2))) char *heap_strf(const char *fmt, ...); -__attribute__((format(printf, 1, 2))) -CORD CORD_asprintf(CORD fmt, ...); -CORD CORD_quoted(CORD str); -CORD CORD_replace(CORD c, CORD to_replace, CORD replacement); // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/compile.c b/compile.c index 6fd8038..c9e08c9 100644 --- a/compile.c +++ b/compile.c @@ -10,6 +10,7 @@ #include "builtins/integers.h" #include "builtins/text.h" #include "compile.h" +#include "cordhelpers.h" #include "enums.h" #include "structs.h" #include "environment.h" diff --git a/cordhelpers.c b/cordhelpers.c new file mode 100644 index 0000000..f0e1ab3 --- /dev/null +++ b/cordhelpers.c @@ -0,0 +1,70 @@ +// Some helper functions for the GC Cord library + +#include +#include + +#include "builtins/util.h" + +__attribute__((format(printf, 1, 2))) +public CORD CORD_asprintf(CORD fmt, ...) +{ + va_list args; + va_start(args, fmt); + CORD c = NULL; + CORD_vsprintf(&c, fmt, args); + va_end(args); + return c; +} + +public CORD CORD_quoted(CORD str) +{ + CORD quoted = "\""; + CORD_pos i; +#pragma GCC diagnostic ignored "-Wsign-conversion" + CORD_FOR(i, str) { + char c = CORD_pos_fetch(i); + switch (c) { + case '\a': quoted = CORD_cat(quoted, "\\a"); break; + case '\b': quoted = CORD_cat(quoted, "\\b"); break; + case '\x1b': quoted = CORD_cat(quoted, "\\e"); break; + case '\f': quoted = CORD_cat(quoted, "\\f"); break; + case '\n': quoted = CORD_cat(quoted, "\\n"); break; + case '\r': quoted = CORD_cat(quoted, "\\r"); break; + case '\t': quoted = CORD_cat(quoted, "\\t"); break; + case '\v': quoted = CORD_cat(quoted, "\\v"); break; + case '"': quoted = CORD_cat(quoted, "\\\""); break; + case '\\': quoted = CORD_cat(quoted, "\\\\"); break; +#pragma GCC diagnostic ignored "-Wpedantic" + case '\x00' ... '\x06': case '\x0E' ... '\x1A': + case '\x1C' ... '\x1F': case '\x7F' ... '\x7F': + CORD_sprintf("ed, "%r\\x%02X", quoted, c); + break; + default: quoted = CORD_cat_char(quoted, c); break; + } + } + quoted = CORD_cat_char(quoted, '"'); + return quoted; +} + +public CORD CORD_replace(CORD c, CORD to_replace, CORD replacement) +{ + size_t len = CORD_len(c); + size_t replaced_len = CORD_len(to_replace); + size_t pos = 0; + CORD ret = CORD_EMPTY; + while (pos < len) { + size_t found = CORD_str(c, pos, to_replace); + if (found == CORD_NOT_FOUND) { + if (pos < len) + ret = CORD_cat(ret, CORD_substr(c, pos, len)); + return ret; + } + if (found > pos) + ret = CORD_cat(ret, CORD_substr(c, pos, found-pos)); + ret = CORD_cat(ret, replacement); + pos = found + replaced_len; + } + return ret; +} + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/cordhelpers.h b/cordhelpers.h new file mode 100644 index 0000000..9a72e93 --- /dev/null +++ b/cordhelpers.h @@ -0,0 +1,14 @@ +#pragma once +// Some helper functions for the GC Cord library + +#include + +#define CORD_appendf(cord, fmt, ...) CORD_sprintf(cord, "%r" fmt, *(cord) __VA_OPT__(,) __VA_ARGS__) +#define CORD_all(...) CORD_catn(sizeof((CORD[]){__VA_ARGS__})/sizeof(CORD), __VA_ARGS__) + +__attribute__((format(printf, 1, 2))) +CORD CORD_asprintf(CORD fmt, ...); +CORD CORD_quoted(CORD str); +CORD CORD_replace(CORD c, CORD to_replace, CORD replacement); + +// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1,\:0 diff --git a/enums.c b/enums.c index 77ad1c1..7268d56 100644 --- a/enums.c +++ b/enums.c @@ -7,6 +7,7 @@ #include "ast.h" #include "builtins/text.h" #include "compile.h" +#include "cordhelpers.h" #include "structs.h" #include "environment.h" #include "typecheck.h" diff --git a/environment.c b/environment.c index 1f5493d..12a470e 100644 --- a/environment.c +++ b/environment.c @@ -7,6 +7,7 @@ #include "builtins/table.h" #include "builtins/text.h" #include "builtins/util.h" +#include "cordhelpers.h" #include "environment.h" #include "typecheck.h" diff --git a/parse.c b/parse.c index ab04119..e98a57d 100644 --- a/parse.c +++ b/parse.c @@ -17,6 +17,7 @@ #include "builtins/text.h" #include "builtins/table.h" #include "builtins/util.h" +#include "cordhelpers.h" // The cache of {filename -> parsed AST} will hold at most this many entries: #ifndef PARSE_CACHE_SIZE diff --git a/structs.c b/structs.c index 7aa2ddf..328fa3b 100644 --- a/structs.c +++ b/structs.c @@ -7,6 +7,7 @@ #include "ast.h" #include "builtins/text.h" #include "compile.h" +#include "cordhelpers.h" #include "environment.h" #include "typecheck.h" #include "builtins/util.h" diff --git a/tomo.c b/tomo.c index 660b7aa..3c5f6cd 100644 --- a/tomo.c +++ b/tomo.c @@ -14,6 +14,7 @@ #include "builtins/datatypes.h" #include "builtins/text.h" #include "compile.h" +#include "cordhelpers.h" #include "parse.h" #include "repl.h" #include "typecheck.h" diff --git a/typecheck.c b/typecheck.c index 743165c..65597a8 100644 --- a/typecheck.c +++ b/typecheck.c @@ -10,6 +10,7 @@ #include "ast.h" #include "builtins/text.h" #include "builtins/util.h" +#include "cordhelpers.h" #include "environment.h" #include "parse.h" #include "typecheck.h" diff --git a/types.c b/types.c index d70346a..321e6a5 100644 --- a/types.c +++ b/types.c @@ -9,6 +9,7 @@ #include "builtins/integers.h" #include "builtins/table.h" #include "builtins/util.h" +#include "cordhelpers.h" #include "types.h" CORD type_to_cord(type_t *t) {