merge parser into dev with ast loops

This commit is contained in:
Matteo Flebus 2026-01-30 19:57:36 +01:00
commit 4d271981df
10 changed files with 343 additions and 26 deletions

View file

@ -17,6 +17,7 @@ libutils_a_SOURCES = \
ast/ast_word.c \
ast/ast_neg.c \
ast/ast_pipe.c \
ast/ast_loop.c \
args/args.c \
vars/vars.c \
ast/ast_assignment.c

View file

@ -8,6 +8,7 @@
#include "ast_end.h"
#include "ast_if.h"
#include "ast_list.h"
#include "ast_loop.h"
#include "ast_neg.h"
#include "ast_pipe.h"
#include "ast_redir.h"

View file

@ -16,6 +16,7 @@ enum ast_type
AST_WORD,
AST_PIPE,
AST_NEG,
AST_LOOP,
AST_ASSIGNMENT
};

37
src/utils/ast/ast_loop.c Normal file
View file

@ -0,0 +1,37 @@
#include "ast_loop.h"
#include <stdbool.h>
#include <stdlib.h>
struct ast *ast_create_loop(struct ast *condition, struct ast *body)
{
struct ast_loop *node_data = malloc(sizeof(struct ast_loop));
if (!node_data)
return NULL;
node_data->condition = condition;
node_data->body = body;
return ast_create(AST_LOOP, node_data);
}
struct ast_loop *ast_get_loop(struct ast *node)
{
if (node == NULL || node->type != AST_LOOP)
return NULL;
return (struct ast_loop *)node->data;
}
bool ast_is_loop(struct ast *node)
{
return node != NULL && node->type == AST_LOOP;
}
void ast_free_loop(struct ast_loop *loop_data)
{
if (loop_data == NULL)
return;
ast_free(&loop_data->condition);
ast_free(&loop_data->body);
free(loop_data);
}

34
src/utils/ast/ast_loop.h Normal file
View file

@ -0,0 +1,34 @@
#ifndef AST_LOOP_H
#define AST_LOOP_H
#include "ast_base.h"
struct ast_loop
{
// Repeat body while condition is true
struct ast *condition;
struct ast *body;
};
/**
* Checks if the given AST node is a loop.
*/
bool ast_is_loop(struct ast *node);
/**
* Retrieves the loop data from the given AST node.
* Assumes that the node is of type AST_LOOP.
*/
struct ast_loop *ast_get_loop(struct ast *node);
/**
* Creates a new AST node representing a loop.
*/
struct ast *ast_create_loop(struct ast* condition, struct ast* body);
/*
* @brief: frees the given ast_loop and sets the pointer to NULL.
*/
void ast_free_loop(struct ast_loop *loop_node);
#endif /* ! AST_LOOP_H */