update: exec works !

This commit is contained in:
maix0 2024-10-10 17:41:23 +02:00
parent 2363fadd02
commit 77e7f65b41
24 changed files with 192 additions and 499 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/02 18:41:16 by maiboyer #+# #+# */
/* Updated: 2024/10/10 15:27:12 by maiboyer ### ########.fr */
/* Updated: 2024/10/10 17:25:40 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -70,7 +70,6 @@ t_error ts_apply_passes(t_vec_token ts, t_vec_token *out)
else
me_printf("Applied '%s' pass\n", g_ts_passes[i].name);
ts = next;
ts_print(&ts);
i++;
}
return (*out = ts, NO_ERROR);
@ -98,7 +97,6 @@ t_error ts_dq_apply_passes(t_vec_token ts, t_vec_token *out)
else
me_printf("Applied '%s' dq_pass\n", g_ts_dq_passes[i].name);
ts = next;
ts_print(&ts);
i++;
}
return (*out = ts, NO_ERROR);

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/09 12:44:53 by maiboyer #+# #+# */
/* Updated: 2024/10/10 15:16:38 by maiboyer ### ########.fr */
/* Updated: 2024/10/10 17:19:30 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -47,7 +47,7 @@ static t_error _create_ast_redir(enum e_token ty, t_ast_node *out)
return (*out = ret, NO_ERROR);
}
t_const_str _token_to_string(t_token *arg)
t_const_str _token_to_string(t_token *arg, bool dollar_exp)
{
t_usize i;
t_string s;
@ -58,7 +58,7 @@ t_const_str _token_to_string(t_token *arg)
s = string_new(16);
if (arg->string.buf != NULL)
{
if (arg->type == TOK_EXPENSION)
if (dollar_exp && arg->type == TOK_EXPENSION)
string_push_char(&s, '$');
string_push(&s, arg->string.buf);
}
@ -67,7 +67,7 @@ t_const_str _token_to_string(t_token *arg)
i = 0;
while (i < arg->subtokens.len)
{
tmp = _token_to_string(&arg->subtokens.buffer[i++]);
tmp = _token_to_string(&arg->subtokens.buffer[i++], false);
string_push(&s, tmp);
str_free((t_str)tmp);
}
@ -80,7 +80,7 @@ static t_error _ast_set_redir_arg(t_ast_node node, t_token *arg)
if (node == NULL || arg == NULL || (node->kind != AST_HEREDOC_REDIRECTION && node->kind != AST_FILE_REDIRECTION))
return (ERROR);
if (node->kind == AST_HEREDOC_REDIRECTION)
node->data.heredoc_redirection.delimiter = (t_str)_token_to_string(arg);
node->data.heredoc_redirection.delimiter = (t_str)_token_to_string(arg, true);
else if (handle_tok_word_inner(arg, &node->data.file_redirection.output))
return (ERROR);
return (NO_ERROR);
@ -104,7 +104,7 @@ t_error _tok_word_expansion(t_token *tok, t_ast_node *out)
t_ast_node ret;
ret = ast_alloc(AST_EXPANSION);
ret->data.expansion.var_name = (t_str)_token_to_string(tok);
ret->data.expansion.var_name = (t_str)_token_to_string(tok, false);
return (*out = ret, NO_ERROR);
}
t_error _tok_word_nquote(t_token *tok, t_ast_node *out)
@ -115,7 +115,7 @@ t_error _tok_word_nquote(t_token *tok, t_ast_node *out)
ret = ast_alloc(AST_WORD);
ret->data.word.kind = AST_WORD_NO_QUOTE;
tmp = ast_alloc(AST_RAW_STRING);
tmp->data.raw_string.str = (t_str)_token_to_string(tok);
tmp->data.raw_string.str = (t_str)_token_to_string(tok, false);
vec_ast_push(&ret->data.word.inner, tmp);
return (*out = ret, NO_ERROR);
}
@ -127,7 +127,7 @@ t_error _tok_word_squote(t_token *tok, t_ast_node *out)
ret = ast_alloc(AST_WORD);
ret->data.word.kind = AST_WORD_SINGLE_QUOTE;
tmp = ast_alloc(AST_RAW_STRING);
tmp->data.raw_string.str = (t_str)_token_to_string(tok);
tmp->data.raw_string.str = (t_str)_token_to_string(tok, false);
vec_ast_push(&ret->data.word.inner, tmp);
return (*out = ret, NO_ERROR);
}
@ -142,7 +142,7 @@ t_error _tok_word_dquote(t_token *tok, t_ast_node *out)
i = 0;
while (i < tok->subtokens.len)
{
if (_tok_word(&tok->subtokens.buffer[i], &tmp))
if (_tok_word(&tok->subtokens.buffer[i++], &tmp))
return (ast_free(ret), ERROR);
vec_ast_push(&ret->data.word.inner, tmp);
}
@ -221,13 +221,13 @@ t_error handle_tok_redir(t_ast_node cmd, t_token *tok)
/// les noms peuvent etre different idk
/// a terme la fonction utilisera t_error et tt;
/// struct s_ast_command `ast/include/ast/_raw_structs.h`
t_ast_node ast_from_cmd(t_token tok)
t_error ast_from_cmd(t_token tok, t_vec_ast *output_queue)
{
t_ast_node ret;
t_usize i;
if (tok.type != TOK_CMD)
me_abort("tok.type != TOK_CMD");
return (ERROR);
ret = ast_alloc(AST_COMMAND);
i = 0;
while (i < tok.subtokens.len)
@ -235,15 +235,17 @@ t_ast_node ast_from_cmd(t_token tok)
if (tok.subtokens.buffer[i].type == TOK_REDIR)
{
if (handle_tok_redir(ret, &tok.subtokens.buffer[i]))
me_abort("handle_tok_redir error");
return (ast_free(ret), ERROR);
}
else if (tok.subtokens.buffer[i].type == TOK_WORD)
{
if (handle_tok_word(ret, &tok.subtokens.buffer[i]))
me_abort("handle_tok_word error");
return (ast_free(ret), ERROR);
}
else
me_abort("handle_tok_cmd not word|redir");
return (ast_free(ret), ERROR);
i++;
}
return (ret);
token_free(tok);
return (vec_ast_push(output_queue, ret), NO_ERROR);
}

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/09 12:44:53 by maiboyer #+# #+# */
/* Updated: 2024/10/10 16:22:39 by maiboyer ### ########.fr */
/* Updated: 2024/10/10 17:26:38 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -16,6 +16,40 @@
#include "me/vec/vec_token.h"
#include "parser/token.h"
static enum e_ast_list_kind _ast_list_get_op(enum e_token ty)
{
if (ty == TOK_AND)
return (AST_LIST_AND);
else if (ty == TOK_OR)
return (AST_LIST_OR);
me_abort("invalid token type for ast_list operator");
return (-1);
}
static t_error _tok_pipeline(t_vec_ast *output_queue, t_ast_node rhs, t_ast_node lhs)
{
t_ast_node ret;
if (rhs->kind == AST_PIPELINE)
{
vec_ast_push_front(&rhs->data.pipeline.statements, lhs);
vec_ast_push(output_queue, rhs);
}
else if (lhs->kind == AST_PIPELINE)
{
vec_ast_push(&lhs->data.pipeline.statements, rhs);
vec_ast_push(output_queue, lhs);
}
else
{
ret = ast_alloc(AST_PIPELINE);
vec_ast_push(&ret->data.pipeline.statements, lhs);
vec_ast_push(&ret->data.pipeline.statements, rhs);
vec_ast_push(output_queue, ret);
}
return (NO_ERROR);
}
/// en fonction de op, qui peut etre: TOK_AND TOK_PIPE TOK_OR
/// choisir le bon ast_node a faire (t_ast_node->data.list + set operator ou t_asdt_node->data.pipeline)
/// pop deux element de output_queue. pour l'instant la fonction doit print une error si il n'y as pas asser d'element
@ -30,39 +64,28 @@
///
/// in the end we should change to using `t_error` and pushing the ast_node directly to output_queue in the function,
/// will change that later tho :)
t_ast_node ast_from_op(t_token tok, t_vec_ast *output_queue)
t_error ast_from_op(t_token tok, t_vec_ast *output_queue)
{
t_ast_node ret;
t_ast_node tmp;
t_ast_node lhs;
t_ast_node rhs;
// this needs have a protection in case output_queue is smaller than 2 elements
// otherwise it is good :)
// you could also make it so TOK_AND and TOK_OR share the same code to win some lines
ret = NULL;
if (tok.type == TOK_AND)
if (!(tok.type == TOK_AND || tok.type == TOK_OR || tok.type == TOK_PIPE))
return (ERROR);
if (output_queue == NULL || output_queue->len < 2)
return (ERROR);
vec_ast_pop(output_queue, &rhs);
vec_ast_pop(output_queue, &lhs);
if (tok.type == TOK_AND || tok.type == TOK_OR)
{
ret = ast_alloc(AST_LIST);
ret->data.list.op = AST_LIST_AND;
vec_ast_pop(output_queue, &ret->data.list.right);
vec_ast_pop(output_queue, &ret->data.list.left);
ret->data.list.op = _ast_list_get_op(tok.type);
ret->data.list.left = lhs;
ret->data.list.right = rhs;
vec_ast_push(output_queue, ret);
}
else if (tok.type == TOK_OR)
{
ret = ast_alloc(AST_LIST);
ret->data.list.op = AST_LIST_OR;
vec_ast_pop(output_queue, &ret->data.list.right);
vec_ast_pop(output_queue, &ret->data.list.left);
}
else if (tok.type == TOK_PIPE)
{
// Here there is some kind of optimization that could be done in the future: if one node is already a AST_PIPELINE, just pus the other node into the right place in it and return the non created AST_PIPELINE node !
ret = ast_alloc(AST_PIPELINE);
vec_ast_pop(output_queue, &tmp);
vec_ast_push(&ret->data.pipeline.statements, tmp);
vec_ast_pop(output_queue, &tmp);
vec_ast_push(&ret->data.pipeline.statements, tmp);
}
else
me_abort("ast_from_op not the good token type gived !\n");
return (ret);
else if (tok.type == TOK_PIPE && _tok_pipeline(output_queue, rhs, lhs))
return (ERROR);
token_free(tok);
return (NO_ERROR);
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/07 18:04:13 by rparodi #+# #+# */
/* Updated: 2024/10/09 12:44:24 by maiboyer ### ########.fr */
/* Updated: 2024/10/10 17:28:21 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
@ -37,7 +37,7 @@ t_str token_name(t_token *token);
/// les noms peuvent etre different idk
/// a terme la fonction utilisera t_error et tt;
/// struct s_ast_command `ast/include/ast/_raw_structs.h`
t_ast_node ast_from_cmd(t_token tok);
t_error ast_from_cmd(t_token tok, t_vec_ast *output_queue);
/// en fonction de op, qui peut etre: TOK_AND TOK_PIPE TOK_OR
/// choisir le bon ast_node a faire (t_ast_node->data.list + set operator ou t_asdt_node->data.pipeline)
@ -49,7 +49,7 @@ t_ast_node ast_from_cmd(t_token tok);
/// struct s_ast_list if (tok.type == TOK_AND || tok.type == TOK_OR)
/// struct s_ast_pipeline if (tok.type == TOK_PIPE)
/// `ast/include/ast/_raw_structs.h`
t_ast_node ast_from_op(t_token tok, t_vec_ast *output_queue);
t_error ast_from_op(t_token tok, t_vec_ast *output_queue);
t_error yarn(t_vec_token ts, t_vec_ast *out)
{
@ -63,7 +63,10 @@ t_error yarn(t_vec_token ts, t_vec_ast *out)
while (!vec_token_pop_front(&ts, &tok))
{
if (tok.type == TOK_CMD)
vec_ast_push(&output_queue, ast_from_cmd(tok));
{
if (ast_from_cmd(tok, &output_queue))
return (vec_token_free(stack), vec_ast_free(output_queue), token_free(tok), ERROR);
}
else if (tok.type == TOK_LPAREN)
vec_token_push(&stack, tok);
else if (tok.type == TOK_OR || tok.type == TOK_AND || tok.type == TOK_PIPE)
@ -71,31 +74,38 @@ t_error yarn(t_vec_token ts, t_vec_ast *out)
while (vec_token_last(&stack) != NULL && vec_token_last(&stack)->type != TOK_LPAREN && _get_precedance(vec_token_last(&stack)) > _get_precedance(&tok))
{
vec_token_pop(&stack, &op);
vec_ast_push(&output_queue, ast_from_op(op, &output_queue));
if (ast_from_op(op, &output_queue))
return (vec_token_free(stack), vec_ast_free(output_queue), token_free(tok), token_free(op), ERROR);
}
vec_token_push(&stack, tok);
}
else if (tok.type == TOK_RPAREN)
{
token_free(tok);
// ici il faut modifier pour push dans un ast_node->data.subshell
// je m'occuperai de ca ce soir/after
while (vec_token_last(&stack) != NULL && vec_token_last(&stack)->type != TOK_LPAREN)
{
vec_token_pop(&stack, &op);
vec_ast_push(&output_queue, ast_from_op(op, &output_queue));
if (ast_from_op(op, &output_queue))
return (vec_token_free(stack), vec_ast_free(output_queue), token_free(op), ERROR);
}
if (!(vec_token_last(&stack) != NULL && vec_token_last(&stack)->type == TOK_LPAREN))
return (ERROR);
return (vec_token_free(stack), vec_ast_free(output_queue), ERROR);
vec_token_pop(&stack, &tok);
token_free(tok);
t_ast_node snode;
t_ast_node tmp;
snode = ast_alloc(AST_SUBSHELL);
vec_ast_pop(&output_queue, &tmp);
vec_ast_push(&snode->data.subshell.body, tmp);
vec_ast_push(&output_queue, snode);
}
}
while (!vec_token_pop(&stack, &op))
{
if (op.type == TOK_LPAREN)
return (token_free(tok), ERROR);
vec_ast_push(&output_queue, ast_from_op(op, &output_queue));
return (token_free(op), ERROR);
if (ast_from_op(op, &output_queue))
return (vec_token_free(stack), vec_ast_free(output_queue), token_free(op), ERROR);
}
vec_token_free(ts);
vec_token_free(stack);