fixes: fixed error when parsing failed

This commit is contained in:
maix0 2024-10-10 17:50:01 +02:00
parent 77e7f65b41
commit a567a5323b
4 changed files with 23 additions and 27 deletions

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/14 12:26:51 by maiboyer #+# #+# */ /* Created: 2024/09/14 12:26:51 by maiboyer #+# #+# */
/* Updated: 2024/09/18 21:47:34 by maiboyer ### ########.fr */ /* Updated: 2024/10/10 17:44:20 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/09 12:44:53 by maiboyer #+# #+# */ /* Created: 2024/10/09 12:44:53 by maiboyer #+# #+# */
/* Updated: 2024/10/10 17:19:30 by maiboyer ### ########.fr */ /* Updated: 2024/10/10 17:48:57 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -110,25 +110,17 @@ t_error _tok_word_expansion(t_token *tok, t_ast_node *out)
t_error _tok_word_nquote(t_token *tok, t_ast_node *out) t_error _tok_word_nquote(t_token *tok, t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp;
ret = ast_alloc(AST_WORD); ret = ast_alloc(AST_RAW_STRING);
ret->data.word.kind = AST_WORD_NO_QUOTE; ret->data.raw_string.str = (t_str)_token_to_string(tok, false);
tmp = ast_alloc(AST_RAW_STRING);
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); return (*out = ret, NO_ERROR);
} }
t_error _tok_word_squote(t_token *tok, t_ast_node *out) t_error _tok_word_squote(t_token *tok, t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;
t_ast_node tmp;
ret = ast_alloc(AST_WORD); ret = ast_alloc(AST_RAW_STRING);
ret->data.word.kind = AST_WORD_SINGLE_QUOTE; ret->data.raw_string.str = (t_str)_token_to_string(tok, false);
tmp = ast_alloc(AST_RAW_STRING);
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); return (*out = ret, NO_ERROR);
} }
t_error _tok_word_dquote(t_token *tok, t_ast_node *out) t_error _tok_word_dquote(t_token *tok, t_ast_node *out)
@ -156,7 +148,7 @@ t_error _tok_word(t_token *tok, t_ast_node *out)
if (_tok_word_expansion(tok, out)) if (_tok_word_expansion(tok, out))
return (ERROR); return (ERROR);
} }
else if (tok->type == TOK_NQUOTE) else if (tok->type == TOK_NQUOTE || tok->type == TOK_WHITESPACE)
{ {
if (_tok_word_nquote(tok, out)) if (_tok_word_nquote(tok, out))
return (ERROR); return (ERROR);

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/06 16:31:41 by rparodi #+# #+# */ /* Created: 2024/09/06 16:31:41 by rparodi #+# #+# */
/* Updated: 2024/10/10 17:39:40 by maiboyer ### ########.fr */ /* Updated: 2024/10/10 17:48:06 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -26,8 +26,8 @@
#include <errno.h> #include <errno.h>
#include <sys/types.h> #include <sys/types.h>
void ft_exit(t_state *maiboyerlpb, t_u8 exit_status); void ft_exit(t_state *maiboyerlpb, t_u8 exit_status);
void parse_str(t_state *state); t_error parse_str(t_state *state);
t_error get_user_input(t_state *state) t_error get_user_input(t_state *state)
{ {
@ -78,7 +78,11 @@ void ft_take_args(t_state *state)
if (state->str_input == NULL) if (state->str_input == NULL)
ft_exit(state, 0); ft_exit(state, 0);
line_history_add(state->str_input); line_history_add(state->str_input);
parse_str(state); if (parse_str(state))
{
mem_free(state->str_input);
continue;
}
exec_shcat(state); exec_shcat(state);
mem_free(state->str_input); mem_free(state->str_input);
} }

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */ /* Created: 2024/03/28 14:40:38 by rparodi #+# #+# */
/* Updated: 2024/10/10 17:37:45 by maiboyer ### ########.fr */ /* Updated: 2024/10/10 17:47:24 by maiboyer ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -83,25 +83,25 @@ t_error populate_env(t_hashmap_env *env, t_str envp[])
t_error yarn(t_vec_token ts, t_vec_ast *output); t_error yarn(t_vec_token ts, t_vec_ast *output);
void parse_str(t_state *state) t_error parse_str(t_state *state)
{ {
t_vec_token tokens; t_vec_token tokens;
t_vec_ast ast; t_vec_ast ast;
if (tokenize(state->str_input, &tokens)) if (tokenize(state->str_input, &tokens))
return; return (ERROR);
if (ts_apply_passes(tokens, &tokens)) if (ts_apply_passes(tokens, &tokens))
return; return (ERROR);
// TODO: remove ts_print(&tokens); // TODO: remove
ts_print(&tokens);
if (yarn(tokens, &ast)) if (yarn(tokens, &ast))
return ((void)printf("failed to ast build\n")); return ((void)printf("failed to ast build\n"), (ERROR));
if (ast.len != 1) if (ast.len != 1)
me_abort("Unhandled error: ast.len != 1"); return (ERROR);
vec_ast_pop(&ast, &state->ast); vec_ast_pop(&ast, &state->ast);
ast_print(state->ast); ast_print(state->ast);
printf("\nast\n"); printf("\nast\n");
vec_ast_free(ast); vec_ast_free(ast);
return (NO_ERROR);
} }
t_i32 main(t_i32 argc, t_str argv[], t_str envp[]) t_i32 main(t_i32 argc, t_str argv[], t_str envp[])