all repos — hex @ 2bd7dd79649a4996a70fcfa973548d3554b323cc

A tiny, minimalist, slightly-esoteric concatenative programming lannguage.

src/parser.c

 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
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
#ifndef HEX_H
#include "hex.h"
#endif

/////////////////////////////////////////
// Tokenizer and Parser Implementation //
/////////////////////////////////////////

// Process a token from the input
hex_token_t *hex_next_token(hex_context_t *ctx, const char **input, hex_file_position_t *position)
{
    const char *ptr = *input;

    // Skip whitespace
    while (isspace(*ptr))
    {
        if (*ptr == '\n')
        {
            position->line++;
            position->column = 1;
        }
        else
        {
            position->column++;
        }
        ptr++;
    }

    if (*ptr == '\0')
    {
        return NULL; // End of input
    }

    hex_token_t *token = (hex_token_t *)calloc(1, sizeof(hex_token_t));
    if (!token)
    {
        return NULL;
    }
    token->type = HEX_TOKEN_INVALID; // explicit for clarity
    token->position = (hex_file_position_t *)calloc(1, sizeof(hex_file_position_t));
    if (!token->position)
    {
        free(token);
        return NULL;
    }
    token->position->line = position->line;
    token->position->column = position->column;
    token->position->filename = position->filename ? strdup(position->filename) : NULL;

    if (*ptr == ';')
    {
        // Comment token
        const char *start = ptr;
        while (*ptr != '\0' && *ptr != '\n')
        {
            ptr++;
            position->column++;
        }
        int len = ptr - start;
        token->value = (char *)malloc(len + 1);
        strncpy(token->value, start, len);
        token->value[len] = '\0';
        token->type = HEX_TOKEN_COMMENT;
    }
    else if (strncmp(ptr, "#|", 2) == 0)
    {
        // Block comment token
        const char *start = ptr;
        ptr += 2; // Skip the "#|" prefix
        position->column += 2;
        while (*ptr != '\0' && strncmp(ptr, "|#", 2) != 0)
        {
            if (*ptr == '\n')
            {
                position->line++;
                position->column = 1;
            }
            else
            {
                position->column++;
            }
            ptr++;
        }
        if (*ptr == '\0')
        {
            token->type = HEX_TOKEN_INVALID;
            token->position->line = position->line;
            token->position->column = position->column;
            hex_error(ctx, "(%d,%d) Unterminated block comment", position->line, position->column);
            return token;
        }
        ptr += 2; // Skip the "|#" suffix
        position->column += 2;
        int len = ptr - start;
        token->value = (char *)malloc(len + 1);
        strncpy(token->value, start, len);
        token->value[len] = '\0';
        token->type = HEX_TOKEN_COMMENT;
    }
    else if (*ptr == '"')
    {
        // String token
        ptr++;
        const char *start = ptr;
        int len = 0;

        while (*ptr != '\0')
        {
            if (*ptr == '\\' && *(ptr + 1) == '"')
            {
                ptr += 2;
                len++;
                position->column += 2;
            }
            if (*ptr == '\\' && *(ptr + 1) == '\\')
            {
                ptr += 2;
                len++;
                position->column += 2;
            }
            else if (*ptr == '"')
            {
                break;
            }
            else if (*ptr == '\n')
            {
                token->type = HEX_TOKEN_INVALID;
                token->position->line = position->line;
                token->position->column = position->column;
                hex_error(ctx, "(%d,%d) Unescaped new line in string", position->line, position->column);
                return token;
            }
            else
            {
                ptr++;
                len++;
                position->column++;
            }
        }

        if (*ptr != '"')
        {
            token->type = HEX_TOKEN_INVALID;
            token->position->line = position->line;
            token->position->column = position->column;
            hex_error(ctx, "(%d,%d) Unterminated string", position->line, position->column);
            return token;
        }

        token->value = (char *)malloc(len + 1);
        char *dst = token->value;

        ptr = start;
        while (*ptr != '\0' && *ptr != '"')
        {
            if (*ptr == '\\' && *(ptr + 1) == '\\')
            {
                *dst++ = '\\';
                *dst++ = '\\';
                ptr += 2;
            }
            else if (*ptr == '\\' && *(ptr + 1) == '"')
            {
                *dst++ = '"';
                ptr += 2;
            }
            else
            {
                *dst++ = *ptr++;
            }
        }
        *dst = '\0';
        ptr++;
        position->column++;
        token->type = HEX_TOKEN_STRING;
    }
    else if (strncmp(ptr, "0x", 2) == 0 || strncmp(ptr, "0X", 2) == 0)
    {
        // Hexadecimal integer token
        const char *start = ptr;
        ptr += 2; // Skip the "0x" prefix
        position->column += 2;
        while (isxdigit(*ptr))
        {
            ptr++;
            position->column++;
        }
        int len = ptr - start;
        token->value = (char *)malloc(len + 1);
        strncpy(token->value, start, len);
        token->value[len] = '\0';
        token->type = HEX_TOKEN_INTEGER;
    }
    else if (*ptr == '(')
    {
        token->type = HEX_TOKEN_QUOTATION_START;
        token->value = (char *)malloc(4); // Allocate extra space for safety
        strcpy(token->value, "(");
        ptr++;
        position->column++;
    }
    else if (*ptr == ')')
    {
        token->type = HEX_TOKEN_QUOTATION_END;
        token->value = (char *)malloc(4); // Allocate extra space for safety
        strcpy(token->value, ")");
        ptr++;
        position->column++;
    }
    else
    {
        // Symbol token
        const char *start = ptr;
        while (*ptr != '\0' && !isspace(*ptr) && *ptr != ';' && *ptr != '(' && *ptr != ')' && *ptr != '"')
        {
            ptr++;
            position->column++;
        }

        int len = ptr - start;
        token->value = (char *)malloc(len + 1);
        strncpy(token->value, start, len);
        token->value[len] = '\0';
        if (hex_valid_native_symbol(ctx, token->value) || hex_valid_user_symbol(ctx, token->value))
        {
            token->type = HEX_TOKEN_SYMBOL;
        }
        else
        {
            token->type = HEX_TOKEN_INVALID;
            token->position->line = position->line;
            token->position->column = position->column;
        }
    }
    *input = ptr;
    return token;
}

