very simple ast expension working

This commit is contained in:
Maieul BOYER 2024-07-02 13:11:42 +02:00
parent dc3f8cfba9
commit c10ce8b165
No known key found for this signature in database
5 changed files with 118 additions and 25 deletions

View file

@ -43,7 +43,6 @@ enum e_ast_node_kind
AST_VARIABLE_ASSIGNMENT = S_AST_NONE | 0x000E,
AST_EXTGLOB = S_AST_NONE | 0x001A,
AST_REGEX = S_AST_NONE | 0x001B,
AST_NUMBER = S_AST_NONE | 0x001C,
AST_FILE_REDIRECTION = S_AST_REDIRECT | 0x000F,
AST_HEREDOC_REDIRECTION = S_AST_REDIRECT | 0x0010,
@ -79,7 +78,6 @@ union u_ast_node_data {
t_ast_heredoc_redirection heredoc_redirection;
t_ast_if if_;
t_ast_list list;
t_ast_number number;
t_ast_pipeline pipeline;
t_ast_program program;
t_ast_raw_string raw_string;

View file

@ -41,7 +41,6 @@ typedef struct s_ast_function_definition t_ast_function_definition;
typedef struct s_ast_heredoc_redirection t_ast_heredoc_redirection;
typedef struct s_ast_if t_ast_if;
typedef struct s_ast_list t_ast_list;
typedef struct s_ast_number t_ast_number;
typedef struct s_ast_pipeline t_ast_pipeline;
typedef struct s_ast_program t_ast_program;
typedef struct s_ast_raw_string t_ast_raw_string;

View file

@ -396,13 +396,4 @@ struct s_ast_regex
t_str pattern;
};
/// Regex
/// ```shell
/// ~pattern
/// ```
struct s_ast_number
{
t_i64 number;
};
#endif /* AST_RAW_STRUCTS_H */

View file

