This commit is contained in:
Maieul BOYER 2024-05-29 16:41:40 +02:00
parent d16b39091a
commit ff1670e264
No known key found for this signature in database
15 changed files with 435 additions and 243 deletions

View file

@ -15,7 +15,6 @@
#include "ast/forward.h"
#include "me/types.h"
#include <iso646.h>
/// @brief Node types enumeration
/// @details This enumeration is used to identify the type of a node
@ -36,6 +35,7 @@ enum e_ast_type
TY_COMMAND_BACKTICKS,
TY_COMMAND_SUBSTITUTION,
TY_COMPOUND_LIST,
TY_DOUBLE_QUOTE_STRING,
TY_ELIF_CLAUSE,
TY_ELSE_CLAUSE,
TY_FOR_COMMAND,
@ -200,9 +200,9 @@ struct s_not
struct s_pipe_list
{
t_ast_type type;
t_command *cmds;
t_usize cmds_len;
t_ast_type type;
t_ast_node **cmds;
t_usize cmds_len;
};
union u_command_inner {
@ -433,7 +433,7 @@ struct s_assignment
struct s_ast_string
{
t_ast_type type;
t_str *value;
t_str value;
};
struct s_name
@ -490,6 +490,14 @@ union u_expension {
t_arithmetic_expansion *arithmetic_expansion;
t_command_substitution *command_substitution;
t_command_backticks *command_backticks;
t_double_quote_string *double_quote_string;
};
struct s_double_quote_string
{
t_ast_type type;
t_expension_or_string *parts;
t_usize parts_len;
};
struct s_parameter_expansion
@ -498,7 +506,7 @@ struct s_parameter_expansion
t_op_in op_pre;
t_ast_string *param;
t_op_in op_in;
t_ast_string *_Nullable word;
t_word *_Nullable word;
};
struct s_arithmetic_expansion
@ -536,6 +544,7 @@ union u_ast_node {
t_command_backticks command_backticks;
t_command_substitution command_substitution;
t_compound_list compound_list;
t_double_quote_string double_quote_string;
t_elif_clause elif_clause;
t_else_clause else_clause;
t_for_command for_command;

View file

@ -36,6 +36,7 @@ typedef struct s_command_backticks t_command_backticks;
typedef struct s_command_substitution t_command_substitution;
typedef struct s_command t_command;
typedef struct s_compound_list t_compound_list;
typedef struct s_double_quote_string t_double_quote_string;
typedef struct s_elif_clause t_elif_clause;
typedef struct s_else_clause t_else_clause;
typedef struct s_for_command t_for_command;
@ -55,7 +56,7 @@ typedef struct s_simple_command t_simple_command;
typedef struct s_subshell_command t_subshell_command;
typedef struct s_until_command t_until_command;
typedef struct s_while_command t_while_command;
typedef struct s_word t_word;
typedef struct s_word t_word;
typedef union u_and_list_body t_and_list_body;
typedef union u_and_or_list_body t_and_or_list_body;

View file

@ -13,6 +13,7 @@
#ifndef FROM_PROGRAM_C
#define FROM_PROGRAM_C
#include "forward.h"
#include "me/types.h"
#include "app/node.h"
@ -90,6 +91,11 @@ t_command *build_command(t_node *node, t_usize size);
/// @note can be null in case of error
t_compound_list *build_compound_list(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
t_double_quote_string *build_double_quote_string(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error
@ -190,7 +196,6 @@ t_while_command *build_while_command(t_node *node, t_usize size);
/// @note can be null in case of error
t_word *build_word(t_node *node, t_usize size);
/// @param node a pointer to an array of `t_node` of size `size`
/// @param size the size of the nodes
/// @note can be null in case of error

View file

@ -1,2 +1 @@
build_ast
from_node

View file

@ -1,13 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* build_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/25 20:41:33 by maiboyer #+# #+# */
/* Updated: 2024/05/25 20:41:48 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "ast/ast.h"

View file

@ -6,10 +6,11 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/28 13:18:44 by maiboyer #+# #+# */
/* Updated: 2024/05/29 00:51:19 by maiboyer ### ########.fr */
/* Updated: 2024/05/29 13:41:57 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "me/str/str.h"
#include "me/types.h"
#include "app/node.h"
@ -17,6 +18,15 @@
#include "gmr/symbols.h"
#include <stdio.h>
t_ast_node *alloc_node(t_ast_type ty)
{
t_ast_node *ptr;
ptr = mem_alloc(sizeof(*ptr));
ptr->type = ty;
return (ptr);
}
t_ast_node *build_comment(t_node *node, t_usize size)
{
t_ast_node *ptr;
@ -24,8 +34,153 @@ t_ast_node *build_comment(t_node *node, t_usize size)
(void)(node);
(void)(size);
ptr = mem_alloc(sizeof(*ptr));
ptr->type = TY_EMPTY;
ptr = alloc_node(TY_EMPTY);
return (ptr);
}
t_and_list *build_and_list(t_node *node, t_usize size)
{
t_and_list *ptr;
t_usize i;
t_usize j;
(void)(size);
if (node == NULL || node->kind != sym_list || size == 0 ||
node->childs_count <= 1)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_AND_LIST);
ptr->cmds = mem_alloc_array(sizeof(t_ast_node *), node->childs_count);
i = 0;
j = 0;
while (i < node->childs_count)
{
if ((node->childs[i].kind == anon_sym_AMP_AMP ||
node->childs[i].kind == anon_sym_PIPE_PIPE) &&
(i++, true))
continue;
ptr->cmds[j++].cmd = (void *)from_node(&node->childs[i]);
i++;
}
ptr->cmds_len = j;
return (ptr);
}
t_or_list *build_or_list(t_node *node, t_usize size)
{
t_or_list *ptr;
t_usize i;
t_usize j;
(void)(size);
if (node == NULL || node->kind != sym_list || size == 0 ||
node->childs_count <= 1)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_OR_LIST);
ptr->cmds = mem_alloc_array(sizeof(t_ast_node *), node->childs_count);
i = 0;
j = 0;
while (i < node->childs_count)
{
if ((node->childs[i].kind == anon_sym_AMP_AMP ||
node->childs[i].kind == anon_sym_PIPE_PIPE) &&
(i++, true))
continue;
ptr->cmds[j++].cmd = (void *)from_node(&node->childs[i]);
i++;
}
ptr->cmds_len = j;
return (ptr);
}
t_ast_string *build_ast_string(t_node *node, t_usize size)
{
t_ast_string *ptr;
(void)(size);
if (node == NULL ||
(node->kind != sym_string_content && node->kind != sym_raw_string &&
node->kind == sym_ansi_c_string) ||
size == 0)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_AST_STRING);
ptr->value = str_clone(node_getstr(node));
return (ptr);
}
t_and_or_list *build_and_or_list(t_node *node, t_usize size)
{
t_and_or_list *ptr;
(void)(node);
(void)(size);
if (node == NULL || node->kind != sym_list || size == 0 ||
node->childs_count <= 1)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_AND_OR_LIST);
if (node->childs[1].kind == anon_sym_PIPE_PIPE)
ptr->cmds.or_list = build_or_list(node, 1);
else if (node->childs[1].kind == anon_sym_AMP_AMP)
ptr->cmds.and_list = build_and_list(node, 1);
else
return (mem_free(ptr), NULL);
return (ptr);
}
t_pipe_list *build_pipe_list(t_node *node, t_usize size)
{
t_pipe_list *ptr;
t_usize i;
t_usize j;
(void)(size);
if (node == NULL || node->kind != sym_pipeline || size == 0 ||
node->childs_count <= 1)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_PIPE_LIST);
ptr->cmds = mem_alloc_array(sizeof(t_ast_node *), node->childs_count);
i = 0;
j = 0;
while (i < node->childs_count)
{
if (node->childs[i].kind == anon_sym_PIPE)
i++;
ptr->cmds[j++] = from_node(&node->childs[i]);
i++;
}
return (ptr);
}
t_double_quote_string *build_double_qoute_string(t_node *node, t_usize size)
{
t_double_quote_string *ptr;
t_usize i;
t_usize j;
(void)(size);
if (node == NULL || node->kind != sym_string || size == 0)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_DOUBLE_QUOTE_STRING);
ptr->parts = mem_alloc_array(sizeof(*ptr->parts), node->childs_count);
i = 0;
j = 0;
while (i < node->childs_count)
ptr->parts[j++].expension->type = (void *)from_node(&node->childs[i++]);
ptr->parts_len = j;
return (ptr);
}
t_not *build_not(t_node *node, t_usize size)
{
t_not *ptr;
(void)(size);
if (node == NULL || node->kind != sym_negated_command || size == 0)
me_abort("Invalid arguments to build ast!");
ptr = (void *)alloc_node(TY_NOT);
ptr->cmd.type = (void *)from_node(&node->childs[1]);
return (ptr);
}
@ -36,9 +191,10 @@ t_ast_node *from_node(t_node *node)
if (node->kind == sym_program)
return ((t_ast_node *)build_program(node, 1));
if (node->kind == sym_word)
return ((t_ast_node *)build_word(node, 1));
if (node->kind == sym_string)
return ((t_ast_node *)build_double_quote_string(node, 1));
if (node->kind == sym_word)
return ((t_ast_node *)build_word(node, 1));
if (node->kind == sym_concatenation)
return ((t_ast_node *)build_word(node, 1));
@ -81,6 +237,12 @@ t_ast_node *from_node(t_node *node)
return ((t_ast_node *)build_command(node, 1));
if (node->kind == sym_command)
return ((t_ast_node *)build_command(node, 1));
if (node->kind == sym_test_command)
return ((t_ast_node *)build_command(node, 1));
if (node->kind == sym_declaration_command)
return ((t_ast_node *)build_command(node, 1));
if (node->kind == sym_unset_command)
return ((t_ast_node *)build_command(node, 1));
if (node->kind == sym_file_redirect)
return ((t_ast_node *)build_redirect_file(node, 1));
@ -100,6 +262,35 @@ t_ast_node *from_node(t_node *node)
if (node->kind == sym_brace_expression)
return ((t_ast_node *)build_brace_command(node, 1));
if (node->kind == sym_compound_statement)
return ((t_ast_node *)build_compound_list(node, 1));
if (node->kind == sym_if_statement)
return ((t_ast_node *)build_if_command(node, 1));
if (node->kind == sym_else_clause)
return ((t_ast_node *)build_else_clause(node, 1));
if (node->kind == sym_elif_clause)
return ((t_ast_node *)build_elif_clause(node, 1));
if (node->kind == sym_case_statement)
return ((t_ast_node *)build_case_command(node, 1));
if (node->kind == sym_case_item)
return ((t_ast_node *)build_case_item(node, 1));
if (node->kind == sym_last_case_item)
return ((t_ast_node *)build_case_item(node, 1));
if (node->kind == sym_while_statement)
return ((t_ast_node *)build_while_command(node, 1));
if (node->kind == sym_for_statement)
return ((t_ast_node *)build_for_command(node, 1));
if (node->kind == sym_function_definition)
return ((t_ast_node *)build_function_definition(node, 1));
printf("unknown node of kind '%s'\n", node->kind_str);
return (NULL);