Merge branch 'parser' into dev

This commit is contained in:
matteo 2026-01-31 13:26:14 +01:00
commit 7a370b1dfa
4 changed files with 53 additions and 10 deletions

View file

@ -149,14 +149,57 @@ struct ast *parse_prefix(struct lexer_context *ctx)
}
}
// TODO NOT IMPLEMENTED
struct ast *parse_funcdec(struct lexer_context *ctx)
{
(void)ctx;
perror("Error: usage of a not implemented function (parse_funcdec)");
struct token *token = PEEK_TOKEN();
struct ast *value = NULL;
char *func_name = NULL;
if (token->type != TOKEN_WORD)
{
return NULL;
}
// word -> func name
POP_TOKEN();
func_name = strdup(token->data);
// (
token = PEEK_TOKEN();
if (token->type != TOKEN_LEFT_PAREN)
{
free(func_name);
return NULL;
}
POP_TOKEN();
// )
token = PEEK_TOKEN();
if (token->type != TOKEN_RIGHT_PAREN)
{
free(func_name);
return NULL;
}
POP_TOKEN();
token = PEEK_TOKEN();
// { \n }
while (token->type == TOKEN_NEWLINE)
{
POP_TOKEN();
token = PEEK_TOKEN();
}
// shell_command -> value
value = parse_shell_command(ctx);
if (value == NULL)
{
free(func_name);
return NULL;
}
return ast_create_function(func_name, value);
}
struct ast *parse_for(struct lexer_context *ctx)
{
(void)ctx;

View file

@ -26,7 +26,7 @@ struct ast *parse_prefix(struct lexer_context *ctx);
/*
* @brief parses a funcdec rule
* @warning NOT IMPLEMENTED
* @warning Work in progress
*
* @code funcdec = WORD '(' ')' {'\n'} shell_command ;
*

View file

@ -223,7 +223,6 @@ struct ast *parse_command(struct lexer_context *ctx)
{
result = parse_shell_command(ctx);
}
// WARNING funcdec seems to require a LL(2) parser
else if (is_first(*token, RULE_FUNCDEC))
{
result = parse_funcdec(ctx);

View file

@ -10,18 +10,19 @@ struct ast_function
};
/**
* Checks if the given AST node is an ast_function
* @brief: Checks if the given AST node is an ast_function
*/
bool ast_is_function(struct ast *node);
/**
* Retrieves the function data from the given AST node.
* @brief: Retrieves the function data from the given AST node.
* Assumes that the node is of type AST_function.
*/
struct ast_function *ast_get_function(struct ast *node);
/**
* Creates a new AST node representing an AST_function
* @brief: Creates a new AST node representing an AST_function
* @warning: name must be already allocated.
*/
struct ast *ast_create_function(char *name, struct ast *value);
/*