}
/* peeks current token */
-#define l_lsl_lex_peek(lex) \
- (lex->code)[lex->pos]
+char l_lsl_lex_peek(struct l_lsl_vm *v, struct l_lsl_lex *lex) {
+ if (lex->pos > lex->code_len) {
+ l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "Lexer out of bounds");
+ }
+ return (lex->code)[lex->pos];
+}
/* peeks current token and then advances cursor */
-#define l_lsl_lex_consume(lex)\
- (lex->code)[lex->pos++]
+char l_lsl_lex_consume(struct l_lsl_vm *v, struct l_lsl_lex *lex) {
+ if (lex->pos >= lex->code_len) {
+ l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "Lexer out of bounds");
+ }
+ return (lex->code)[lex->pos++];
+}
/* advances cursor and then peeks current token */
-#define l_lsl_lex_advance(v) \
- (lex->code)[++lex->pos]
+char l_lsl_lex_advance(struct l_lsl_vm *v, struct l_lsl_lex *lex) {
+ if (lex->pos >= lex->code_len) {
+ l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "Lexer out of bounds");
+ }
+ return (lex->code)[++lex->pos];
+}
/* writes a char to token and advances token_len */
-#define l_lsl_lex_write_token(t, c) \
- t.val[t.len] = c; \
- t.len += 1;
+void l_lsl_lex_write_token(struct l_lsl_vm *v, struct l_lsl_lex *lex,
+ struct l_lsl_token *t, char c) {
+ if (t->len >= L_LSL_TOK_MAX) {
+ l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "Token too large");
+ }
+ t->val[t->len] = c;
+ t->len += 1;
+}
-#define l_lsl_lex_term_token(t) \
- t.val[t.len] = '\0';
+void l_lsl_lex_term_token(struct l_lsl_vm *v, struct l_lsl_lex *lex,
+ struct l_lsl_token *t) {
+ if (t->len >= L_LSL_TOK_MAX) {
+ l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "Token too large");
+ }
+ t->val[t->len] = '\0';
+}
char tokbuf[L_LSL_TOK_MAX];
struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, struct l_lsl_lex *lex) {
- c = l_lsl_lex_peek(lex);
+ c = l_lsl_lex_peek(v, lex);
/* consume all spaces */
while (l_lsl_isspace(c)) {
if (c == '\n') {
lex->line_num++;
}
- c = l_lsl_lex_advance(lex);
+ c = l_lsl_lex_advance(v, lex);
}
if (l_lsl_isnum(c)) {
if (c == '0') {
/* special base */
/* specific base number */
- c = l_lsl_lex_advance(lex);
+ c = l_lsl_lex_advance(v, lex);
if (l_lsl_isnum(c)) {
/* octal */
while (l_lsl_isoct(c)) {
- l_lsl_lex_write_token(t, c);
- c = l_lsl_lex_advance(lex);
+ l_lsl_lex_write_token(v, lex, &t, c);
+ c = l_lsl_lex_advance(v, lex);
}
- c = l_lsl_lex_advance(lex);
+ c = l_lsl_lex_advance(v, lex);
t.base = 8;
} else if (c == 'x') {
/* hex */
- c = l_lsl_lex_advance(lex);
+ c = l_lsl_lex_advance(v, lex);
while (l_lsl_ishexnum(c)) {
- l_lsl_lex_write_token(t, c);
- c = l_lsl_lex_advance(lex);
+ l_lsl_lex_write_token(v, lex, &t, c);
+ c = l_lsl_lex_advance(v, lex);
}
t.base = 16;
} else if (c == 'b') {
/* bin */
- c = l_lsl_lex_advance(lex);
+ c = l_lsl_lex_advance(v, lex);
while (l_lsl_isbin(c)) {
- l_lsl_lex_write_token(t, c);
- c = l_lsl_lex_advance(lex);
+ l_lsl_lex_write_token(v, lex, &t, c);
+ c = l_lsl_lex_advance(v, lex);
}
- c = l_lsl_lex_advance(lex);
+ c = l_lsl_lex_advance(v, lex);
t.base = 2;
} else {
l_lsl_lex_err(v, lex, L_LSL_ERR_INVALID_NUMBER_BASE,
} else {
/* decimal number */
while (l_lsl_isnum(c)) {
- l_lsl_lex_write_token(t, c);
- c = l_lsl_lex_advance(lex);
+ l_lsl_lex_write_token(v, lex, &t, c);
+ c = l_lsl_lex_advance(v, lex);
}
t.base = 10;
}
} else if (c == '\'' || c == '\"') {
/* string literal */
/* consume initial term */
- c = l_lsl_lex_advance(lex);
+ c = l_lsl_lex_advance(v, lex);
string_term = c;
while (c && c != string_term) {
- c = l_lsl_lex_advance(lex);
+ c = l_lsl_lex_advance(v, lex);
}
} else {
l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "unknown char: %c\n", c);
t.type = L_LSL_TOK_NONE;
}
- l_lsl_lex_term_token(t);
+ l_lsl_lex_term_token(v, lex, &t);
/* end of a token must be a term */
- c = l_lsl_lex_peek(lex);
+ c = l_lsl_lex_peek(v, lex);
if (!l_lsl_isterm(c)) {
l_lsl_lex_err(v, lex, L_LSL_ERR_LEX, "unexpected end of token\n", c);
t.type = L_LSL_TOK_NONE;