feat(lexer): rework with struct token done
This commit is contained in:
parent
204bb515af
commit
8262fdece8
2 changed files with 65 additions and 14 deletions
|
|
@ -31,42 +31,92 @@ static bool is_special_char(char c)
|
||||||
return c == '\'' || c == '\n' || c == ';' || c == 'EOF';
|
return c == '\'' || c == '\n' || c == ';' || c == 'EOF';
|
||||||
}
|
}
|
||||||
|
|
||||||
static void new_token_keyword(struct token *tok, char *begin, ssize_t size)
|
|
||||||
|
/* @brief: if a special character is found at [begin],
|
||||||
|
* [tok->token_type] is set accordingly
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static void set_token_spechar(struct token *tok, char *begin, ssize_t size)
|
||||||
{
|
{
|
||||||
|
if (size != 1)
|
||||||
|
return;
|
||||||
|
if (begin[0] == 'EOF')
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_EOF;
|
||||||
|
}
|
||||||
|
else if (begin[0] == ';')
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_NEWLINE;
|
||||||
|
}
|
||||||
|
else if (begin[0] == '\'')
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_QUOTE;
|
||||||
|
}
|
||||||
|
else if (begin[0] == ';')
|
||||||
|
{
|
||||||
|
tok->type = TOKEN_SEMICOLON;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* @brief: if a keyword is found at [begin],
|
||||||
|
* [tok->token_type] is set accordingly
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
|
||||||
|
{
|
||||||
|
if (tok->type != TOKEN_NULL)
|
||||||
|
return;
|
||||||
if (strncmp(begin, "if", size) == 0)
|
if (strncmp(begin, "if", size) == 0)
|
||||||
{
|
{
|
||||||
tok->type = TOKEN_IF;
|
tok->type = TOKEN_IF;
|
||||||
}
|
}
|
||||||
if (strncmp(begin, "fi", size) == 0)
|
else if (strncmp(begin, "fi", size) == 0)
|
||||||
{
|
{
|
||||||
tok->type = TOKEN_FI;
|
tok->type = TOKEN_FI;
|
||||||
}
|
}
|
||||||
if (strncmp(begin, "then", size) == 0)
|
else if (strncmp(begin, "then", size) == 0)
|
||||||
{
|
{
|
||||||
tok->type = TOKEN_THEN;
|
tok->type = TOKEN_THEN;
|
||||||
}
|
}
|
||||||
if (strncmp(begin, "else", size) == 0)
|
else if (strncmp(begin, "else", size) == 0)
|
||||||
{
|
{
|
||||||
tok->type = TOKEN_ELSE;
|
tok->type = TOKEN_ELSE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* @brief: if token_type has not yet been set, then it is a TOKEN_WORD
|
||||||
|
* Also allocates the data and fills it.
|
||||||
|
*/
|
||||||
|
static void set_token_word(struct token *tok, char *begin, ssize_t size)
|
||||||
|
{
|
||||||
|
if (tok->token_type == TOKEN_NULL)
|
||||||
|
{
|
||||||
|
char *token_data = calloc(size + 1, sizeof(char));
|
||||||
|
strncpy(res, begin, size);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
struct token *new_token(char *begin, ssize_t size)
|
struct token *new_token(char *begin, ssize_t size)
|
||||||
{
|
{
|
||||||
struct token *res = calloc(1, sizeof(struct token));
|
struct token *tok = calloc(1, sizeof(struct token));
|
||||||
if (res == NULL)
|
if (tok == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
// checks which type of token
|
set_token_spechar(tok, begin, size);
|
||||||
|
set_token_keyword(tok, begin, size);
|
||||||
|
set_token_word(tok, begin, size);
|
||||||
|
|
||||||
// is special char
|
return tok;
|
||||||
|
}
|
||||||
|
|
||||||
// is keyword
|
void free_token(struct token *tok)
|
||||||
|
{
|
||||||
// otherwise -> WORD
|
if (tok == NULL)
|
||||||
char *token_data = calloc(size + 1, sizeof(char));
|
return;
|
||||||
strncpy(res, begin, size);
|
if (tok->data != NULL)
|
||||||
return res;
|
free(tok->data);
|
||||||
|
free(tok);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *stream_init(void)
|
char *stream_init(void)
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
enum token_type
|
enum token_type
|
||||||
{
|
{
|
||||||
|
TOKEN_NULL = 0,
|
||||||
TOKEN_EOF,
|
TOKEN_EOF,
|
||||||
TOKEN_WORD,
|
TOKEN_WORD,
|
||||||
TOKEN_NEWLINE,
|
TOKEN_NEWLINE,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue