public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
45a3d63f0f109b14e82071c53d68bc07451b24d7 blob 8273 bytes (raw)

  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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
 
/*
 * Copyright (C) 2024 olang maintainers
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#include "lexer.h"

#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>

void
lexer_init(lexer_t *lexer, string_view_t source)
{
    assert(lexer);
    lexer->source = source;
    lexer->offset = 0;
    lexer->row = 0;
    lexer->bol = 0;
}

static char
lexer_current_char(lexer_t *lexer);

static void
lexer_skip_char(lexer_t *lexer);

static bool
lexer_is_eof(lexer_t *lexer);

static bool
lexer_is_not_eof(lexer_t *lexer);

static bool
_isspace(char c);

static void
lexer_init_char_value_token(lexer_t *lexer, token_t *token, token_kind_t kind);

static void
lexer_init_str_value_token(lexer_t *lexer, token_t *token, token_kind_t kind, size_t start_offset);

static void
lexer_init_eof_token(lexer_t *lexer, token_t *token);

static token_kind_t
lexer_str_to_token_kind(string_view_t text);

void
lexer_next_token(lexer_t *lexer, token_t *token)
{
    if (lexer_is_eof(lexer)) {
        lexer_init_eof_token(lexer, token);
        return;
    }

    char current_char = lexer_current_char(lexer);

    if (_isspace(current_char)) {
        while (_isspace(current_char) && lexer_is_not_eof(lexer)) {
            lexer_skip_char(lexer);
            current_char = lexer_current_char(lexer);
        }
    }

    while (lexer_is_not_eof(lexer)) {
        if (isalpha(current_char)) {
            size_t start_offset = lexer->offset;
            while (isalnum(current_char) && lexer_is_not_eof(lexer)) {
                lexer_skip_char(lexer);
                current_char = lexer_current_char(lexer);
            }

            string_view_t text = { .chars = lexer->source.chars + start_offset, .size = lexer->offset - start_offset };

            lexer_init_str_value_token(lexer, token, lexer_str_to_token_kind(text), start_offset);
            return;
        }

        if (isdigit(current_char)) {
            size_t start_offset = lexer->offset;
            while (isdigit(current_char) && lexer_is_not_eof(lexer)) {
                lexer_skip_char(lexer);
                current_char = lexer_current_char(lexer);
            }

            lexer_init_str_value_token(lexer, token, TOKEN_NUMBER, start_offset);
            return;
        }

        switch (current_char) {
            case '(': {
                lexer_init_char_value_token(lexer, token, TOKEN_OPAREN);
                lexer_skip_char(lexer);
                return;
            }
            case ')': {
                lexer_init_char_value_token(lexer, token, TOKEN_CPAREN);
                lexer_skip_char(lexer);
                return;
            }
            case ':': {
                lexer_init_char_value_token(lexer, token, TOKEN_COLON);
                lexer_skip_char(lexer);
                return;
            }
            case '{': {
                lexer_init_char_value_token(lexer, token, TOKEN_OCURLY);
                lexer_skip_char(lexer);
                return;
            }
            case '}': {
                lexer_init_char_value_token(lexer, token, TOKEN_CCURLY);
                lexer_skip_char(lexer);
                return;
            }
            case '+': {
                lexer_init_char_value_token(lexer, token, TOKEN_PLUS);
                lexer_skip_char(lexer);
                return;
            }
            case '-': {
                lexer_init_char_value_token(lexer, token, TOKEN_DASH);
                lexer_skip_char(lexer);
                return;
            }
            case '*': {
                lexer_init_char_value_token(lexer, token, TOKEN_STAR);
                lexer_skip_char(lexer);
                return;
            }
            case '/': {
                lexer_init_char_value_token(lexer, token, TOKEN_SLASH);
                lexer_skip_char(lexer);
                return;
            }
            case '\n': {
                lexer_init_char_value_token(lexer, token, TOKEN_LF);
                lexer_skip_char(lexer);
                return;
            }
            default: {
                lexer_init_char_value_token(lexer, token, TOKEN_UNKNOWN);
                lexer_skip_char(lexer);
                return;
            }
        }
    }

    if (lexer_is_eof(lexer)) {
        lexer_init_eof_token(lexer, token);
        return;
    }
}

static char *token_kind_str_table[] = {
    [TOKEN_UNKNOWN] = "unknown", [TOKEN_IDENTIFIER] = "identifier",
    [TOKEN_NUMBER] = "number",   [TOKEN_FN] = "fn",
    [TOKEN_RETURN] = "return",   [TOKEN_LF] = "line_feed",
    [TOKEN_OPAREN] = "(",        [TOKEN_CPAREN] = ")",
    [TOKEN_COLON] = ":",         [TOKEN_OCURLY] = "{",
    [TOKEN_CCURLY] = "}",        [TOKEN_PLUS] = "+",
    [TOKEN_DASH] = "-",          [TOKEN_STAR] = "*",
    [TOKEN_SLASH] = "/",         [TOKEN_EOF] = "EOF",
};

char *
token_kind_to_cstr(token_kind_t kind)
{
    assert(kind < sizeof(token_kind_str_table));
    return token_kind_str_table[kind];
}

static char
lexer_current_char(lexer_t *lexer)
{
    return lexer->source.chars[lexer->offset];
}

static void
lexer_skip_char(lexer_t *lexer)
{
    assert(lexer->offset < lexer->source.size);
    if (lexer_current_char(lexer) == '\n') {
        lexer->row++;
        lexer->bol = ++lexer->offset;
    } else {
        lexer->offset++;
    }
}

static bool
lexer_is_eof(lexer_t *lexer)
{
    return lexer->offset >= lexer->source.size;
}

static bool
lexer_is_not_eof(lexer_t *lexer)
{
    return !lexer_is_eof(lexer);
}

static bool
_isspace(char c)
{
    return c != '\n' && isspace(c);
}

static void
lexer_init_char_value_token(lexer_t *lexer, token_t *token, token_kind_t kind)
{
    string_view_t str = { .chars = lexer->source.chars + lexer->offset, .size = 1 };
    token_loc_t location = { .offset = lexer->offset, .row = lexer->row, .bol = lexer->bol };
    *token = (token_t){ .kind = kind, .value = str, .location = location };
}

static void
lexer_init_str_value_token(lexer_t *lexer, token_t *token, token_kind_t kind, size_t start_offset)
{
    string_view_t str = { .chars = lexer->source.chars + start_offset, .size = lexer->offset - start_offset };
    token_loc_t location = { .offset = start_offset, .row = lexer->row, .bol = lexer->bol };
    *token = (token_t){ .kind = kind, .value = str, .location = location };
}

static void
lexer_init_eof_token(lexer_t *lexer, token_t *token)
{
    string_view_t str = { 0 };
    token_loc_t location = { .offset = lexer->offset, .row = lexer->row, .bol = lexer->bol };
    *token = (token_t){ .kind = TOKEN_EOF, .value = str, .location = location };
}

static token_kind_t
lexer_str_to_token_kind(string_view_t text)
{
    if (string_view_eq_to_cstr(text, "return")) {
        return TOKEN_RETURN;
    }

    if (string_view_eq_to_cstr(text, "fn")) {
        return TOKEN_FN;
    }

    return TOKEN_IDENTIFIER;
}

void
lexer_peek_next(lexer_t *lexer, token_t *token)
{
    lexer_lookahead(lexer, token, 1);
}

void
lexer_lookahead(lexer_t *lexer, token_t *token, size_t n)
{
    size_t previous_offset = lexer->offset;
    size_t previous_row = lexer->row;
    size_t previous_bol = lexer->bol;

    for (size_t i = 0; i < n; ++i) {
        lexer_next_token(lexer, token);
    }

    lexer->offset = previous_offset;
    lexer->row = previous_row;
    lexer->bol = previous_bol;
}

string_view_t
lexer_get_token_line(lexer_t *lexer, token_t *token)
{
    size_t offset = token->location.bol;
    string_view_t line = { .chars = lexer->source.chars + offset, .size = 0 };

    while ((line.size + offset) < lexer->source.size && line.chars[line.size] != '\n' && line.chars[line.size] != 0) {
        ++line.size;
    }

    return line;
}
debug log:

solving 45a3d63 ...
found 45a3d63 in http://lists.johnnyrichard.com/olang/20240313212855.174554-2-johnny@johnnyrichard.com/
found dd6f11d in https://git.johnnyrichard.com/olang.git
preparing index
index prepared:
100644 dd6f11d70371c7fdee35f5d7d0fa530a764b7069	src/lexer.c

applying [1/1] http://lists.johnnyrichard.com/olang/20240313212855.174554-2-johnny@johnnyrichard.com/
diff --git a/src/lexer.c b/src/lexer.c
index dd6f11d..45a3d63 100644

Checking patch src/lexer.c...
Applied patch src/lexer.c cleanly.

index at:
100644 45a3d63f0f109b14e82071c53d68bc07451b24d7	src/lexer.c

Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox