2018-02-10 22:15:51 -08:00
|
|
|
#ifndef LIBABACUS_TREE_H
|
|
|
|
#define LIBABACUS_TREE_H
|
|
|
|
|
2018-02-11 22:32:42 -08: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-02-10 22:15:51 -08:00
|
|
|
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.
|
|
|
|
*/
|
2018-02-11 21:09:41 -08:00
|
|
|
struct libab_tree_s {
|
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-10 22:15:51 -08:00
|
|
|
#endif
|