feat(parser + ast): function parsing support
This commit is contained in:
parent
a63632005c
commit
132f4f3a53
5 changed files with 54 additions and 11 deletions
|
|
@ -149,14 +149,57 @@ struct ast *parse_prefix(struct lexer_context *ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO NOT IMPLEMENTED
|
|
||||||
struct ast *parse_funcdec(struct lexer_context *ctx)
|
struct ast *parse_funcdec(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
(void)ctx;
|
struct token *token = PEEK_TOKEN();
|
||||||
perror("Error: usage of a not implemented function (parse_funcdec)");
|
struct ast *value = NULL;
|
||||||
|
char *func_name = NULL;
|
||||||
|
|
||||||
|
if (token->type != TOKEN_WORD)
|
||||||
|
{
|
||||||
return NULL;
|
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(name);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return ast_create_function(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
struct ast *parse_for(struct lexer_context *ctx)
|
struct ast *parse_for(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
(void)ctx;
|
(void)ctx;
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ struct ast *parse_prefix(struct lexer_context *ctx);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* @brief parses a funcdec rule
|
* @brief parses a funcdec rule
|
||||||
* @warning NOT IMPLEMENTED
|
* @warning Work in progress
|
||||||
*
|
*
|
||||||
* @code funcdec = WORD '(' ')' {'\n'} shell_command ;
|
* @code funcdec = WORD '(' ')' {'\n'} shell_command ;
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -223,7 +223,6 @@ struct ast *parse_command(struct lexer_context *ctx)
|
||||||
{
|
{
|
||||||
result = parse_shell_command(ctx);
|
result = parse_shell_command(ctx);
|
||||||
}
|
}
|
||||||
// WARNING funcdec seems to require a LL(2) parser
|
|
||||||
else if (is_first(*token, RULE_FUNCDEC))
|
else if (is_first(*token, RULE_FUNCDEC))
|
||||||
{
|
{
|
||||||
result = parse_funcdec(ctx);
|
result = parse_funcdec(ctx);
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ struct ast *ast_create_function(char *name, struct ast *value)
|
||||||
if (!function_data)
|
if (!function_data)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
function_data->name = strdup(name);
|
function_data->name = name;
|
||||||
function_data->value = value;
|
function_data->value = value;
|
||||||
|
|
||||||
return ast_create(AST_FUNCTION, function_data);
|
return ast_create(AST_FUNCTION, function_data);
|
||||||
|
|
|
||||||
|
|
@ -9,18 +9,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);
|
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.
|
* Assumes that the node is of type AST_function.
|
||||||
*/
|
*/
|
||||||
struct ast_function *ast_get_function(struct ast *node);
|
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);
|
struct ast *ast_create_function(char *name, struct ast *value);
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue