adding the normed version

This commit is contained in:
Raphaël 2024-08-09 16:26:57 +02:00
parent dae891f20f
commit 1ecfba4340
6 changed files with 249 additions and 166 deletions

View file

@ -0,0 +1,114 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dev_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/09 15:07:10 by rparodi #+# #+# */
/* Updated: 2024/08/09 15:16:00 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ast/_from_node.h"
#include "ast/ast.h"
#include "gmr/field_identifiers.h"
#include "gmr/field_identifiers.h"
#include "gmr/symbols.h"
#include "gmr/symbols.h"
#include "me/str/str.h"
#include "me/types.h"
#include "me/vec/vec_ast.h"
#include "parser/api.h"
#include <stdio.h>
t_error build_sym_negated_command(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_negated_command)
return (ERROR);
if (ts_node_child_count(self) != 1)
return (ERROR);
if (ast_from_node(ts_node_child(self, 1), input, &ret))
return (ERROR);
_add_negation(&ret);
return (*out = ret, NO_ERROR);
}
t_error build_sym_pipeline(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
t_ast_node tmp;
t_usize i;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_pipeline)
return (ERROR);
ret = ast_alloc(AST_PIPELINE);
i = 0;
while (i < ts_node_child_count(self))
{
if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true))
continue ;
if (ast_from_node(ts_node_child(self, i), input, &tmp))
return (ast_free(ret), ERROR);
vec_ast_push(&ret->data.pipeline.statements, tmp);
i++;
}
return (*out = ret, NO_ERROR);
}
t_error build_sym_comment(t_parse_node self, t_const_str input, t_ast_node *out)
{
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_comment)
return (ERROR);
*out = ast_alloc(AST_EMPTY);
return (NO_ERROR);
}
t_error build_sym_variable_assignment(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
t_parse_node temp_ast;
(void)(self);
(void)(input);
(void)(out);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_variable_assignment)
return (ERROR);
ret = ast_alloc(AST_VARIABLE_ASSIGNMENT);
if (ts_node_child_count(self) >= 2)
{
temp_ast = ts_node_child(self, 0);
if (ts_node_symbol(temp_ast) != sym_variable_name)
return (ast_free(ret), ERROR);
ret->data.variable_assignment.name = _extract_str(temp_ast, input);
}
if (ts_node_child_count(self) > 2)
{
if (ast_from_node(ts_node_child(self, 2), \
input, &ret->data.variable_assignment.value))
return (ast_free(ret), ERROR);
}
return (*out = ret, NO_ERROR);
}

View file

View file

@ -0,0 +1,96 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* file_node.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/09 16:25:02 by rparodi #+# #+# */
/* Updated: 2024/08/09 16:26:09 by rparodi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ast/_from_node.h"
#include "ast/ast.h"
#include "gmr/field_identifiers.h"
#include "gmr/field_identifiers.h"
#include "gmr/symbols.h"
#include "gmr/symbols.h"
#include "me/str/str.h"
#include "me/types.h"
#include "me/vec/vec_ast.h"
#include "parser/api.h"
#include <stdio.h>
t_error build_sym_file_redirect(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
t_ast_node tmp;
t_usize i;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_file_redirect)
return (ERROR);
ret = ast_alloc(AST_FILE_REDIRECTION);
i = 0;
while (i < ts_node_child_count(self))
{
if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true))
continue ;
if (ts_node_field_id_for_child(self, i) == field_op)
{
ret->data.file_redirection.op = \
_get_redirection_op(ts_node_child(self, i));
}
if (ts_node_field_id_for_child(self, i) == field_dest)
{
if (ast_from_node(ts_node_child(self, i), input, &tmp))
return (ast_free(ret), ERROR);
ret->data.file_redirection.output = tmp;
}
i++;
}
return (*out = ret, NO_ERROR);
}
t_error build_sym_number(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_number)
return (ERROR);
ret = ast_alloc(AST_RAW_STRING);
ret->data.raw_string.str = _extract_str(self, input);
ret->data.raw_string.len = str_len(ret->data.raw_string.str);
return (*out = ret, NO_ERROR);
}
t_error build_sym_file_descriptor(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_file_descriptor)
return (ERROR);
ret = ast_alloc(AST_RAW_STRING);
ret->data.raw_string.str = _extract_str(self, input);
ret->data.raw_string.len = str_len(ret->data.raw_string.str);
return (*out = ret, NO_ERROR);
}

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/26 10:55:52 by rparodi #+# #+# */ /* Created: 2024/07/26 10:55:52 by rparodi #+# #+# */
/* Updated: 2024/08/06 19:00:51 by rparodi ### ########.fr */ /* Updated: 2024/08/09 16:26:13 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -150,42 +150,6 @@ t_error build_sym_command_substitution(\
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_file_descriptor(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_file_descriptor)
return (ERROR);
ret = ast_alloc(AST_RAW_STRING);
ret->data.raw_string.str = _extract_str(self, input);
ret->data.raw_string.len = str_len(ret->data.raw_string.str);
return (*out = ret, NO_ERROR);
}
t_error build_sym_number(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_number)
return (ERROR);
ret = ast_alloc(AST_RAW_STRING);
ret->data.raw_string.str = _extract_str(self, input);
ret->data.raw_string.len = str_len(ret->data.raw_string.str);
return (*out = ret, NO_ERROR);
}
/* if (ts_node_field_id_for_child(self, i) == field_fd) */ /* if (ts_node_field_id_for_child(self, i) == field_fd) */
/* { */ /* { */
/* if (ast_from_node(ts_node_child(self, i), input, &tmp)) */ /* if (ast_from_node(ts_node_child(self, i), input, &tmp)) */
@ -193,70 +157,6 @@ t_error build_sym_number(\
/* ret->data.file_redirection.input = tmp; */ /* ret->data.file_redirection.input = tmp; */
/* } */ /* } */
t_error build_sym_file_redirect(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
t_ast_node tmp;
t_usize i;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_file_redirect)
return (ERROR);
ret = ast_alloc(AST_FILE_REDIRECTION);
i = 0;
while (i < ts_node_child_count(self))
{
if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true))
continue ;
if (ts_node_field_id_for_child(self, i) == field_op)
{
ret->data.file_redirection.op = \
_get_redirection_op(ts_node_child(self, i));
}
if (ts_node_field_id_for_child(self, i) == field_dest)
{
if (ast_from_node(ts_node_child(self, i), input, &tmp))
return (ast_free(ret), ERROR);
ret->data.file_redirection.output = tmp;
}
i++;
}
return (*out = ret, NO_ERROR);
}
t_error build_sym_pipeline(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
t_ast_node tmp;
t_usize i;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_pipeline)
return (ERROR);
ret = ast_alloc(AST_PIPELINE);
i = 0;
while (i < ts_node_child_count(self))
{
if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true))
continue ;
if (ast_from_node(ts_node_child(self, i), input, &tmp))
return (ast_free(ret), ERROR);
vec_ast_push(&ret->data.pipeline.statements, tmp);
i++;
}
return (*out = ret, NO_ERROR);
}
t_error build_sym_do_group(\ t_error build_sym_do_group(\
t_parse_node self, t_const_str input, t_ast_node *out) t_parse_node self, t_const_str input, t_ast_node *out)
{ {
@ -343,26 +243,6 @@ t_error build_sym_subshell(\
// t_error buildw // t_error buildw
t_error build_sym_negated_command(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_negated_command)
return (ERROR);
if (ts_node_child_count(self) != 1)
return (ERROR);
if (ast_from_node(ts_node_child(self, 1), input, &ret))
return (ERROR);
_add_negation(&ret);
return (*out = ret, NO_ERROR);
}
t_error build_sym_compound_statement(\ t_error build_sym_compound_statement(\
t_parse_node self, t_const_str input, t_ast_node *out) t_parse_node self, t_const_str input, t_ast_node *out)
{ {
@ -402,49 +282,6 @@ t_error build_sym_compound_statement(\
return (*out = ret, NO_ERROR); return (*out = ret, NO_ERROR);
} }
t_error build_sym_comment(t_parse_node self, t_const_str input, t_ast_node *out)
{
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_comment)
return (ERROR);
*out = ast_alloc(AST_EMPTY);
return (NO_ERROR);
}
t_error build_sym_variable_assignment(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
t_parse_node temp_ast;
(void)(self);
(void)(input);
(void)(out);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_variable_assignment)
return (ERROR);
ret = ast_alloc(AST_VARIABLE_ASSIGNMENT);
if (ts_node_child_count(self) >= 2)
{
temp_ast = ts_node_child(self, 0);
if (ts_node_symbol(temp_ast) != sym_variable_name)
return (ast_free(ret), ERROR);
ret->data.variable_assignment.name = _extract_str(temp_ast, input);
}
if (ts_node_child_count(self) > 2)
{
if (ast_from_node(ts_node_child(self, 2), \
input, &ret->data.variable_assignment.value))
return (ast_free(ret), ERROR);
}
return (*out = ret, NO_ERROR);
}
t_error build_sym_string(t_parse_node self, t_const_str input, t_ast_node *out) t_error build_sym_string(t_parse_node self, t_const_str input, t_ast_node *out)
{ {
t_ast_node ret; t_ast_node ret;

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/06 18:45:21 by rparodi #+# #+# */ /* Created: 2024/08/06 18:45:21 by rparodi #+# #+# */
/* Updated: 2024/08/09 14:35:02 by rparodi ### ########.fr */ /* Updated: 2024/08/09 14:38:26 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -22,6 +22,42 @@
#include "parser/api.h" #include "parser/api.h"
#include <stdio.h> #include <stdio.h>
t_error build_sym_file_redirect(\
t_parse_node self, t_const_str input, t_ast_node *out)
{
t_ast_node ret;
t_ast_node tmp;
t_usize i;
(void)(out);
(void)(input);
(void)(self);
if (out == NULL)
return (ERROR);
if (ts_node_symbol(self) != sym_file_redirect)
return (ERROR);
ret = ast_alloc(AST_FILE_REDIRECTION);
i = 0;
while (i < ts_node_child_count(self))
{
if (!ts_node_is_named(ts_node_child(self, i)) && (i++, true))
continue ;
if (ts_node_field_id_for_child(self, i) == field_op)
{
ret->data.file_redirection.op = \
_get_redirection_op(ts_node_child(self, i));
}
if (ts_node_field_id_for_child(self, i) == field_dest)
{
if (ast_from_node(ts_node_child(self, i), input, &tmp))
return (ast_free(ret), ERROR);
ret->data.file_redirection.output = tmp;
}
i++;
}
return (*out = ret, NO_ERROR);
}
t_error build_sym_redirected_statement(\ t_error build_sym_redirected_statement(\
t_parse_node self, t_const_str input, t_ast_node *out) t_parse_node self, t_const_str input, t_ast_node *out)
{ {

View file

@ -6,7 +6,7 @@
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */ /* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/06 18:34:12 by rparodi #+# #+# */ /* Created: 2024/08/06 18:34:12 by rparodi #+# #+# */
/* Updated: 2024/08/09 14:34:51 by rparodi ### ########.fr */ /* Updated: 2024/08/09 14:42:13 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */