feat(lexer): implementing assignements

This commit is contained in:
matteo 2026-01-29 11:18:56 +01:00
parent e1291107d4
commit f0a7173b11
3 changed files with 41 additions and 6 deletions

View file

@ -78,6 +78,21 @@ static bool update_lexing_mode(char *stream, ssize_t i,
return *lexing_mode != mode_before_update; return *lexing_mode != mode_before_update;
} }
/* @brief: updates the flags only_digits and equal_count
* according to the character at stream[i].
*/
static void update_flags(char *stream, ssize_t i, struct lexer_context *ctx)
{
if (stream[i] == '=')
{
ctx->equal_count++;
}
else if (!isdigit(stream[i]) && ctx->only_digits)
{
ctx->only_digits = false;
}
}
struct token *peek_token(struct lexer_context *ctx) struct token *peek_token(struct lexer_context *ctx)
{ {
// we already created the upcoming token during the previous call to peek() // we already created the upcoming token during the previous call to peek()
@ -100,6 +115,7 @@ struct token *peek_token(struct lexer_context *ctx)
if (!update_lexing_mode(stream, i, &lexing_mode) if (!update_lexing_mode(stream, i, &lexing_mode)
&& lexing_mode == LEXER_NORMAL) && lexing_mode == LEXER_NORMAL)
{ {
update_flags(stream, i, ctx);
if (is_special_char(stream, i)) if (is_special_char(stream, i))
{ {
if (i == 0) // where we create spe_char token if (i == 0) // where we create spe_char token
@ -121,7 +137,7 @@ struct token *peek_token(struct lexer_context *ctx)
i++; i++;
} }
struct token *tok = new_token(stream, i, ctx->only_digits); struct token *tok = new_token(stream, i, ctx->only_digits, ctx->equal_count);
// if token is comment, we don't want it // if token is comment, we don't want it
if (tok->type == TOKEN_COMMENT) if (tok->type == TOKEN_COMMENT)

View file

@ -14,6 +14,9 @@ struct lexer_context
// tells us if we only lexed digits in current token. // tells us if we only lexed digits in current token.
bool only_digits; bool only_digits;
// usefull to detect assignments, and syntax errors with '='.
int equal_count;
struct token *previous_token; struct token *previous_token;
struct token *current_token; struct token *current_token;
}; };
@ -34,10 +37,13 @@ enum token_type
// Blanks // Blanks
TOKEN_NULL = 0, TOKEN_NULL = 0,
TOKEN_EOF, TOKEN_EOF,
TOKEN_WORD,
TOKEN_NEWLINE, TOKEN_NEWLINE,
// SPecial characters // words
TOKEN_WORD,
TOKEN_ASSIGNMENT_WORD,
// Special characters
TOKEN_GRAVE, TOKEN_GRAVE,
TOKEN_SEMICOLON, TOKEN_SEMICOLON,
TOKEN_COMMENT, TOKEN_COMMENT,
@ -87,12 +93,13 @@ struct token
*/ */
bool is_special_char(char *stream, ssize_t i); bool is_special_char(char *stream, ssize_t i);
/* @brief: return a newly allocated token, with the corresponding type. /* @brief: return a newly allocated token, with the type corresponding
* to the context given in arguments.
* The data contains [size] char, starting from [begin]. * The data contains [size] char, starting from [begin].
* *
* @return: NULL on error, a token otherwise. * @return: NULL on error, a token otherwise.
*/ */
struct token *new_token(char *begin, ssize_t size, bool only_digits); struct token *new_token(char *begin, ssize_t size, bool only_digits, int equal_count);
/* @brief: frees the token given in argument /* @brief: frees the token given in argument
*/ */

View file

@ -66,5 +66,17 @@ struct ast *parse_redirection(struct lexer_context *ctx)
struct ast *parse_prefix(struct lexer_context *ctx) struct ast *parse_prefix(struct lexer_context *ctx)
{ {
struct token *token = TOKEN_PEEK();
if (token->type == TOKEN_ASSIGNMENT_WORD)
{
token = TOKEN_POP();
return ast_create_assignment_word(token->data);
}
else if (is_first(*token, RULE_REDIRECTION))
return parse_redirection(ctx); return parse_redirection(ctx);
else
{
perror("Syntax error: expected a prefix (redirection or assignment)");
return NULL;
}
} }