aboutsummaryrefslogtreecommitdiff
path: root/src/ls_lex.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/ls_lex.c')
-rw-r--r--src/ls_lex.c108
1 files changed, 54 insertions, 54 deletions
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);
}