diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | ast.c | 1 | ||||
| -rw-r--r-- | ast.h | 1 | ||||
| -rw-r--r-- | builtins/array.h | 1 | ||||
| -rw-r--r-- | builtins/bool.c | 1 | ||||
| -rw-r--r-- | builtins/bool.h | 1 | ||||
| -rw-r--r-- | builtins/c_string.c | 1 | ||||
| -rw-r--r-- | builtins/c_string.h | 1 | ||||
| -rw-r--r-- | builtins/channel.c | 1 | ||||
| -rw-r--r-- | builtins/channel.h | 1 | ||||
| -rw-r--r-- | builtins/files.c | 1 | ||||
| -rw-r--r-- | builtins/functions.h | 1 | ||||
| -rw-r--r-- | builtins/integers.c | 1 | ||||
| -rw-r--r-- | builtins/integers.h | 1 | ||||
| -rw-r--r-- | builtins/memory.h | 1 | ||||
| -rw-r--r-- | builtins/nums.c | 1 | ||||
| -rw-r--r-- | builtins/nums.h | 1 | ||||
| -rw-r--r-- | builtins/optionals.h | 1 | ||||
| -rw-r--r-- | builtins/path.h | 1 | ||||
| -rw-r--r-- | builtins/pointer.c | 1 | ||||
| -rw-r--r-- | builtins/pointer.h | 1 | ||||
| -rw-r--r-- | builtins/shell.h | 1 | ||||
| -rw-r--r-- | builtins/thread.h | 1 | ||||
| -rw-r--r-- | builtins/tomo.h | 1 | ||||
| -rw-r--r-- | builtins/util.c | 63 | ||||
| -rw-r--r-- | builtins/util.h | 8 | ||||
| -rw-r--r-- | compile.c | 1 | ||||
| -rw-r--r-- | cordhelpers.c | 70 | ||||
| -rw-r--r-- | cordhelpers.h | 14 | ||||
| -rw-r--r-- | enums.c | 1 | ||||
| -rw-r--r-- | environment.c | 1 | ||||
| -rw-r--r-- | parse.c | 1 | ||||
| -rw-r--r-- | structs.c | 1 | ||||
| -rw-r--r-- | tomo.c | 1 | ||||
| -rw-r--r-- | typecheck.c | 1 | ||||
| -rw-r--r-- | types.c | 1 |
36 files changed, 96 insertions, 92 deletions
@@ -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 $@ @@ -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", @@ -2,6 +2,7 @@ // Logic defining ASTs (abstract syntax trees) to represent code +#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> #include <stdlib.h> diff --git a/builtins/array.h b/builtins/array.h index 91272f9c..478cd773 100644 --- a/builtins/array.h +++ b/builtins/array.h @@ -3,7 +3,6 @@ // Functions that operate on arrays #include <stdbool.h> -#include <gc/cord.h> #include "datatypes.h" #include "functions.h" diff --git a/builtins/bool.c b/builtins/bool.c index 7759cf38..14406c5f 100644 --- a/builtins/bool.c +++ b/builtins/bool.c @@ -2,7 +2,6 @@ #include <ctype.h> #include <err.h> #include <gc.h> -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> #include <stdlib.h> diff --git a/builtins/bool.h b/builtins/bool.h index d6e5307a..98b2ac06 100644 --- a/builtins/bool.h +++ b/builtins/bool.h @@ -2,7 +2,6 @@ // Boolean functions/type info -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/c_string.c b/builtins/c_string.c index ab9c606e..392565ab 100644 --- a/builtins/c_string.c +++ b/builtins/c_string.c @@ -2,7 +2,6 @@ #include <ctype.h> #include <err.h> #include <gc.h> -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> #include <stdlib.h> diff --git a/builtins/c_string.h b/builtins/c_string.h index e596bf40..d4c1caa7 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 <gc/cord.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/channel.c b/builtins/channel.c index 5b046ef5..ffe9318b 100644 --- a/builtins/channel.c +++ b/builtins/channel.c @@ -3,7 +3,6 @@ #include <ctype.h> #include <err.h> #include <gc.h> -#include <gc/cord.h> #include <math.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/channel.h b/builtins/channel.h index bb74da87..8deb0569 100644 --- a/builtins/channel.h +++ b/builtins/channel.h @@ -3,7 +3,6 @@ // Functions that operate on channels (thread-safe arrays) #include <stdbool.h> -#include <gc/cord.h> #include "datatypes.h" #include "types.h" diff --git a/builtins/files.c b/builtins/files.c index 6ff3f988..4a4220e7 100644 --- a/builtins/files.c +++ b/builtins/files.c @@ -9,6 +9,7 @@ #include <libgen.h> #include <limits.h> #include <stdarg.h> +#include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/param.h> diff --git a/builtins/functions.h b/builtins/functions.h index 16e1cd22..3077e089 100644 --- a/builtins/functions.h +++ b/builtins/functions.h @@ -2,7 +2,6 @@ // Built-in functions -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/integers.c b/builtins/integers.c index cbdf6f5a..65ac5c82 100644 --- a/builtins/integers.c +++ b/builtins/integers.c @@ -1,7 +1,6 @@ // Integer type infos and methods #include <ctype.h> #include <gc.h> -#include <gc/cord.h> #include <gmp.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/integers.h b/builtins/integers.h index 641d4e28..c0fceb34 100644 --- a/builtins/integers.h +++ b/builtins/integers.h @@ -2,7 +2,6 @@ // Integer type infos and methods -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> #include <stdlib.h> diff --git a/builtins/memory.h b/builtins/memory.h index 9bde4617..701ea68a 100644 --- a/builtins/memory.h +++ b/builtins/memory.h @@ -2,7 +2,6 @@ // Type info and methods for "Memory" opaque type -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/nums.c b/builtins/nums.c index cfa14191..c54e8fc5 100644 --- a/builtins/nums.c +++ b/builtins/nums.c @@ -2,7 +2,6 @@ #include <float.h> #include <gc.h> -#include <gc/cord.h> #include <math.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/nums.h b/builtins/nums.h index 51f65c57..78f32c1e 100644 --- a/builtins/nums.h +++ b/builtins/nums.h @@ -2,7 +2,6 @@ // Type infos and methods for Nums (floating point) -#include <gc/cord.h> #include <math.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/optionals.h b/builtins/optionals.h index 5f0a407f..e37d5345 100644 --- a/builtins/optionals.h +++ b/builtins/optionals.h @@ -2,7 +2,6 @@ // Optional types -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/path.h b/builtins/path.h index 64a1d72d..e0d85258 100644 --- a/builtins/path.h +++ b/builtins/path.h @@ -2,7 +2,6 @@ // A lang for filesystem paths -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/pointer.c b/builtins/pointer.c index 9067ebd7..7113f839 100644 --- a/builtins/pointer.c +++ b/builtins/pointer.c @@ -2,7 +2,6 @@ #include <ctype.h> #include <err.h> #include <gc.h> -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> #include <stdlib.h> diff --git a/builtins/pointer.h b/builtins/pointer.h index c3c622cf..faa95316 100644 --- a/builtins/pointer.h +++ b/builtins/pointer.h @@ -2,7 +2,6 @@ // Type infos and methods for Pointer types -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/shell.h b/builtins/shell.h index cf102c28..48c59abc 100644 --- a/builtins/shell.h +++ b/builtins/shell.h @@ -2,7 +2,6 @@ // A lang for Shell Command Language -#include <gc/cord.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/thread.h b/builtins/thread.h index 2e3298cb..52091677 100644 --- a/builtins/thread.h +++ b/builtins/thread.h @@ -4,7 +4,6 @@ #include <pthread.h> #include <stdbool.h> -#include <gc/cord.h> #include "datatypes.h" #include "types.h" diff --git a/builtins/tomo.h b/builtins/tomo.h index b3a60f94..b487f09e 100644 --- a/builtins/tomo.h +++ b/builtins/tomo.h @@ -4,7 +4,6 @@ // import #include <gc.h> -#include <gc/cord.h> #include <gmp.h> #include <stdbool.h> #include <stdint.h> diff --git a/builtins/util.c b/builtins/util.c index 20f60c01..5cff1133 100644 --- a/builtins/util.c +++ b/builtins/util.c @@ -1,7 +1,6 @@ // Built-in utility functions #include <ctype.h> #include <gc.h> -#include <gc/cord.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -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 568e2cf0..a497fcf5 100644 --- a/builtins/util.h +++ b/builtins/util.h @@ -4,7 +4,6 @@ #include <assert.h> #include <gc.h> -#include <gc/cord.h> #include <stdbool.h> #include <string.h> #include <err.h> @@ -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 @@ -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 00000000..f0e1ab38 --- /dev/null +++ b/cordhelpers.c @@ -0,0 +1,70 @@ +// Some helper functions for the GC Cord library + +#include <gc/cord.h> +#include <stdarg.h> + +#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 00000000..9a72e93c --- /dev/null +++ b/cordhelpers.h @@ -0,0 +1,14 @@ +#pragma once +// Some helper functions for the GC Cord library + +#include <gc/cord.h> + +#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 @@ -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 1f5493df..12a470e2 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" @@ -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 @@ -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" @@ -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 743165ce..65597a8a 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" @@ -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) { |
