(code)[++v->lex_pos]
/* writes a char to token and advances token_len */
-#define l_lsl_lex_write_token(tokbuf, token_len, c) \
- tokbuf[*token_len] = c; \
- *token_len += 1;
+#define l_lsl_lex_write_token(t, c) \
+ t.val[t.len] = c; \
+ t.len += 1;
-#define l_lsl_lex_term_token(tokbuf, token_len) \
- tokbuf[*token_len] = '\0';
+#define l_lsl_lex_term_token(t) \
+ t.val[t.len] = '\0';
char tokbuf[L_LSL_TOK_MAX];
-struct l_lsl_value* l_lsl_next_token(struct l_lsl_vm *v, const char *code,
- u32 *token_len) {
+struct l_lsl_token l_lsl_next_token(struct l_lsl_vm *v, const char *code) {
char c = 0;
- struct l_lsl_value *value = l_lsl_value_alloc(v);
+ struct l_lsl_token t;
+ u_memset(&t, 0, sizeof(t));
+ t.val = tokbuf;
- *token_len = 0;
c = l_lsl_lex_peek(v, code);
if (l_lsl_isnum(c)) {
- value->type = L_LSL_TYPE_INT;
+ t.type = L_LSL_TOK_INT;
if (c == '0') {
/* special base */
/* specific base number */
if (l_lsl_isnum(c)) {
/* octal */
while (l_lsl_isoct(c)) {
- l_lsl_lex_write_token(tokbuf, token_len, c);
+ l_lsl_lex_write_token(t, c);
c = l_lsl_lex_advance(v, code);
}
c = l_lsl_lex_advance(v, code);
- l_lsl_lex_term_token(tokbuf, token_len);
- value->data.int_val = u_strtol(tokbuf, LRTS_NULL, 8);
+ t.base = 8;
} else if (c == 'x') {
/* hex */
c = l_lsl_lex_advance(v, code);
while (l_lsl_ishexnum(c)) {
- l_lsl_lex_write_token(tokbuf, token_len, c);
+ l_lsl_lex_write_token(t, c);
c = l_lsl_lex_advance(v, code);
}
- l_lsl_lex_term_token(tokbuf, token_len);
- value->data.int_val = u_strtol(tokbuf, LRTS_NULL, 16);
+ t.base = 16;
} else if (c == 'b') {
/* bin */
c = l_lsl_lex_advance(v, code);
while (l_lsl_isbin(c)) {
- l_lsl_lex_write_token(tokbuf, token_len, c);
+ l_lsl_lex_write_token(t, c);
c = l_lsl_lex_advance(v, code);
}
c = l_lsl_lex_advance(v, code);
- l_lsl_lex_term_token(tokbuf, token_len);
- value->data.int_val = u_strtol(tokbuf, LRTS_NULL, 2);
+ t.base = 2;
} else {
l_lsl_err(v, L_LSL_ERR_INVALID_NUMBER_BASE,
"unknown number base: 0%c\n", c);
- return LRTS_NULL;
+ t.type = L_LSL_TOK_NONE;
}
} else {
/* decimal number */
while (l_lsl_isnum(c)) {
- l_lsl_lex_write_token(tokbuf, token_len, c);
+ l_lsl_lex_write_token(t, c);
c = l_lsl_lex_advance(v, code);
}
- l_lsl_lex_term_token(tokbuf, token_len);
-
- value->data.int_val = u_strtol(tokbuf, LRTS_NULL, 10);
+ t.base = 10;
}
} else if (l_lsl_isident(c)) {
/* identifier */
} else {
l_lsl_err(v, L_LSL_ERR_LEX, "unknown char: %c\n", c);
- return LRTS_NULL;
+ t.type = L_LSL_TOK_NONE;
}
+
+ l_lsl_lex_term_token(t);
-
- /* now we have a token
- * return a value type based
- * on the token type
- */
-
-
- return value;
+ return t;
}
struct l_lsl_value* l_lsl_value_alloc(struct l_lsl_vm *v) {
struct l_lsl_value* l_lsl_compile(struct l_lsl_vm *v, const char *code,
u32 len) {
struct l_lsl_value *val = LRTS_NULL;
- u32 token_len;
+ struct l_lsl_token tok;
/* TODO: length check */
LRTS_UNUSED(len);
- val = l_lsl_next_token(v, code, &token_len);
+ tok = l_lsl_next_token(v, code);
+
+ switch (tok.type) {
+ case L_LSL_TOK_INT:
+ val = l_lsl_value_alloc(v);
+ val->type = L_LSL_TYPE_INT;
+ val->data.int_val = u_strtol(tok.val, LRTS_NULL, tok.base);
+ break;
+ default:
+ v->err = L_LSL_ERR_LEX;
+ }
return val;
}