aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
blob: be3461c48443a530daae30b8912aa775f5e4fbce (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
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "../include/parser.h"
#include "../include/bytecode.h"
#include "../include/std/hash_map.h"
#include <stdio.h>

typedef struct {
    TslTokenizer tokenizer;
    TslBytecodeWriter bytecode_writer;
    TslHashMap variables;
} TslParser;

static int tsl_parser_parse_rhs(TslParser *self);
static int tsl_parser_parse_expressions(TslParser *self, TslToken end_token);

static void tsl_parser_init(TslParser *self, const char *code, size_t code_size) {
    tsl_tokenizer_init(&self->tokenizer, code, code_size);
    tsl_bytecode_writer_init(&self->bytecode_writer);
    tsl_hash_map_init(&self->variables);
}

static void tsl_parser_deinit(TslParser *self) {
    tsl_bytecode_writer_deinit(&self->bytecode_writer);
    tsl_hash_map_deinit(&self->variables);
}

static uint64_t hash_string_view(const void *data, size_t size) {
    uint64_t result = 0xdec05eba;
    const uint8_t *p = data;
    while(size) {
        result = ((result << 5) + result) + *p;
        ++p;
        --size;
    }
    return result;
}

static int tsl_parser_parse_map(TslParser *self) {
    #define parse_map_element_separator                                     \
        if(!tsl_tokenizer_accept(&self->tokenizer, TSL_TOKEN_COLON))        \
            return -1;                                                      \
        if(tsl_parser_parse_rhs(self) != 0)                                 \
            return -1;                                                      \
        token = tsl_tokenizer_next(&self->tokenizer);                       \
        if(token == TSL_TOKEN_COMMA) {                                      \
            continue;                                                       \
        } else if(token == TSL_TOKEN_RBRACE) {                              \
            return 0;                                                       \
        } else {                                                            \
            fprintf(stderr, "Error: Expected ',' or '}', got TODO\n");      \
            return -1;                                                      \
        }

    for(;;) {
        TslToken token = tsl_tokenizer_next(&self->tokenizer);
        if(token == TSL_TOKEN_NUM) {
            printf("rhs num: %ld\n", self->tokenizer.number_value);
            parse_map_element_separator
        } else if(token == TSL_TOKEN_BOOL) {
            printf("rhs bool: %s\n", self->tokenizer.bool_value ? "true" : "false");
            parse_map_element_separator
        } else if(token == TSL_TOKEN_NULL) {
            printf("rhs null\n");
            parse_map_element_separator
        } else if(token == TSL_TOKEN_STRING) {
            printf("rhs string: |%.*s|\n", self->tokenizer.string.size, self->tokenizer.string.data);
            parse_map_element_separator
        } else if(token == TSL_TOKEN_RBRACE) {
            /* '}' after trailing ',' or an empty map */
            return 0;
        } else {
            fprintf(stderr, "Error: Expected '}', got TODO\n");
            return -1;
        }

        token = tsl_tokenizer_next(&self->tokenizer);
    }
}

static int tsl_parser_parse_list(TslParser *self) {
    #define parse_list_element_separator                                    \
        token = tsl_tokenizer_next(&self->tokenizer);                       \
        if(token == TSL_TOKEN_COMMA) {                                      \
            continue;                                                       \
        } else if(token == TSL_TOKEN_RBRACKET) {                            \
            return 0;                                                       \
        } else {                                                            \
            fprintf(stderr, "Error: Expected ',' or ']', got TODO\n");      \
            return -1;                                                      \
        }

    for(;;) {
        /* TODO: Use tsl_parser_parse_rhs instead */
        TslToken token = tsl_tokenizer_next(&self->tokenizer);
        if(token == TSL_TOKEN_NUM) {
            printf("rhs num: %ld\n", self->tokenizer.number_value);
            parse_list_element_separator
        } else if(token == TSL_TOKEN_BOOL) {
            printf("rhs bool: %s\n", self->tokenizer.bool_value ? "true" : "false");
            parse_list_element_separator
        } else if(token == TSL_TOKEN_NULL) {
            printf("rhs null\n");
            parse_list_element_separator
        } else if(token == TSL_TOKEN_STRING) {
            printf("rhs string: |%.*s|\n", self->tokenizer.string.size, self->tokenizer.string.data);
            parse_list_element_separator
        } else if(token == TSL_TOKEN_RBRACKET) {
            /* ']' after trailing ',' or an empty list */
            return 0;
        } else {
            fprintf(stderr, "Error: Expected '}', got TODO\n");
            return -1;
        }
    }
}

/* FN_BODY = '{' EXPRS '}' */
static int tsl_parser_parse_fn_body(TslParser *self) {
    if(!tsl_tokenizer_accept(&self->tokenizer, TSL_TOKEN_LBRACE))
        return -1;
    return tsl_parser_parse_expressions(self, TSL_TOKEN_RBRACE);
}

/* FN = '(' (IDENTIFIER ',')* ')' FN_BODY */
static int tsl_parser_parse_fn(TslParser *self) {
    if(!tsl_tokenizer_accept(&self->tokenizer, TSL_TOKEN_LPAREN))
        return -1;

    for(;;) {
        TslToken token = tsl_tokenizer_next(&self->tokenizer);
        if(token == TSL_TOKEN_RPAREN) {
            return tsl_parser_parse_fn_body(self);
        } else if(token == TSL_TOKEN_IDENTIFIER) {
            TslStringView param_name = self->tokenizer.identifier;
            token = tsl_tokenizer_next(&self->tokenizer);
            if(token == TSL_TOKEN_COMMA) {
                continue;
            } else if(token == TSL_TOKEN_RPAREN) {
                return tsl_parser_parse_fn_body(self);
            } else {
                fprintf(stderr, "Error: Expected ',' or ')', got TODO\n");
                return -1;
            }
        } else {
            fprintf(stderr, "Error: Expected parameter name or ')', got TODO\n");
            return -1;
        }
    }
}

/* VAR_INDEX = '[' RHS ']' */
static int tsl_parser_parse_var_indexing(TslParser *self) {
    if(!tsl_tokenizer_accept(&self->tokenizer, TSL_TOKEN_LBRACKET))
        return -1;

    if(tsl_parser_parse_rhs(self) != 0)
        return -1;

    if(tsl_tokenizer_accept(&self->tokenizer, TSL_TOKEN_RBRACKET))
        return 0;
    else
        return -1;
}

/* FUNC_CALL = '(' (RHS ',')* ')' */
static int tsl_parser_parse_func_call(TslParser *self) {
    if(!tsl_tokenizer_accept(&self->tokenizer, TSL_TOKEN_LPAREN))
        return -1;

    for(;;) {
        TslToken token = tsl_tokenizer_peek(&self->tokenizer);
        if(token == TSL_TOKEN_RPAREN) {
            tsl_tokenizer_consume_peek(&self->tokenizer); /* consume previous TSL_TOKEN_RPAREN */
            return 0;
        } else {
            if(tsl_parser_parse_rhs(self) != 0)
                return -1;
            token = tsl_tokenizer_next(&self->tokenizer);
            if(token == TSL_TOKEN_COMMA) {
                continue;
            } else if(token == TSL_TOKEN_RPAREN) {
                return 0;
            } else {
                fprintf(stderr, "Error: Expected ',' or ')', got TODO\n");
                return -1;
            }
        }
    }
}

/* TODO: Do not allow empty command */
/* TODO: Allow command inside another command */
/* COMMAND = TODO */
static int tsl_parser_parse_command(TslParser *self) {
    if(!tsl_tokenizer_accept(&self->tokenizer, TSL_TOKEN_LPAREN))
        return -1;

    for(;;) {
        TslStringView command_arg;
        TslCommandToken command_token = tsl_tokenizer_next_command_arg(&self->tokenizer, &command_arg);
        if(command_token == TSL_COMMAND_TOKEN_ARG) {
            printf("command arg: |%.*s|\n", (int)command_arg.size, command_arg.data);
        } else if(command_token == TSL_COMMAND_TOKEN_END) {
            return 0;
        } else {
            fprintf(stderr, "Error: Expected command argument or ')', got TODO\n");
            return -1;
        }
    }
}

/* RHS_SUB = (VAR_INDEX|FUNC_CALL RHS_SUB?)|(TOKEN_ARITHMETIC RHS) */
static int tsl_parser_parse_rhs_sub(TslParser *self) {
    TslToken token = tsl_tokenizer_peek(&self->tokenizer);
    if(token == TSL_TOKEN_LBRACKET) {
        if(tsl_parser_parse_var_indexing(self) != 0)
            return -1;
        return tsl_parser_parse_rhs_sub(self);
    } else if(token == TSL_TOKEN_LPAREN) {
        if(tsl_parser_parse_func_call(self) != 0)
            return -1;
        return tsl_parser_parse_rhs_sub(self);
    } else if(token == TSL_TOKEN_ARITHMETIC) {
        tsl_tokenizer_consume_peek(&self->tokenizer); /* consume previous TSL_TOKEN_ARITHMETIC */
        printf("rhs sub arithmetic symbol: %c\n", self->tokenizer.arithmetic_symbol);
        /* TODO: Handle arithmetic expression precedence */
        return tsl_parser_parse_rhs(self);
    }
    /*
        No sub expression found, possibly a new expression after this (a new expression on a new line), let that
        part of the code handle error if there is any instead.
    */
    return 0;
}

/* RHS_SUB_ARITHMETIC = TOKEN_ARITHMETIC RHS */
static int tsl_parser_parse_rhs_sub_arithmetic(TslParser *self) {
    TslToken token = tsl_tokenizer_peek(&self->tokenizer);
    if(token == TSL_TOKEN_ARITHMETIC) {
        tsl_tokenizer_consume_peek(&self->tokenizer); /* consume previous TSL_TOKEN_ARITHMETIC */
        printf("rhs sub arithmetic symbol: %c\n", self->tokenizer.arithmetic_symbol);
        /* TODO: Handle arithmetic expression precedence */
        return tsl_parser_parse_rhs(self);
    }
    /*
        No sub expression found, possibly a new expression after this (a new expression on a new line), let that
        part of the code handle error if there is any instead.
    */
    return 0;
}

/* RHS = (IDENTIFIER|NUM|BOOL|NULL|STRING|MAP|LIST|('fn' FN)|('$' COMMAND)) RHS_SUB? */
int tsl_parser_parse_rhs(TslParser *self) {
    TslToken token = tsl_tokenizer_next(&self->tokenizer);
    if(token == TSL_TOKEN_IDENTIFIER) {
        TslStringView var_name = self->tokenizer.identifier;
        printf("rhs var: %.*s\n", (int)var_name.size, var_name.data);
        return tsl_parser_parse_rhs_sub(self);
    } else if(token == TSL_TOKEN_NUM) {
        TslRegister dst_reg = tsl_bytecode_writer_get_unique_register(&self->bytecode_writer);
        if(dst_reg == -1) {
            fprintf(stderr, "Error: Too many registers used\n");
            return -1;
        }

        if(!tsl_bytecode_writer_load_number(&self->bytecode_writer, dst_reg, self->tokenizer.number_value))
            return -1;

        printf("rhs num: %f\n", self->tokenizer.number_value);
        return tsl_parser_parse_rhs_sub_arithmetic(self);
    } else if(token == TSL_TOKEN_BOOL) {
        printf("rhs bool: %s\n", self->tokenizer.bool_value ? "true" : "false");
    } else if(token == TSL_TOKEN_NULL) {
        printf("rhs null\n");
    } else if(token == TSL_TOKEN_STRING) {
        printf("rhs string: |%.*s|\n", self->tokenizer.string.size, self->tokenizer.string.data);
    } else if(token == TSL_TOKEN_LBRACE) {
        return tsl_parser_parse_map(self);
    } else if(token == TSL_TOKEN_LBRACKET) {
        return tsl_parser_parse_list(self);
    } else if(token == TSL_TOKEN_FN) {
        return tsl_parser_parse_fn(self);
    } else if(token == TSL_TOKEN_DOLLAR_SIGN) {
        return tsl_parser_parse_command(self);
    } else {
        fprintf(stderr, "Error: Expected variable, number, bool, null, map, list, function or command, got TODO (%d) (line: %d)\n", token, tsl_tokenizer_get_line_by_index(&self->tokenizer, self->tokenizer.prev_code_index));
        return -1;
    }
    return 0;
}

/*
    EXPR = IDENTIFIER ('=' RHS)|RHS_SUB
    EXPRS = EXPR*
*/
int tsl_parser_parse_expressions(TslParser *self, TslToken end_token) {
    for(;;) {
        TslToken token = tsl_tokenizer_next(&self->tokenizer);
        if(token == TSL_TOKEN_IDENTIFIER) {
            TslStringView identifier = self->tokenizer.identifier;
            printf("identifier: %.*s\n", identifier.size, identifier.data);
            if(tsl_tokenizer_peek(&self->tokenizer) == TSL_TOKEN_EQUAL) {
                void *existing_variable = tsl_hash_map_get(&self->variables, &identifier, hash_string_view);
                if(!existing_variable) {
                    fprintf(stderr, "Variable declaration: %.*s\n", identifier.size, identifier.data);
                    if(!tsl_hash_map_insert(&self->variables, &identifier, "a", 1, hash_string_view))
                        return -1;
                }
                tsl_tokenizer_consume_peek(&self->tokenizer); /* consume previous TSL_TOKEN_EQUAL */
                if(tsl_parser_parse_rhs(self) != 0)
                    return -1;
            } else {
                if(tsl_parser_parse_rhs_sub(self) != 0)
                    return -1;
            }
        } else if(token == end_token) {
            break;
        } else {
            fprintf(stderr, "Error: Expected identifier, got TODO\n");
            return -1;
        }
    }
    return 0;
}

int tsl_parse(const char *code, size_t code_size) {
    int result;
    TslParser parser;
    tsl_parser_init(&parser, code, code_size);
    result = tsl_parser_parse_expressions(&parser, TSL_TOKEN_END_OF_FILE);
    tsl_parser_deinit(&parser);
    return result;
}