-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_term.c
64 lines (54 loc) · 1.75 KB
/
token_term.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Implementace překladače imperativního jazyka IFJ22
*
* @file token_term.c
* @author Josef Kuchař ([email protected])
* @author Matej Sirovatka ([email protected])
* @author Tomáš Běhal ([email protected])
* @author Šimon Benčík ([email protected])
* @brief Implementation of token terminal functions
*/
#include "token_term.h"
#include <stdio.h>
#include "error.h"
token_term_t* token_term_new(token_t value, bool terminal) {
token_term_t* new = malloc(sizeof(token_term_t));
if (!new) {
error_exit(ERR_INTERNAL);
}
new->result = value.type;
new->left = NULL;
new->right = NULL;
new->value = value;
new->terminal = terminal;
return new;
}
void token_term_pprint(token_term_t token) {
printf("%s[%d]\n", token_to_string(token.result), token.terminal);
}
void token_graph_print(token_term_t* token, int depth, int side) {
if (token == NULL) {
return;
}
if (token->left != NULL) {
printf("\"%s (%d_%d)\" -> \"%s (%d_%d)\"\n", token_to_string(token->value.type), depth,
side, token_to_string(token->left->value.type), depth + 1, side * 2);
}
if (token->right != NULL) {
printf("\"%s (%d_%d)\" -> \"%s (%d_%d)\"\n", token_to_string(token->value.type), depth,
side, token_to_string(token->right->value.type), depth + 1, side * 2 + 1);
}
token_graph_print(token->left, depth + 1, side * 2);
token_graph_print(token->right, depth + 1, side * 2 + 1);
}
void token_term_free(token_term_t* root) {
if (!root) {
return;
}
token_term_free(root->left);
token_term_free(root->right);
root->left = NULL;
root->right = NULL;
token_free(&root->value);
free(root);
}