Starting to work on ternary parser

This commit is contained in:
Raphaël 2024-07-22 15:43:17 +02:00
parent a00653bd30
commit 3260d8704e
4 changed files with 54 additions and 12 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 14:23:40 by maiboyer #+# #+# */ /* Created: 2024/06/11 14:23:40 by maiboyer #+# #+# */
/* Updated: 2024/07/21 17:42:37 by rparodi ### ########.fr */ /* Updated: 2024/07/22 14:27:54 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,6 +21,7 @@ enum e_ast_node_kind
AST_ARITHMETIC_BINARY, AST_ARITHMETIC_BINARY,
AST_ARITHMETIC_EXPANSION, //RAPH AST_ARITHMETIC_EXPANSION, //RAPH
AST_ARITHMETIC_LITTERAL, //RAPH AST_ARITHMETIC_LITTERAL, //RAPH
AST_ARITHMETIC_POSTFIX, //RAPH
AST_CASE, AST_CASE,
AST_CASE_ITEM, AST_CASE_ITEM,
AST_COMMAND, AST_COMMAND,
@ -53,6 +54,7 @@ union u_ast_node_data
t_ast_arithmetic_binary arithmetic_binary; t_ast_arithmetic_binary arithmetic_binary;
t_ast_arithmetic_literal arithmetic_literal; //RAPH t_ast_arithmetic_literal arithmetic_literal; //RAPH
t_ast_arithmetic_expansion arithmetic_expansion; //RAPH t_ast_arithmetic_expansion arithmetic_expansion; //RAPH
t_ast_arithmetic_postfix arithmetic_postfix; //RAPH
t_ast_case case_; t_ast_case case_;
t_ast_case_item case_item; t_ast_case_item case_item;
t_ast_command command; t_ast_command command;

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 14:24:24 by maiboyer #+# #+# */ /* Created: 2024/06/11 14:24:24 by maiboyer #+# #+# */
/* Updated: 2024/07/21 17:43:13 by rparodi ### ########.fr */ /* Updated: 2024/07/22 14:30:41 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,6 +27,7 @@ typedef struct s_ast_node *t_ast_node;
typedef struct s_ast_arithmetic_expansion t_ast_arithmetic_expansion; typedef struct s_ast_arithmetic_expansion t_ast_arithmetic_expansion;
typedef struct s_ast_arithmetic_literal t_ast_arithmetic_literal; typedef struct s_ast_arithmetic_literal t_ast_arithmetic_literal;
typedef struct s_ast_arithmetic_binary t_ast_arithmetic_binary; typedef struct s_ast_arithmetic_binary t_ast_arithmetic_binary;
typedef struct s_ast_arithmetic_postfix t_ast_arithmetic_postfix;
typedef struct s_ast_case t_ast_case; typedef struct s_ast_case t_ast_case;
typedef struct s_ast_case_item t_ast_case_item; typedef struct s_ast_case_item t_ast_case_item;
typedef struct s_ast_command t_ast_command; typedef struct s_ast_command t_ast_command;

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/14 17:46:58 by maiboyer #+# #+# */ /* Created: 2024/06/14 17:46:58 by maiboyer #+# #+# */
/* Updated: 2024/07/22 13:18:49 by maiboyer ### ########.fr */ /* Updated: 2024/07/22 14:27:09 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -45,6 +45,8 @@ enum e_ast_arithmetic_operator
ARITH_DIVIDE, ARITH_DIVIDE,
ARITH_MULT, ARITH_MULT,
ARITH_MOD, ARITH_MOD,
ARITH_INCREMENT,
ARITH_DECREMENT,
}; };
/* /*
@ -424,17 +426,18 @@ struct s_ast_arithmetic_binary
t_ast_node rhs; t_ast_node rhs;
}; };
/// NUMBER
/*struct s_ast_arithmetic_literal*/
/*{*/
/* t_i64 value;*/
/*};*/
// RAPH // RAPH
struct s_ast_arithmetic_literal struct s_ast_arithmetic_literal
{ {
t_ast_node value; t_ast_node value;
}; };
struct s_ast_arithmetic_postfix
{
t_ast_node value;
t_ast_arithmetic_operator op;
};
/// Command Substitution /// Command Substitution
/// ```shell /// ```shell
/// $(command) /// $(command)

View file

