feat(lexer): negation

This commit is contained in:
Matteo Flebus 2026-01-27 16:44:53 +01:00
parent c48d86c8de
commit 9003675c40
2 changed files with 7 additions and 16 deletions

View file

@ -60,34 +60,22 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
{ {
if (tok->type != TOKEN_NULL || size == 0) if (tok->type != TOKEN_NULL || size == 0)
return; return;
if (strncmp(begin, "if", size) == 0 && size == 2) if (strncmp(begin, "!", size) == 0 && size == 1)
{ tok->type = TOKEN_NEGATION;
else if (strncmp(begin, "if", size) == 0 && size == 2)
tok->type = TOKEN_IF; tok->type = TOKEN_IF;
}
else if (strncmp(begin, "fi", size) == 0 && size == 2) else if (strncmp(begin, "fi", size) == 0 && size == 2)
{
tok->type = TOKEN_FI; tok->type = TOKEN_FI;
}
else if (strncmp(begin, "then", size) == 0 && size == 4) else if (strncmp(begin, "then", size) == 0 && size == 4)
{
tok->type = TOKEN_THEN; tok->type = TOKEN_THEN;
}
else if (strncmp(begin, "else", size) == 0 && size == 4) else if (strncmp(begin, "else", size) == 0 && size == 4)
{
tok->type = TOKEN_ELSE; tok->type = TOKEN_ELSE;
}
else if (strncmp(begin, "elif", size) == 0 && size == 4) else if (strncmp(begin, "elif", size) == 0 && size == 4)
{
tok->type = TOKEN_ELIF; tok->type = TOKEN_ELIF;
}
else if (strncmp(begin, "&&", size) == 0 && size == 2) else if (strncmp(begin, "&&", size) == 0 && size == 2)
{
tok->type = TOKEN_AND; tok->type = TOKEN_AND;
}
else if (strncmp(begin, "||", size) == 0 && size == 2) else if (strncmp(begin, "||", size) == 0 && size == 2)
{
tok->type = TOKEN_OR; tok->type = TOKEN_OR;
}
// no keywords found. // no keywords found.
if (tok->type == TOKEN_NULL) if (tok->type == TOKEN_NULL)
@ -95,7 +83,10 @@ static void set_token_keyword(struct token *tok, char *begin, ssize_t size)
tok->data = calloc(size + 1, sizeof(char)); tok->data = calloc(size + 1, sizeof(char));
if (tok->data == NULL) if (tok->data == NULL)
{
perror("could not allocate memory in lexer");
return; return;
}
strncpy(tok->data, begin, size); strncpy(tok->data, begin, size);
} }

View file

@ -49,7 +49,7 @@ enum token_type
TOKEN_LEFT_BRACKET, TOKEN_LEFT_BRACKET,
TOKEN_RIGHT_BRACKET, TOKEN_RIGHT_BRACKET,
TOKEN_PIPE, TOKEN_PIPE,
TOKEN_NEGATION, // TODO handle TOKEN_NEGATION,
// TODO merge into one and use the data field // TODO merge into one and use the data field
// (Too difficult to handle in the parser because of firsts) // (Too difficult to handle in the parser because of firsts)