aboutsummaryrefslogtreecommitdiff
path: root/ls_run.c
diff options
context:
space:
mode:
Diffstat (limited to 'ls_run.c')
-rw-r--r--ls_run.c118
1 files changed, 59 insertions, 59 deletions
diff --git a/ls_run.c b/ls_run.c
index 307b751..ca361df 100644
--- a/ls_run.c
+++ b/ls_run.c
@@ -31,15 +31,15 @@ typedef struct {
char ** cache;
size_t * cache_offsets;
-} file_fetcher_ctx_t;
+} file_fetcher_t;
// --- PRIVATE CONSTANTS -------------------------------------------------------
// --- PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
-static int _fetcher(void * ctx, uint16_t loc);
+static int _fetcher(void * arg, uint16_t loc);
static void _usage(char const * argv0, bool short_text);
-static void _tokenize(ls_context_t * ctx);
-static void _line_trace_hook(ls_context_t * ctx);
+static void _tokenize(ls_t * ls);
+static void _line_trace_hook(ls_t * ls);
// --- PUBLIC VARIABLES --------------------------------------------------------
// --- PRIVATE VARIABLES -------------------------------------------------------
@@ -105,39 +105,39 @@ int main(int argc, char ** argv)
size_t const n_cache_pages = 3;
size_t const cache_page_size = 8; // This should be bigger lol
- file_fetcher_ctx_t fctx;
- fctx.f = f;
- fctx.cache_page_size = cache_page_size;
- fctx.n_cache_pages = n_cache_pages;
- fctx.n_page = (size_t) -1;
+ file_fetcher_t fet;
+ fet.f = f;
+ fet.cache_page_size = cache_page_size;
+ fet.n_cache_pages = n_cache_pages;
+ fet.n_page = (size_t) -1;
assert(statbuf.st_size >= 0);
- fctx.total_size = (size_t) statbuf.st_size;
- fctx.cache = calloc(n_cache_pages, sizeof(*fctx.cache));
- fctx.cache_offsets = calloc(n_cache_pages, sizeof(*fctx.cache_offsets));
+ fet.total_size = (size_t) statbuf.st_size;
+ fet.cache = calloc(n_cache_pages, sizeof(*fet.cache));
+ fet.cache_offsets = calloc(n_cache_pages, sizeof(*fet.cache_offsets));
for (size_t i = 0; i < n_cache_pages; i++)
{
- fctx.cache[i] = calloc(cache_page_size, sizeof(char));
- fctx.cache_offsets[i] = (size_t) -1;
+ fet.cache[i] = calloc(cache_page_size, sizeof(char));
+ fet.cache_offsets[i] = (size_t) -1;
}
- ls_context_t ls_ctx;
- ls_ctx.fetcher = _fetcher;
- ls_ctx.fetcher_ctx = &fctx;
- ls_ctx.line_trace_hook = tron ? _line_trace_hook : NULL;
+ ls_t ls;
+ ls.fetcher = _fetcher;
+ ls.fetcher_arg = &fet;
+ ls.line_trace_hook = tron ? _line_trace_hook : NULL;
if (action == ACT_TOKENIZE)
{
- ls_ctx.pc = 0;
- ls_ctx.labels = NULL;
- _tokenize(&ls_ctx);
+ ls.pc = 0;
+ ls.labels = NULL;
+ _tokenize(&ls);
exit(EXIT_SUCCESS);
}
ls_value_t pool[1000];
- ls_error_t e = ls_run(&ls_ctx, pool, 1000);
+ ls_error_t e = ls_run(&ls, pool, 1000);
uint16_t line = 0, col = 0;
- ls_translate_pc(&ls_ctx, ls_ctx.pc, &line, &col);
+ ls_translate_pc(&ls, ls.pc, &line, &col);
if (e == LS_STOPPED)
{
@@ -154,7 +154,7 @@ int main(int argc, char ** argv)
{
printf("\n\n==== POOL ====\n");
- printf("First free: <%.4u>\n\n", (unsigned)(ls_ctx.pool - pool));
+ printf("First free: <%.4u>\n\n", (unsigned)(ls.pool - pool));
for (size_t i = 0; i < sizeof(pool)/sizeof(pool[0]); i++)
{
ls_print_value(stdout, &pool[i], &pool[0]);
@@ -169,40 +169,40 @@ int main(int argc, char ** argv)
// --- PRIVATE FUNCTION DEFINITIONS --------------------------------------------
-static int _fetcher(void * ctx, uint16_t loc)
+static int _fetcher(void * arg, uint16_t loc)
{
- file_fetcher_ctx_t * fctx = (file_fetcher_ctx_t *) ctx;
+ file_fetcher_t * self = (file_fetcher_t *) arg;
- if (loc >= fctx->total_size)
+ if (loc >= self->total_size)
return -LS_NO_MORE_PROGRAM;
// Extremely rudimentary cache fetch. A realistic one would be more
// heavily optimized for hits.
- if (fctx->n_page != (size_t) -1 &&
- fctx->cache_offsets[fctx->n_page] != (size_t) -1 &&
- fctx->cache_offsets[fctx->n_page] <= loc &&
- loc - fctx->cache_offsets[fctx->n_page] < fctx->cache_page_size)
+ if (self->n_page != (size_t) -1 &&
+ self->cache_offsets[self->n_page] != (size_t) -1 &&
+ self->cache_offsets[self->n_page] <= loc &&
+ loc - self->cache_offsets[self->n_page] < self->cache_page_size)
{
// HIT
- char c = fctx->cache[fctx->n_page][
- loc - fctx->cache_offsets[fctx->n_page]
+ char c = self->cache[self->n_page][
+ loc - self->cache_offsets[self->n_page]
];
return (unsigned char) c;
}
else
{
- for (size_t i = 0; i < fctx->n_cache_pages; i++)
+ for (size_t i = 0; i < self->n_cache_pages; i++)
{
- if (fctx->cache_offsets[i] != (size_t) -1 &&
- fctx->cache_offsets[i] <= loc &&
- loc - fctx->cache_offsets[i] < fctx->cache_page_size)
+ if (self->cache_offsets[i] != (size_t) -1 &&
+ self->cache_offsets[i] <= loc &&
+ loc - self->cache_offsets[i] < self->cache_page_size)
{
if (cachedebug)
fprintf(stderr, ">FAR HIT: %"PRIu16"\n", loc);
- fctx->n_page = i;
- char c = fctx->cache[fctx->n_page][
- loc - fctx->cache_offsets[fctx->n_page]
+ self->n_page = i;
+ char c = self->cache[self->n_page][
+ loc - self->cache_offsets[self->n_page]
];
return (unsigned char) c;
@@ -211,22 +211,22 @@ static int _fetcher(void * ctx, uint16_t loc)
if (cachedebug)
fprintf(stderr, ">MISS: %"PRIu16"\n", loc);
- fctx->n_page = (fctx->n_page + 1) % fctx->n_cache_pages;
- size_t file_loc = (loc / fctx->cache_page_size)
- * fctx->cache_page_size;
+ self->n_page = (self->n_page + 1) % self->n_cache_pages;
+ size_t file_loc = (loc / self->cache_page_size)
+ * self->cache_page_size;
assert(file_loc <= LONG_MAX);
- fseek(fctx->f, (long) file_loc, SEEK_SET);
- fctx->cache_offsets[fctx->n_page] = file_loc;
- size_t n = fread(fctx->cache[fctx->n_page],
- fctx->cache_page_size, 1, fctx->f);
- if (n < fctx->cache_page_size)
+ fseek(self->f, (long) file_loc, SEEK_SET);
+ self->cache_offsets[self->n_page] = file_loc;
+ size_t n = fread(self->cache[self->n_page],
+ self->cache_page_size, 1, self->f);
+ if (n < self->cache_page_size)
{
- if (ferror(fctx->f))
+ if (ferror(self->f))
return -LS_DEVICE_IO_ERROR;
}
- char c = fctx->cache[fctx->n_page][
- loc - fctx->cache_offsets[fctx->n_page]
+ char c = self->cache[self->n_page][
+ loc - self->cache_offsets[self->n_page]
];
return (unsigned char) c;
@@ -250,17 +250,17 @@ static void _usage(char const * argv0, bool short_text)
fprintf(stderr, " -t trace executing line numbers\n");
}
-static void _tokenize(ls_context_t * ctx)
+static void _tokenize(ls_t * ls)
{
ls_value_t pool[100]; // for labels
- ls_init_ctx(ctx, pool, sizeof pool / sizeof pool[0]);
+ ls_init(ls, pool, sizeof pool / sizeof pool[0]);
- if (setjmp(ctx->error_jmp_buf))
+ if (setjmp(ls->error_jmp_buf))
{
uint16_t line = 0, col = 0;
- ls_translate_pc(ctx, ctx->pc, &line, &col);
+ ls_translate_pc(ls, ls->pc, &line, &col);
fprintf(stderr, "error %d at %u:%u",
- (int) ctx->error, line, col);
+ (int) ls->error, line, col);
exit(EXIT_FAILURE);
}
@@ -268,7 +268,7 @@ static void _tokenize(ls_context_t * ctx)
for (;;)
{
- ls_lex(ctx, &tok);
+ ls_lex(ls, &tok);
switch (tok.ty)
{
@@ -342,12 +342,12 @@ static void _tokenize(ls_context_t * ctx)
}
}
-static void _line_trace_hook(ls_context_t * ctx)
+static void _line_trace_hook(ls_t * ls)
{
uint16_t line = 0, col = 0;
bool cachedebug_hold = cachedebug;
cachedebug = false;
- ls_translate_pc(ctx, ctx->pc, &line, &col);
+ ls_translate_pc(ls, ls->pc, &line, &col);
cachedebug = cachedebug_hold;
fprintf(stderr, ">line %"PRIu16"\n", line);
}