merge new parser into dev
This commit is contained in:
commit
d707c6180b
16 changed files with 725 additions and 228 deletions
|
|
@ -10,7 +10,7 @@
|
|||
|
||||
void ast_free(struct ast **node)
|
||||
{
|
||||
if (*node == NULL)
|
||||
if (node == NULL || *node == NULL)
|
||||
return;
|
||||
// ast void does not need to be freed.
|
||||
if (ast_is_if(*node))
|
||||
|
|
|
|||
|
|
@ -9,6 +9,7 @@
|
|||
#include "ast_list.h"
|
||||
#include "ast_redir.h"
|
||||
#include "ast_void.h"
|
||||
#include "ast_word.h"
|
||||
|
||||
/**
|
||||
* Prints the Graphviz DOT representation of the given AST to stdout.
|
||||
|
|
|
|||
|
|
@ -11,7 +11,8 @@ enum ast_type
|
|||
AST_AND_OR,
|
||||
AST_REDIR,
|
||||
AST_VOID,
|
||||
AST_CMD
|
||||
AST_CMD,
|
||||
AST_WORD
|
||||
};
|
||||
|
||||
struct ast
|
||||
|
|
|
|||
45
src/utils/ast/ast_word.c
Normal file
45
src/utils/ast/ast_word.c
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
#include "ast_word.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct ast *ast_create_word(char *word)
|
||||
{
|
||||
struct ast_word *ast_node = malloc(sizeof(struct ast_word));
|
||||
if (ast_node == NULL)
|
||||
return NULL;
|
||||
|
||||
ast_node->type = AST_WORD;
|
||||
ast_node->word = strdup(word);
|
||||
struct ast *res = ast_create(AST_WORD, ast_node);
|
||||
if (res == NULL)
|
||||
{
|
||||
free(ast_node);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
struct ast_word *ast_get_word(struct ast *node)
|
||||
{
|
||||
if (node == NULL || node->type != AST_WORD)
|
||||
return NULL;
|
||||
|
||||
return node->data;
|
||||
}
|
||||
|
||||
bool ast_is_word(struct ast *node)
|
||||
{
|
||||
return node && node->type == AST_WORD;
|
||||
}
|
||||
|
||||
void ast_free_word(struct ast_word *ast_node)
|
||||
{
|
||||
if (ast_node == NULL)
|
||||
return;
|
||||
|
||||
free(ast_node->word);
|
||||
free(ast_node);
|
||||
}
|
||||
35
src/utils/ast/ast_word.h
Normal file
35
src/utils/ast/ast_word.h
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#ifndef AST_WORD_H
|
||||
#define AST_WORD_H
|
||||
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "ast_base.h"
|
||||
|
||||
struct ast_word
|
||||
{
|
||||
enum ast_type type;
|
||||
char *word;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if the given AST node is a command.
|
||||
*/
|
||||
bool ast_is_word(struct ast *node);
|
||||
|
||||
/**
|
||||
* Retrieves the command data from the given AST node.
|
||||
* Assumes that the node is of type AST_CMD.
|
||||
*/
|
||||
struct ast_word *ast_get_word(struct ast *node);
|
||||
|
||||
/**
|
||||
* Creates a new AST node representing a command.
|
||||
*/
|
||||
struct ast *ast_create_word(char* word);
|
||||
|
||||
/*
|
||||
* @brief: frees the given ast_command and sets the pointer to NULL.
|
||||
*/
|
||||
void ast_free_word(struct ast_word *ast_node);
|
||||
|
||||
#endif /* ! AST_WORD_H */
|
||||
Loading…
Add table
Add a link
Reference in a new issue