@ -82,6 +82,10 @@ void ast_free(t_ast_node elem)
{ {
ast_free(elem->data.arithmetic_expansion.expr); ast_free(elem->data.arithmetic_expansion.expr);
} }
if (elem->kind == AST_ARITHMETIC_POSTFIX)
{
ast_free(elem->data.arithmetic_postfix.value);
}
if (elem->kind == AST_ARITHMETIC_BINARY) if (elem->kind == AST_ARITHMETIC_BINARY)
{ {
ast_free(elem->data.arithmetic_binary.lhs); ast_free(elem->data.arithmetic_binary.lhs);
@ -225,6 +229,11 @@ t_ast_node ast_alloc(t_ast_node_kind kind)
{ {
ret->data.arithmetic_expansion.expr = NULL; ret->data.arithmetic_expansion.expr = NULL;
} }
if (kind == AST_ARITHMETIC_POSTFIX)
{
ret->data.arithmetic_postfix.value = NULL;
ret->data.arithmetic_postfix.op = 0;
}
if (kind == AST_ARITHMETIC_BINARY) if (kind == AST_ARITHMETIC_BINARY)
{ {
ret->data.arithmetic_binary.lhs = NULL; ret->data.arithmetic_binary.lhs = NULL;
@ -568,6 +577,7 @@ t_ast_redirection_kind _get_redirection_op(t_parse_node self)
return (me_abort("invalid redirection symbol"), 0); return (me_abort("invalid redirection symbol"), 0);
} }
// RAPH
t_ast_arithmetic_operator _parse_operator(t_parse_node self) t_ast_arithmetic_operator _parse_operator(t_parse_node self)
{ {
t_symbol symbol; t_symbol symbol;
@ -583,6 +593,10 @@ t_ast_arithmetic_operator _parse_operator(t_parse_node self)
return (ARITH_DIVIDE); return (ARITH_DIVIDE);
if (symbol == anon_sym_PERCENT) if (symbol == anon_sym_PERCENT)
return (ARITH_MOD); return (ARITH_MOD);
if (symbol == anon_sym_PLUS_PLUS)
return (ARITH_INCREMENT);
if (symbol == anon_sym_DASH_DASH)
return (ARITH_DECREMENT);
return (me_abort("invalid arithmetic operator"), 0); return (me_abort("invalid arithmetic operator"), 0);
} }
@ -623,12 +637,12 @@ t_error build_sym_command_substitution(t_parse_node self, t_const_str input, t_a
/* FUNCTION DONE*/ /* FUNCTION DONE*/
t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_literal(t_parse_node self, t_const_str input, t_ast_node *out); t_error build_sym_arithmetic_literal(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_parenthesized_expression(t_parse_node self, t_const_str input, t_ast_node *out);
t_error build_sym_arithmetic_postfix_expression(t_parse_node self, t_const_str input, t_ast_node *out);
/* FUNCTION THAT ARE NOT DONE */ /* FUNCTION THAT ARE NOT DONE */
// TODO: This is your homework raph // TODO: This is your homework raph
t_error build_sym_arithmetic_parenthesized_expression(t_parse_node self, t_const_str input, t_ast_node *out);
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_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_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_arithmetic_expansion(t_parse_node self, t_const_str input, t_ast_node *out);
@ -695,6 +709,28 @@ t_error build_sym_arithmetic_parenthesized_expression(t_parse_node self, t_const
return (ast_from_node(ts_node_child(self, 1), input, out)); return (ast_from_node(ts_node_child(self, 1), input, out));
} }
t_error build_sym_arithmetic_postfix_expression(t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
t_ast_node tmp;
t_usize i;
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_arithmetic_postfix_expression)
return (ERROR);
ret = ast_alloc(AST_ARITHMETIC_POSTFIX);
if (ts_node_child_count(self) != 2)
return (ast_free(ret), ERROR);
if (ast_from_node(ts_node_child(self, 0), input, &ret->data.arithmetic_postfix.value))
return (ast_free(ret), ERROR);
if (ts_node_field_id_for_child(self, 1) == field_op)
ret->data.arithmetic_postfix.op = _parse_operator(ts_node_child(self, i));
else
return (ast_free(ret), ERROR);
return (*out = ret, NO_ERROR);
}
t_error build_sym_command_substitution(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_ast_node ret; t_ast_node ret;