libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
stack.h File Reference
#include <stdbool.h>
#include "types/err.h"
#include "types/stack.h"
Include dependency graph for stack.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

void stack_init (stack_t *stack)
 Initializes the given stack.
 
void stack_close (stack_t *stack)
 Closes the given stack.
 
ast_node_tstack_push (stack_t *stack, node_type_t type, token_t *token)
 Push a new value on the stack.
 
ast_node_tstack_insert (stack_t *stack, ast_node_t *source)
 Push a new value on the stack copying from source pointer.
 
error_code_t stack_pop (stack_t *stack, ast_node_t *value)
 Pop a value from the stack.
 
bool stack_isempty (stack_t *stack)
 Check if the given stack is empty.
 

Function Documentation

◆ stack_close()

void stack_close ( stack_t * stack)

Closes the given stack.

Parameters
stackThe stack to be closed.

Definition at line 20 of file stack.c.

21{
22 free(stack->values);
23 stack->values = NULL;
24}
A struct representing a dynamic stack.
Definition stack.h:12
ast_node_t * values
Array of values on the stack.
Definition stack.h:13

◆ stack_init()

void stack_init ( stack_t * stack)

Initializes the given stack.

Parameters
stackThe stack to be initialized.

Definition at line 11 of file stack.c.

12{
13 stack->length = 0;
14
15 stack->values = malloc(sizeof(ast_node_t) * STACK_INITIAL_LENGTH);
17}
unsigned int length
Number of elements on the stack.
Definition stack.h:14
unsigned int _size
Current number of elements that fits on the stack.
Definition stack.h:15
struct ast_node ast_node_t
#define STACK_INITIAL_LENGTH
Definition stack.h:5

◆ stack_insert()

ast_node_t * stack_insert ( stack_t * stack,
ast_node_t * source )

Push a new value on the stack copying from source pointer.

Parameters
stackThe stack where tbe value will be pushed.
sourceThe AST node content.
Returns
A pointer for the new value inside the stack.

Definition at line 37 of file stack.c.

38{
40
41 ast_copy(&stack->values[stack->length], source);
42
43 return &stack->values[stack->length++];
44}
void ast_copy(ast_node_t *destiny, ast_node_t *source)
Copies the content of source to destinty AST nodes.
Definition ast.c:64
void _stack_ensure_size(stack_t *stack)
Definition stack.c:65

◆ stack_isempty()

bool stack_isempty ( stack_t * stack)

Check if the given stack is empty.

Parameters
stackThe stack to check.
Returns
true on stack is empty.
false if stack has values.

Definition at line 59 of file stack.c.

60{
61 return stack->length == 0;
62}

◆ stack_pop()

error_code_t stack_pop ( stack_t * stack,
ast_node_t * value )

Pop a value from the stack.

Parameters
stackThe stack from where the value will be poped.
valuePointer for where the poped value will be saved.
Returns
ERR_EMPTY on stack is empty.
ERR_OK on value is poped successful.

Definition at line 47 of file stack.c.

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}
@ ERR_OK
Definition err.h:15
@ ERR_EMPTY
Definition err.h:16
bool stack_isempty(stack_t *stack)
Check if the given stack is empty.
Definition stack.c:59

◆ stack_push()

ast_node_t * stack_push ( stack_t * stack,
node_type_t type,
token_t * token )

Push a new value on the stack.

Parameters
stackThe stack where tbe value will be pushed.
typeThe AST node type.
tokenThe AST node token.
Returns
A pointer for the new value inside the stack.

Definition at line 27 of file stack.c.

28{
30
31 ast_node_init(&stack->values[stack->length], NULL, type, token);
32
33 return &stack->values[stack->length++];
34}
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
Struct for a Mya token.
Definition token.h:32