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
|
/*
* Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
*
* SPDX-License-Identifier: LGPL-3.0-or-later
*
* This file is part of obe.
*
* obe is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* obe 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 Lesser General Public License for more
* details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with obe. If not, see <https://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <obe/array.h>
#include <obe/lexer.h>
#include <obe/parser.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
static obe_ir_function_t obe_parser_parse_function(obe_parser_t* parser);
inline static bool expected_next_token(obe_lexer_t *lexer, obe_token_t *token,
obe_token_kind_t expected_kind);
static bool expected_token(obe_lexer_t *lexer, obe_token_t *token,
obe_token_kind_t expected_kind);
void
obe_parser_init(obe_parser_t* parser, obe_lexer_t* lexer, obe_arena_t* arena)
{
assert(parser && lexer && arena);
parser->lexer = lexer;
parser->arena = arena;
}
void
obe_parser_next_expected_token(obe_parser_t* parser,
obe_token_t* token,
obe_token_kind_t kind)
{
obe_lexer_next_token(parser->lexer, token);
if (token->kind != kind) {
fprintf(stderr,
"%s:%zu:%zu: syntax error: expected token <%s> but got <%s>.\n",
parser->lexer->filename,
token->loc.lineno + 1,
token->loc.offset - token->loc.lineoffset + 1,
obe_token_to_cstr(kind),
obe_token_to_cstr(token->kind));
exit(EXIT_FAILURE);
}
}
obe_ir_translation_unit_t*
obe_parser_parse(obe_parser_t* parser)
{
obe_ir_translation_unit_t* tu = obe_ir_translation_unit_new(parser->arena);
// FIXME: Add support to parse multiple functions
obe_array_append(tu->funcs, obe_parser_parse_function(parser));
return tu;
}
static obe_ir_function_t
obe_parser_parse_function(obe_parser_t* parser)
{
obe_token_t token;
expected_next_token(parser->lexer, &token, TOKEN_KW_FN);
expected_next_token(parser->lexer, &token, TOKEN_IDENT);
obe_ir_function_t func = { .name = token.value, .instrs = obe_array(parser->arena, obe_ir_inst_t) };
expected_next_token(parser->lexer, &token, TOKEN_LBRACE);
obe_lexer_next_token(parser->lexer, &token);
while (token.kind != TOKEN_EOF && token.kind != TOKEN_RBRACE) {
switch (token.kind) {
case TOKEN_KW_RETURN:
{
expected_next_token(parser->lexer, &token, TOKEN_IDENT);
char value[token.value.length + 1];
memcpy(value, token.value.chars, token.value.length);
value[token.value.length] = 0;
obe_ir_inst_t inst = {
.kind = OBE_IR_INST_RETURN,
.operand1 = (obe_ir_operand_t) {
.kind = OBE_IR_OPERAND_IDENT,
.value = obe_arena_strdup(parser->arena, value)
}
};
obe_array_append(func.instrs, inst);
expected_next_token(parser->lexer, &token, TOKEN_SEMICOLON);
} break;
case TOKEN_KW_BR:
{
expected_next_token(parser->lexer, &token, TOKEN_IDENT);
char op1[token.value.length + 1];
memcpy(op1, token.value.chars, token.value.length);
op1[token.value.length] = 0;
expected_next_token(parser->lexer, &token, TOKEN_LABEL);
char dest[token.value.length + 1];
memcpy(dest, token.value.chars, token.value.length);
dest[token.value.length] = 0;
expected_next_token(parser->lexer, &token, TOKEN_SEMICOLON);
obe_ir_inst_t inst = {
.kind = OBE_IR_INST_BR,
.dest = obe_arena_strdup(parser->arena, dest),
.operand1 = (obe_ir_operand_t) {
.kind = OBE_IR_OPERAND_IDENT,
.value = obe_arena_strdup(parser->arena, op1)
}
};
obe_array_append(func.instrs, inst);
} break;
case TOKEN_LABEL:
{
char label[token.value.length + 1];
memcpy(label, token.value.chars, token.value.length);
label[token.value.length] = 0;
expected_next_token(parser->lexer, &token, TOKEN_COLON);
obe_ir_inst_t inst = {
.kind = OBE_IR_INST_LABEL,
.dest = obe_arena_strdup(parser->arena, label)
};
obe_array_append(func.instrs, inst);
} break;
case TOKEN_IDENT:
{
obe_string_t val = token.value;
char dest[token.value.length + 1];
memcpy(dest, token.value.chars, token.value.length);
dest[token.value.length] = 0;
expected_next_token(parser->lexer, &token, TOKEN_COLON);
expected_next_token(parser->lexer, &token, TOKEN_INT);
expected_next_token(parser->lexer, &token, TOKEN_EQ);
expected_next_token(parser->lexer, &token, TOKEN_NUMBER);
char number[token.value.length + 1];
memcpy(number, token.value.chars, token.value.length);
number[token.value.length] = 0;
obe_ir_inst_t inst = {
.kind = OBE_IR_INST_CONST,
.type = OBE_IR_TYPE_INT,
.dest = obe_arena_strdup(parser->arena, dest),
.operand1 = (obe_ir_operand_t) {
.kind = OBE_IR_OPERAND_LITERAL,
.value = obe_arena_strdup(parser->arena, number)
}
};
obe_array_append(func.instrs, inst);
expected_next_token(parser->lexer, &token, TOKEN_SEMICOLON);
} break;
default:
{
printf("%s:%ld:%ld: token <" PRIs ">\n",
parser->lexer->filename,
token.loc.lineno + 1,
token.loc.offset - token.loc.lineoffset + 1,
PRIsARG(token.value));
}
}
obe_lexer_next_token(parser->lexer, &token);
}
expected_token(parser->lexer, &token, TOKEN_RBRACE);
return func;
}
static bool
expected_token(obe_lexer_t *lexer, obe_token_t *token, obe_token_kind_t expected_kind)
{
if (token->kind != expected_kind) {
fprintf(stderr,
"%s:%lu:%lu: syntax error: got '" PRIs "' token but expect '%s'\n",
lexer->filename,
token->loc.lineno + 1,
token->loc.offset - lexer->loc.lineoffset + 1,
PRIsARG(token->value),
obe_token_to_cstr(expected_kind));
exit(EXIT_FAILURE);
}
return true;
}
inline static bool
expected_next_token(obe_lexer_t *lexer,
obe_token_t *token,
obe_token_kind_t expected_kind)
{
obe_lexer_next_token(lexer, token);
return expected_token(lexer, token, expected_kind);
}
|