// data type value
union ulas_val {
- int int_value;
- char *str_value;
+ int intv;
+ char *strv;
};
// literal value
.PHONY: tags
tags:
- ctags --recurse=yes --exclude=.git --exclude=bin --exclude=obj --exclude=scripts
+ ctags --recurse=yes --exclude=.git --exclude=bin --exclude=obj --extras=* --fields=* --c-kinds=* --language-force=C
.PHONY:
ccmds:
ulascfg.preproc_only = 0;
}
+#define ASSERT_INT_TOTOK(expected_val, expected_rc, token) \
+ { \
+ int rc = 0; \
+ struct ulas_tok tok = ulas_totok((token), strlen(token), &rc); \
+ assert((expected_rc) == rc); \
+ assert(tok.type == ULAS_TOKLITERAL); \
+ assert(tok.lit.type == ULAS_INT); \
+ assert(tok.lit.val.intv == (expected_val)); \
+ }
+
void test_totok(void) {
TESTBEGIN("totok");
+
+ // regular ints
+ ASSERT_INT_TOTOK(10, 0, "10");
+ ASSERT_INT_TOTOK(0x1A, 0, "0x1A");
+ ASSERT_INT_TOTOK(5, 0, "0b101");
+
+ // chars
+ ASSERT_INT_TOTOK('a', 0, "'a'");
+ ASSERT_INT_TOTOK('\n', 0, "'\\n'");
+ ASSERT_INT_TOTOK('\\', 0, "'\\\\'");
+ // char - not terminated
+ ASSERT_INT_TOTOK('a', -1, "'a");
+ // bad escape
+ ASSERT_INT_TOTOK(0, -1, "'\\z'");
+ // unterminated escape
+ ASSERT_INT_TOTOK('\n', -1, "'\\n");
+
TESTEND("totok");
}
// integer
tok.type = ULAS_TOKLITERAL;
tok.lit.type = ULAS_INT;
- tok.lit.val.int_value = (int)strtol(buf, &buf, 0);
+
+ // 0b prefix is not supported in strtol... so we implement it by hand
+ if (*buf == 'b') {
+ buf++;
+ tok.lit.val.intv = (int)strtol(buf, &buf, 2);
+ } else {
+ tok.lit.val.intv = (int)strtol(buf - 1, &buf, 0);
+ }
} else if (first == '\'') {
tok.type = ULAS_TOKLITERAL;
tok.lit.type = ULAS_INT;
- buf++;
if (*buf == '\\') {
buf++;
- tok.lit.val.int_value = ulas_unescape(*buf, rc);
+ tok.lit.val.intv = ulas_unescape(*buf, rc);
} else {
- tok.lit.val.int_value = (int)*buf;
+ tok.lit.val.intv = (int)*buf;
}
buf++;
if (*buf != '\'') {
*rc = -1;
ULASERR("Unterminated character sequence\n");
+ goto end;
}
+ buf++;
break;
} else if (ulas_isname(buf, n)) {
// literal. we can resolve it now
tok.type = ULAS_TOKSYMBOL;
tok.lit.type = ULAS_STR;
} else {
-
ULASERR("Unexpected token: %s\n", buf);
*rc = -1;
goto end;
return 0;
}
- return lit->val.int_value;
+ return lit->val.intv;
}
char *ulas_litchar(struct ulas_lit *lit, int *rc) {
return NULL;
}
- return lit->val.str_value;
+ return lit->val.strv;
}
struct ulas_tokbuf ulas_tokbuf(void) {