2018-02-10 22:15:51 -08:00
|
|
|
#ifndef LIBABACUS_TREE_H
|
|
|
|
#define LIBABACUS_TREE_H
|
|
|
|
|
2018-03-08 21:10:05 -08:00
|
|
|
#include "parsetype.h"
|
2018-04-21 14:09:01 -07:00
|
|
|
#include "result.h"
|
2018-02-10 22:15:51 -08:00
|
|
|
#include "vec.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enum to represent the variant of a tree node.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
enum libab_tree_variant_e {
|
2018-03-08 21:13:25 -08:00
|
|
|
TREE_NONE,
|
|
|
|
TREE_BASE,
|
|
|
|
TREE_ID,
|
|
|
|
TREE_NUM,
|
|
|
|
TREE_OP,
|
2018-03-16 23:09:41 -07:00
|
|
|
TREE_RESERVED_OP,
|
2018-03-17 18:25:23 -07:00
|
|
|
TREE_PREFIX_OP,
|
|
|
|
TREE_POSTFIX_OP,
|
2018-03-08 21:13:25 -08:00
|
|
|
TREE_BLOCK,
|
|
|
|
TREE_VOID,
|
2018-08-10 16:06:30 -07:00
|
|
|
TREE_TRUE,
|
|
|
|
TREE_FALSE,
|
2018-03-08 21:13:25 -08:00
|
|
|
TREE_IF,
|
|
|
|
TREE_WHILE,
|
|
|
|
TREE_DOWHILE,
|
|
|
|
TREE_CALL,
|
|
|
|
TREE_FUN,
|
2018-03-08 21:31:24 -08:00
|
|
|
TREE_FUN_PARAM,
|
|
|
|
TREE_RETURN
|
2018-02-10 22:15:51 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A tree node that has been parsed from the input tokens.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
struct libab_tree_s {
|
2018-03-08 21:10:05 -08:00
|
|
|
/**
|
|
|
|
* The parse type of this node, if applicable.
|
|
|
|
*/
|
2018-04-17 22:14:07 -07:00
|
|
|
libab_ref type;
|
2018-02-10 22:15:51 -08:00
|
|
|
/**
|
|
|
|
* The variant of tree node.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
enum libab_tree_variant_e variant;
|
2018-02-10 22:15:51 -08:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
};
|
|
|
|
|
2018-02-11 21:09:41 -08:00
|
|
|
typedef enum libab_tree_variant_e libab_tree_variant;
|
|
|
|
typedef struct libab_tree_s libab_tree;
|
2018-02-10 22:15:51 -08:00
|
|
|
|
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-25 22:57:45 -08:00
|
|
|
/**
|
|
|
|
* Determines if the given tree node variant
|
|
|
|
* should contain a string.
|
|
|
|
* @param var the variant of the tree node.
|
|
|
|
* @return true if the tree node variant contains a string.
|
|
|
|
*/
|
|
|
|
int libab_tree_has_string(libab_tree_variant var);
|
2018-03-08 21:10:05 -08:00
|
|
|
/**
|
|
|
|
* Determines if the given tree node variant
|
|
|
|
* should contain a parse type.
|
|
|
|
* @param var the variant of the tree node.
|
|
|
|
* @return true if the tree node variant contains a type.
|
|
|
|
*/
|
|
|
|
int libab_tree_has_type(libab_tree_variant var);
|
2018-03-24 00:36:10 -07:00
|
|
|
/**
|
|
|
|
* Determines if the given tree node
|
|
|
|
* variant contains a scope.
|
|
|
|
* @param var the variant of the tree node.
|
|
|
|
* @return true if the node variant contains a scope.
|
|
|
|
*/
|
|
|
|
int libab_tree_has_scope(libab_tree_variant var);
|
2018-02-25 22:57:45 -08:00
|
|
|
/**
|
|
|
|
* Determines if the given tree node variant
|
|
|
|
* should contain a vector.
|
|
|
|
* @param var the variant of the tree node.
|
|
|
|
* @return true if the tree node variant contains a vector.
|
|
|
|
*/
|
|
|
|
int libab_tree_has_vector(libab_tree_variant var);
|
2018-02-17 19:20:10 -08:00
|
|
|
/**
|
|
|
|
* Frees the given tree recursively,
|
|
|
|
* deleting the children first and the moving on
|
2018-04-21 14:09:01 -07:00
|
|
|
* to the parents.
|
2018-02-17 19:20:10 -08:00
|
|
|
* @param tree the tree to free.
|
|
|
|
*/
|
|
|
|
void libab_tree_free_recursive(libab_tree* tree);
|
2018-06-12 23:34:18 -07:00
|
|
|
/**
|
|
|
|
* Frees the tree, taking into account reference-counted
|
|
|
|
* nodes.
|
|
|
|
* @param tree the tree to free.
|
|
|
|
*/
|
|
|
|
void libab_tree_refcount_free(libab_tree* tree);
|
2018-02-17 12:55:50 -08:00
|
|
|
|
2018-02-10 22:15:51 -08:00
|
|
|
#endif
|