Browse Source

Added hexadecmial support

- Added TOKEN_HEX to lexer and parser
remotes/origin/HEAD
Seth Stubbs 5 months ago
parent
commit
e93f2d646a
  1. 7
      src/main.c
  2. 17
      src/ulex.c
  3. 1
      src/ulex.h
  4. 20
      src/uparse.c

7
src/main.c

@ -3,12 +3,7 @@
int main() {
UASTRootNode *tree = UP_parseSource(
"int a;\n"
"int b;"
"a = 8;\n"
"b = 64 / a / 2;"
"prntint a;"
"prntint b;"
"prntint 0xFF;"
);
UA_genTal(tree, fopen("bin/out.tal", "w"));

17
src/ulex.c

@ -84,6 +84,10 @@ int isNumeric(char c) {
return c >= '0' && c <= '9';
}
int isHex(char c) {
return isNumeric(c) || ((c >= 'A' && c <= 'F') || (c >= 'a' && c <= 'f'));
}
int isWhitespace(char c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t';
}
@ -115,6 +119,19 @@ UTokenType identifierType(ULexState *state) {
}
UToken readNumber(ULexState *state) {
switch(peek(state)) {
case 'x': /* hexadecimal number */
next(state);
/* consume hexadecimal */
while(isHex(peek(state)))
next(state);
return makeToken(state, TOKEN_HEX);
/* TODO: add binary encoding, eg. "0b01000101" */
default: break;/* its a normal number, fall through and continue parsing */
}
while (isNumeric(peek(state)))
next(state);

1
src/ulex.h

@ -11,6 +11,7 @@ typedef enum {
/* literals */
TOKEN_IDENT,
TOKEN_NUMBER,
TOKEN_HEX,
TOKEN_CHAR_LIT,
TOKEN_LEFT_BRACE,

20
src/uparse.c

@ -24,17 +24,6 @@ UASTNode* expression(UParseState *state);
UASTNode* statement(UParseState *state);
ParseRule ruleTable[];
int str2int(char *str, int len) {
int i;
int ret = 0;
for(i = 0; i < len; ++i) {
ret = ret * 10 + (str[i] - '0');
}
return ret;
}
/* ==================================[[ generic helper functions ]]================================== */
void errorAt(UToken *token, int line, const char *fmt, va_list args) {
@ -168,7 +157,13 @@ ParseRule* getRule(UTokenType type) {
/* ==================================[[ parse functions ]]================================== */
UASTNode* number(UParseState *state, UASTNode *left, Precedence currPrec) {
int num = str2int(state->previous.str, state->previous.len);
int num = strtol(state->previous.str, NULL, 10);
printf("got number %d! from token '%.*s' [%d]\n", num, state->previous.len, state->previous.str, state->previous.type);
return newNumNode(state, state->previous, NULL, NULL, num);
}
UASTNode* hexnum(UParseState *state, UASTNode *left, Precedence currPrec) {
int num = strtol(state->previous.str + 2, NULL, 16); /* +2 to skip 0x */
printf("got number %d! from token '%.*s' [%d]\n", num, state->previous.len, state->previous.str, state->previous.type);
return newNumNode(state, state->previous, NULL, NULL, num);
}
@ -229,6 +224,7 @@ ParseRule ruleTable[] = {
/* literals */
{identifer, NULL, PREC_LITERAL}, /* TOKEN_IDENT */
{number, NULL, PREC_LITERAL}, /* TOKEN_NUMBER */
{hexnum, NULL, PREC_LITERAL}, /* TOKEN_HEX */
{NULL, NULL, PREC_NONE}, /* TOKEN_CHAR_LIT */
{NULL, NULL, PREC_NONE}, /* TOKEN_LEFT_BRACE */

Loading…
Cancel
Save