summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-12-10 10:14:00 -0500
committerLeo Tenenbaum <pommicket@gmail.com>2019-12-10 10:14:11 -0500
commite73b5a6cb8930d8fc46edd0112d1e292746b7ada (patch)
treec35cb9eeca665f409899d8e461d1b3e61f2b7a64
parentef38e72c7c2ff1134f83772a8aaed98ef995ba01 (diff)
changed Location to be smaller
-rw-r--r--err.c2
-rw-r--r--location.c7
-rw-r--r--tokenizer.c13
-rw-r--r--types.h4
4 files changed, 16 insertions, 10 deletions
diff --git a/err.c b/err.c
index f2b51aa..fdebbfc 100644
--- a/err.c
+++ b/err.c
@@ -79,7 +79,7 @@ static void warn_print_header_(Location where) {
}
static void err_print_location_text(Location where) {
- const char *text = where.code;
+ const char *text = where.ctx->str + where.pos;
const char *end = strchr(text, '\n');
int has_newline = end != NULL;
if (!has_newline)
diff --git a/location.c b/location.c
index b092827..eac8e26 100644
--- a/location.c
+++ b/location.c
@@ -5,14 +5,15 @@
*/
static bool location_after(Location a, Location b) { /* a is after b? */
assert(a.ctx == b.ctx);
- return a.code > b.code;
+ return a.pos > b.pos;
}
/* for debugging */
static void fprint_location(FILE *out, Location location) {
- char *newline = strchr(location.code, '\n');
+ char *str = location.ctx->str + location.pos;
+ char *newline = strchr(str, '\n');
if (newline) *newline = 0;
- fprintf(out, "Line %ld: %s\n", (long)location.line, location.code);
+ fprintf(out, "Line %ld: %s\n", (long)location.line, str);
if (newline) *newline = '\n';
}
diff --git a/tokenizer.c b/tokenizer.c
index 2841961..a4037ad 100644
--- a/tokenizer.c
+++ b/tokenizer.c
@@ -143,7 +143,10 @@ static char tokr_esc_seq(Tokenizer *t) {
/* to be used during tokenization */
static void tokenization_err(Tokenizer *t, const char *fmt, ...) {
va_list args;
- Location where = {t->line, t->s, t->err_ctx};
+ Location where;
+ where.line = t->line;
+ where.ctx = t->err_ctx;
+ where.pos = (CodePos)(t->s - where.ctx->str);
va_start(args, fmt);
err_vprint(where, fmt, args);
va_end(args);
@@ -181,13 +184,13 @@ static void tokr_err_(
static void tokr_put_location(Tokenizer *tokr, Token *t) {
t->where.line = tokr->line;
- t->where.code = tokr->s;
t->where.ctx = tokr->err_ctx;
+ t->where.pos = (CodePos)(tokr->s - t->where.ctx->str);
}
static void tokr_get_location(Tokenizer *tokr, Token *t) {
tokr->line = t->where.line;
- tokr->s = t->where.code;
+ tokr->s = t->where.pos + t->where.ctx->str;
}
/*
@@ -270,7 +273,7 @@ static bool tokenize_string(Tokenizer *t, char *str) {
/* it's a directive */
Token *token = tokr_add(t);
tokr_put_location(t, token);
- token->where.code = start_s;
+ token->where.pos = (CodePos)(start_s - token->where.ctx->str);
token->kind = TOKEN_DIRECT;
token->direct = direct;
continue;
@@ -287,7 +290,7 @@ static bool tokenize_string(Tokenizer *t, char *str) {
/* it's a keyword */
Token *token = tokr_add(t);
tokr_put_location(t, token);
- token->where.code = start_s;
+ token->where.pos = (CodePos)(start_s - token->where.ctx->str);
token->kind = TOKEN_KW;
token->kw = kw;
continue;
diff --git a/types.h b/types.h
index 9937b88..7ad5886 100644
--- a/types.h
+++ b/types.h
@@ -53,6 +53,7 @@ typedef double F64;
typedef U32 IdentID; /* identifier ID for cgen (anonymous variables) */
typedef U32 LineNo;
+typedef U32 CodePos;
/* for keeping track of whence something came */
#ifdef TOC_DEBUG
@@ -67,12 +68,13 @@ typedef U32 LineNo;
typedef struct Location {
LineNo line;
- char *code;
+ CodePos pos; /* position in ctx->str */
struct ErrCtx *ctx;
} Location;
typedef struct ErrCtx {
const char *filename;
+ char *str; /* file contents */
bool enabled;
Location *instance_stack; /* stack of locations which generate the instances we're dealing with */
} ErrCtx;