aboutsummaryrefslogtreecommitdiff
path: root/src/token.c
blob: 935f23ea222e723cd494f69e69a15932e2ebd2d2 (plain) (blame)
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
#include <stdlib.h>

#include "include/token.h"

token_t* token_init(int type, char val) {
   token_t* token;

   token = emalloc(sizeof(struct TOKEN_STRUC));
   token->type = type;
   token->val = emalloc(2);
   *token->val = val;
   token->val[1] = '\0';
   token->nxt = NULL;

   return token;
}

void token_destroy(token_t* token) {
   if (token->nxt) {
      token_destroy(token->nxt);
      token->nxt = NULL;
   }

   free(token->val);
   free(token);
}

token_t* token_last(token_t* token) {
   token_t* t;

   while (t->nxt) {
      t = t->nxt;
   }

   return t;
}

void token_add_char(token_t* token, char c) {
   size_t orig;

   orig = strlen(token->val);

   token->val = erealloc(token->val, orig + sizeof c + 1);
   token->val[orig] = c;
   token->val[orig + 1] = '\0';
}

void token_print(token_t* token) {

   log_dbg("token/t=%d\t/v=%s", token->type, token->val);

   if (token->nxt) {
      token_print(token->nxt);
   }
}