166 lines
4.6 KiB
C
166 lines
4.6 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* scripting_node.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2024/08/06 18:34:12 by rparodi #+# #+# */
|
|
/* Updated: 2024/08/09 14:42:13 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_function_definition(\
|
|
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_function_definition)
|
|
return (ERROR);
|
|
ret = ast_alloc(AST_FUNCTION_DEFINITION);
|
|
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_name)
|
|
ret->data.function_definition.name = \
|
|
_extract_str(ts_node_child(self, i), input);
|
|
if (ts_node_field_id_for_child(self, i) == field_body)
|
|
{
|
|
if (ast_from_node(ts_node_child(self, i), input, &tmp))
|
|
return (ast_free(ret), ERROR);
|
|
vec_ast_push(&ret->data.function_definition.body, tmp);
|
|
}
|
|
i++;
|
|
}
|
|
return (*out = ret, NO_ERROR);
|
|
}
|
|
|
|
t_error build_sym_case_statement(\
|
|
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_case_statement)
|
|
return (ERROR);
|
|
ret = ast_alloc(AST_CASE);
|
|
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_value)
|
|
if (ast_from_node(ts_node_child(self, i), \
|
|
input, &ret->data.case_.word))
|
|
return (ast_free(ret), ERROR);
|
|
if (ts_node_field_id_for_child(self, i) == field_cases)
|
|
{
|
|
if (ast_from_node(ts_node_child(self, i), input, &tmp))
|
|
return (ast_free(ret), ERROR);
|
|
vec_ast_push(&ret->data.case_.cases, tmp);
|
|
}
|
|
i++;
|
|
}
|
|
return (*out = ret, NO_ERROR);
|
|
}
|
|
|
|
t_error build_sym__case_item_last(\
|
|
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__case_item_last)
|
|
return (ERROR);
|
|
ret = ast_alloc(AST_CASE_ITEM);
|
|
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_value)
|
|
{
|
|
if (ast_from_node(ts_node_child(self, i), input, &tmp))
|
|
return (ast_free(ret), ERROR);
|
|
vec_ast_push(&ret->data.case_item.pattern, tmp);
|
|
}
|
|
if (ts_node_field_id_for_child(self, i) == field_body)
|
|
{
|
|
if (ast_from_node(ts_node_child(self, i), input, &tmp))
|
|
return (ast_free(ret), ERROR);
|
|
vec_ast_push(&ret->data.case_item.body, tmp);
|
|
}
|
|
i++;
|
|
}
|
|
return (*out = ret, NO_ERROR);
|
|
}
|
|
|
|
t_error build_sym_case_item(\
|
|
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_case_item)
|
|
return (ERROR);
|
|
ret = ast_alloc(AST_CASE_ITEM);
|
|
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_value)
|
|
{
|
|
if (ast_from_node(ts_node_child(self, i), input, &tmp))
|
|
return (ast_free(ret), ERROR);
|
|
vec_ast_push(&ret->data.case_item.pattern, tmp);
|
|
}
|
|
if (ts_node_field_id_for_child(self, i) == field_body)
|
|
{
|
|
if (ast_from_node(ts_node_child(self, i), input, &tmp))
|
|
return (ast_free(ret), ERROR);
|
|
vec_ast_push(&ret->data.case_item.body, tmp);
|
|
}
|
|
i++;
|
|
}
|
|
return (*out = ret, NO_ERROR);
|
|
}
|