aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlexis Lockwood2021-06-27 18:39:09 -0400
committerAlexis Lockwood2021-06-27 18:39:09 -0400
commit3204af5b4f995d699e0a83f86942693a5550d906 (patch)
tree3815e95b57582739312c98e76d75510490eae3a3 /src
parent0550cf6ca455d4fcad2ac4bf17ea2697df582b63 (diff)
Get rid of 'ctx' term
Diffstat (limited to 'src')
-rw-r--r--src/ls.c16
-rw-r--r--src/ls.h42
-rw-r--r--src/ls_expr.c40
-rw-r--r--src/ls_expr.h4
-rw-r--r--src/ls_goto.c60
-rw-r--r--src/ls_goto.h8
-rw-r--r--src/ls_internal.c148
-rw-r--r--src/ls_internal.h36
-rw-r--r--src/ls_kw_impl.c148
-rw-r--r--src/ls_kw_impl_GOSUB_RETURN.c76
-rw-r--r--src/ls_kw_impl_GOTO.c18
-rw-r--r--src/ls_kw_impl_PRINT.c8
-rw-r--r--src/ls_lex.c108
-rw-r--r--src/ls_lex.h2
14 files changed, 359 insertions, 355 deletions
diff --git a/src/ls.c b/src/ls.c
index 0f984fb..8164a95 100644
--- a/src/ls.c
+++ b/src/ls.c
@@ -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;
diff --git a/src/ls.h b/src/ls.h
index 4db66e6..38a2b38 100644
--- a/src/ls.h
+++ b/src/ls.h
@@ -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)