[ADD] Add basic number parser

master
LoaD Accumulator 10 months ago
parent 8bb09e2b78
commit 6b3b4e32e8
Signed by: lda
GPG Key ID: 6898757653ABE3E6

@ -1 +1,2 @@
int e = 217 / 100;
int ia = e;

@ -34,6 +34,7 @@ typedef struct LexerToken LexerToken;
X(TOKEN_COMMA) \
X(TOKEN_DOT) \
X(TOKEN_IDENTIFIER) \
X(TOKEN_NUMBER) \
X(TOKEN_ERROR)
typedef enum LexerTokenType {
TOKENS TOKEN_EOF

@ -1,5 +1,6 @@
#include <Lexer.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
@ -24,7 +25,8 @@ typedef struct LexerKeychar {
typedef enum State {
STATE_NONE = 0,
STATE_LOOKING_IDENTIFIER
STATE_LOOKING_IDENTIFIER,
STATE_LOOKING_NUMBER
} State;
struct LexerState {
@ -180,6 +182,23 @@ LexerReturnIdentifier(LexerState * state)
return ret;
}
extern LexerToken *
LexerReturnNumber(LexerState * state)
{
LexerToken * ret = NULL;
if (!state)
{
return NULL;
}
ret = Malloc(sizeof(LexerToken));
ret->type = TOKEN_NUMBER;
ret->line = state->savedLine;
ret->column = state->savedColumn;
ret->magic = strtol(state->magicbuffer, NULL, 10);
return ret;
}
static LexerToken *
LexerReturnEOF(LexerState * state)
{
@ -268,6 +287,18 @@ IsIdentifierRest(int c)
return IsIdentifierStart(c) || isdigit(c);
}
/* TODO: Deal with other bases */
static int
IsNumber(int c)
{
return isdigit(c);
}
static int
IsHex(int c)
{
return IsNumber(c) || ((tolower(c) >= 'a') && (tolower(c) <= 'f'));
}
/* Tries to match for a symbol */
static int
MatchSymbol(LexerState * state, const char * str)
@ -367,6 +398,9 @@ LexerTokenInfo(LexerToken * token)
case TOKEN_IDENTIFIER:
Log(LOG_INFO, "Identifier token '%s' at (%d:%d)", token->magic, token->line, token->column);
break;
case TOKEN_NUMBER:
Log(LOG_INFO, "Number token '%d' at (%d:%d)", (long) token->magic, token->line, token->column);
break;
default:
Log(LOG_INFO, "Token %s at (%d:%d)", TOKEN_NAME[token->type], token->line, token->column);
}
@ -379,7 +413,7 @@ LexerTokenFree(LexerToken * token)
{
return;
}
if (token->magic)
if (token->magic && token->type != TOKEN_NUMBER)
{
Free(token->magic);
}
@ -463,6 +497,16 @@ LexerNext(LexerState * state)
return LexerNext(state);
}
if (IsNumber(c))
{
state->state = STATE_LOOKING_NUMBER;
/* Add the character to our buffer */
LexerInitBuffer(state);
LexerPushBuffer(state, c);
return LexerNext(state);
}
LexerUngetc(state, c);
break;
@ -471,7 +515,7 @@ LexerNext(LexerState * state)
while(1)
{
c = LexerGetc(state);
if (IsWhitespace(c) || IsKeychar(c) > -1)
if (!IsIdentifierRest(c))
{
/* If we have a whitespace, then we're over. */
LexerToken *ret = LexerReturnIdentifier(state);
@ -485,6 +529,23 @@ LexerNext(LexerState * state)
LexerPushBuffer(state, c);
}
break;
case STATE_LOOKING_NUMBER:
while(1)
{
c = LexerGetc(state);
if (!IsNumber(c))
{
/* If we have a whitespace, then we're over. */
LexerToken *ret = LexerReturnNumber(state);
LexerUngetc(state, c);
LexerFreeBuffer(state);
state->state = STATE_NONE;
return ret;
}
LexerPushBuffer(state, c);
}
}
/* Emit an error.
* TODO: Make it more advanced */

Loading…
Cancel
Save