int hex_valid_native_symbol(hex_context_t *ctx, const char *symbol)
{
    hex_doc_entry_t *doc = malloc(sizeof(hex_doc_entry_t));
    if (hex_get_doc(ctx->docs, symbol, doc))
    {
        free(doc);
        return 1;
    }
    free(doc);
    return 0;
}

int32_t hex_parse_integer(const char *hex_str)
{
    // Parse the hexadecimal string as an unsigned 32-bit integer
    uint32_t unsigned_value = (uint32_t)strtoul(hex_str, NULL, 16);

    // Cast the unsigned value to a signed 32-bit integer
    return (int32_t)unsigned_value;
}

// Helper function to clean up quotation parsing resources
static void hex_cleanup_quotation_parsing(hex_token_t *token, hex_item_t **quotation, size_t size, hex_context_t *ctx)
{
    if (token)
    {
        hex_free_token(token);
    }
    if (quotation)
    {
        hex_free_list(ctx, quotation, size);
    }
}

int hex_parse_quotation(hex_context_t *ctx, const char **input, hex_item_t *result, hex_file_position_t *position)
{
    hex_item_t **quotation = NULL;
    size_t capacity = 2;
    size_t size = 0;
    int balanced = 1;
    hex_token_t *token = NULL;

    quotation = (hex_item_t **)calloc(capacity, sizeof(hex_item_t *));
    if (!quotation)
    {
        hex_error(ctx, "[parse quotation] Memory allocation failed");
        return 1;
    }

    while ((token = hex_next_token(ctx, input, position)) != NULL)
    {
        if (token->type == HEX_TOKEN_QUOTATION_END)
        {
            balanced--;
            hex_free_token(token); // Free the end token
            token = NULL;          // Prevent double-free in cleanup
            break;
        }

        // Handle capacity expansion
        if (size >= capacity)
        {
            capacity *= 2;
            hex_item_t **new_quotation = (hex_item_t **)realloc(quotation, capacity * sizeof(hex_item_t *));
            if (!new_quotation)
            {
                hex_error(ctx, "(%d,%d), Memory allocation failed", position->line, position->column);
                hex_cleanup_quotation_parsing(token, quotation, size, ctx);
                return 1;
            }
            quotation = new_quotation;
        }

        hex_item_t *item = NULL;
        int parse_success = 1;

        if (token->type == HEX_TOKEN_INTEGER)
        {
            item = hex_integer_item(ctx, hex_parse_integer(token->value));
            hex_free_token(token); // Token no longer needed for integers
            token = NULL;          // Prevent double-free in cleanup
        }
        else if (token->type == HEX_TOKEN_STRING)
        {
            item = hex_string_item(ctx, token->value);
            hex_free_token(token); // Token no longer needed for strings
            token = NULL;          // Prevent double-free in cleanup
        }
        else if (token->type == HEX_TOKEN_SYMBOL)
        {
            if (hex_valid_native_symbol(ctx, token->value))
            {
                item = calloc(1, sizeof(hex_item_t));
                if (item)
                {
                    hex_item_t *value = calloc(1, sizeof(hex_item_t));
                    if (hex_get_symbol(ctx, token->value, value))
                    {
                        item->type = HEX_TYPE_NATIVE_SYMBOL;
                        item->data.fn_value = value->data.fn_value;
                        item->token = token;
                        token = NULL; // Token is now owned by item, prevent double-free
                        free(value);  // wrapper only
                    }
                    else
                    {
                        hex_error(ctx, "(%d,%d) Unable to reference native symbol: %s", position->line, position->column, token->value);
                        free(item);
                        item = NULL;
                        parse_success = 0;
                    }
                }
                else
                {
                    parse_success = 0;
                }
            }
            else
            {
                item = calloc(1, sizeof(hex_item_t));
                if (item)
                {
                    item->type = HEX_TYPE_USER_SYMBOL;
                    item->token = token;
                    token = NULL; // Token is now owned by item, prevent double-free
                }
                else
                {
                    parse_success = 0;
                }
            }

            if (item && item->token && item->token->position && position->filename)
            {
                if (item->token->position->filename)
                {
                    free((void *)item->token->position->filename);
                }
                item->token->position->filename = strdup(position->filename);
            }
        }
        else if (token->type == HEX_TOKEN_QUOTATION_START)
        {
            item = calloc(1, sizeof(hex_item_t));
            if (item)
            {
                item->type = HEX_TYPE_QUOTATION;
                if (hex_parse_quotation(ctx, input, item, position) != 0)
                {
                    free(item);
                    item = NULL;
                    parse_success = 0;
                }
            }
            else
            {
                parse_success = 0;
            }
            hex_free_token(token); // Token no longer needed after parsing
            token = NULL;          // Prevent double-free in cleanup
        }
        else if (token->type == HEX_TOKEN_COMMENT)
        {
            // Ignore comments
            hex_free_token(token);
            token = NULL; // Prevent double-free in cleanup
            continue;
        }
        else
        {
            hex_error(ctx, "(%d,%d) Unexpected token in quotation: %s", position->line, position->column, token->value);
            parse_success = 0;
        }

        // Check if parsing failed or item creation failed
        if (!parse_success || !item)
        {
            if (!item)
            {
                hex_error(ctx, "(%d,%d) Failed to create item", position->line, position->column);
            }
            hex_cleanup_quotation_parsing(token, quotation, size, ctx);
            return 1;
        }

        quotation[size] = item;
        size++;
    }

    if (balanced != 0)
    {
        hex_error(ctx, "(%d,%d) Unterminated quotation", position->line, position->column);
        hex_cleanup_quotation_parsing(token, quotation, size, ctx);
        return 1;
    }

    result->type = HEX_TYPE_QUOTATION;
    result->data.quotation_value = quotation;
    result->quotation_size = size;
    result->is_operator = 0;
    return 0;
}