Just one last funtion of my homework \!
This commit is contained in:
parent
3260d8704e
commit
f89462e25e
5 changed files with 79 additions and 11 deletions
|
|
@ -424,7 +424,7 @@ module.exports = grammar({
|
||||||
$._arithmetic_expression,
|
$._arithmetic_expression,
|
||||||
)),
|
)),
|
||||||
prec(PREC.UNARY, seq(
|
prec(PREC.UNARY, seq(
|
||||||
field('op', alias(tokenLiterals(1, '-', '+', '~'), $.operator)),
|
field('op', alias(tokenLiterals(1, '-', '+'), $.operator)),
|
||||||
$._arithmetic_expression,
|
$._arithmetic_expression,
|
||||||
)),
|
)),
|
||||||
prec.right(PREC.UNARY, seq(
|
prec.right(PREC.UNARY, seq(
|
||||||
|
|
|
||||||
|
|
@ -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/22 14:27:54 by rparodi ### ########.fr */
|
/* Updated: 2024/07/22 16:52:19 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -22,6 +22,8 @@ enum e_ast_node_kind
|
||||||
AST_ARITHMETIC_EXPANSION, //RAPH
|
AST_ARITHMETIC_EXPANSION, //RAPH
|
||||||
AST_ARITHMETIC_LITTERAL, //RAPH
|
AST_ARITHMETIC_LITTERAL, //RAPH
|
||||||
AST_ARITHMETIC_POSTFIX, //RAPH
|
AST_ARITHMETIC_POSTFIX, //RAPH
|
||||||
|
AST_ARITHMETIC_TERNARY, //RAPH
|
||||||
|
AST_ARITHMETIC_UNARY, //RAPH
|
||||||
AST_CASE,
|
AST_CASE,
|
||||||
AST_CASE_ITEM,
|
AST_CASE_ITEM,
|
||||||
AST_COMMAND,
|
AST_COMMAND,
|
||||||
|
|
@ -55,6 +57,8 @@ union u_ast_node_data
|
||||||
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_arithmetic_postfix arithmetic_postfix; //RAPH
|
||||||
|
t_ast_arithmetic_ternary arithmetic_ternary; //RAPH
|
||||||
|
t_ast_arithmetic_unary arithmetic_unary; //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;
|
||||||
|
|
|
||||||
|
|
@ -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/22 14:30:41 by rparodi ### ########.fr */
|
/* Updated: 2024/07/22 16:52:42 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -28,6 +28,8 @@ 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_arithmetic_postfix t_ast_arithmetic_postfix;
|
||||||
|
typedef struct s_ast_arithmetic_ternary t_ast_arithmetic_ternary;
|
||||||
|
typedef struct s_ast_arithmetic_unary t_ast_arithmetic_unary;
|
||||||
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;
|
||||||
|
|
|
||||||
|
|
@ -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 14:27:09 by rparodi ### ########.fr */
|
/* Updated: 2024/07/22 16:51:56 by rparodi ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
|
@ -47,6 +47,7 @@ enum e_ast_arithmetic_operator
|
||||||
ARITH_MOD,
|
ARITH_MOD,
|
||||||
ARITH_INCREMENT,
|
ARITH_INCREMENT,
|
||||||
ARITH_DECREMENT,
|
ARITH_DECREMENT,
|
||||||
|
ARITH_NOT,
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
@ -432,6 +433,19 @@ struct s_ast_arithmetic_literal
|
||||||
t_ast_node value;
|
t_ast_node value;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct s_ast_arithmetic_ternary //RAPH
|
||||||
|
{
|
||||||
|
t_ast_node condition;
|
||||||
|
t_ast_node then;
|
||||||
|
t_ast_node else_;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct s_ast_arithmetic_unary //RAPH
|
||||||
|
{
|
||||||
|
t_ast_arithmetic_operator operator;
|
||||||
|
t_ast_node value;
|
||||||
|
};
|
||||||
|
|
||||||
struct s_ast_arithmetic_postfix
|
struct s_ast_arithmetic_postfix
|
||||||
{
|
{
|
||||||
t_ast_node value;
|
t_ast_node value;
|
||||||
|
|
|
||||||
|
|
@ -597,6 +597,8 @@ t_ast_arithmetic_operator _parse_operator(t_parse_node self)
|
||||||
return (ARITH_INCREMENT);
|
return (ARITH_INCREMENT);
|
||||||
if (symbol == anon_sym_DASH_DASH)
|
if (symbol == anon_sym_DASH_DASH)
|
||||||
return (ARITH_DECREMENT);
|
return (ARITH_DECREMENT);
|
||||||
|
if (symbol == anon_sym_BANG)
|
||||||
|
return (ARITH_NOT);
|
||||||
return (me_abort("invalid arithmetic operator"), 0);
|
return (me_abort("invalid arithmetic operator"), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -639,12 +641,12 @@ t_error build_sym_arithmetic_binary_expression(t_parse_node self, t_const_str in
|
||||||
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_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_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);
|
||||||
|
|
||||||
/* 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_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_arithmetic_expansion(t_parse_node self, t_const_str input, t_ast_node *out);
|
||||||
|
|
||||||
// TODO: This is my homework, it'll need to be handled in a special way I feel...
|
// TODO: This is my homework, it'll need to be handled in a special way I feel...
|
||||||
|
|
@ -712,25 +714,71 @@ 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_postfix_expression(t_parse_node self, t_const_str input, t_ast_node *out)
|
||||||
{
|
{
|
||||||
t_ast_node ret;
|
t_ast_node ret;
|
||||||
t_ast_node tmp;
|
|
||||||
t_usize i;
|
|
||||||
|
|
||||||
if (out == NULL)
|
if (out == NULL)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
if (ts_node_symbol(self) != sym_arithmetic_postfix_expression)
|
if (ts_node_symbol(self) != sym_arithmetic_postfix_expression)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
ret = ast_alloc(AST_ARITHMETIC_POSTFIX);
|
|
||||||
if (ts_node_child_count(self) != 2)
|
if (ts_node_child_count(self) != 2)
|
||||||
return (ast_free(ret), ERROR);
|
return (ERROR);
|
||||||
|
ret = ast_alloc(AST_ARITHMETIC_POSTFIX);
|
||||||
if (ast_from_node(ts_node_child(self, 0), input, &ret->data.arithmetic_postfix.value))
|
if (ast_from_node(ts_node_child(self, 0), input, &ret->data.arithmetic_postfix.value))
|
||||||
return (ast_free(ret), ERROR);
|
return (ast_free(ret), ERROR);
|
||||||
if (ts_node_field_id_for_child(self, 1) == field_op)
|
if (ts_node_field_id_for_child(self, 1) == field_op)
|
||||||
ret->data.arithmetic_postfix.op = _parse_operator(ts_node_child(self, i));
|
ret->data.arithmetic_postfix.op = _parse_operator(ts_node_child(self, 1));
|
||||||
else
|
else
|
||||||
return (ast_free(ret), ERROR);
|
return (ast_free(ret), ERROR);
|
||||||
return (*out = ret, NO_ERROR);
|
return (*out = ret, NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
t_error build_sym_arithmetic_ternary_expression(t_parse_node self, t_const_str input, t_ast_node *out)
|
||||||
|
{
|
||||||
|
t_ast_node ret;
|
||||||
|
t_usize i;
|
||||||
|
|
||||||
|
if (out == NULL)
|
||||||
|
return (ERROR);
|
||||||
|
if (ts_node_child_count(self) != 5)
|
||||||
|
return (ERROR);
|
||||||
|
if (ts_node_symbol(self) != sym_arithmetic_ternary_expression)
|
||||||
|
return (ERROR);
|
||||||
|
i = 0;
|
||||||
|
ret = ast_alloc(AST_ARITHMETIC_TERNARY);
|
||||||
|
while (i < ts_node_child_count(self))
|
||||||
|
{
|
||||||
|
if (ts_node_field_id_for_child(self, i) == field_cond)
|
||||||
|
if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_ternary.condition))
|
||||||
|
return (ast_free(ret), ERROR);
|
||||||
|
if (ts_node_field_id_for_child(self, i) == field_then)
|
||||||
|
if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_ternary.then))
|
||||||
|
return (ast_free(ret), ERROR);
|
||||||
|
if (ts_node_field_id_for_child(self, i) == field_else)
|
||||||
|
if (ast_from_node(ts_node_child(self, i), input, &ret->data.arithmetic_ternary.else_))
|
||||||
|
return (ast_free(ret), ERROR);
|
||||||
|
i++;
|
||||||
|
};
|
||||||
|
return (*out = ret, NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error build_sym_arithmetic_unary_expression(t_parse_node self, t_const_str input, t_ast_node *out)
|
||||||
|
{
|
||||||
|
t_ast_node ret;
|
||||||
|
|
||||||
|
if (out == NULL)
|
||||||
|
return (ERROR);
|
||||||
|
if (ts_node_symbol(self) != sym_arithmetic_unary_expression)
|
||||||
|
return (ERROR);
|
||||||
|
if (ts_node_child_count(self) != 2)
|
||||||
|
return (ERROR);
|
||||||
|
ret = ast_alloc(AST_ARITHMETIC_UNARY);
|
||||||
|
if (ts_node_field_id_for_child(self, 0) == field_op)
|
||||||
|
ret->data.arithmetic_unary.operator = _parse_operator(ts_node_child(self, 0));
|
||||||
|
if (ast_from_node(ts_node_child(self, 1), input, &ret->data.arithmetic_unary.value))
|
||||||
|
return (ast_free(ret), ERROR);
|
||||||
|
return (*out = ret, NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
//PLUS RAPH
|
||||||
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;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue