public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
3316e0772d0a2d3d265a73c5c29a8cc17242395d blob 5037 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
 
/*
 * 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 <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>

#include "lexer.h"
#include "parser.h"

static bool
skip_expected_token(parser_t *parser, token_kind_t expected_kind);

static bool
expected_token(parser_t *parser, token_t *token, token_kind_t kind);

static bool
parser_parse_type(parser_t *parser, type_t *type);

static ast_node_t *
parser_parse_block(parser_t *parser);

static void
skip_line_feeds(lexer_t *lexer);

void
parser_init(parser_t *parser, lexer_t *lexer, arena_t *arena, char *file_path)
{
    assert(parser && "parser is required");
    assert(lexer && "lexer is required");
    assert(file_path && "file_path is required");
    parser->lexer = lexer;
    parser->arena = arena;
    parser->file_path = file_path;
}

ast_node_t *
parser_parse_fn_definition(parser_t *parser)
{
    if (!skip_expected_token(parser, TOKEN_FN)) {
        return NULL;
    }

    skip_line_feeds(parser->lexer);

    token_t fn_name_token;

    if (!expected_token(parser, &fn_name_token, TOKEN_IDENTIFIER)) {
        return NULL;
    }

    skip_line_feeds(parser->lexer);

    if (!skip_expected_token(parser, TOKEN_OPAREN)) {
        return NULL;
    }

    skip_line_feeds(parser->lexer);

    if (!skip_expected_token(parser, TOKEN_CPAREN)) {
        return NULL;
    }

    skip_line_feeds(parser->lexer);

    if (!skip_expected_token(parser, TOKEN_COLON)) {
        return NULL;
    }

    skip_line_feeds(parser->lexer);

    type_t fn_return_type;
    if (!parser_parse_type(parser, &fn_return_type)) {
        return NULL;
    }

    skip_line_feeds(parser->lexer);

    ast_node_t *block = parser_parse_block(parser);
    if (block == NULL) {
        return NULL;
    }

    return ast_new_node_fn_def(parser->arena, fn_name_token.value, fn_return_type, block);
}

static bool
parser_parse_type(parser_t *parser, type_t *type)
{
    token_t token;

    if (!expected_token(parser, &token, TOKEN_IDENTIFIER)) {
        return false;
    }

    if (string_view_eq_to_cstr(token.value, "u32")) {
        *type = TYPE_U32;
        return true;
    }

    return false;
}

static ast_node_t *
parser_parse_block(parser_t *parser)
{
    token_t number_token;
    if (!skip_expected_token(parser, TOKEN_OCURLY)) {
        return false;
    }

    skip_line_feeds(parser->lexer);

    ast_node_t *node_block = ast_new_node_block(parser->arena);

    if (!skip_expected_token(parser, TOKEN_RETURN)) {
        return false;
    }

    ast_node_t *node_return_stmt = ast_new_node_return_stmt(parser->arena);
    assert(node_return_stmt);

    if (!expected_token(parser, &number_token, TOKEN_NUMBER)) {
        return false;
    }

    ast_node_t *literal_node = ast_new_node_literal_u32(parser->arena, string_view_to_u32(number_token.value));
    assert(literal_node);

    node_return_stmt->data.as_return_stmt.data = literal_node;

    list_append(node_block->data.as_block.nodes, node_return_stmt);

    if (!skip_expected_token(parser, TOKEN_LF)) {
        return false;
    }

    skip_line_feeds(parser->lexer);

    if (!skip_expected_token(parser, TOKEN_CCURLY)) {
        return false;
    }

    return node_block;
}

static bool
skip_expected_token(parser_t *parser, token_kind_t expected_kind)
{
    token_t token;
    return expected_token(parser, &token, expected_kind);
}

static bool
expected_token(parser_t *parser, token_t *token, token_kind_t expected_kind)
{
    lexer_next_token(parser->lexer, token);

    if (token->kind != expected_kind) {
        fprintf(stderr,
                "%s:%lu:%lu: error: got '" SV_FMT "' token but expect <%s>\n",
                parser->file_path,
                token->location.row + 1,
                (token->location.offset - token->location.bol) + 1,
                SV_ARG(token->value),
                token_kind_to_cstr(expected_kind));

        string_view_t line = lexer_get_token_line(parser->lexer, token);
        fprintf(stderr, "" SV_FMT "\n", SV_ARG(line));
        fprintf(stderr, "%*s\n", (int)(token->location.offset - token->location.bol + 1), "^");

        exit(EXIT_FAILURE);
    }
    return true;
}

static void
skip_line_feeds(lexer_t *lexer)
{
    token_t token;
    lexer_peek_next(lexer, &token);

    while (token.kind == TOKEN_LF) {
        lexer_next_token(lexer, &token);
        lexer_peek_next(lexer, &token);
    }
}
debug log:

solving 3316e07 ...
found 3316e07 in https://git.johnnyrichard.com/olang.git

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