aboutsummaryrefslogtreecommitdiff
path: root/src/lexer.c
blob: efdc7188996e9d70bc1f01667bd6a5087ff17bcd (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>

#include "include/lexer.h" 

lexer_t* lexer_init(char* src) {
   lexer_t* lexer;

   lexer = emalloc(sizeof(struct LEXER_STRUC));

   lexer->src = src;
   lexer->state = LEXER_STATE_REG;
   lexer->tokenl = NULL;
   lexer->tokenl_last = NULL;
   lexer->tokenc = 0;

   return lexer;
}

void lexer_destroy(lexer_t* lexer) {
   free(lexer);
}

void lexer_add_token(lexer_t* lexer, token_t* token) {
   token_t* t;

   if (lexer->tokenl) {
      lexer->tokenl_last->nxt = token;
      lexer->tokenl_last = token;
   } else {
      lexer->tokenl = token;
      lexer->tokenl_last = token;
   }

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

   lexer->tokenc ++;
}

void lexer_add_current_char(lexer_t* lexer, int type) {
   char* c;    /* get the current character as a string */
   token_t* t; /* the token to be added */

   c = ecalloc(2, sizeof(char));
   c[0] = *lexer->src;
   c[1] = '\0';

   t = token_init(type, c);

   lexer_add_token(lexer, t);
}

void lexer_do_reg(lexer_t* lexer) {
   switch (*lexer->src) {
      case SYNTAX_APPLY:
         lexer_add_current_char(lexer, TOKEN_APPLY);
         break;
      default:
         lexer_add_current_char(lexer, TOKEN_UNKNOWN);
   }
}

void lexer_run(lexer_t* lexer) {
   while (*lexer->src) {
      if (lexer->state == LEXER_STATE_REG) { lexer_do_reg(lexer); }
      lexer->src ++;
   }
}