feat(lexer): rework using a struct token and functions associated - unstable
This commit is contained in:
parent
a9df97740f
commit
204bb515af
2 changed files with 60 additions and 34 deletions
|
|
@ -28,31 +28,43 @@ static void save_state(char *stream, ssize_t i)
|
|||
*/
|
||||
static bool is_special_char(char c)
|
||||
{
|
||||
return c == '\'' || c == '\n' || c == ';';
|
||||
return c == '\'' || c == '\n' || c == ';' || c == 'EOF';
|
||||
}
|
||||
|
||||
/* @return: true if a keyword from the grammar was found, false otherwise.
|
||||
*
|
||||
*/
|
||||
static bool is_keyword(char *stream, ssize_t i)
|
||||
static void new_token_keyword(struct token *tok, char *begin, ssize_t size)
|
||||
{
|
||||
if (i == 2)
|
||||
if (strncmp(begin, "if", size) == 0)
|
||||
{
|
||||
return strcmp(stream, "if") == 0 || strcmp(stream, "fi") == 0;
|
||||
tok->type = TOKEN_IF;
|
||||
}
|
||||
if (i == 4)
|
||||
if (strncmp(begin, "fi", size) == 0)
|
||||
{
|
||||
return strcmp(stream, "then") || strcmp(stream, "else")
|
||||
|| strcmp(stream, "elif");
|
||||
tok->type = TOKEN_FI;
|
||||
}
|
||||
if (strncmp(begin, "then", size) == 0)
|
||||
{
|
||||
tok->type = TOKEN_THEN;
|
||||
}
|
||||
if (strncmp(begin, "else", size) == 0)
|
||||
{
|
||||
tok->type = TOKEN_ELSE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
char *new_token(char *begin, ssize_t size)
|
||||
struct token *new_token(char *begin, ssize_t size)
|
||||
{
|
||||
char *res = calloc(size + 1, sizeof(char));
|
||||
struct token *res = calloc(1, sizeof(struct token));
|
||||
if (res == NULL)
|
||||
return NULL;
|
||||
|
||||
// checks which type of token
|
||||
|
||||
// is special char
|
||||
|
||||
// is keyword
|
||||
|
||||
// otherwise -> WORD
|
||||
char *token_data = calloc(size + 1, sizeof(char));
|
||||
strncpy(res, begin, size);
|
||||
return res;
|
||||
}
|
||||
|
|
@ -77,7 +89,7 @@ char *stream_init(void)
|
|||
return stream;
|
||||
}
|
||||
|
||||
char *peek_token(void)
|
||||
struct token *peek_token(void)
|
||||
{
|
||||
char *stream = stream_init();
|
||||
|
||||
|
|
@ -95,18 +107,13 @@ char *peek_token(void)
|
|||
{
|
||||
break;
|
||||
}
|
||||
else if (is_keyword(stream, i))
|
||||
{
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
return new_token(stream, i);
|
||||
}
|
||||
|
||||
char *pop_token(void)
|
||||
struct token *pop_token(void)
|
||||
{
|
||||
char *stream = stream_init();
|
||||
|
||||
|
|
@ -124,11 +131,6 @@ char *pop_token(void)
|
|||
{
|
||||
break;
|
||||
}
|
||||
else if (is_keyword(stream, i))
|
||||
{
|
||||
i++;
|
||||
break;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue