aboutsummaryrefslogtreecommitdiff
path: root/src/token.c
blob: 3479a86da1f4c0d9a1b0b6f173683ebedf8cdf11 (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
56
#include "include/token.h"

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

   token = emalloc(sizeof(token_t));
   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) { return; }
   
   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;

   t = token;

   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) {
   if (!token) { return; }
   log_dbg("token/t=%d\t/v=%s", token->type, token->val); 
   if (token->nxt) {
      token_print(token->nxt);
   }
}