libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
stack.c
Go to the documentation of this file.
1#include <stdlib.h>
2
3#include "ast.h"
4#include "stack.h"
5#include "types/err.h"
6
7void
9
10void
18
19void
21{
22 free(stack->values);
23 stack->values = NULL;
24}
25
35
38{
40
41 ast_copy(&stack->values[stack->length], source);
42
43 return &stack->values[stack->length++];
44}
45
48{
49 if (stack_isempty(stack)) {
50 return ERR_EMPTY;
51 }
52
53 ast_copy(value, &stack->values[--stack->length]);
54
55 return ERR_OK;
56}
57
58bool
60{
61 return stack->length == 0;
62}
63
64void
66{
67 if (stack->length < stack->_size) {
68 return;
69 }
70
72 stack->values = realloc(stack->values, sizeof(ast_node_t) * stack->_size);
73}
void ast_node_init(ast_node_t *node, ast_node_t *parent, node_type_t type, token_t *token)
Initializes an AST node.
Definition ast.c:17
void ast_copy(ast_node_t *destiny, ast_node_t *source)
Copies the content of source to destinty AST nodes.
Definition ast.c:64
enum error_code error_code_t
Enumeration of error codes.
@ ERR_OK
Definition err.h:15
@ ERR_EMPTY
Definition err.h:16
void stack_init(stack_t *stack)
Initializes the given stack.
Definition stack.c:11
ast_node_t * stack_push(stack_t *stack, node_type_t type, token_t *token)
Push a new value on the stack.
Definition stack.c:27
void stack_close(stack_t *stack)
Closes the given stack.
Definition stack.c:20
error_code_t stack_pop(stack_t *stack, ast_node_t *value)
Pop a value from the stack.
Definition stack.c:47
bool stack_isempty(stack_t *stack)
Check if the given stack is empty.
Definition stack.c:59
ast_node_t * stack_insert(stack_t *stack, ast_node_t *source)
Push a new value on the stack copying from source pointer.
Definition stack.c:37
void _stack_ensure_size(stack_t *stack)
Definition stack.c:65
A struct representing a dynamic stack.
Definition stack.h:12
unsigned int length
Number of elements on the stack.
Definition stack.h:14
ast_node_t * values
Array of values on the stack.
Definition stack.h:13
unsigned int _size
Current number of elements that fits on the stack.
Definition stack.h:15
Struct for a Mya token.
Definition token.h:32
enum node_type node_type_t
struct ast_node ast_node_t
#define STACK_INITIAL_LENGTH
Definition stack.h:5
#define STACK_LENGTH_INCREMENT
Definition stack.h:6
struct stack stack_t
A struct representing a dynamic stack.
struct token token_t
Struct for a Mya token.