aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/eval/operation.c21
-rw-r--r--src/parser/expr.c37
-rw-r--r--src/parser/parser.c51
3 files changed, 91 insertions, 18 deletions
diff --git a/src/eval/operation.c b/src/eval/operation.c
index 3b6275a..3df3c85 100644
--- a/src/eval/operation.c
+++ b/src/eval/operation.c
@@ -6,7 +6,7 @@
/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/17 15:27:22 by charles #+# #+# */
-/* Updated: 2020/10/10 13:01:56 by cacharle ### ########.fr */
+/* Updated: 2020/10/10 18:17:15 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,7 +21,7 @@
** stat code of the right hand side otherwise.
*/
-int eval_operation(int fds[2], t_env env, t_ast *ast)
+int eval_operation(int fds[2], t_env env, t_ast *ast)
{
int status;
int left_fds[2];
@@ -44,7 +44,20 @@ int eval_operation(int fds[2], t_env env, t_ast *ast)
#define PIPES_PREV_OUTPUT 2
-static int st_run_piped(
+/*
+** \brief Evaluate a piped expression (i.e command or parenthesis)
+** \param env Environment
+** \param ast AST of the expression to evaluate
+** \param pipes File descriptor to setup in the child
+** pipes[0] - read end
+** pipes[1] - write end
+** pipes[2] - output of the previous command
+** \param is_last Setup file dscriptors differently if
+** it's the last expression in the pipeline
+** \return pid of the child process
+*/
+
+static pid_t st_run_piped(
t_env env, t_ast *ast, int pipes[3], bool is_last)
{
pid_t pid;
@@ -78,7 +91,7 @@ static int st_run_piped(
** \return Status of the last command in the pipeline
*/
-int eval_pipeline(t_env env, t_ast *ast)
+int eval_pipeline(t_env env, t_ast *ast)
{
t_ftlst *curr;
int pipes[3];
diff --git a/src/parser/expr.c b/src/parser/expr.c
index f4c82ce..8ee7866 100644
--- a/src/parser/expr.c
+++ b/src/parser/expr.c
@@ -6,7 +6,7 @@
/* By: cacharle <me@cacharle.xyz> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/10 08:42:24 by cacharle #+# #+# */
-/* Updated: 2020/10/10 09:24:33 by cacharle ### ########.fr */
+/* Updated: 2020/10/10 18:28:02 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -19,6 +19,13 @@
** push last str token
*/
+/*
+** \brief Push tokens which bellong to a redirection in a token list
+** \param input Tokens where to extract the redirection tokens
+** \param redirs List where to push the redirections tokens
+** \return Parsed struct with the unconsumed input
+*/
+
t_parsed *parse_redir(t_tok_lst *input, t_tok_lst **redirs)
{
t_parsed *ret;
@@ -39,6 +46,13 @@ t_parsed *parse_redir(t_tok_lst *input, t_tok_lst **redirs)
return (parsed_new(NULL, input));
}
+/*
+** \brief Parse a command (this is the continuation of
+** parse_cmd splited for norm complience)
+** \param input Input tokens
+** \return Parsed struct containning a command AST
+*/
+
static t_parsed *st_parse_cmd_body(t_tok_lst *input)
{
t_ast *ast;
@@ -66,6 +80,14 @@ static t_parsed *st_parse_cmd_body(t_tok_lst *input)
return (parsed_new(ast, input));
}
+/*
+** \brief Check if the first token can be included in a command
+** then parse a command
+** \param input Input tokens
+** \return Parsed error if the first token isn't a redirection or string
+** Parsed containning a command AST otherwise
+*/
+
t_parsed *parse_cmd(t_tok_lst *input)
{
t_parsed *ret;
@@ -80,6 +102,14 @@ t_parsed *parse_cmd(t_tok_lst *input)
return (st_parse_cmd_body(input));
}
+/*
+** \brief Parse a parenthesized expression
+** \param input Input tokens
+** \return Parsed containning a parenthesis AST
+** Parsed error if parenthesis don't match
+** \note Parenthesis can be followed by redirections
+*/
+
static t_parsed *st_parse_parenthesis(t_tok_lst *input)
{
t_parsed *parsed;
@@ -110,8 +140,9 @@ static t_parsed *st_parse_parenthesis(t_tok_lst *input)
}
/*
-** try to parse parenthesis, if fail parse a command.
-** parenthesis can be followed by redirections
+** \brief Try to parse parenthesis, if fail parse a command.
+** \param input Input tokens
+** \return Whatever parse_parenthesis or parse_cmd returns
*/
t_parsed *parse_expr(t_tok_lst *input)
diff --git a/src/parser/parser.c b/src/parser/parser.c
index 85e2214..4fde122 100644
--- a/src/parser/parser.c
+++ b/src/parser/parser.c
@@ -6,7 +6,7 @@
/* By: nahaddac <nahaddac@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/06/17 18:09:04 by nahaddac #+# #+# */
-/* Updated: 2020/10/10 09:24:28 by cacharle ### ########.fr */
+/* Updated: 2020/10/10 18:38:37 by charles ### ########.fr */
/* */
/* ************************************************************************** */
@@ -18,7 +18,7 @@
#include "minishell.h"
#include "parser.h"
-static char *g_sep_str_lookup[] = {
+static char *g_sep_str_lookup[] = {
[TAG_END] = ";",
[TAG_OR] = "||",
[TAG_REDIR_IN] = "<",
@@ -30,13 +30,21 @@ static char *g_sep_str_lookup[] = {
[TAG_PARENT_OPEN] = "(",
};
-t_parsed *destroy_ret(t_parsed *destroyed, t_parsed *ret)
+t_parsed *destroy_ret(t_parsed *destroyed, t_parsed *ret)
{
parsed_destroy(destroyed);
return (ret);
}
-t_parsed *parse_pipeline(t_tok_lst *input)
+/*
+** \brief Parse a expression pipeline
+** \param input Input tokens
+** \return Parsed containning a pipeline of expression
+** Parsed error if a pipe is missing
+** or the expression parser return an error
+*/
+
+t_parsed *parse_pipeline(t_tok_lst *input)
{
t_parsed *expr;
t_parsed *tail;
@@ -65,7 +73,17 @@ t_parsed *parse_pipeline(t_tok_lst *input)
return (tail);
}
-t_parsed *parse_op_build(t_parsed *left, t_parsed *right, enum e_tok sep_tag)
+/*
+** \brief Build a operation AST node from left, right components
+** and a separator tag
+** \param left Left hand side of the operation
+** \param right Right hand side of the operation
+** \param sep_tag Separator tag of the operation
+** \return Parsed containning an operation AST
+*/
+
+static t_parsed *st_parse_op_build(
+ t_parsed *left, t_parsed *right, enum e_tok sep_tag)
{
t_ast *ast;
@@ -73,17 +91,21 @@ t_parsed *parse_op_build(t_parsed *left, t_parsed *right, enum e_tok sep_tag)
ast->op.left = left->ast;
ast->op.right = right->ast;
ast->op.sep = sep_tag;
- free(left);
- left = parsed_new(ast, right->rest);
+ left->rest = right->rest;
+ left->ast = ast;
free(right);
return (left);
}
/*
-** parse and operation (| ; && ||)
+** \brief Parse an operation (&&, ||, ;)
+** \param input Input tokens
+** \return Parsed containning an operation AST
+** Parsed error if separator isn't valid
+** or left/right parser failed
*/
-t_parsed *parse_op(t_tok_lst *input)
+t_parsed *parse_op(t_tok_lst *input)
{
t_parsed *left;
t_parsed *right;
@@ -109,10 +131,17 @@ t_parsed *parse_op(t_tok_lst *input)
right = parse_op(input);
if (parsed_check(right))
return (destroy_ret(left, right));
- return (parse_op_build(left, right, sep_tag));
+ return (st_parse_op_build(left, right, sep_tag));
}
-t_parsed *parse(t_tok_lst *input)
+/*
+** \brief Parse the lexer output into an AST
+** \param input Lexer output, a list of tokens
+** \return Parsed containning the AST to evaluate
+** Parsed error if one of the parser failed
+*/
+
+t_parsed *parse(t_tok_lst *input)
{
t_parsed *parsed;
t_parsed *ret;