summaryrefslogtreecommitdiff
path: root/src/lexer.c
blob: db4c44b52ebeb2cd5dac2e7026951a321f2c584e (plain)
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "./include/lexer.h"
#include "./include/macros.h"
#include "./include/token.h"

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void lexer_reset(lexer_t *lexer, char *source) {
  lexer->source = source;
  lexer->c = source[0];
  lexer->i = 0;
  lexer->row = 1;
  lexer->col = 1;
  lexer->finished = false;
}

lexer_t *init_lexer(char *source) {
  lexer_t *l = (lexer_t *)malloc(sizeof(lexer_t));
  if (l == NULL)
    die("malloc on lexer");
  lexer_reset(l, source);
  return l;
}

void lexer_move(lexer_t *lexer) {
  if (lexer->c != '\0') {
    lexer->i++;
    lexer->c = lexer->source[lexer->i];
    if (lexer->c == '\n') {
      lexer->row++;
      lexer->col = 0;
    }
  } else
    lexer->finished = true;
}

void lexer_ignore_whitespace(lexer_t *lexer) {
  while (isspace(lexer->c)) {
    if (lexer->c == '\0')
      return;
    lexer_move(lexer);
  }
}

void lexer_skip_comment(lexer_t *lexer) {
  while (lexer->c != '\n' && lexer->c != '\0')
    lexer_move(lexer);
}

static bool is_valid_id_char(char c) {
  if (c == '(' || c == ')' || isdigit(c) || c == '"' || c == '\'' || c == '#' ||
      c == '.' || isspace(c))
    return false;
  return true;
}

static char *char_to_string(char c) {
  char *s = (char *)malloc(2 * sizeof(char));
  s[0] = c;
  s[1] = '\0';
  return s;
}

token_t *lexer_collect_bool(lexer_t *lexer) {
  lexer_move(lexer);
  if (lexer->c == 't') {
    lexer_move(lexer);
    return init_token(TOKEN_BOOL, "T", lexer->row, lexer->col);
  } else if (lexer->c == 'f') {
    lexer_move(lexer);
    return init_token(TOKEN_BOOL, "F", lexer->row, lexer->col);
  } else
    return NULL;
}

token_t *lexer_collect_id(lexer_t *lexer) {
  char *ret = (char *)malloc(1);
  ret[0] = '\0';

  while (is_valid_id_char(lexer->c)) {
    ret = realloc(ret, (strlen(ret) + 2));
    strcat(ret, char_to_string(lexer->c));
    lexer_move(lexer);
  }
  return init_token(TOKEN_ID, ret, lexer->row, lexer->col);
}

token_t *lexer_collect_num(lexer_t *lexer) {
  char *ret = (char *)malloc(1);
  ret[0] = '\0';
  bool is_float = false;
  while (isdigit(lexer->c) || (lexer->c == '.' && !is_float)) {
    if (lexer->c == '.')
      is_float = true;
    ret = realloc(ret, (strlen(ret) + 2));
    strcat(ret, char_to_string(lexer->c));
    lexer_move(lexer);
  }
  if (is_float)
    return init_token(TOKEN_FLOAT, ret, lexer->row, lexer->col);
  return init_token(TOKEN_INT, ret, lexer->row, lexer->col);
}

token_t *lexer_collect_string(lexer_t *lexer) {
  char *ret = (char *)malloc(1);
  ret[0] = '\0';
  lexer_move(lexer);
  while (lexer->c != '"') {
    ret = realloc(ret, (strlen(ret) + 2));
    strcat(ret, char_to_string(lexer->c));
    lexer_move(lexer);
  }
  lexer_move(lexer);
  return init_token(TOKEN_STRING, ret, lexer->row, lexer->col);
}

static token_t *lexer_move_with(lexer_t *lexer, token_t *token) {
  lexer_move(lexer);
  return token;
}

token_t *lexer_collect_next(lexer_t *lexer) {
  if (lexer->c == '\0' || lexer->c == EOF) {
    lexer->finished = true;
    return NULL;
  }

  if (isspace(lexer->c)) {
    lexer_ignore_whitespace(lexer);
  }
  if (lexer->c == '\0' || lexer->c == EOF) {
    lexer->finished = true;
    return NULL;
  }
  if (isdigit(lexer->c))
    return lexer_collect_num(lexer);
  else if (is_valid_id_char(lexer->c))
    return lexer_collect_id(lexer);
  else if (lexer->c == '"')
    return lexer_collect_string(lexer);
  else if (lexer->c == '#')
    return lexer_collect_bool(lexer);
  else if (lexer->c == '(')
    return lexer_move_with(
        lexer, init_token(TOKEN_LPAREN, "(", lexer->row, lexer->col));
  else if (lexer->c == ')')
    return lexer_move_with(
        lexer, init_token(TOKEN_RPAREN, ")", lexer->row, lexer->col));
  else if (lexer->c == '\'')
    return lexer_move_with(
        lexer, init_token(TOKEN_QUOTE, "'", lexer->row, lexer->col));
  else if (lexer->c == '.')
    return lexer_move_with(
        lexer, init_token(TOKEN_PERIOD, ".", lexer->row, lexer->col));
  else {
    printf("returns null\n");
    return NULL;
  }
}