libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
tkstack.c File Reference
#include <stdlib.h>
#include "ast.h"
#include "tkstack.h"
#include "types/err.h"
Include dependency graph for tkstack.c:

Go to the source code of this file.

Functions

void _tkstack_ensure_size (tkstack_t *stack)
 
void tkstack_init (tkstack_t *stack)
 Initializes the given stack.
 
void tkstack_close (tkstack_t *stack)
 Closes the given stack.
 
void tkstack_push (tkstack_t *stack, token_t *token)
 Push a new value on the stack.
 
token_ttkstack_pop (tkstack_t *stack)
 Pop a value from the stack.
 
token_ttkstack_peek (tkstack_t *stack)
 Peeks the value on top of the stack without removing it.
 
bool tkstack_isempty (tkstack_t *stack)
 Check if the given stack is empty.
 

Function Documentation

◆ _tkstack_ensure_size()

void _tkstack_ensure_size ( tkstack_t * stack)

Definition at line 61 of file tkstack.c.

62{
63 if (stack->length < stack->_size) {
64 return;
65 }
66
68 stack->values = realloc(stack->values, sizeof(token_t*) * stack->_size);
69}
A struct representing a dynamic stack.
Definition aststack.h:12
unsigned int length
Number of elements on the stack.
Definition aststack.h:14
ast_node_t * values
Array of values on the stack.
Definition aststack.h:13
unsigned int _size
Current number of elements that fits on the stack.
Definition aststack.h:15
#define TKSTACK_LENGTH_INCREMENT
Definition tkstack.h:6
struct token token_t
Struct for a Mya token.

◆ tkstack_close()

void tkstack_close ( tkstack_t * stack)

Closes the given stack.

Parameters
stackThe stack to be closed.

Definition at line 20 of file tkstack.c.

21{
22 free(stack->values);
23 stack->values = NULL;
24}

◆ tkstack_init()

void tkstack_init ( tkstack_t * stack)

Initializes the given stack.

Parameters
stackThe stack to be initialized.

Definition at line 11 of file tkstack.c.

12{
13 stack->length = 0;
14
15 stack->values = malloc(sizeof(token_t*) * TKSTACK_INITIAL_LENGTH);
17}
#define TKSTACK_INITIAL_LENGTH
Definition tkstack.h:5

◆ tkstack_isempty()

bool tkstack_isempty ( tkstack_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 55 of file tkstack.c.

56{
57 return stack->length == 0;
58}

◆ tkstack_peek()

token_t * tkstack_peek ( tkstack_t * stack)

Peeks the value on top of the stack without removing it.

Parameters
stackThe stack to peek into.
Returns
NULL if stack is empty.
Pointer to the top node in the stack.

Definition at line 45 of file tkstack.c.

46{
48 return NULL;
49 }
50
51 return stack->values[stack->length - 1];
52}
bool tkstack_isempty(tkstack_t *stack)
Check if the given stack is empty.
Definition tkstack.c:55

◆ tkstack_pop()

token_t * tkstack_pop ( tkstack_t * stack)

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
NULL when the queue is empty.
Pointer to the item.

Definition at line 35 of file tkstack.c.

36{
38 return NULL;
39 }
40
41 return stack->values[--stack->length];
42}

◆ tkstack_push()

void tkstack_push ( tkstack_t * stack,
token_t * token )

Push a new value on the stack.

Parameters
stackThe stack where tbe value will be pushed.
tokenPointer to a token to add on the stack.

Definition at line 27 of file tkstack.c.

28{
30
32}
Struct for a Mya token.
Definition token.h:34
void _tkstack_ensure_size(tkstack_t *stack)
Definition tkstack.c:61