norm: almost everything
This commit is contained in:
parent
8122ecfc95
commit
54a1041aa7
9 changed files with 311 additions and 264 deletions
|
|
@ -20,10 +20,12 @@ token_utils \
|
||||||
tokenizer \
|
tokenizer \
|
||||||
tokenizer_utils \
|
tokenizer_utils \
|
||||||
ts_print \
|
ts_print \
|
||||||
yarn/ast_cmd \
|
yard/yard \
|
||||||
yarn/ast_op \
|
yard/yard_cmd \
|
||||||
yarn/yarn \
|
yard/yard_cmd_word \
|
||||||
yarn/yarn_utils \
|
yard/yard_cmd_word2 \
|
||||||
|
yard/yard_op \
|
||||||
|
yard/yard_utils \
|
||||||
|
|
||||||
GEN_FILES = \
|
GEN_FILES = \
|
||||||
\
|
\
|
||||||
|
|
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* yarn.c :+: :+: :+: */
|
/* yard.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
|
@ -23,7 +23,7 @@ t_str token_name(t_token *token);
|
||||||
t_error ast_from_cmd(t_token tok, t_vec_ast *output_queue);
|
t_error ast_from_cmd(t_token tok, t_vec_ast *output_queue);
|
||||||
t_error 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_parenthesis(\
|
t_error _yard_parenthesis(\
|
||||||
t_token tok, t_vec_token *stack, t_vec_ast *output_queue)
|
t_token tok, t_vec_token *stack, t_vec_ast *output_queue)
|
||||||
{
|
{
|
||||||
t_token op;
|
t_token op;
|
||||||
|
|
@ -50,7 +50,7 @@ t_error _yarn_parenthesis(\
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error _yarn_operator(t_token tok, t_vec_token *stack, t_vec_ast *output_queue)
|
t_error _yard_operator(t_token tok, t_vec_token *stack, t_vec_ast *output_queue)
|
||||||
{
|
{
|
||||||
t_token op;
|
t_token op;
|
||||||
|
|
||||||
|
|
@ -65,7 +65,7 @@ t_error _yarn_operator(t_token tok, t_vec_token *stack, t_vec_ast *output_queue)
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error _yarn_final(t_vec_token stack, t_vec_ast output_q, t_vec_ast *out)
|
t_error _yard_final(t_vec_token stack, t_vec_ast output_q, t_vec_ast *out)
|
||||||
{
|
{
|
||||||
t_token op;
|
t_token op;
|
||||||
|
|
||||||
|
|
@ -81,7 +81,7 @@ t_error _yarn_final(t_vec_token stack, t_vec_ast output_q, t_vec_ast *out)
|
||||||
return (*out = output_q, NO_ERROR);
|
return (*out = output_q, NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error _yarn_loop(t_token tok, t_vec_token *stack, t_vec_ast *output_q)
|
t_error _yard_loop(t_token tok, t_vec_token *stack, t_vec_ast *output_q)
|
||||||
{
|
{
|
||||||
if (tok.type == TOK_CMD)
|
if (tok.type == TOK_CMD)
|
||||||
{
|
{
|
||||||
|
|
@ -91,12 +91,12 @@ t_error _yarn_loop(t_token tok, t_vec_token *stack, t_vec_ast *output_q)
|
||||||
else if (tok.type == TOK_OR \
|
else if (tok.type == TOK_OR \
|
||||||
|| tok.type == TOK_AND || tok.type == TOK_PIPE)
|
|| tok.type == TOK_AND || tok.type == TOK_PIPE)
|
||||||
{
|
{
|
||||||
if (_yarn_operator(tok, stack, output_q))
|
if (_yard_operator(tok, stack, output_q))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
else if (tok.type == TOK_RPAREN)
|
else if (tok.type == TOK_RPAREN)
|
||||||
{
|
{
|
||||||
if (_yarn_parenthesis(tok, stack, output_q))
|
if (_yard_parenthesis(tok, stack, output_q))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
}
|
}
|
||||||
else if (tok.type == TOK_LPAREN)
|
else if (tok.type == TOK_LPAREN)
|
||||||
|
|
@ -106,7 +106,7 @@ t_error _yarn_loop(t_token tok, t_vec_token *stack, t_vec_ast *output_q)
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error yarn(t_vec_token ts, t_vec_ast *out)
|
t_error yard(t_vec_token ts, t_vec_ast *out)
|
||||||
{
|
{
|
||||||
t_token tok;
|
t_token tok;
|
||||||
t_vec_ast output_q;
|
t_vec_ast output_q;
|
||||||
|
|
@ -116,9 +116,9 @@ t_error yarn(t_vec_token ts, t_vec_ast *out)
|
||||||
stack = vec_token_new(16, token_free);
|
stack = vec_token_new(16, token_free);
|
||||||
while (!vec_token_pop_front(&ts, &tok))
|
while (!vec_token_pop_front(&ts, &tok))
|
||||||
{
|
{
|
||||||
if (_yarn_loop(tok, &stack, &output_q))
|
if (_yard_loop(tok, &stack, &output_q))
|
||||||
return (vec_token_free(stack), vec_ast_free(output_q), ERROR);
|
return (vec_token_free(stack), vec_ast_free(output_q), ERROR);
|
||||||
}
|
}
|
||||||
vec_token_free(ts);
|
vec_token_free(ts);
|
||||||
return (_yarn_final(stack, output_q, out));
|
return (_yard_final(stack, output_q, out));
|
||||||
}
|
}
|
||||||
123
parser/src/yard/yard_cmd.c
Normal file
123
parser/src/yard/yard_cmd.c
Normal file
|
|
@ -0,0 +1,123 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* ast_cmd.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/09 12:44:53 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/10/12 16:20:15 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ast/ast.h"
|
||||||
|
#include "me/str/str.h"
|
||||||
|
#include "me/string/string.h"
|
||||||
|
#include "me/types.h"
|
||||||
|
#include "me/vec/vec_ast.h"
|
||||||
|
#include "me/vec/vec_token.h"
|
||||||
|
#include "parser/token.h"
|
||||||
|
|
||||||
|
t_error handle_tok_word_inner(t_token *tok, t_ast_node *out);
|
||||||
|
t_error _tok_word(t_token *tok, t_ast_node *out);
|
||||||
|
t_const_str _token_to_string(t_token *arg, bool dollar_exp);
|
||||||
|
t_error handle_tok_word(t_ast_node cmd, t_token *tok);
|
||||||
|
|
||||||
|
static bool _is_carret(enum e_token type)
|
||||||
|
{
|
||||||
|
return (type == TOK_DLCARRET || type == TOK_DRCARRET || type == TOK_LCARRET
|
||||||
|
|| type == TOK_RCARRET);
|
||||||
|
}
|
||||||
|
|
||||||
|
static t_error _create_ast_redir(enum e_token ty, t_ast_node *out)
|
||||||
|
{
|
||||||
|
t_ast_node ret;
|
||||||
|
|
||||||
|
ret = NULL;
|
||||||
|
if (ty == TOK_DLCARRET)
|
||||||
|
{
|
||||||
|
ret = ast_alloc(AST_HEREDOC_REDIRECTION);
|
||||||
|
ret->data.heredoc_redirection.op = AST_REDIR_HEREDOC;
|
||||||
|
}
|
||||||
|
else if (ty == TOK_RCARRET || ty == TOK_LCARRET || ty == TOK_DRCARRET)
|
||||||
|
ret = ast_alloc(AST_FILE_REDIRECTION);
|
||||||
|
else
|
||||||
|
return (ERROR);
|
||||||
|
if (ty == TOK_DRCARRET)
|
||||||
|
ret->data.file_redirection.op = AST_REDIR_APPEND;
|
||||||
|
else if (ty == TOK_LCARRET)
|
||||||
|
ret->data.file_redirection.op = AST_REDIR_INPUT;
|
||||||
|
else if (ty == TOK_DRCARRET)
|
||||||
|
ret->data.file_redirection.op = AST_REDIR_OUTPUT;
|
||||||
|
return (*out = ret, NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
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,
|
||||||
|
true);
|
||||||
|
else if (handle_tok_word_inner(arg, &node->data.file_redirection.output))
|
||||||
|
return (ERROR);
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error handle_tok_redir(t_ast_node cmd, t_token *tok)
|
||||||
|
{
|
||||||
|
t_ast_node redir;
|
||||||
|
|
||||||
|
if (cmd == NULL || tok == NULL || tok->type != TOK_REDIR
|
||||||
|
|| cmd->kind != AST_COMMAND)
|
||||||
|
return (ERROR);
|
||||||
|
if (!(tok->subtokens.len == 2 && _is_carret(tok->subtokens.buffer[0].type)
|
||||||
|
&& tok->subtokens.buffer[1].type == TOK_WORD))
|
||||||
|
return (ERROR);
|
||||||
|
if (_create_ast_redir(tok->subtokens.buffer[0].type, &redir))
|
||||||
|
return (ERROR);
|
||||||
|
if (_ast_set_redir_arg(redir, &tok->subtokens.buffer[1]))
|
||||||
|
return (ast_free(redir), ERROR);
|
||||||
|
vec_ast_push(&cmd->data.command.prefixes, redir);
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// la fonction doit prendre une t_token de type TOK_CMD qui contient que
|
||||||
|
/// deux type de subtokens TOK_WORD ou TOK_REDIR
|
||||||
|
/// un TOK_WORD == un arguemnt
|
||||||
|
/// un TOK_REDIR == une redirection
|
||||||
|
/// les deux sont dans le bonne ordre.
|
||||||
|
/// il faut push les TOK_REDIR dans ast_node->data.command.prefix;
|
||||||
|
/// let TOK_WORD dans ast_node->data.command.arguements;
|
||||||
|
/// 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_error ast_from_cmd(t_token tok, t_vec_ast *output_queue)
|
||||||
|
{
|
||||||
|
t_ast_node ret;
|
||||||
|
t_usize i;
|
||||||
|
|
||||||
|
if (tok.type != TOK_CMD)
|
||||||
|
return (ERROR);
|
||||||
|
ret = ast_alloc(AST_COMMAND);
|
||||||
|
i = 0;
|
||||||
|
while (i < tok.subtokens.len)
|
||||||
|
{
|
||||||
|
if (tok.subtokens.buffer[i].type == TOK_REDIR)
|
||||||
|
{
|
||||||
|
if (handle_tok_redir(ret, &tok.subtokens.buffer[i]))
|
||||||
|
return (ast_free(ret), ERROR);
|
||||||
|
}
|
||||||
|
else if (tok.subtokens.buffer[i].type == TOK_WORD)
|
||||||
|
{
|
||||||
|
if (handle_tok_word(ret, &tok.subtokens.buffer[i]))
|
||||||
|
return (ast_free(ret), ERROR);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return (ast_free(ret), ERROR);
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
token_free(tok);
|
||||||
|
return (vec_ast_push(output_queue, ret), NO_ERROR);
|
||||||
|
}
|
||||||
85
parser/src/yard/yard_cmd_word.c
Normal file
85
parser/src/yard/yard_cmd_word.c
Normal file
|
|
@ -0,0 +1,85 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* yard_cmd_word.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/12 16:14:55 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/10/12 16:19:44 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ast/ast.h"
|
||||||
|
#include "me/str/str.h"
|
||||||
|
#include "me/string/string.h"
|
||||||
|
#include "me/types.h"
|
||||||
|
#include "me/vec/vec_ast.h"
|
||||||
|
#include "me/vec/vec_token.h"
|
||||||
|
#include "parser/token.h"
|
||||||
|
|
||||||
|
t_error handle_tok_word_inner(t_token *tok, t_ast_node *out);
|
||||||
|
t_error _tok_word(t_token *tok, t_ast_node *out);
|
||||||
|
t_const_str _token_to_string(t_token *arg, bool dollar_exp);
|
||||||
|
|
||||||
|
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, false);
|
||||||
|
return (*out = ret, NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error _tok_word_nquote(t_token *tok, t_ast_node *out)
|
||||||
|
{
|
||||||
|
t_ast_node ret;
|
||||||
|
|
||||||
|
ret = ast_alloc(AST_RAW_STRING);
|
||||||
|
ret->data.raw_string.str = (t_str)_token_to_string(tok, false);
|
||||||
|
return (*out = ret, NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error _tok_word_squote(t_token *tok, t_ast_node *out)
|
||||||
|
{
|
||||||
|
t_ast_node ret;
|
||||||
|
t_ast_node tmp;
|
||||||
|
|
||||||
|
ret = ast_alloc(AST_WORD);
|
||||||
|
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);
|
||||||
|
ret->data.word.kind = AST_WORD_SINGLE_QUOTE;
|
||||||
|
return (*out = ret, NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error _tok_word_dquote(t_token *tok, t_ast_node *out)
|
||||||
|
{
|
||||||
|
t_ast_node ret;
|
||||||
|
t_ast_node tmp;
|
||||||
|
t_usize i;
|
||||||
|
|
||||||
|
ret = ast_alloc(AST_WORD);
|
||||||
|
ret->data.word.kind = AST_WORD_DOUBLE_QUOTE;
|
||||||
|
i = 0;
|
||||||
|
while (i < tok->subtokens.len)
|
||||||
|
{
|
||||||
|
if (_tok_word(&tok->subtokens.buffer[i++], &tmp))
|
||||||
|
return (ast_free(ret), ERROR);
|
||||||
|
vec_ast_push(&ret->data.word.inner, tmp);
|
||||||
|
}
|
||||||
|
return (*out = ret, NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error _tok_word(t_token *tok, t_ast_node *out)
|
||||||
|
{
|
||||||
|
if (tok->type == TOK_EXPENSION)
|
||||||
|
return (_tok_word_expansion(tok, out));
|
||||||
|
else if (tok->type == TOK_NQUOTE || tok->type == TOK_WHITESPACE)
|
||||||
|
return (_tok_word_nquote(tok, out));
|
||||||
|
else if (tok->type == TOK_SQUOTE)
|
||||||
|
return (_tok_word_squote(tok, out));
|
||||||
|
else if (tok->type == TOK_DQUOTE)
|
||||||
|
return (_tok_word_dquote(tok, out));
|
||||||
|
return (ERROR);
|
||||||
|
}
|
||||||
84
parser/src/yard/yard_cmd_word2.c
Normal file
84
parser/src/yard/yard_cmd_word2.c
Normal file
|
|
@ -0,0 +1,84 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* yard_cmd_utils.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2024/10/12 16:19:15 by maiboyer #+# #+# */
|
||||||
|
/* Updated: 2024/10/12 16:19:54 by maiboyer ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "ast/ast.h"
|
||||||
|
#include "me/str/str.h"
|
||||||
|
#include "me/string/string.h"
|
||||||
|
#include "me/types.h"
|
||||||
|
#include "me/vec/vec_ast.h"
|
||||||
|
#include "me/vec/vec_token.h"
|
||||||
|
#include "parser/token.h"
|
||||||
|
|
||||||
|
t_error handle_tok_word_inner(t_token *tok, t_ast_node *out);
|
||||||
|
t_error _tok_word(t_token *tok, t_ast_node *out);
|
||||||
|
t_const_str _token_to_string(t_token *arg, bool dollar_exp);
|
||||||
|
|
||||||
|
t_const_str _token_to_string(t_token *arg, bool dollar_exp)
|
||||||
|
{
|
||||||
|
t_usize i;
|
||||||
|
t_string s;
|
||||||
|
t_const_str tmp;
|
||||||
|
|
||||||
|
if (arg == NULL || arg->type == TOK_NONE)
|
||||||
|
return (NULL);
|
||||||
|
s = string_new(16);
|
||||||
|
if (arg->string.buf != NULL)
|
||||||
|
{
|
||||||
|
if (dollar_exp && arg->type == TOK_EXPENSION)
|
||||||
|
string_push_char(&s, '$');
|
||||||
|
string_push(&s, arg->string.buf);
|
||||||
|
}
|
||||||
|
else if (arg->subtokens.buffer != NULL)
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
while (i < arg->subtokens.len)
|
||||||
|
{
|
||||||
|
tmp = _token_to_string(&arg->subtokens.buffer[i++], false);
|
||||||
|
string_push(&s, tmp);
|
||||||
|
str_free((t_str)tmp);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (s.buf);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error handle_tok_word(t_ast_node cmd, t_token *tok)
|
||||||
|
{
|
||||||
|
t_ast_node word;
|
||||||
|
|
||||||
|
if (cmd == NULL || cmd->kind != AST_COMMAND || tok == NULL
|
||||||
|
|| tok->type != TOK_WORD)
|
||||||
|
return (ERROR);
|
||||||
|
if (handle_tok_word_inner(tok, &word))
|
||||||
|
return (ERROR);
|
||||||
|
vec_ast_push(&cmd->data.command.cmd_word, word);
|
||||||
|
return (NO_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
t_error handle_tok_word_inner(t_token *tok, t_ast_node *out)
|
||||||
|
{
|
||||||
|
t_usize i;
|
||||||
|
t_ast_node ret;
|
||||||
|
t_ast_node tmp;
|
||||||
|
|
||||||
|
if (tok == NULL || out == NULL || tok->type != TOK_WORD)
|
||||||
|
return (ERROR);
|
||||||
|
i = 0;
|
||||||
|
ret = ast_alloc(AST_WORD);
|
||||||
|
ret->data.word.kind = AST_WORD_NO_QUOTE;
|
||||||
|
while (i < tok->subtokens.len)
|
||||||
|
{
|
||||||
|
if (_tok_word(&tok->subtokens.buffer[i++], &tmp))
|
||||||
|
return (ast_free(ret), ERROR);
|
||||||
|
vec_ast_push(&ret->data.word.inner, tmp);
|
||||||
|
}
|
||||||
|
return (*out = ret, NO_ERROR);
|
||||||
|
}
|
||||||
|
|
@ -1,7 +1,7 @@
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* ::: :::::::: */
|
/* ::: :::::::: */
|
||||||
/* yarn_utils.c :+: :+: :+: */
|
/* yard_utils.c :+: :+: :+: */
|
||||||
/* +:+ +:+ +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
|
@ -1,247 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ast_cmd.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2024/10/09 12:44:53 by maiboyer #+# #+# */
|
|
||||||
/* Updated: 2024/10/11 22:35:33 by maiboyer ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include "ast/ast.h"
|
|
||||||
#include "me/str/str.h"
|
|
||||||
#include "me/string/string.h"
|
|
||||||
#include "me/types.h"
|
|
||||||
#include "me/vec/vec_ast.h"
|
|
||||||
#include "me/vec/vec_token.h"
|
|
||||||
#include "parser/token.h"
|
|
||||||
|
|
||||||
t_error handle_tok_word_inner(t_token *tok, t_ast_node *out);
|
|
||||||
|
|
||||||
static bool _is_carret(enum e_token type)
|
|
||||||
{
|
|
||||||
return (type == TOK_DLCARRET || type == TOK_DRCARRET || type == TOK_LCARRET || type == TOK_RCARRET);
|
|
||||||
}
|
|
||||||
|
|
||||||
static t_error _create_ast_redir(enum e_token ty, t_ast_node *out)
|
|
||||||
{
|
|
||||||
t_ast_node ret;
|
|
||||||
ret = NULL;
|
|
||||||
if (ty == TOK_DLCARRET)
|
|
||||||
{
|
|
||||||
ret = ast_alloc(AST_HEREDOC_REDIRECTION);
|
|
||||||
ret->data.heredoc_redirection.op = AST_REDIR_HEREDOC;
|
|
||||||
}
|
|
||||||
else if (ty == TOK_RCARRET || ty == TOK_LCARRET || ty == TOK_DRCARRET)
|
|
||||||
ret = ast_alloc(AST_FILE_REDIRECTION);
|
|
||||||
else
|
|
||||||
return (ERROR);
|
|
||||||
if (ty == TOK_DRCARRET)
|
|
||||||
ret->data.file_redirection.op = AST_REDIR_APPEND;
|
|
||||||
else if (ty == TOK_LCARRET)
|
|
||||||
ret->data.file_redirection.op = AST_REDIR_INPUT;
|
|
||||||
else if (ty == TOK_DRCARRET)
|
|
||||||
ret->data.file_redirection.op = AST_REDIR_OUTPUT;
|
|
||||||
return (*out = ret, NO_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_const_str _token_to_string(t_token *arg, bool dollar_exp)
|
|
||||||
{
|
|
||||||
t_usize i;
|
|
||||||
t_string s;
|
|
||||||
t_const_str tmp;
|
|
||||||
|
|
||||||
if (arg == NULL || arg->type == TOK_NONE)
|
|
||||||
return (NULL);
|
|
||||||
s = string_new(16);
|
|
||||||
if (arg->string.buf != NULL)
|
|
||||||
{
|
|
||||||
if (dollar_exp && arg->type == TOK_EXPENSION)
|
|
||||||
string_push_char(&s, '$');
|
|
||||||
string_push(&s, arg->string.buf);
|
|
||||||
}
|
|
||||||
else if (arg->subtokens.buffer != NULL)
|
|
||||||
{
|
|
||||||
i = 0;
|
|
||||||
while (i < arg->subtokens.len)
|
|
||||||
{
|
|
||||||
tmp = _token_to_string(&arg->subtokens.buffer[i++], false);
|
|
||||||
string_push(&s, tmp);
|
|
||||||
str_free((t_str)tmp);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (s.buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
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, true);
|
|
||||||
else if (handle_tok_word_inner(arg, &node->data.file_redirection.output))
|
|
||||||
return (ERROR);
|
|
||||||
return (NO_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_error handle_tok_word(t_ast_node cmd, t_token *tok)
|
|
||||||
{
|
|
||||||
t_ast_node word;
|
|
||||||
|
|
||||||
if (cmd == NULL || cmd->kind != AST_COMMAND || tok == NULL || tok->type != TOK_WORD)
|
|
||||||
return (ERROR);
|
|
||||||
if (handle_tok_word_inner(tok, &word))
|
|
||||||
return (ERROR);
|
|
||||||
vec_ast_push(&cmd->data.command.cmd_word, word);
|
|
||||||
return (NO_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_error _tok_word(t_token *tok, t_ast_node *out);
|
|
||||||
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, false);
|
|
||||||
return (*out = ret, NO_ERROR);
|
|
||||||
}
|
|
||||||
t_error _tok_word_nquote(t_token *tok, t_ast_node *out)
|
|
||||||
{
|
|
||||||
t_ast_node ret;
|
|
||||||
|
|
||||||
ret = ast_alloc(AST_RAW_STRING);
|
|
||||||
ret->data.raw_string.str = (t_str)_token_to_string(tok, false);
|
|
||||||
return (*out = ret, NO_ERROR);
|
|
||||||
}
|
|
||||||
t_error _tok_word_squote(t_token *tok, t_ast_node *out)
|
|
||||||
{
|
|
||||||
t_ast_node ret;
|
|
||||||
t_ast_node tmp;
|
|
||||||
|
|
||||||
ret = ast_alloc(AST_WORD);
|
|
||||||
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);
|
|
||||||
ret->data.word.kind = AST_WORD_SINGLE_QUOTE;
|
|
||||||
return (*out = ret, NO_ERROR);
|
|
||||||
}
|
|
||||||
t_error _tok_word_dquote(t_token *tok, t_ast_node *out)
|
|
||||||
{
|
|
||||||
t_ast_node ret;
|
|
||||||
t_ast_node tmp;
|
|
||||||
t_usize i;
|
|
||||||
|
|
||||||
ret = ast_alloc(AST_WORD);
|
|
||||||
ret->data.word.kind = AST_WORD_DOUBLE_QUOTE;
|
|
||||||
i = 0;
|
|
||||||
while (i < tok->subtokens.len)
|
|
||||||
{
|
|
||||||
if (_tok_word(&tok->subtokens.buffer[i++], &tmp))
|
|
||||||
return (ast_free(ret), ERROR);
|
|
||||||
vec_ast_push(&ret->data.word.inner, tmp);
|
|
||||||
}
|
|
||||||
return (*out = ret, NO_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_error _tok_word(t_token *tok, t_ast_node *out)
|
|
||||||
{
|
|
||||||
if (tok->type == TOK_EXPENSION)
|
|
||||||
{
|
|
||||||
if (_tok_word_expansion(tok, out))
|
|
||||||
return (ERROR);
|
|
||||||
}
|
|
||||||
else if (tok->type == TOK_NQUOTE || tok->type == TOK_WHITESPACE)
|
|
||||||
{
|
|
||||||
if (_tok_word_nquote(tok, out))
|
|
||||||
return (ERROR);
|
|
||||||
}
|
|
||||||
else if (tok->type == TOK_SQUOTE)
|
|
||||||
{
|
|
||||||
if (_tok_word_squote(tok, out))
|
|
||||||
return (ERROR);
|
|
||||||
}
|
|
||||||
else if (tok->type == TOK_DQUOTE)
|
|
||||||
{
|
|
||||||
if (_tok_word_dquote(tok, out))
|
|
||||||
return (ERROR);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return (ERROR);
|
|
||||||
return (NO_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_error handle_tok_word_inner(t_token *tok, t_ast_node *out)
|
|
||||||
{
|
|
||||||
t_usize i;
|
|
||||||
t_ast_node ret;
|
|
||||||
t_ast_node tmp;
|
|
||||||
|
|
||||||
if (tok == NULL || out == NULL || tok->type != TOK_WORD)
|
|
||||||
return (ERROR);
|
|
||||||
i = 0;
|
|
||||||
ret = ast_alloc(AST_WORD);
|
|
||||||
ret->data.word.kind = AST_WORD_NO_QUOTE;
|
|
||||||
while (i < tok->subtokens.len)
|
|
||||||
{
|
|
||||||
if (_tok_word(&tok->subtokens.buffer[i++], &tmp))
|
|
||||||
return (ast_free(ret), ERROR);
|
|
||||||
vec_ast_push(&ret->data.word.inner, tmp);
|
|
||||||
}
|
|
||||||
return (*out = ret, NO_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_error handle_tok_redir(t_ast_node cmd, t_token *tok)
|
|
||||||
{
|
|
||||||
t_ast_node redir;
|
|
||||||
|
|
||||||
if (cmd == NULL || tok == NULL || tok->type != TOK_REDIR || cmd->kind != AST_COMMAND)
|
|
||||||
return (ERROR);
|
|
||||||
if (!(tok->subtokens.len == 2 && _is_carret(tok->subtokens.buffer[0].type) && tok->subtokens.buffer[1].type == TOK_WORD))
|
|
||||||
return (ERROR);
|
|
||||||
if (_create_ast_redir(tok->subtokens.buffer[0].type, &redir))
|
|
||||||
return (ERROR);
|
|
||||||
if (_ast_set_redir_arg(redir, &tok->subtokens.buffer[1]))
|
|
||||||
return (ast_free(redir), ERROR);
|
|
||||||
vec_ast_push(&cmd->data.command.prefixes, redir);
|
|
||||||
return (NO_ERROR);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// la fonction doit prendre une t_token de type TOK_CMD qui contient que deux type de subtokens TOK_WORD ou TOK_REDIR
|
|
||||||
/// un TOK_WORD == un arguemnt
|
|
||||||
/// un TOK_REDIR == une redirection
|
|
||||||
/// les deux sont dans le bonne ordre.
|
|
||||||
/// il faut push les TOK_REDIR dans ast_node->data.command.prefix;
|
|
||||||
/// let TOK_WORD dans ast_node->data.command.arguements;
|
|
||||||
/// 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_error ast_from_cmd(t_token tok, t_vec_ast *output_queue)
|
|
||||||
{
|
|
||||||
t_ast_node ret;
|
|
||||||
t_usize i;
|
|
||||||
|
|
||||||
if (tok.type != TOK_CMD)
|
|
||||||
return (ERROR);
|
|
||||||
ret = ast_alloc(AST_COMMAND);
|
|
||||||
i = 0;
|
|
||||||
while (i < tok.subtokens.len)
|
|
||||||
{
|
|
||||||
if (tok.subtokens.buffer[i].type == TOK_REDIR)
|
|
||||||
{
|
|
||||||
if (handle_tok_redir(ret, &tok.subtokens.buffer[i]))
|
|
||||||
return (ast_free(ret), ERROR);
|
|
||||||
}
|
|
||||||
else if (tok.subtokens.buffer[i].type == TOK_WORD)
|
|
||||||
{
|
|
||||||
if (handle_tok_word(ret, &tok.subtokens.buffer[i]))
|
|
||||||
return (ast_free(ret), ERROR);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
return (ast_free(ret), ERROR);
|
|
||||||
i++;
|
|
||||||
}
|
|
||||||
token_free(tok);
|
|
||||||
return (vec_ast_push(output_queue, ret), NO_ERROR);
|
|
||||||
}
|
|
||||||
|
|
@ -82,7 +82,7 @@ t_error populate_env(t_hashmap_env *env, t_str envp[])
|
||||||
return (NO_ERROR);
|
return (NO_ERROR);
|
||||||
}
|
}
|
||||||
|
|
||||||
t_error yarn(t_vec_token ts, t_vec_ast *output);
|
t_error yard(t_vec_token ts, t_vec_ast *output);
|
||||||
|
|
||||||
t_error parse_str(t_state *state)
|
t_error parse_str(t_state *state)
|
||||||
{
|
{
|
||||||
|
|
@ -93,7 +93,7 @@ t_error parse_str(t_state *state)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
if (ts_apply_passes(tokens, &tokens))
|
if (ts_apply_passes(tokens, &tokens))
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
if (yarn(tokens, &ast))
|
if (yard(tokens, &ast))
|
||||||
return ((void)printf("failed to ast build\n"), (ERROR));
|
return ((void)printf("failed to ast build\n"), (ERROR));
|
||||||
if (ast.len != 1)
|
if (ast.len != 1)
|
||||||
return (ERROR);
|
return (ERROR);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue