norm: almost everything

This commit is contained in:
maix0 2024-10-12 16:22:37 +02:00
parent 8122ecfc95
commit 54a1041aa7
9 changed files with 311 additions and 264 deletions

124
parser/src/yard/yard.c Normal file
View file

@ -0,0 +1,124 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* yard.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/07 18:04:13 by rparodi #+# #+# */
/* Updated: 2024/10/12 16:11:10 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "ast/ast.h"
#include "me/printf/printf.h"
#include "me/printf/printf.h"
#include "me/types.h"
#include "me/vec/vec_token.h"
#include "parser/token.h"
#include <stdio.h>
int _get_prec(t_token *token);
t_str token_name(t_token *token);
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 _yard_parenthesis(\
t_token tok, t_vec_token *stack, t_vec_ast *output_queue)
{
t_token op;
t_ast_node snode;
t_ast_node tmp;
token_free(tok);
while (vec_token_last(stack) != NULL && \
vec_token_last(stack)->type != TOK_LPAREN)
{
vec_token_pop(stack, &op);
if (ast_from_op(op, output_queue))
return (token_free(op), ERROR);
}
if (!(vec_token_last(stack) != NULL && \
vec_token_last(stack)->type == TOK_LPAREN))
return (ERROR);
vec_token_pop(stack, &tok);
token_free(tok);
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);
return (NO_ERROR);
}
t_error _yard_operator(t_token tok, t_vec_token *stack, t_vec_ast *output_queue)
{
t_token op;
while (vec_token_last(stack) != NULL && vec_token_last(stack)->type \
!= TOK_LPAREN && _get_prec(vec_token_last(stack)) > _get_prec(&tok))
{
vec_token_pop(stack, &op);
if (ast_from_op(op, output_queue))
return (token_free(tok), token_free(op), ERROR);
}
vec_token_push(stack, tok);
return (NO_ERROR);
}
t_error _yard_final(t_vec_token stack, t_vec_ast output_q, t_vec_ast *out)
{
t_token op;
while (!vec_token_pop(&stack, &op))
{
if (op.type == TOK_LPAREN)
return (token_free(op), ERROR);
if (ast_from_op(op, &output_q))
return (vec_token_free(stack), vec_ast_free(output_q), \
token_free(op), ERROR);
}
vec_token_free(stack);
return (*out = output_q, NO_ERROR);
}
t_error _yard_loop(t_token tok, t_vec_token *stack, t_vec_ast *output_q)
{
if (tok.type == TOK_CMD)
{
if (ast_from_cmd(tok, output_q))
return (ERROR);
}
else if (tok.type == TOK_OR \
|| tok.type == TOK_AND || tok.type == TOK_PIPE)
{
if (_yard_operator(tok, stack, output_q))
return (ERROR);
}
else if (tok.type == TOK_RPAREN)
{
if (_yard_parenthesis(tok, stack, output_q))
return (ERROR);
}
else if (tok.type == TOK_LPAREN)
vec_token_push(stack, tok);
else
return (me_eprintf("Syntax Error\n"), ERROR);
return (NO_ERROR);
}
t_error yard(t_vec_token ts, t_vec_ast *out)
{
t_token tok;
t_vec_ast output_q;
t_vec_token stack;
output_q = vec_ast_new(16, ast_free);
stack = vec_token_new(16, token_free);
while (!vec_token_pop_front(&ts, &tok))
{
if (_yard_loop(tok, &stack, &output_q))
return (vec_token_free(stack), vec_ast_free(output_q), ERROR);
}
vec_token_free(ts);
return (_yard_final(stack, output_q, out));
}

123
parser/src/yard/yard_cmd.c Normal file
View 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);
}

View 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);
}

View 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);
}

95
parser/src/yard/yard_op.c Normal file
View file

@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ast_op.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/09 12:44:53 by maiboyer #+# #+# */
/* Updated: 2024/10/12 15:55:51 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "ast/ast.h"
#include "me/types.h"
#include "me/vec/vec_ast.h"
#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 inline 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
/// utilise me_abort(MSG) pour faire un abort et print le msg + la stacktrace.
///
/// a terme la fonction utilisera t_error et tt;
///
/// 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`
///
///
/// 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_error ast_from_op(t_token tok, t_vec_ast *output_queue)
{
t_ast_node ret;
t_ast_node lhs;
t_ast_node rhs;
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_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_PIPE && _tok_pipeline(output_queue, rhs, lhs))
return (ERROR);
token_free(tok);
return (NO_ERROR);
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* yard_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/12 16:11:05 by maiboyer #+# #+# */
/* Updated: 2024/10/12 16:11:19 by maiboyer ### ########.fr */
/* */
/* ************************************************************************** */
#include "parser/token.h"
int _get_prec(t_token *token)
{
if (!token)
return (-1);
else if (token->type == TOK_PIPE)
return (2);
else if (token->type == TOK_AND || token->type == TOK_OR)
return (1);
return (0);
}