diff options
-rw-r--r-- | README | 3 | ||||
-rw-r--r-- | ls_minify.c | 54 | ||||
-rw-r--r-- | ls_run.c | 118 | ||||
-rw-r--r-- | src/ls.c | 16 | ||||
-rw-r--r-- | src/ls.h | 42 | ||||
-rw-r--r-- | src/ls_expr.c | 40 | ||||
-rw-r--r-- | src/ls_expr.h | 4 | ||||
-rw-r--r-- | src/ls_goto.c | 60 | ||||
-rw-r--r-- | src/ls_goto.h | 8 | ||||
-rw-r--r-- | src/ls_internal.c | 148 | ||||
-rw-r--r-- | src/ls_internal.h | 36 | ||||
-rw-r--r-- | src/ls_kw_impl.c | 148 | ||||
-rw-r--r-- | src/ls_kw_impl_GOSUB_RETURN.c | 76 | ||||
-rw-r--r-- | src/ls_kw_impl_GOTO.c | 18 | ||||
-rw-r--r-- | src/ls_kw_impl_PRINT.c | 8 | ||||
-rw-r--r-- | src/ls_lex.c | 108 | ||||
-rw-r--r-- | src/ls_lex.h | 2 | ||||
-rw-r--r-- | test/test_internal.c | 178 | ||||
-rw-r--r-- | test/tsupport.c | 34 | ||||
-rw-r--r-- | test/tsupport.h | 30 |
20 files changed, 568 insertions, 563 deletions
@@ -42,13 +42,14 @@ Originally written by Alexis Lockwood in 2021. Ⓐ - Better testing - Compile-time options for: - Internal asserts for better LS_INTERNAL_ERROR info - - Hardwiring the context to a static, this should reduce code size a lot + - Hardwiring the ls object to a static, this should reduce code size a lot - Disabling features not needed, including - Hooks - Label cache - Fetcher (always execute in RAM) - OPER_* should be LS_OPER_* - Get rid of all this _ctx_ shit, it's a weird habit, they're just objects +- Use underscore prefix in ls_t for non user-controlled fields ╒════════════════════════╕ │ THE SCRIPTING LANGUAGE │ diff --git a/ls_minify.c b/ls_minify.c index bc0cca7..b1ba82d 100644 --- a/ls_minify.c +++ b/ls_minify.c @@ -26,13 +26,13 @@ typedef struct { char const * s; size_t len; -} file_fetcher_ctx_t; +} file_fetcher_t; typedef struct { - ls_context_t ls; + ls_t ls; ls_token_t tok; ls_token_ty_t last_type; - file_fetcher_ctx_t * fctx; + file_fetcher_t * fet; bool add_space; FILE * f_out; } minifier_t; @@ -40,9 +40,9 @@ typedef struct { // --- 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 _minify(file_fetcher_ctx_t * fctx, FILE * f_out); +static void _minify(file_fetcher_t * fet, FILE * f_out); static void _min_number(minifier_t * min, ls_token_t tok); static void _min_word(minifier_t * min, ls_token_t tok); @@ -108,14 +108,14 @@ int main(int argc, char ** argv) exit(EXIT_FAILURE); } - file_fetcher_ctx_t fctx; + file_fetcher_t fet; assert(statbuf.st_size >= 0); - fctx.len = (size_t) statbuf.st_size; - char * s = malloc(fctx.len); - size_t n = fread(s, 1, fctx.len, f); - fctx.s = s; + fet.len = (size_t) statbuf.st_size; + char * s = malloc(fet.len); + size_t n = fread(s, 1, fet.len, f); + fet.s = s; - if (n != fctx.len) + if (n != fet.len) { if (ferror(f)) perror("fread"); @@ -142,7 +142,7 @@ int main(int argc, char ** argv) else f = stdout; - _minify(&fctx, f); + _minify(&fet, f); free(s); if (f != stdout) @@ -153,14 +153,14 @@ 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 * fet = (file_fetcher_t *) arg; - if ((size_t) loc >= fctx->len) + if ((size_t) loc >= fet->len) return -LS_NO_MORE_PROGRAM; else - return (ls_uchar) fctx->s[(size_t) loc]; + return (ls_uchar) fet->s[(size_t) loc]; } static void _usage(char const * argv0, bool short_text) @@ -178,14 +178,14 @@ static void _usage(char const * argv0, bool short_text) fprintf(stderr, " -u un-minify\n"); } -static void _minify(file_fetcher_ctx_t * fctx, FILE * f_out) +static void _minify(file_fetcher_t * fet, FILE * f_out) { - minifier_t min = {.fctx = fctx}; + minifier_t min = {.fet = fet}; ls_value_t pool[100]; // for labels - ls_init_ctx(&min.ls, pool, sizeof pool / sizeof pool[0]); + ls_init(&min.ls, pool, sizeof pool / sizeof pool[0]); min.ls.fetcher = _fetcher; - min.ls.fetcher_ctx = (void *) fctx; + min.ls.fetcher_arg = (void *) fet; if (setjmp(min.ls.error_jmp_buf)) { @@ -322,23 +322,23 @@ static void _min_keyword(minifier_t * min, ls_token_t tok) ls_addr_t pc_end; for (pc_start = min->ls.pc;; pc_start++) { - if (pc_start >= min->fctx->len) + if (pc_start >= min->fet->len) break; - if (!(min->fctx->s[pc_start] == ' ' - || min->fctx->s[pc_start] == '\t')) + if (!(min->fet->s[pc_start] == ' ' + || min->fet->s[pc_start] == '\t')) break; } for (pc_end = pc_start;; pc_end++) { - if (pc_end >= min->fctx->len) + if (pc_end >= min->fet->len) break; - if (min->fctx->s[pc_end] == '\n') + if (min->fet->s[pc_end] == '\n') break; } if (un_minify || keep_rems) { - fwrite(&min->fctx->s[pc_start], + fwrite(&min->fet->s[pc_start], (size_t)(pc_end - pc_start), 1, min->f_out); fprintf(min->f_out, "\n"); @@ -379,7 +379,7 @@ static void _min_string(minifier_t * min, ls_token_t tok) i <= tok.body.string_val.pc_end; i++) { - fprintf(min->f_out, "%c", min->fctx->s[i]); + fprintf(min->f_out, "%c", min->fet->s[i]); } fprintf(min->f_out, "\""); @@ -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); } @@ -25,18 +25,18 @@ // --- PRIVATE VARIABLES ------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -ls_error_t ls_run(ls_context_t * ctx, ls_value_t * pool, size_t szpool) +ls_error_t ls_run(ls_t * self, ls_value_t * pool, size_t szpool) { - if (setjmp(ctx->error_jmp_buf)) + if (setjmp(self->error_jmp_buf)) { - return ctx->error; + return self->error; } - ls_init_ctx(ctx, pool, szpool); + ls_init(self, pool, szpool); - while (!ctx->stop) + while (!self->stop) { - if (!ls_exec_line(ctx)) + if (!ls_exec_line(self)) return LS_OK; } @@ -138,7 +138,7 @@ void ls_print_value(FILE * stream, ls_value_t * value, ls_value_t * first) } } -bool ls_translate_pc(ls_context_t * ctx, ls_addr_t pc, uint16_t * line, +bool ls_translate_pc(ls_t * self, ls_addr_t pc, uint16_t * line, uint16_t * col) { uint16_t n_line = 1, n_col = 1; @@ -146,7 +146,7 @@ bool ls_translate_pc(ls_context_t * ctx, ls_addr_t pc, uint16_t * line, for (ls_addr_t i = 0; i <= pc; i++) { - int f = ctx->fetcher(ctx->fetcher_ctx, i); + int f = self->fetcher(self->fetcher_arg, i); if (f <= 0) return false; @@ -24,7 +24,7 @@ // --- PRIVATE DATATYPES ------------------------------------------------------- // --- PUBLIC DATATYPES -------------------------------------------------------- -/// Fetcher. This function accepts a void* context and a byte address, and +/// Fetcher. This function accepts a void* argument and a byte address, and /// returns the byte at that location. /// /// Fetchers not reading out of fast memory (for example, ones reading from a @@ -34,28 +34,31 @@ /// from ls_error_t. If no more characters are present, return /// -LS_NO_MORE_PROGRAM, but do not perform cleanup operations --- the parser /// must be allowed to read off the end. -typedef int (*ls_fetcher_t)(void * ctx, uint16_t loc); +typedef int (*ls_fetcher_t)(void * arg, uint16_t loc); // fw decl -struct ls_context_s; +struct ls_s; -/// Main LS execution context struct. -typedef struct ls_context_s { - /// Fetcher to retrieve data. See ls_run(). +/// Main LS execution struct. +typedef struct ls_s { + // --------- begin user-controlled fields --------- + + /// Fetcher to retrieve data. See ls_run(). User-controlled. ls_fetcher_t fetcher; - /// Context for fetcher - void * fetcher_ctx; + /// Argument for fetcher. User-controlled. + void * fetcher_arg; /// Line trace hook. If not null, this will be called for each line. - void (*line_trace_hook)(struct ls_context_s * ctx); + /// User-controlled. + void (*line_trace_hook)(struct ls_s * ctx); /// Set true to stop execution on the next line. The interpreter will /// exit with error LS_STOPPED. Note that LS_STOPPED is also given if - /// the interpreter encounters the END statement. + /// the interpreter encounters the END statement. User-controlled. bool stop; - // --------- end items relevant to the user --------- + // --------- end user-controlled fields --------- /// Execution stack. ls_value_t * callstack; @@ -84,26 +87,27 @@ typedef struct ls_context_s { /// Next label cache entry. This rotates so that the oldest entry is /// always overwritten. uint8_t label_cache_i; -} ls_context_t; +} ls_t; // --- PUBLIC CONSTANTS -------------------------------------------------------- // --- PUBLIC VARIABLES -------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -/// Execute a script, returning any unhandled errors. The context should first -/// be initialized by providing a fetcher, assigned to ctx->fetcher and -/// ctx->fetcher_ctx. All other fields will be managed by ls_run(). +/// Execute a script, returning any unhandled errors. The user-controlled +/// fields in the ls_t should be initialized first (these are between +/// begin/end "user-controlled fields", and documented as such in their +/// doc comments). /// -/// @param ctx - context, initialized with fetcher +/// @param self /// @param pool - an array of ls_value_t to use as the allocation pool /// @param szpool - number of elements in @a pool -ls_error_t ls_run(ls_context_t * ctx, ls_value_t * pool, size_t szpool); +ls_error_t ls_run(ls_t * self, ls_value_t * pool, size_t szpool); /// Convert a program counter value to a line and column number. This is /// expensive and should only be called in rare cases (for example, printing /// out a line number in an error message). /// -/// @param ctx - context +/// @param self /// @param pc - program counter to look up /// @param[out] line - 1-based line number /// @param[out] col - 1-based column number @@ -111,7 +115,7 @@ ls_error_t ls_run(ls_context_t * ctx, ls_value_t * pool, size_t szpool); /// @retval false - the PC doesn't exist /// /// If the PC doesn't exist, @a line and @a col are unchanged. -bool ls_translate_pc(ls_context_t * ctx, ls_addr_t pc, uint16_t * line, +bool ls_translate_pc(ls_t * self, ls_addr_t pc, uint16_t * line, uint16_t * col); /// Print out a ls_value_t. For diagnostics. diff --git a/src/ls_expr.c b/src/ls_expr.c index 29a49f6..95a6e38 100644 --- a/src/ls_expr.c +++ b/src/ls_expr.c @@ -34,7 +34,7 @@ typedef struct { } ls_operdef_t; typedef struct __packed{ - ls_context_t * ctx; + ls_t * self; uint8_t opstack[OPSTACK_N]; uint8_t opstack_i; ls_value_t * outstack; @@ -136,10 +136,10 @@ static const ls_operdef_t _opers[] = { // --- PUBLIC FUNCTIONS -------------------------------------------------------- -void ls_eval_expr(ls_context_t * ctx, ls_value_t * val, ls_token_t * firsttok) +void ls_eval_expr(ls_t * self, ls_value_t * val, ls_token_t * firsttok) { ls_shuntingyard_t yard = { - .ctx = ctx, + .self = self, .opstack_i = 0, .outstack = NULL, .nest = 0, @@ -157,9 +157,9 @@ void ls_eval_expr(ls_context_t * ctx, ls_value_t * val, ls_token_t * firsttok) while (!done) { - ls_addr_t pc_save = ctx->pc; + ls_addr_t pc_save = self->pc; - ls_lex(ctx, &tok); + ls_lex(self, &tok); have_token: switch (tok.ty) { @@ -189,7 +189,7 @@ have_token: if (done) { // Don't eat the token that ended the line - ctx->pc = pc_save; + self->pc = pc_save; } } @@ -197,14 +197,14 @@ have_token: _pop_oper_and_apply(&yard); if (!yard.outstack) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); *val = *yard.outstack; if (yard.outstack->next) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); - ls_free(ctx, yard.outstack); + ls_free(self, yard.outstack); } // --- PRIVATE FUNCTION DEFINITIONS -------------------------------------------- @@ -220,7 +220,7 @@ static ls_oper_t _pop(ls_shuntingyard_t * yard) if (yard->opstack_i) return yard->opstack[--yard->opstack_i]; else - ls_throw_err(yard->ctx, LS_INTERNAL_ERROR); + ls_throw_err(yard->self, LS_INTERNAL_ERROR); // unreachable return NO_OPER; } @@ -228,7 +228,7 @@ static ls_oper_t _pop(ls_shuntingyard_t * yard) static void _push(ls_shuntingyard_t * yard, ls_oper_t oper) { if (yard->opstack_i >= OPSTACK_N - 1) - ls_throw_err(yard->ctx, LS_OUT_OF_MEMORY); + ls_throw_err(yard->self, LS_OUT_OF_MEMORY); else yard->opstack[yard->opstack_i++] = oper; } @@ -243,13 +243,13 @@ static void _pop_oper_and_apply(ls_shuntingyard_t * yard) ls_value_t * rhs = yard->outstack; if (!rhs) - ls_throw_err(yard->ctx, LS_SYNTAX_ERROR); + ls_throw_err(yard->self, LS_SYNTAX_ERROR); ls_value_t * lhs = rhs->next; if (!p_op->unary) { if (!lhs) - ls_throw_err(yard->ctx, LS_SYNTAX_ERROR); + ls_throw_err(yard->self, LS_SYNTAX_ERROR); yard->outstack = rhs; rhs->next = lhs->next; } @@ -261,12 +261,12 @@ static void _pop_oper_and_apply(ls_shuntingyard_t * yard) ); if (!p_op->unary) - ls_free(yard->ctx, lhs); + ls_free(yard->self, lhs); } static void _handle_number(ls_shuntingyard_t * yard, ls_token_t const * tok) { - ls_value_t * v = ls_alloc(yard->ctx); + ls_value_t * v = ls_alloc(yard->self); v->ty = LS_TY_INT; v->body.integer.value = tok->body.number_val; v->prev = NULL; @@ -277,14 +277,14 @@ static void _handle_number(ls_shuntingyard_t * yard, ls_token_t const * tok) static void _handle_word(ls_shuntingyard_t * yard, ls_token_t * tok) { - ls_value_t * var = ls_find_var(yard->ctx, tok->body.word_val); + ls_value_t * var = ls_find_var(yard->self, tok->body.word_val); if (!var) - ls_throw_err(yard->ctx, LS_UNDEFINED_VARIABLE); + ls_throw_err(yard->self, LS_UNDEFINED_VARIABLE); // TODO: types tok->ty = LS_TOK_NUMBER; - tok->body.number_val = ls_read_int_var(yard->ctx, var); + tok->body.number_val = ls_read_int_var(yard->self, var); } static bool _handle_keyword(ls_shuntingyard_t * yard, ls_token_t * tok) @@ -377,13 +377,13 @@ static bool _handle_other(ls_shuntingyard_t * yard, ls_token_t const * tok) case LS_TOK_STATEMENT_SEP: case LS_TOK_NONE: if (yard->nest) - ls_throw_err(yard->ctx, LS_SYNTAX_ERROR); + ls_throw_err(yard->self, LS_SYNTAX_ERROR); else return true; break; default: - ls_throw_err(yard->ctx, LS_SYNTAX_ERROR); + ls_throw_err(yard->self, LS_SYNTAX_ERROR); } return false; diff --git a/src/ls_expr.h b/src/ls_expr.h index 9ecbfe8..69dc309 100644 --- a/src/ls_expr.h +++ b/src/ls_expr.h @@ -24,9 +24,9 @@ /// Parse and evaluate an expression. Any errors are raised. /// -/// @param ctx +/// @param self /// @param val - outparam for the resulting value /// @param tok - optional first token to be included. NULL if none. -void ls_eval_expr(ls_context_t * ctx, ls_value_t * val, ls_token_t * firsttok); +void ls_eval_expr(ls_t * self, ls_value_t * val, ls_token_t * firsttok); #endif // !defined(LS_EXPR_H) diff --git a/src/ls_goto.c b/src/ls_goto.c index 1c198c3..3572be6 100644 --- a/src/ls_goto.c +++ b/src/ls_goto.c @@ -21,26 +21,26 @@ // --- PRIVATE CONSTANTS ------------------------------------------------------- // --- PRIVATE FUNCTION PROTOTYPES --------------------------------------------- -static bool _rewind_line(ls_context_t * ctx); +static bool _rewind_line(ls_t * self); // --- PUBLIC VARIABLES -------------------------------------------------------- // --- PRIVATE VARIABLES ------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -void ls_goto_num(ls_context_t * ctx, bool backward, uint16_t num) +void ls_goto_num(ls_t * self, bool backward, uint16_t num) { ls_addr_t target = LS_ADDR_NULL; for (size_t i = 0; i < LS_LABEL_CACHE_SIZE; i++) { - if (ctx->label_cache[i].pc == LS_ADDR_NULL) + if (self->label_cache[i].pc == LS_ADDR_NULL) continue; - if (ctx->label_cache[i].num == num) + if (self->label_cache[i].num == num) { - if ((ctx->label_cache[i].pc > ctx->pc && !backward) || - (ctx->label_cache[i].pc <= ctx->pc && backward)) + if ((self->label_cache[i].pc > self->pc && !backward) || + (self->label_cache[i].pc <= self->pc && backward)) { - target = ctx->label_cache[i].pc; + target = self->label_cache[i].pc; break; } } @@ -48,7 +48,7 @@ void ls_goto_num(ls_context_t * ctx, bool backward, uint16_t num) // In case we can't find the label, save pc to "unwind" for a // more helpful error later - ls_addr_t pc = ctx->pc; + ls_addr_t pc = self->pc; if (target == LS_ADDR_NULL && !backward) { @@ -56,13 +56,13 @@ void ls_goto_num(ls_context_t * ctx, bool backward, uint16_t num) for (;;) { ls_token_t tok; - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (tok.ty == LS_TOK_NUM_LABEL) { if (tok.body.number_val == num) { - target = ctx->pc; + target = self->pc; break; } } @@ -77,43 +77,43 @@ void ls_goto_num(ls_context_t * ctx, bool backward, uint16_t num) { ls_token_t tok; - if (!_rewind_line(ctx)) + if (!_rewind_line(self)) goto throw; // Quickly saving and restoring our location avoids // a costly second rewind to undo the ls_lex(). - ls_addr_t bw_pc = ctx->pc++; - ls_lex(ctx, &tok); + ls_addr_t bw_pc = self->pc++; + ls_lex(self, &tok); if (tok.ty == LS_TOK_NUM_LABEL) { if (tok.body.number_val == num) { - target = ctx->pc; + target = self->pc; break; } } - ctx->pc = bw_pc; + self->pc = bw_pc; } } if (target == LS_ADDR_NULL) { throw: - ctx->pc = pc; - ls_throw_err(ctx, LS_UNDEFINED_LABEL); + self->pc = pc; + ls_throw_err(self, LS_UNDEFINED_LABEL); } else { - ctx->pc = target; + self->pc = target; } } -void ls_goto_ident(ls_context_t * ctx, char const * ident) +void ls_goto_ident(ls_t * self, char const * ident) { ls_value_t * label = NULL; - for (ls_value_t * i = ctx->labels; i; i = i->next) + for (ls_value_t * i = self->labels; i; i = i->next) { if (!strncmp(i->body.label.ident, ident, LS_IDENT_LEN)) { @@ -127,17 +127,17 @@ void ls_goto_ident(ls_context_t * ctx, char const * ident) for (;;) { ls_token_t tok; - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (tok.ty == LS_TOK_STR_LABEL) { // New labels are always put at the head, // so test there. Don't just read the token // in case some processing was done. - if (!strncmp(ctx->labels->body.label.ident, + if (!strncmp(self->labels->body.label.ident, ident, LS_IDENT_LEN)) { - label = ctx->labels; + label = self->labels; break; } } @@ -149,27 +149,27 @@ void ls_goto_ident(ls_context_t * ctx, char const * ident) } if (!label) - ls_throw_err(ctx, LS_UNDEFINED_LABEL); + ls_throw_err(self, LS_UNDEFINED_LABEL); - ctx->pc = label->body.label.pc; + self->pc = label->body.label.pc; } // --- PRIVATE FUNCTION DEFINITIONS -------------------------------------------- -static bool _rewind_line(ls_context_t * ctx) +static bool _rewind_line(ls_t * self) { - if (ctx->pc == 0) + if (self->pc == 0) return false; - ls_addr_t pc = (ls_addr_t)(ctx->pc - 1); + ls_addr_t pc = (ls_addr_t)(self->pc - 1); for (;;) { - unsigned char ch = ls_fetch(ctx, pc); + unsigned char ch = ls_fetch(self, pc); if (ch == '\n') { - ctx->pc = pc; + self->pc = pc; return true; } diff --git a/src/ls_goto.h b/src/ls_goto.h index 412123e..39ff271 100644 --- a/src/ls_goto.h +++ b/src/ls_goto.h @@ -23,16 +23,16 @@ /// Go to a numbered label. /// -/// @param ctx - context +/// @param self /// @param backward - whether to look backwards for the label (otherwise /// forwards) /// @param num - number to go to -void ls_goto_num(ls_context_t * ctx, bool backward, uint16_t num); +void ls_goto_num(ls_t * self, bool backward, uint16_t num); /// Go to an ident (string) label. /// -/// @param ctx - context +/// @param self /// @param ident - ident to go to -void ls_goto_ident(ls_context_t * ctx, char const * ident); +void ls_goto_ident(ls_t * self, char const * ident); #endif // !defined(LS_GOTO_H) diff --git a/src/ls_internal.c b/src/ls_internal.c index 670b1dc..f739a3d 100644 --- a/src/ls_internal.c +++ b/src/ls_internal.c @@ -21,7 +21,7 @@ // --- PRIVATE MACROS ---------------------------------------------------------- /// Check a return code and throw if it is an error. -#define CHECK_THROW(ctx, v) do {ls_error_t _e = (v); _throw_err((ctx), _e);} \ +#define CHECK_THROW(self, v) do {ls_error_t _e = (v); _throw_err((self), _e);} \ while (0) #define IS_WORD_CHAR(ch) (isalpha((ch)) || (ch) == '_' || (ch) >= 0x80) @@ -37,28 +37,28 @@ // --- PRIVATE VARIABLES ------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -void ls_init_ctx(ls_context_t * ctx, ls_value_t * pool, size_t szpool) +void ls_init(ls_t * self, ls_value_t * pool, size_t szpool) { - ctx->error = LS_OK; + self->error = LS_OK; - ctx->pool = pool; + self->pool = pool; if (pool) { - ctx->pool[szpool - 1].ty = LS_TY_PRISTINE; - ctx->pool[szpool - 1].next = NULL; - ctx->pool[szpool - 1].prev = NULL; + self->pool[szpool - 1].ty = LS_TY_PRISTINE; + self->pool[szpool - 1].next = NULL; + self->pool[szpool - 1].prev = NULL; for (size_t i = 0; i < szpool - 1; i++) { - ctx->pool[i].ty = LS_TY_PRISTINE; - ctx->pool[i].next = &ctx->pool[i + 1]; - ctx->pool[i].prev = NULL; + self->pool[i].ty = LS_TY_PRISTINE; + self->pool[i].next = &self->pool[i + 1]; + self->pool[i].prev = NULL; } - ctx->callstack = ctx->pool; - ctx->pool = ctx->pool->next; - *ctx->callstack = (ls_value_t) { + self->callstack = self->pool; + self->pool = self->pool->next; + *self->callstack = (ls_value_t) { .ty = LS_TY_SCTX_CALL, .prev = NULL, .next = NULL, @@ -69,14 +69,14 @@ void ls_init_ctx(ls_context_t * ctx, ls_value_t * pool, size_t szpool) }; } - ctx->funcs = NULL; - ctx->labels = NULL; - ctx->pc = 0; - ctx->stop = false; + self->funcs = NULL; + self->labels = NULL; + self->pc = 0; + self->stop = false; for (size_t i = 0; i < LS_LABEL_CACHE_SIZE; i++) - ctx->label_cache[i].pc = LS_ADDR_NULL; - ctx->label_cache_i = 0; + self->label_cache[i].pc = LS_ADDR_NULL; + self->label_cache_i = 0; } ls_kw_t ls_convert_kw(char const * word) @@ -108,52 +108,52 @@ ls_kw_t ls_convert_kw(char const * word) return LS_NOT_A_KW; } -void ls_throw_err(ls_context_t * ctx, ls_error_t e) +void ls_throw_err(ls_t * self, ls_error_t e) { if (e) { - ctx->error = e; - longjmp(ctx->error_jmp_buf, 1); + self->error = e; + longjmp(self->error_jmp_buf, 1); } } -ls_uchar ls_fetch_rel(ls_context_t * ctx, int offset) +ls_uchar ls_fetch_rel(ls_t * self, int offset) { - if (ctx->pc + offset > UINT16_MAX || ctx->pc + offset < 0) - ls_throw_err(ctx, LS_INTERNAL_ERROR); - return ls_fetch(ctx, (ls_addr_t)(ctx->pc + offset)); + if (self->pc + offset > UINT16_MAX || self->pc + offset < 0) + ls_throw_err(self, LS_INTERNAL_ERROR); + return ls_fetch(self, (ls_addr_t)(self->pc + offset)); } -ls_uchar ls_fetch(ls_context_t * ctx, ls_addr_t pc) +ls_uchar ls_fetch(ls_t * self, ls_addr_t pc) { - int rc = ctx->fetcher(ctx->fetcher_ctx, pc); + int rc = self->fetcher(self->fetcher_arg, pc); if (rc == -LS_NO_MORE_PROGRAM) return 0; else if (rc > 0) return (ls_uchar) rc; else - ls_throw_err(ctx, -rc); + ls_throw_err(self, -rc); return 0; } -ls_value_t * ls_alloc(ls_context_t * ctx) +ls_value_t * ls_alloc(ls_t * self) { - if (!ctx->pool) - ls_throw_err(ctx, LS_OUT_OF_MEMORY); + if (!self->pool) + ls_throw_err(self, LS_OUT_OF_MEMORY); - ls_value_t * v = ctx->pool; - ctx->pool = v->next; + ls_value_t * v = self->pool; + self->pool = v->next; return v; } -void ls_free(ls_context_t * ctx, ls_value_t * v) +void ls_free(ls_t * self, ls_value_t * v) { v->ty = LS_TY_NOT_ALLOC; - v->next = ctx->pool; - ctx->pool = v; + v->next = self->pool; + self->pool = v; } -void ls_free_val(ls_context_t * ctx, ls_value_t * v) +void ls_free_val(ls_t * self, ls_value_t * v) { ls_value_t * child; @@ -174,16 +174,16 @@ void ls_free_val(ls_context_t * ctx, ls_value_t * v) for (; child; child = next) { next = child->next; - ls_free(ctx, child); + ls_free(self, child); } - ls_free(ctx, v); + ls_free(self, v); } -size_t ls_mem_avail(ls_context_t * ctx) +size_t ls_mem_avail(ls_t * self) { size_t count = 0; - ls_value_t * iter = ctx->pool; + ls_value_t * iter = self->pool; while (iter) { @@ -194,9 +194,9 @@ size_t ls_mem_avail(ls_context_t * ctx) return count; } -ls_value_t * ls_find_var(ls_context_t * ctx, char const * name) +ls_value_t * ls_find_var(ls_t * self, char const * name) { - for (ls_value_t * scope = ctx->callstack; scope; scope = scope->prev) + for (ls_value_t * scope = self->callstack; scope; scope = scope->prev) { for (ls_value_t * var = scope->next; var; var = var->next) { @@ -214,51 +214,51 @@ ls_value_t * ls_find_var(ls_context_t * ctx, char const * name) return NULL; } -ls_value_t * ls_new_var(ls_context_t * ctx, char const * name) +ls_value_t * ls_new_var(ls_t * self, char const * name) { - ls_value_t * var = ls_alloc(ctx); + ls_value_t * var = ls_alloc(self); var->ty = LS_TY_INT_VAR; memset(var->body.int_var.ident, 0, LS_IDENT_LEN); strncpy(var->body.int_var.ident, name, LS_IDENT_LEN); var->body.int_var.value = 0; - var->next = ctx->callstack->next; - ctx->callstack->next = var; + var->next = self->callstack->next; + self->callstack->next = var; return var; } -ls_int_t ls_read_int_var(ls_context_t * ctx, ls_value_t * var) +ls_int_t ls_read_int_var(ls_t * self, ls_value_t * var) { if (var->ty == LS_TY_INT_VAR) return var->body.int_var.value; else - ls_throw_err(ctx, LS_TYPE_MISMATCH); + ls_throw_err(self, LS_TYPE_MISMATCH); // Unreachable return 0; } -void ls_write_int_var(ls_context_t * ctx, ls_value_t * var, ls_int_t val) +void ls_write_int_var(ls_t * self, ls_value_t * var, ls_int_t val) { if (var->ty == LS_TY_VAR) { if (var->body.var.value) - ls_free(ctx, var->body.var.value); + ls_free(self, var->body.var.value); var->ty = LS_TY_INT_VAR; } if (var->ty != LS_TY_INT_VAR) - ls_throw_err(ctx, LS_INTERNAL_ERROR); + ls_throw_err(self, LS_INTERNAL_ERROR); var->body.int_var.value = val; } -void ls_write_var(ls_context_t * ctx, ls_value_t * var, ls_value_t * val) +void ls_write_var(ls_t * self, ls_value_t * var, ls_value_t * val) { if (val->ty == LS_TY_INT) { - ls_write_int_var(ctx, var, val->body.integer.value); + ls_write_int_var(self, var, val->body.integer.value); } else { @@ -269,66 +269,66 @@ void ls_write_var(ls_context_t * ctx, ls_value_t * var, ls_value_t * val) } if (var->body.var.value) - ls_free_val(ctx, var->body.var.value); + ls_free_val(self, var->body.var.value); - var->body.var.value = ls_alloc(ctx); + var->body.var.value = ls_alloc(self); memcpy(var->body.var.value, val, sizeof(ls_value_t)); } } -bool ls_exec_line(ls_context_t * ctx) +bool ls_exec_line(ls_t * self) { ls_token_t tok = {.ty = LS_TOK_STATEMENT_SEP}; while (tok.ty == LS_TOK_STATEMENT_SEP) - ls_lex(ctx, &tok); + ls_lex(self, &tok); - if (ctx->line_trace_hook) - ctx->line_trace_hook(ctx); + if (self->line_trace_hook) + self->line_trace_hook(self); if (tok.ty == LS_TOK_STR_LABEL) { // TODO: handle string labels - ls_lex(ctx, &tok); + ls_lex(self, &tok); } else if (tok.ty == LS_TOK_NUM_LABEL) { - ls_lex(ctx, &tok); + ls_lex(self, &tok); } // LET is handled here because of the slight complexity of implicit-LET if (tok.ty == LS_TOK_KEYWORD && tok.body.keyword_val == LS_KW_LET) { - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (tok.ty != LS_TOK_WORD) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); } // Now both implicit and explicit LET can be handled if (tok.ty == LS_TOK_WORD) { - ls_value_t * var = ls_find_var(ctx, tok.body.word_val); + ls_value_t * var = ls_find_var(self, tok.body.word_val); ls_token_t tok_eq; - ls_lex(ctx, &tok_eq); + ls_lex(self, &tok_eq); if (tok_eq.ty != LS_TOK_OPERATOR || tok_eq.body.oper_val != OPER_EQ) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); ls_value_t val; - ls_eval_expr(ctx, &val, NULL); + ls_eval_expr(self, &val, NULL); if (val.ty != LS_TY_INT) - ls_throw_err(ctx, LS_TYPE_MISMATCH); + ls_throw_err(self, LS_TYPE_MISMATCH); if (!var) - var = ls_new_var(ctx, tok.body.word_val); + var = ls_new_var(self, tok.body.word_val); - ls_write_int_var(ctx, var, val.body.integer.value); + ls_write_int_var(self, var, val.body.integer.value); } else if (tok.ty == LS_TOK_KEYWORD) { - ls_kwmap[tok.body.keyword_val - LS_KW_OFFSET].fun(ctx); + ls_kwmap[tok.body.keyword_val - LS_KW_OFFSET].fun(self); } else if (tok.ty == LS_TOK_STATEMENT_SEP) { @@ -340,7 +340,7 @@ bool ls_exec_line(ls_context_t * ctx) } else { - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); } return true; diff --git a/src/ls_internal.h b/src/ls_internal.h index 45892b1..524f5b4 100644 --- a/src/ls_internal.h +++ b/src/ls_internal.h @@ -37,7 +37,7 @@ /// Define a keyword function. This is used by the code generated by gen_kws.py #define LS_KW_FUN(n) \ void ls_kw_fun_ ## n ( \ - ls_context_t * ctx \ + ls_t * self \ ) /// Initial hash value. Set a uint8_t to this and then use @@ -63,7 +63,7 @@ // --- PRIVATE DATATYPES ------------------------------------------------------- // --- PUBLIC DATATYPES -------------------------------------------------------- -typedef void (*ls_kw_fun_t)(ls_context_t * ctx); +typedef void (*ls_kw_fun_t)(ls_t * self); typedef struct { char const LS_PROGMEM * name; @@ -99,16 +99,16 @@ extern const LS_PROGMEM ls_kwdef_t ls_kwmap[]; // --- PUBLIC VARIABLES -------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -/// Initialize a context as if to run, but do not execute. +/// Initialize an ls_t as if to run, but do not execute. /// /// @param pool - pool to load (can be null, but then pool and callstack will /// not get initialized) -void ls_init_ctx(ls_context_t * ctx, ls_value_t * pool, size_t szpool); +void ls_init(ls_t * self, ls_value_t * pool, size_t szpool); /// Identify what type the next token is, but do not consume it. /// /// Can throw on internal errors, but returns LS_TOK_INVALID on a syntax error. -ls_token_ty_t ls_ident_token(ls_context_t * ctx, int offset); +ls_token_ty_t ls_ident_token(ls_t * self, int offset); /// Try to convert a word to a keyword. Note that this does not take /// abbreviations. Those are converted simply by casting to ls_kw_t after @@ -120,57 +120,57 @@ ls_kw_t ls_convert_kw(char const * word); /// Throw an error, to be caught by the setjmp in the interpreter. If the error /// is LS_OK, no throw occurs. -void ls_throw_err(ls_context_t * ctx, ls_error_t e); +void ls_throw_err(ls_t * self, ls_error_t e); /// Fetch a character, relative to the current one. Throws if the fetcher /// returns an error other than LS_NO_PROGRAM. If there is no character, /// returns nul. -ls_uchar ls_fetch_rel(ls_context_t * ctx, int offset); +ls_uchar ls_fetch_rel(ls_t * self, int offset); /// Fetch a character at a specific location. If there is no character, returns /// nul. -ls_uchar ls_fetch(ls_context_t * ctx, ls_addr_t pc); +ls_uchar ls_fetch(ls_t * self, ls_addr_t pc); /// Allocate from the pool, throwing LS_OUT_OF_MEMORY if not possible. Always /// returns a valid pointer (or doesn't return) -ls_value_t * ls_alloc(ls_context_t * ctx); +ls_value_t * ls_alloc(ls_t * self); /// Deallocate, returning to the pool. -void ls_free(ls_context_t * ctx, ls_value_t * v); +void ls_free(ls_t * self, ls_value_t * v); /// Free a value, including anything it points to -void ls_free_val(ls_context_t * ctx, ls_value_t * v); +void ls_free_val(ls_t * self, ls_value_t * v); /// Count the number of free blocks -size_t ls_mem_avail(ls_context_t * ctx); +size_t ls_mem_avail(ls_t * self); /// Find and return a variable by name. /// /// @retval NULL if the variable does not exist -ls_value_t * ls_find_var(ls_context_t * ctx, char const * name); +ls_value_t * ls_find_var(ls_t * self, char const * name); /// Create a new variable in the current scope. /// /// @return the variable -ls_value_t * ls_new_var(ls_context_t * ctx, char const * name); +ls_value_t * ls_new_var(ls_t * self, char const * name); /// Read an integer variable. If the variable does not contain an integer, /// throw LS_TYPE_MISMATCH. -ls_int_t ls_read_int_var(ls_context_t * ctx, ls_value_t * var); +ls_int_t ls_read_int_var(ls_t * self, ls_value_t * var); /// Write an integer variable. If the variable contains a different type, its /// type will be changed. -void ls_write_int_var(ls_context_t * ctx, ls_value_t * var, ls_int_t val); +void ls_write_int_var(ls_t * self, ls_value_t * var, ls_int_t val); /// Write a variable of any type. val is not consumed and is assumed not /// to live in the pool (space is allocated as needed). -void ls_write_var(ls_context_t * ctx, ls_value_t * var, ls_value_t * val); +void ls_write_var(ls_t * self, ls_value_t * var, ls_value_t * val); /// Execute one line. /// /// @return whether there was a line to execute. This will return false if /// the script reaches the end, or if the END keyword or an external abort /// occur. -bool ls_exec_line(ls_context_t * ctx); +bool ls_exec_line(ls_t * self); #endif // !defined(LS_INTERNAL_H) diff --git a/src/ls_kw_impl.c b/src/ls_kw_impl.c index 901852f..9b4b818 100644 --- a/src/ls_kw_impl.c +++ b/src/ls_kw_impl.c @@ -22,126 +22,126 @@ // --- PRIVATE FUNCTION PROTOTYPES --------------------------------------------- /// Call for keywords without implementations. -static void _no_impl(ls_context_t * ctx); +static void _no_impl(ls_t * self); -void ls_kw_fun_GOTO(ls_context_t * ctx); +void ls_kw_fun_GOTO(ls_t * self); // --- PUBLIC VARIABLES -------------------------------------------------------- // --- PRIVATE VARIABLES ------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -void ls_kw_fun_ABS(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_AND(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_AS(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_ASC(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_AT(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_ATN(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_CALL(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_CAT(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_CHR(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_CLOSE(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_COS(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_COUNT(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_DATA(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_DEC(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_DEF(ls_context_t * ctx) { _no_impl(ctx); } - -void ls_kw_fun_END(ls_context_t * ctx) { ls_throw_err(ctx, LS_STOPPED); } - -void ls_kw_fun_EQV(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_ERASE(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_ERROR(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_FN(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_FOR(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_HEX(ls_context_t * ctx) { _no_impl(ctx); } - -void ls_kw_fun_IF(ls_context_t * ctx) { +void ls_kw_fun_ABS(ls_t * self) { _no_impl(self); } +void ls_kw_fun_AND(ls_t * self) { _no_impl(self); } +void ls_kw_fun_AS(ls_t * self) { _no_impl(self); } +void ls_kw_fun_ASC(ls_t * self) { _no_impl(self); } +void ls_kw_fun_AT(ls_t * self) { _no_impl(self); } +void ls_kw_fun_ATN(ls_t * self) { _no_impl(self); } +void ls_kw_fun_CALL(ls_t * self) { _no_impl(self); } +void ls_kw_fun_CAT(ls_t * self) { _no_impl(self); } +void ls_kw_fun_CHR(ls_t * self) { _no_impl(self); } +void ls_kw_fun_CLOSE(ls_t * self) { _no_impl(self); } +void ls_kw_fun_COS(ls_t * self) { _no_impl(self); } +void ls_kw_fun_COUNT(ls_t * self) { _no_impl(self); } +void ls_kw_fun_DATA(ls_t * self) { _no_impl(self); } +void ls_kw_fun_DEC(ls_t * self) { _no_impl(self); } +void ls_kw_fun_DEF(ls_t * self) { _no_impl(self); } + +void ls_kw_fun_END(ls_t * self) { ls_throw_err(self, LS_STOPPED); } + +void ls_kw_fun_EQV(ls_t * self) { _no_impl(self); } +void ls_kw_fun_ERASE(ls_t * self) { _no_impl(self); } +void ls_kw_fun_ERROR(ls_t * self) { _no_impl(self); } +void ls_kw_fun_FN(ls_t * self) { _no_impl(self); } +void ls_kw_fun_FOR(ls_t * self) { _no_impl(self); } +void ls_kw_fun_HEX(ls_t * self) { _no_impl(self); } + +void ls_kw_fun_IF(ls_t * self) { // TODO how to support ELSE - the current parsing method doesn't // really allow it. Should we bother? ls_value_t cond; - ls_eval_expr(ctx, &cond, NULL); + ls_eval_expr(self, &cond, NULL); if (cond.ty != LS_TY_INT) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); if (cond.body.integer.value == 0) { // TODO: factor this out (also in REM) for (;;) { - ls_uchar uch = ls_fetch_rel(ctx, 0); + ls_uchar uch = ls_fetch_rel(self, 0); if (uch == 0 || uch == '\n') return; - ctx->pc++; + self->pc++; } return; } else { ls_token_t tok; - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (tok.ty != LS_TOK_KEYWORD) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); if (tok.body.keyword_val == LS_KW_GOTO) - ls_kw_fun_GOTO(ctx); + ls_kw_fun_GOTO(self); else // TODO - need to factor something out of ls_exec_line - _no_impl(ctx); + _no_impl(self); } } -void ls_kw_fun_IMP(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_INPUT(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_LEFT(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_LET(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_LOG(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_MID(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_NEXT(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_NOT(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_OCT(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_ON(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_OR(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_OPEN(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_PACK(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_RANDOMIZE(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_READ(ls_context_t * ctx) { _no_impl(ctx); } - -void ls_kw_fun_REM(ls_context_t * ctx) +void ls_kw_fun_IMP(ls_t * self) { _no_impl(self); } +void ls_kw_fun_INPUT(ls_t * self) { _no_impl(self); } +void ls_kw_fun_LEFT(ls_t * self) { _no_impl(self); } +void ls_kw_fun_LET(ls_t * self) { _no_impl(self); } +void ls_kw_fun_LOG(ls_t * self) { _no_impl(self); } +void ls_kw_fun_MID(ls_t * self) { _no_impl(self); } +void ls_kw_fun_NEXT(ls_t * self) { _no_impl(self); } +void ls_kw_fun_NOT(ls_t * self) { _no_impl(self); } +void ls_kw_fun_OCT(ls_t * self) { _no_impl(self); } +void ls_kw_fun_ON(ls_t * self) { _no_impl(self); } +void ls_kw_fun_OR(ls_t * self) { _no_impl(self); } +void ls_kw_fun_OPEN(ls_t * self) { _no_impl(self); } +void ls_kw_fun_PACK(ls_t * self) { _no_impl(self); } +void ls_kw_fun_RANDOMIZE(ls_t * self) { _no_impl(self); } +void ls_kw_fun_READ(ls_t * self) { _no_impl(self); } + +void ls_kw_fun_REM(ls_t * self) { for (;;) { - ls_uchar uch = ls_fetch_rel(ctx, 0); + ls_uchar uch = ls_fetch_rel(self, 0); if (uch == 0 || uch == '\n') return; - ctx->pc++; + self->pc++; } } -void ls_kw_fun_RESTORE(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_RIGHT(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_RND(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_SIN(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_SQR(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_STEP(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_SWAP(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_TAN(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_THEN(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_TO(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_UNPACK(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_UNTIL(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_VAL(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_WEND(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_WHILE(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_WRITE(ls_context_t * ctx) { _no_impl(ctx); } -void ls_kw_fun_XOR(ls_context_t * ctx) { _no_impl(ctx); } +void ls_kw_fun_RESTORE(ls_t * self) { _no_impl(self); } +void ls_kw_fun_RIGHT(ls_t * self) { _no_impl(self); } +void ls_kw_fun_RND(ls_t * self) { _no_impl(self); } +void ls_kw_fun_SIN(ls_t * self) { _no_impl(self); } +void ls_kw_fun_SQR(ls_t * self) { _no_impl(self); } +void ls_kw_fun_STEP(ls_t * self) { _no_impl(self); } +void ls_kw_fun_SWAP(ls_t * self) { _no_impl(self); } +void ls_kw_fun_TAN(ls_t * self) { _no_impl(self); } +void ls_kw_fun_THEN(ls_t * self) { _no_impl(self); } +void ls_kw_fun_TO(ls_t * self) { _no_impl(self); } +void ls_kw_fun_UNPACK(ls_t * self) { _no_impl(self); } +void ls_kw_fun_UNTIL(ls_t * self) { _no_impl(self); } +void ls_kw_fun_VAL(ls_t * self) { _no_impl(self); } +void ls_kw_fun_WEND(ls_t * self) { _no_impl(self); } +void ls_kw_fun_WHILE(ls_t * self) { _no_impl(self); } +void ls_kw_fun_WRITE(ls_t * self) { _no_impl(self); } +void ls_kw_fun_XOR(ls_t * self) { _no_impl(self); } // --- PRIVATE FUNCTION DEFINITIONS -------------------------------------------- -static void _no_impl(ls_context_t * ctx) +static void _no_impl(ls_t * self) { - ls_throw_err(ctx, LS_BAD_KEYWORD); + ls_throw_err(self, LS_BAD_KEYWORD); } diff --git a/src/ls_kw_impl_GOSUB_RETURN.c b/src/ls_kw_impl_GOSUB_RETURN.c index 1d3d45d..fa4b3da 100644 --- a/src/ls_kw_impl_GOSUB_RETURN.c +++ b/src/ls_kw_impl_GOSUB_RETURN.c @@ -24,23 +24,23 @@ // --- PRIVATE VARIABLES ------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -void ls_kw_fun_GOSUB(ls_context_t * ctx) +void ls_kw_fun_GOSUB(ls_t * self) { - ctx->callstack->body.sctx_call.pc = ctx->pc; + self->callstack->body.sctx_call.pc = self->pc; ls_token_t tok; - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (tok.ty != LS_TOK_WORD) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); char ident[LS_IDENT_LEN]; memcpy(ident, tok.body.word_val, LS_IDENT_LEN); - ls_value_t * frame = ls_alloc(ctx); + ls_value_t * frame = ls_alloc(self); *frame = (ls_value_t) { .ty = LS_TY_SCTX_CALL, - .prev = ctx->callstack, + .prev = self->callstack, .next = NULL, .body.sctx_call = { .pc = LS_ADDR_NULL, @@ -48,15 +48,15 @@ void ls_kw_fun_GOSUB(ls_context_t * ctx) }, }; - ctx->callstack = frame; - ctx->callstack->body.sctx_call.pc = ctx->pc; + self->callstack = frame; + self->callstack->body.sctx_call.pc = self->pc; - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (tok.ty == LS_TOK_OPERATOR && tok.body.oper_val == OPER_LPAREN) { for (;;) { - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (tok.ty == LS_TOK_WORD) { @@ -64,16 +64,16 @@ void ls_kw_fun_GOSUB(ls_context_t * ctx) memcpy(arg_ident, tok.body.word_val, LS_IDENT_LEN); - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (!(tok.ty == LS_TOK_OPERATOR && tok.body.oper_val == OPER_EQ)) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); ls_value_t val; - ls_eval_expr(ctx, &val, NULL); + ls_eval_expr(self, &val, NULL); - ls_value_t * var = ls_new_var(ctx, arg_ident); - ls_write_var(ctx, var, &val); + ls_value_t * var = ls_new_var(self, arg_ident); + ls_write_var(self, var, &val); } else if (tok.ty == LS_TOK_COMMA) continue; @@ -81,65 +81,65 @@ void ls_kw_fun_GOSUB(ls_context_t * ctx) && tok.body.oper_val == OPER_RPAREN) break; else - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); } - ls_lex(ctx, &tok); + ls_lex(self, &tok); } // AS is parsed by RETURN, just check that the line is actually valid if (tok.ty == LS_TOK_KEYWORD && tok.body.keyword_val == LS_KW_AS) { - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (tok.ty != LS_TOK_WORD) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); - ls_lex(ctx, &tok); + ls_lex(self, &tok); } if (tok.ty != LS_TOK_STATEMENT_SEP && tok.ty != LS_TOK_NONE) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); - ls_goto_ident(ctx, ident); + ls_goto_ident(self, ident); } -void ls_kw_fun_RETURN(ls_context_t * ctx) +void ls_kw_fun_RETURN(ls_t * self) { ls_token_t tok; ls_value_t val = {.ty = LS_TY_INT, .body.integer.value = 0}; - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (!LS_TOK_EOS(tok)) - ls_eval_expr(ctx, &val, &tok); + ls_eval_expr(self, &val, &tok); // Free the scope ls_value_t * next; - for (ls_value_t * i = ctx->callstack->next; i; i = next) + for (ls_value_t * i = self->callstack->next; i; i = next) { next = i->next; - ls_free(ctx, i); + ls_free(self, i); } - ls_value_t * frame = ctx->callstack->prev; - ls_addr_t pc = ctx->callstack->body.sctx_call.pc; - ls_free(ctx, ctx->callstack); - ctx->callstack = frame; - ctx->pc = pc; + ls_value_t * frame = self->callstack->prev; + ls_addr_t pc = self->callstack->body.sctx_call.pc; + ls_free(self, self->callstack); + self->callstack = frame; + self->pc = pc; while (!LS_TOK_EOS(tok)) { if (tok.ty == LS_TOK_KEYWORD && tok.body.keyword_val == LS_KW_AS) { - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (tok.ty != LS_TOK_WORD) // This was already checked! - ls_throw_err(ctx, LS_INTERNAL_ERROR); - ls_value_t * var = ls_find_var(ctx, tok.body.word_val); + ls_throw_err(self, LS_INTERNAL_ERROR); + ls_value_t * var = ls_find_var(self, tok.body.word_val); if (!var) - var = ls_new_var(ctx, tok.body.word_val); - ls_write_var(ctx, var, &val); + var = ls_new_var(self, tok.body.word_val); + ls_write_var(self, var, &val); } - ls_lex(ctx, &tok); + ls_lex(self, &tok); } } diff --git a/src/ls_kw_impl_GOTO.c b/src/ls_kw_impl_GOTO.c index c349fc1..cbabeb0 100644 --- a/src/ls_kw_impl_GOTO.c +++ b/src/ls_kw_impl_GOTO.c @@ -24,40 +24,40 @@ // --- PRIVATE VARIABLES ------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -void ls_kw_fun_GOTO(ls_context_t * ctx) +void ls_kw_fun_GOTO(ls_t * self) { // Allowed syntax: // GOTO ident // GOTO +num // GOTO -num ls_token_t tok; - ls_lex(ctx, &tok); + ls_lex(self, &tok); bool backward = false; switch (tok.ty) { case LS_TOK_WORD: - ls_goto_ident(ctx, tok.body.word_val); + ls_goto_ident(self, tok.body.word_val); break; case LS_TOK_OPERATOR: if (tok.body.oper_val == OPER_SUB) backward = true; else if (tok.body.oper_val != OPER_ADD) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); - ls_lex(ctx, &tok); + ls_lex(self, &tok); if (tok.ty != LS_TOK_NUMBER) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); if (tok.body.number_val > UINT16_MAX || tok.body.number_val < 0) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); - ls_goto_num(ctx, backward, (uint16_t)(tok.body.number_val)); + ls_goto_num(self, backward, (uint16_t)(tok.body.number_val)); break; default: - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); } } diff --git a/src/ls_kw_impl_PRINT.c b/src/ls_kw_impl_PRINT.c index 4acca32..9c137b4 100644 --- a/src/ls_kw_impl_PRINT.c +++ b/src/ls_kw_impl_PRINT.c @@ -25,7 +25,7 @@ static void _print_value(FILE * f, ls_value_t const * value); // --- PRIVATE VARIABLES ------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -void ls_kw_fun_PRINT(ls_context_t * ctx) +void ls_kw_fun_PRINT(ls_t * self) { ls_token_t tok; ls_value_t val; @@ -36,7 +36,7 @@ void ls_kw_fun_PRINT(ls_context_t * ctx) for (;;) { - ls_lex(ctx, &tok); + ls_lex(self, &tok); switch (tok.ty) { @@ -45,7 +45,7 @@ void ls_kw_fun_PRINT(ls_context_t * ctx) i <= tok.body.string_val.pc_end; i++) { - unsigned char c = ls_fetch(ctx, i); + unsigned char c = ls_fetch(self, i); fprintf(file, "%c", c); } break; @@ -60,7 +60,7 @@ void ls_kw_fun_PRINT(ls_context_t * ctx) default: print_lf = true; - ls_eval_expr(ctx, &val, &tok); + ls_eval_expr(self, &val, &tok); _print_value(file, &val); break; } diff --git a/src/ls_lex.c b/src/ls_lex.c index 797fab7..7c34096 100644 --- a/src/ls_lex.c +++ b/src/ls_lex.c @@ -42,20 +42,20 @@ typedef enum { // --- PRIVATE FUNCTION PROTOTYPES --------------------------------------------- static ch_kind_t _ident_ch_kind(unsigned char ch); -static void _lex_oper(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]); -static void _lex_num(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]); -static void _lex_word(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]); -static void _lex_str(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]); -static void _lex_kw(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]); +static void _lex_oper(ls_t * self, ls_token_t * tok, ls_uchar ch[2]); +static void _lex_num(ls_t * self, ls_token_t * tok, ls_uchar ch[2]); +static void _lex_word(ls_t * self, ls_token_t * tok, ls_uchar ch[2]); +static void _lex_str(ls_t * self, ls_token_t * tok, ls_uchar ch[2]); +static void _lex_kw(ls_t * self, ls_token_t * tok, ls_uchar ch[2]); // --- PUBLIC VARIABLES -------------------------------------------------------- // --- PRIVATE VARIABLES ------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -void ls_lex(ls_context_t * ctx, ls_token_t * tok) +void ls_lex(ls_t * self, ls_token_t * tok) { if (!tok) - ls_throw_err(ctx, LS_INTERNAL_ERROR); + ls_throw_err(self, LS_INTERNAL_ERROR); tok->ty = LS_TOK_INVALID; unsigned char ch[2]; @@ -63,9 +63,9 @@ void ls_lex(ls_context_t * ctx, ls_token_t * tok) bool skip_rem = false; do { - ch[0] = ls_fetch_rel(ctx, 0); + ch[0] = ls_fetch_rel(self, 0); ch_kind = _ident_ch_kind(ch[0]); - ctx->pc++; + self->pc++; if (ch_kind == CH_KIND_REM) skip_rem = true; @@ -73,29 +73,29 @@ void ls_lex(ls_context_t * ctx, ls_token_t * tok) skip_rem = false; } while (ch_kind == CH_KIND_SPACE || skip_rem); - ch[1] = ls_fetch_rel(ctx, 0); + ch[1] = ls_fetch_rel(self, 0); switch (ch_kind) { case CH_KIND_OPER: - _lex_oper(ctx, tok, ch); + _lex_oper(self, tok, ch); break; case CH_KIND_DIGIT: case CH_KIND_AMPER: - _lex_num(ctx, tok, ch); + _lex_num(self, tok, ch); break; case CH_KIND_LETTER: - _lex_word(ctx, tok, ch); + _lex_word(self, tok, ch); break; case CH_KIND_STR: - _lex_str(ctx, tok, ch); + _lex_str(self, tok, ch); break; case CH_KIND_SIGIL: - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); break; case CH_KIND_COMMA: @@ -109,11 +109,11 @@ void ls_lex(ls_context_t * ctx, ls_token_t * tok) case CH_KIND_LABEL: // These are actually parsed inside digit/word parsers - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); break; case CH_KIND_KW: - _lex_kw(ctx, tok, ch); + _lex_kw(self, tok, ch); break; case CH_KIND_END: @@ -121,7 +121,7 @@ void ls_lex(ls_context_t * ctx, ls_token_t * tok) break; case CH_KIND_INVALID: default: - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); break; } } @@ -221,7 +221,7 @@ static ch_kind_t _ident_ch_kind(unsigned char ch) return CH_KIND_INVALID; } -static void _lex_oper(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) +static void _lex_oper(ls_t * self, ls_token_t * tok, ls_uchar ch[2]) { tok->ty = LS_TOK_OPERATOR; tok->body.oper_val = NO_OPER; @@ -249,7 +249,7 @@ static void _lex_oper(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) if (ch[0] == ops[i][0] && ch[1] == ops[i][1]) { tok->body.oper_val = i; - ctx->pc++; // consume second char + self->pc++; // consume second char break; } } else { @@ -265,7 +265,7 @@ static void _lex_oper(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) tok->ty = LS_TOK_INVALID; } -static void _lex_num(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) +static void _lex_num(ls_t * self, ls_token_t * tok, ls_uchar ch[2]) { uint8_t radix = 10; tok->ty = LS_TOK_NUMBER; @@ -285,9 +285,9 @@ static void _lex_num(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) else nch = 1; - ctx->pc = (ls_addr_t)(ctx->pc + nch - 1); - ch[0] = ls_fetch_rel(ctx, 0); - ctx->pc++; + self->pc = (ls_addr_t)(self->pc + nch - 1); + ch[0] = ls_fetch_rel(self, 0); + self->pc++; } for (;;) @@ -303,28 +303,28 @@ static void _lex_num(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) { if (tok->body.number_val > LS_ADDR_MAX || tok->body.number_val < 0) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); - ctx->label_cache_i++; - ctx->label_cache_i %= LS_LABEL_CACHE_SIZE; + self->label_cache_i++; + self->label_cache_i %= LS_LABEL_CACHE_SIZE; ls_label_cache_t * lc - = &ctx->label_cache[ctx->label_cache_i]; - lc->pc = ctx->pc; + = &self->label_cache[self->label_cache_i]; + lc->pc = self->pc; lc->num = (ls_addr_t)(tok->body.number_val); tok->ty = LS_TOK_NUM_LABEL; break; } else { - ctx->pc--; + self->pc--; break; } - ch[0] = ls_fetch_rel(ctx, 0); - ctx->pc++; + ch[0] = ls_fetch_rel(self, 0); + self->pc++; if (digit >= radix) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); else if (tok->ty != LS_TOK_NUM_LABEL) tok->body.number_val = tok->body.number_val @@ -332,7 +332,7 @@ static void _lex_num(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) } } -static void _lex_word(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) +static void _lex_word(ls_t * self, ls_token_t * tok, ls_uchar ch[2]) { tok->ty = LS_TOK_WORD; memset(tok->body.word_val, 0, sizeof(tok->body.word_val)); @@ -341,9 +341,9 @@ static void _lex_word(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) if (i < sizeof(tok->body.word_val) - 1 && ch[0] <= CHAR_MAX) tok->body.word_val[i] = (char) ch[0]; - ch[0] = ls_fetch_rel(ctx, 0); + ch[0] = ls_fetch_rel(self, 0); ch_kind_t ch_kind = _ident_ch_kind(ch[0]); - ctx->pc++; + self->pc++; if (ch_kind == CH_KIND_LABEL) { @@ -353,7 +353,7 @@ static void _lex_word(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) else if (ch_kind != CH_KIND_LETTER && ch_kind != CH_KIND_DIGIT) { - ctx->pc--; + self->pc--; break; } } @@ -363,7 +363,7 @@ static void _lex_word(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) if (kw != LS_NOT_A_KW) { if (tok->ty == LS_TOK_STR_LABEL) - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); tok->ty = LS_TOK_KEYWORD; tok->body.keyword_val = kw; @@ -374,7 +374,7 @@ static void _lex_word(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) // seeing it again or shadowing it. ls_value_t * label = NULL; - for (ls_value_t * i = ctx->labels; i; i = i->next) + for (ls_value_t * i = self->labels; i; i = i->next) { if (!strncmp(tok->body.word_val, i->body.label.ident, LS_IDENT_LEN)) @@ -386,43 +386,43 @@ static void _lex_word(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) if (label) { - if (label->body.label.pc != ctx->pc) - ls_throw_err(ctx, LS_DUPLICATE_DEFINITION); + if (label->body.label.pc != self->pc) + ls_throw_err(self, LS_DUPLICATE_DEFINITION); } else { - label = ls_alloc(ctx); + label = ls_alloc(self); label->ty = LS_TY_LABEL; - label->next = ctx->labels; + label->next = self->labels; strncpy(label->body.label.ident, tok->body.word_val, LS_IDENT_LEN); - label->body.label.pc = ctx->pc; - ctx->labels = label; + label->body.label.pc = self->pc; + self->labels = label; } } } -static void _lex_str(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) +static void _lex_str(ls_t * self, ls_token_t * tok, ls_uchar ch[2]) { // TODO: handle escapes tok->ty = LS_TOK_STRING; - tok->body.string_val.pc_start = ctx->pc; + tok->body.string_val.pc_start = self->pc; for (;;) { - ch[0] = ls_fetch_rel(ctx, 0); + ch[0] = ls_fetch_rel(self, 0); if (ch[0] == '"') { - tok->body.string_val.pc_end = (ls_addr_t)(ctx->pc - 1); + tok->body.string_val.pc_end = (ls_addr_t)(self->pc - 1); break; } if (ch[0] == '\n' || ch[0] == 0) - ls_throw_err(ctx, LS_SYNTAX_ERROR); - ctx->pc++; + ls_throw_err(self, LS_SYNTAX_ERROR); + self->pc++; } - ctx->pc++; + self->pc++; } -static void _lex_kw(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) +static void _lex_kw(ls_t * self, ls_token_t * tok, ls_uchar ch[2]) { if (ch[0] >= LS_KW_OFFSET && ch[0] < LS_MAX_KWS) { @@ -430,5 +430,5 @@ static void _lex_kw(ls_context_t * ctx, ls_token_t * tok, ls_uchar ch[2]) tok->body.keyword_val = ch[0]; } else - ls_throw_err(ctx, LS_SYNTAX_ERROR); + ls_throw_err(self, LS_SYNTAX_ERROR); } diff --git a/src/ls_lex.h b/src/ls_lex.h index d55dfc1..2f84ddb 100644 --- a/src/ls_lex.h +++ b/src/ls_lex.h @@ -23,6 +23,6 @@ // --- PUBLIC FUNCTIONS -------------------------------------------------------- /// Grab one decoded token off the stream -void ls_lex(ls_context_t * ctx, ls_token_t * tok); +void ls_lex(ls_t * self, ls_token_t * tok); #endif // !defined(LS_LEX_H) diff --git a/test/test_internal.c b/test/test_internal.c index 0f08112..cb0572a 100644 --- a/test/test_internal.c +++ b/test/test_internal.c @@ -19,11 +19,11 @@ DECLTEST(_ls_lex) { - ls_context_t ctx; + ls_t ls; ls_token_t tok; - LS_TEST_CATCH(ctx) + LS_TEST_CATCH(ls) { - munit_errorf("Caught error: %d\n", ctx.error); + munit_errorf("Caught error: %d\n", ls.error); return MUNIT_FAIL; } @@ -35,64 +35,64 @@ DECLTEST(_ls_lex) }; for (size_t i = 0; operstrings[i]; i++) { - ls_test_setup(&ctx, operstrings[i]); - ls_lex(&ctx, &tok); + ls_test_setup(&ls, operstrings[i]); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR); munit_assert_int(tok.body.oper_val, ==, OPER_LT); - ls_lex(&ctx, &tok); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR); munit_assert_int(tok.body.oper_val, ==, OPER_LEQ); - ls_lex(&ctx, &tok); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR); munit_assert_int(tok.body.oper_val, ==, OPER_EQ); - ls_lex(&ctx, &tok); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR); munit_assert_int(tok.body.oper_val, ==, OPER_NEQ); - ls_lex(&ctx, &tok); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR); munit_assert_int(tok.body.oper_val, ==, OPER_GEQ); - ls_lex(&ctx, &tok); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_OPERATOR); munit_assert_int(tok.body.oper_val, ==, OPER_GT); } // Numbers are done separately in _ls_parse_num - ls_test_setup(&ctx, "foo"); - ls_lex(&ctx, &tok); + ls_test_setup(&ls, "foo"); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_WORD); munit_assert_string_equal(tok.body.word_val, "foo"); - ls_test_setup(&ctx, "foo bar"); - ls_lex(&ctx, &tok); + ls_test_setup(&ls, "foo bar"); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_WORD); munit_assert_string_equal(tok.body.word_val, "foo"); - ls_lex(&ctx, &tok); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_WORD); munit_assert_string_equal(tok.body.word_val, "bar"); - ls_test_setup(&ctx, "abittoolong foo"); - ls_lex(&ctx, &tok); + ls_test_setup(&ls, "abittoolong foo"); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_WORD); munit_assert_string_equal(tok.body.word_val, "abittoolo"); - ls_lex(&ctx, &tok); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_WORD); munit_assert_string_equal(tok.body.word_val, "foo"); - ls_test_setup(&ctx, " hasspace"); - ls_lex(&ctx, &tok); + ls_test_setup(&ls, " hasspace"); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_WORD); munit_assert_string_equal(tok.body.word_val, "hasspace"); - ls_lex(&ctx, &tok); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_NONE); - ls_test_setup(&ctx, "_foo_"); - ls_lex(&ctx, &tok); + ls_test_setup(&ls, "_foo_"); + ls_lex(&ls, &tok); munit_assert_int(tok.ty, ==, LS_TOK_WORD); munit_assert_string_equal(tok.body.word_val, "_foo_"); - ls_test_setup(&ctx, "\x7F"); - LS_EXPECT_ERR(ctx, LS_SYNTAX_ERROR, ls_lex(&ctx, &tok)); + ls_test_setup(&ls, "\x7F"); + LS_EXPECT_ERR(ls, LS_SYNTAX_ERROR, ls_lex(&ls, &tok)); return MUNIT_OK; } @@ -113,65 +113,65 @@ DECLTEST(_ls_convert_kw) DECLTEST(_ls_parse_num) { - ls_context_t ctx; - LS_TEST_CATCH(ctx) + ls_t ls; + LS_TEST_CATCH(ls) { - munit_errorf("Caught error %d\n", ctx.error); + munit_errorf("Caught error %d\n", ls.error); return MUNIT_FAIL; } ls_value_t val; ls_token_t tok; - ls_test_setup(&ctx, "42, &H42, &o42, &42, &b101010"); + ls_test_setup(&ls, "42, &H42, &o42, &42, &b101010"); - ls_eval_expr(&ctx, &val, NULL); + ls_eval_expr(&ls, &val, NULL); munit_assert_int(val.body.integer.value, ==, 42); - ls_lex(&ctx, &tok); // eat the comma + ls_lex(&ls, &tok); // eat the comma - ls_eval_expr(&ctx, &val, NULL); + ls_eval_expr(&ls, &val, NULL); munit_assert_int(val.body.integer.value, ==, 0x42); - ls_lex(&ctx, &tok); // eat the comma + ls_lex(&ls, &tok); // eat the comma - ls_eval_expr(&ctx, &val, NULL); + ls_eval_expr(&ls, &val, NULL); munit_assert_int(val.body.integer.value, ==, 042); - ls_lex(&ctx, &tok); // eat the comma + ls_lex(&ls, &tok); // eat the comma - ls_eval_expr(&ctx, &val, NULL); + ls_eval_expr(&ls, &val, NULL); munit_assert_int(val.body.integer.value, ==, 042); - ls_lex(&ctx, &tok); // eat the comma + ls_lex(&ls, &tok); // eat the comma - ls_eval_expr(&ctx, &val, NULL); + ls_eval_expr(&ls, &val, NULL); munit_assert_int(val.body.integer.value, ==, 42); - ls_lex(&ctx, &tok); // eat the comma + ls_lex(&ls, &tok); // eat the comma - ls_test_setup(&ctx, "0e42"); - LS_EXPECT_ERR(ctx, LS_SYNTAX_ERROR, ls_eval_expr(&ctx, &val, NULL)); + ls_test_setup(&ls, "0e42"); + LS_EXPECT_ERR(ls, LS_SYNTAX_ERROR, ls_eval_expr(&ls, &val, NULL)); return MUNIT_OK; } static void _check_expr(char const * s, int expected) { char const * testing = NULL; - ls_context_t ctx; + ls_t ls; - LS_TEST_CATCH(ctx) + LS_TEST_CATCH(ls) { if (testing) munit_errorf("Caught error %d in \"%s\"\n", - ctx.error, testing); + ls.error, testing); else - munit_errorf("Caught error %d\n", ctx.error); + munit_errorf("Caught error %d\n", ls.error); } testing = s; ls_value_t val; - ls_test_setup(&ctx, s); - size_t sz_pool = ls_mem_avail(&ctx); - ls_eval_expr(&ctx, &val, NULL); + ls_test_setup(&ls, s); + size_t sz_pool = ls_mem_avail(&ls); + ls_eval_expr(&ls, &val, NULL); munit_assert_int(val.ty, ==, LS_TY_INT); munit_assert_int(val.body.integer.value, ==, expected); - munit_assert_size(sz_pool, ==, ls_mem_avail(&ctx)); + munit_assert_size(sz_pool, ==, ls_mem_avail(&ls)); } DECLTEST(_ls_eval_expr) @@ -215,50 +215,50 @@ DECLTEST(_ls_eval_expr) DECLTEST(_ls_lookup_var) { - ls_context_t ctx; - LS_TEST_CATCH(ctx) + ls_t ls; + LS_TEST_CATCH(ls) { - munit_errorf("Caught error: %d\n", ctx.error); + munit_errorf("Caught error: %d\n", ls.error); return MUNIT_FAIL; } - ls_test_setup(&ctx, "2*foo + bar/2"); + ls_test_setup(&ls, "2*foo + bar/2"); // Create the global stack frame, currently there's no function to // do that - ctx.callstack = ls_alloc(&ctx); - ctx.callstack->ty = LS_TY_SCTX_CALL; - ctx.callstack->prev = NULL; - ctx.callstack->next = NULL; - ctx.callstack->body.sctx_call.pc = LS_ADDR_NULL; - ctx.callstack->body.sctx_call.readptr = LS_ADDR_NULL; + ls.callstack = ls_alloc(&ls); + ls.callstack->ty = LS_TY_SCTX_CALL; + ls.callstack->prev = NULL; + ls.callstack->next = NULL; + ls.callstack->body.sctx_call.pc = LS_ADDR_NULL; + ls.callstack->body.sctx_call.readptr = LS_ADDR_NULL; - ls_value_t * var_foo = ls_new_var(&ctx, "foo"); - ls_write_int_var(&ctx, var_foo, 20); + ls_value_t * var_foo = ls_new_var(&ls, "foo"); + ls_write_int_var(&ls, var_foo, 20); - ls_value_t * var_bar = ls_new_var(&ctx, "bar"); - ls_write_int_var(&ctx, var_bar, 4); + ls_value_t * var_bar = ls_new_var(&ls, "bar"); + ls_write_int_var(&ls, var_bar, 4); ls_value_t val; - size_t sz_pool = ls_mem_avail(&ctx); - ls_eval_expr(&ctx, &val, NULL); + size_t sz_pool = ls_mem_avail(&ls); + ls_eval_expr(&ls, &val, NULL); munit_assert_int(val.ty, ==, LS_TY_INT); munit_assert_int(val.body.integer.value, ==, 42); - munit_assert_size(sz_pool, ==, ls_mem_avail(&ctx)); + munit_assert_size(sz_pool, ==, ls_mem_avail(&ls)); return MUNIT_OK; } DECLTEST(_ls_let) { - ls_context_t ctx; - LS_TEST_CATCH(ctx) + ls_t ls; + LS_TEST_CATCH(ls) { - munit_errorf("Caught error: %d\n", ctx.error); + munit_errorf("Caught error: %d\n", ls.error); return MUNIT_FAIL; } - ls_test_setup(&ctx, + ls_test_setup(&ls, "x = 13\n" "let y = x * 100\n" "z = y + 37\n" @@ -266,19 +266,19 @@ DECLTEST(_ls_let) // Create the global stack frame, currently there's no function to // do that - ctx.callstack = ls_alloc(&ctx); - ctx.callstack->ty = LS_TY_SCTX_CALL; - ctx.callstack->prev = NULL; - ctx.callstack->next = NULL; - ctx.callstack->body.sctx_call.pc = LS_ADDR_NULL; - ctx.callstack->body.sctx_call.readptr = LS_ADDR_NULL; + ls.callstack = ls_alloc(&ls); + ls.callstack->ty = LS_TY_SCTX_CALL; + ls.callstack->prev = NULL; + ls.callstack->next = NULL; + ls.callstack->body.sctx_call.pc = LS_ADDR_NULL; + ls.callstack->body.sctx_call.readptr = LS_ADDR_NULL; - ls_exec_line(&ctx); - ls_exec_line(&ctx); - ls_exec_line(&ctx); + ls_exec_line(&ls); + ls_exec_line(&ls); + ls_exec_line(&ls); - ls_value_t * var_z = ls_find_var(&ctx, "z"); - munit_assert_int(ls_read_int_var(&ctx, var_z), ==, 1337); + ls_value_t * var_z = ls_find_var(&ls, "z"); + munit_assert_int(ls_read_int_var(&ls, var_z), ==, 1337); return MUNIT_OK; } @@ -286,19 +286,19 @@ DECLTEST(_ls_let) DECLTEST(_ls_run) { static const size_t szpool = 1000; - ls_context_t ctx; + ls_t ls; ls_value_t pool[szpool]; - ls_setup_run(&ctx, + ls_setup_run(&ls, "x = 13;let y=x*100\n" "z = y + 37\n" ); - ls_error_t e = ls_run(&ctx, pool, szpool); + ls_error_t e = ls_run(&ls, pool, szpool); munit_assert_int(e, ==, LS_OK); - ls_value_t * var_z = ls_find_var(&ctx, "z"); - munit_assert_int(ls_read_int_var(&ctx, var_z), ==, 1337); + ls_value_t * var_z = ls_find_var(&ls, "z"); + munit_assert_int(ls_read_int_var(&ls, var_z), ==, 1337); /* fprintf(stdout, "\n"); for (size_t i = 0; i < 16; i++) @@ -313,10 +313,10 @@ DECLTEST(_ls_run) DECLTEST(_ls_simple_script) { static const size_t szpool = 1000; - ls_context_t ctx; + ls_t ls; ls_value_t pool[szpool]; - ls_setup_run(&ctx, + ls_setup_run(&ls, "x = 13\n" "y = 24\n" "y = y + x\n" @@ -325,7 +325,7 @@ DECLTEST(_ls_simple_script) "print \"x = \", x, \", twelve = \", 12\n" ); - ls_error_t e = ls_run(&ctx, pool, szpool); + ls_error_t e = ls_run(&ls, pool, szpool); munit_assert_int(e, ==, LS_OK); fprintf(stdout, "\n"); diff --git a/test/tsupport.c b/test/tsupport.c index 9b3582c..706fd44 100644 --- a/test/tsupport.c +++ b/test/tsupport.c @@ -20,7 +20,7 @@ static void _setup_pool(ls_value_t * pool, size_t n); static void _setup_label_cache(ls_label_cache_t * label_cache); -static int _fetcher(void * ctx, uint16_t loc); +static int _fetcher(void * arg, uint16_t loc); // --- PUBLIC VARIABLES -------------------------------------------------------- // --- PRIVATE VARIABLES ------------------------------------------------------- @@ -29,25 +29,25 @@ static ls_value_t _pool[LS_TEST_NPOOL]; // --- PUBLIC FUNCTIONS -------------------------------------------------------- -void ls_test_setup(ls_context_t * ctx, char const * text) +void ls_test_setup(ls_t * ls, char const * text) { _setup_pool(_pool, LS_TEST_NPOOL); - ctx->callstack = NULL; - ctx->funcs = NULL; - ctx->pool = _pool; - ctx->pc = 0; - ctx->fetcher = _fetcher; - ctx->fetcher_ctx = (void *) text; - ctx->line_trace_hook = NULL; - _setup_label_cache(ctx->label_cache); - ctx->label_cache_i = 0; + ls->callstack = NULL; + ls->funcs = NULL; + ls->pool = _pool; + ls->pc = 0; + ls->fetcher = _fetcher; + ls->fetcher_arg = (void *) text; + ls->line_trace_hook = NULL; + _setup_label_cache(ls->label_cache); + ls->label_cache_i = 0; } -void ls_setup_run(ls_context_t * ctx, char const * text) +void ls_setup_run(ls_t * ls, char const * text) { - ctx->fetcher = _fetcher; - ctx->fetcher_ctx = (void *) text; - ctx->line_trace_hook = NULL; + ls->fetcher = _fetcher; + ls->fetcher_arg = (void *) text; + ls->line_trace_hook = NULL; } // --- PRIVATE FUNCTION DEFINITIONS -------------------------------------------- @@ -74,9 +74,9 @@ static void _setup_label_cache(ls_label_cache_t * label_cache) } } -static int _fetcher(void * ctx, uint16_t loc) +static int _fetcher(void * arg, uint16_t loc) { - char const * text = (char const *) ctx; + char const * text = (char const *) arg; // Slightly naughty memoization of pointer bc i can't be arsed to // make the API cleaner right now. We just need to implement read-past- diff --git a/test/tsupport.h b/test/tsupport.h index 40d4b1c..a7842cc 100644 --- a/test/tsupport.h +++ b/test/tsupport.h @@ -23,32 +23,32 @@ /// /// Used as a block: /// -/// LS_TEST_CATCH(ctx, evar) { +/// LS_TEST_CATCH(ls, evar) { /// handle_error(evar); /// } /// -/// @param ctx - context +/// @param ls /// @param evar - name of variable to create in the block -#define LS_TEST_CATCH(ctx) if (setjmp((ctx).error_jmp_buf)) +#define LS_TEST_CATCH(ls) if (setjmp((ls).error_jmp_buf)) /// Execute some code, expecting an error and asserting on it. /// -/// @param ctx - context +/// @param ls /// @param e - error expected /// @param code - code body to execute -#define LS_EXPECT_ERR(ctx, e, code) \ +#define LS_EXPECT_ERR(ls, e, code) \ do { \ jmp_buf _holdbuf; \ - memcpy(&_holdbuf, &(ctx).error_jmp_buf, sizeof(jmp_buf)); \ + memcpy(&_holdbuf, &(ls).error_jmp_buf, sizeof(jmp_buf)); \ bool got_error = false; \ - if (setjmp((ctx).error_jmp_buf)) { \ - munit_assert_int((ctx).error, ==, (e)); \ + if (setjmp((ls).error_jmp_buf)) { \ + munit_assert_int((ls).error, ==, (e)); \ got_error = true; \ } else { \ code ; \ } \ munit_assert_true(got_error); \ - memcpy(&(ctx).error_jmp_buf, &_holdbuf, sizeof(jmp_buf)); \ + memcpy(&(ls).error_jmp_buf, &_holdbuf, sizeof(jmp_buf)); \ } while (0) // --- PRIVATE DATATYPES ------------------------------------------------------- @@ -61,17 +61,17 @@ // --- PUBLIC VARIABLES -------------------------------------------------------- // --- PUBLIC FUNCTIONS -------------------------------------------------------- -/// Set up a new context with a string as its source. You must also set up the +/// Set up a new ls_t with a string as its source. You must also set up the /// jump buffer, for which you can use LS_TEST_CATCH(). /// /// An internal static pool of LS_TEST_NPOOL will be initialized. /// -/// @param ctx - context to set up +/// @param ls /// @param text - the source text -void ls_test_setup(ls_context_t * ctx, char const * text); +void ls_test_setup(ls_t * ls, char const * text); -/// Set up the context's fetcher to return from a string, but don't initialize -/// anything else. Mainly for use with ls_run(). -void ls_setup_run(ls_context_t * ctx, char const * text); +/// Set up the fetcher to return from a string, but don't initialize anything +/// else. Mainly for use with ls_run(). +void ls_setup_run(ls_t * ls, char const * text); #endif // !defined(TSUPPORT_H) |