libabacus/include/tree.h

92 lines
1.7 KiB
C
Raw Normal View History

#ifndef LIBABACUS_TREE_H
#define LIBABACUS_TREE_H
2018-02-11 22:32:42 -08:00
#include "result.h"
#include "vec.h"
/**
* Enum to represent the variant of a tree node.
*/
enum libab_tree_variant_e {
NONE,
BASE,
ID,
STR,
CHAR,
NUM,
BOOL,
KW,
OP,
UNARY_OP,
BLOCK,
FUN,
IF,
WHILE,
DOWHILE,
FOR,
CALL,
RETURN
};
/**
* A tree node that has been parsed from the input tokens.
*/
struct libab_tree_s {
/**
* The variant of tree node.
*/
enum libab_tree_variant_e variant;
/**
* The string value of this tree, if applicable.
*/
char* string_value;
/**
* The int value of this tree, if applicable.
*/
int int_value;
/**
* The children of this tree node. This vector
* will not be initialized if this tree node
* type does not usually have children.
*/
vec children;
/**
* The line on which this tree starts.
*/
size_t line;
/**
* The index in the string where this line begins.
*/
size_t line_from;
/**
* The beginning in the string of this tree.
*/
size_t from;
/**
* The index in the string of the next
* thing that isn't part of this tree.
*/
size_t to;
};
typedef enum libab_tree_variant_e libab_tree_variant;
typedef struct libab_tree_s libab_tree;
2018-02-17 12:55:50 -08:00
/**
* Frees the given tree, using its
* variant as a hint as to what
* variables need to be freed.
* @param tree the tree to free.
*/
void libab_tree_free(libab_tree* tree);
2018-02-17 19:20:10 -08:00
/**
* Frees the given tree recursively,
* deleting the children first and the moving on
* to the parents.
* @param tree the tree to free.
*/
void libab_tree_free_recursive(libab_tree* tree);
2018-02-17 12:55:50 -08:00
#endif