@ -213,9 +213,7 @@ void ast_free(t_ast_node elem)
{
mem_free(elem->data.regex.pattern);
}
if (elem->kind == AST_NUMBER)
{
}
mem_free(elem);
}
@ -376,10 +374,6 @@ t_ast_node ast_alloc(t_ast_node_kind kind)
{
ret->data.regex.pattern = NULL;
}
if (kind == AST_NUMBER)
{
ret->data.number.number = 0;
}
return (ret);
}
@ -496,6 +490,57 @@ t_str _extract_str(t_parse_node self, t_const_str input)
result = str_substring(input, start, end - start);
return (result);
}
/*
E_OP_NONE = 0, // ${var}
E_OP_DEFAULT, // ${var-word}
E_OP_ASSIGN_DEFAULT, // ${var=word}
E_OP_ERROR, // ${var?word}
E_OP_ALTERNATE, // ${var+word}
E_OP_DEFAULT_COLON, // ${var:-word}
E_OP_ASSIGN_DEFAULT_COLON, // ${var:=word}
E_OP_ERROR_COLON, // ${var:?word}
E_OP_ALTERNATE_COLON, // ${var:+word}
E_OP_LENGTH, // ${#var}
E_OP_SMALLEST_PREFIX, // ${var#pattern}
E_OP_LARGEST_PREFIX, // ${var##pattern}
E_OP_SMALLEST_SUFFIX, // ${var%pattern}
E_OP_LARGEST_SUFFIX, // ${var%%pattern}
*/
t_ast_expansion_operator _extract_exp_op(t_parse_node self, t_usize child_index)
{
t_ast_expansion_operator kind;
t_symbol symbol;
kind = E_OP_NONE;
symbol = ts_node_grammar_symbol(self);
if (symbol == anon_sym_DASH)
kind = E_OP_DEFAULT;
if (symbol == anon_sym_EQ)
kind = E_OP_ASSIGN_DEFAULT;
if (symbol == anon_sym_QMARK)
kind = E_OP_ERROR;
if (symbol == anon_sym_PLUS)
kind = E_OP_ALTERNATE;
if (symbol == anon_sym_COLON_DASH)
kind = E_OP_DEFAULT_COLON;
if (symbol == anon_sym_COLON_EQ)
kind = E_OP_ASSIGN_DEFAULT_COLON;
if (symbol == anon_sym_COLON_QMARK)
kind = E_OP_ERROR_COLON;
if (symbol == anon_sym_COLON_PLUS)
kind = E_OP_ALTERNATE_COLON;
if (symbol == anon_sym_POUND)
kind = E_OP_SMALLEST_PREFIX;
if (symbol == sym__immediate_double_hash)
kind = E_OP_LARGEST_PREFIX;
if (symbol == anon_sym_PERCENT)
kind = E_OP_SMALLEST_SUFFIX;
if (symbol == anon_sym_PERCENT_PERCENT)
kind = E_OP_LARGEST_SUFFIX;
if (symbol == anon_sym_POUND && child_index == 1)
kind = E_OP_LENGTH;
return (kind);
}
t_ast_redirection_kind _get_redirection_op(t_parse_node self)
{
@ -554,6 +599,8 @@ t_error build_sym_variable_assignment(t_parse_node self, t_const_str input, t_as
t_error build_sym_while_statement(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_word(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_simple_expansion(t_parse_node self, t_const_str input, t_ast_node *out);
/* FUNCTION THAT ARE NOT DONE */
t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str input, t_ast_node *out);
@ -562,16 +609,14 @@ t_error build_sym_arithmetic_parenthesized_expression(t_parse_node self, t_const
t_error build_sym_arithmetic_postfix_expression(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_ternary_expression(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_unary_expression(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_expansion(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_command_substitution(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_expansion(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_expansion_expression(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_expansion_regex(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_simple_expansion(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_heredoc_redirect(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_simple_heredoc_body(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_heredoc_body(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_heredoc_content(t_parse_node self, t_const_str input, t_ast_node *out);
@ -580,6 +625,66 @@ t_error build_sym_heredoc_start(t_parse_node self, t_const_str input, t_ast_node
#include <stdio.h>
t_error build_sym_expansion(t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
t_ast_node tmp;
t_usize i;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_simple_expansion)
return (ERROR);
ret = ast_alloc(AST_EXPANSION);
ret->data.expansion.kind = E_OP_NONE;
i = 0;
while (i < ts_node_child_count(self))
{
if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true))
continue;
if (ts_node_field_id_for_child(self, i) == field_variable)
ret->data.expansion.var_name = _extract_str(ts_node_child(self, i), input);
else if (ts_node_field_id_for_child(self, i) == field_op)
ret->data.expansion.kind = _extract_exp_op(ts_node_child(self, i), i);
else
{
if (ast_from_node(ts_node_child(self, i), input, &tmp))
return (ast_free(ret), ERROR);
vec_ast_push(&ret->data.expansion.args, tmp);
}
i++;
}
return (*out = ret, NO_ERROR);
}
t_error build_sym_simple_expansion(t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
t_usize i;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_simple_expansion)
return (ERROR);
ret = ast_alloc(AST_EXPANSION);
ret->data.expansion.kind = E_OP_NONE;
i = 0;
while (i < ts_node_child_count(self))
{
if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true))
continue;
ret->data.expansion.var_name = _extract_str(ts_node_child(self, i), input);
i++;
}
return (*out = ret, NO_ERROR);
}
t_error build_sym_file_descriptor(t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
@ -589,7 +694,7 @@ t_error build_sym_file_descriptor(t_parse_node self, t_const_str input, t_ast_no
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_regex)
if (ts_node_symbol(self) != sym_file_descriptor)
return (ERROR);
ret = ast_alloc(AST_RAW_STRING);
ret->data.raw_string.str = _extract_str(self, input);
@ -606,7 +711,7 @@ t_error build_sym_number(t_parse_node self, t_const_str input, t_ast_node *out)
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_regex)
if (ts_node_symbol(self) != sym_number)
return (ERROR);
ret = ast_alloc(AST_RAW_STRING);
ret->data.raw_string.str = _extract_str(self, input);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/28 18:36:40 by maiboyer #+# #+# */
/* Updated: 2024/06/29 21:18:46 by maiboyer ### ########.fr */
/* Updated: 2024/07/02 13:11:18 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */