aboutsummaryrefslogtreecommitdiff
path: root/src/ls_kw_impl_GOTO.c
diff options
context:
space:
mode:
authorAlexis Lockwood2021-06-25 22:13:44 -0400
committerAlexis Lockwood2021-06-25 22:14:38 -0400
commit36e94483dce8a218fd28b19ea4a439493505114b (patch)
tree5a9fcae7e06338a8e992ff92cfeb735cf06b6009 /src/ls_kw_impl_GOTO.c
parentfc95da592dbda9dd93d9953a60248a43b20d92f4 (diff)
More efficient rewind
Diffstat (limited to 'src/ls_kw_impl_GOTO.c')
-rw-r--r--src/ls_kw_impl_GOTO.c25
1 files changed, 18 insertions, 7 deletions
diff --git a/src/ls_kw_impl_GOTO.c b/src/ls_kw_impl_GOTO.c
index 1951ec2..6829f2b 100644
--- a/src/ls_kw_impl_GOTO.c
+++ b/src/ls_kw_impl_GOTO.c
@@ -20,6 +20,7 @@
// --- PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
static void _goto_num(ls_context_t * ctx, bool backward, uint16_t num);
+static void _goto_ident(ls_context_t * ctx, char const * ident);
static bool _rewind_line(ls_context_t * ctx);
// --- PUBLIC VARIABLES --------------------------------------------------------
@@ -40,8 +41,7 @@ void ls_kw_fun_GOTO(ls_context_t * ctx)
switch (tok.kind)
{
case LS_TOK_WORD:
- // GOTO ident
- ls_throw_err(ctx, LS_BAD_KEYWORD);
+ _goto_ident(ctx, tok.body.word_val);
break;
case LS_TOK_OPERATOR:
if (tok.body.oper_val == OPER_SUB)
@@ -117,10 +117,10 @@ static void _goto_num(ls_context_t * ctx, bool backward, uint16_t num)
ls_token_t tok;
if (!_rewind_line(ctx))
- ls_throw_err(ctx, LS_UNDEFINED_LABEL);
+ goto throw;
- ++ctx->pc;
- ++ctx->line;
+ uint16_t pc = ctx->pc++;
+ uint16_t line = ctx->line++;
ls_lex(ctx, &tok);
if (tok.kind == LS_TOK_NUM_LABEL)
{
@@ -132,13 +132,14 @@ static void _goto_num(ls_context_t * ctx, bool backward, uint16_t num)
}
}
- if (!_rewind_line(ctx))
- ls_throw_err(ctx, LS_UNDEFINED_LABEL);
+ ctx->pc = pc;
+ ctx->line = line;
}
}
if (target == (uint16_t) -1)
{
+throw:
ctx->pc = pc;
ctx->line = line;
ls_throw_err(ctx, LS_UNDEFINED_LABEL);
@@ -173,3 +174,13 @@ static bool _rewind_line(ls_context_t * ctx)
--pc;
}
}
+
+static void _goto_ident(ls_context_t * ctx, char const * ident)
+{
+ // GOTO to an identifier. This must scan ctx->labels (which has yet
+ // to be added) for the ident. If it's not found, it must scan
+ // forward for it (and make sure to add it)
+ //
+ // TODO: idents are stored non nul terminated in the token and in
+ // the value... we should implement a compare function for that
+}