#define l_lsl_lex_advance(v, code) \
(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_term_token(tokbuf, token_len) \
tokbuf[*token_len] = '\0';
*token_len = 0;
c = l_lsl_lex_peek(v, code);
-
if (l_lsl_isnum(c)) {
value->type = L_LSL_TYPE_INT;
- /* decimal number */
- while (l_lsl_isnum(c)) {
- tokbuf[*token_len] = c;
- *token_len += 1;
+ if (c == '0') {
+ /* special base */
+ /* specific base number */
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);
- } else if (c == '0') {
- value->type = L_LSL_TYPE_INT;
- /* specific base number */
- c = l_lsl_lex_advance(v, code);
- if (l_lsl_isnum(c)) {
- /* octal */
- l_lsl_lex_term_token(tokbuf, token_len);
- } else if (c == 'x') {
- /* hex */
- l_lsl_lex_term_token(tokbuf, token_len);
- } else if (c == 'b') {
- /* bin */
- l_lsl_lex_term_token(tokbuf, token_len);
+ if (l_lsl_isnum(c)) {
+ /* octal */
+ c = l_lsl_lex_advance(v, code);
+ l_lsl_lex_term_token(tokbuf, token_len);
+ } 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);
+ 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);
+ } else if (c == 'b') {
+ /* bin */
+ c = l_lsl_lex_advance(v, code);
+ l_lsl_lex_term_token(tokbuf, token_len);
+ } else {
+ l_lsl_err(v, L_LSL_ERR_INVALID_NUMBER_BASE,
+ "unknown number base: 0%c\n", c);
+ return LRTS_NULL;
+ }
} else {
- l_lsl_err(v, L_LSL_ERR_INVALID_NUMBER_BASE,
- "unknown number base: 0%c\n", c);
- return LRTS_NULL;
- }
-
+ /* decimal number */
+ while (l_lsl_isnum(c)) {
+ l_lsl_lex_write_token(tokbuf, token_len, 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);
+ }
} else {
l_lsl_err(v, L_LSL_ERR_LEX, "unknown char: %c\n", c);
return LRTS_NULL;
struct l_lsl_vm v = l_lsl_vm_init();
struct l_lsl_value *res = l_lsl_exec(&v, src_file);
enum l_lsl_error err = v.err;
+
+ u_fprintf(u_stderr, "%s\n", src_file);
l_lsl_vm_free(&v);
int t_test_lsl() {
struct l_lsl_value v = l_lsl_value_init();
+ /* simple int value */
v.type = L_LSL_TYPE_INT;
v.data.int_val = 123;
T_LSL_ASSERT(v, L_LSL_ERR_OK, "./src/tests/lsl/intliteral.lsl",
t_lsl_vlue_assert_int);
+
+ /* simple hex value */
+ v.type = L_LSL_TYPE_INT;
+ v.data.int_val = 0xabc123;
+ T_LSL_ASSERT(v, L_LSL_ERR_OK, "./src/tests/lsl/hexliteral.lsl",
+ t_lsl_vlue_assert_int);
return 0;
}