all repos — hex @ 9cda0007e098d5a6930dac0650b69dff5a6ec9d7

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

src/symboltable.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
#ifndef HEX_H
#include "hex.h"
#endif

int hex_symboltable_set(hex_context_t *ctx, const char *symbol)
{
    hex_symbol_table_t *table = ctx->symbol_table;
    size_t len = strlen(symbol);

    if (len > HEX_MAX_SYMBOL_LENGTH)
    {
        return -1;
    }

    if (table->count >= HEX_MAX_USER_SYMBOLS)
    {
        return -1; // Table full
    }

    for (uint16_t i = 0; i < table->count; ++i)
    {
        if (strcmp(table->symbols[i], symbol) == 0)
        {
            return 0;
        }
    }

    table->symbols[table->count] = strdup(symbol);
    table->count++;
    return 0;
}

int hex_symboltable_get_index(hex_context_t *ctx, const char *symbol)
{
    hex_symbol_table_t *table = ctx->symbol_table;
    for (uint16_t i = 0; i < table->count; ++i)
    {
        if (strcmp(table->symbols[i], symbol) == 0)
        {
            return i;
        }
    }
    return -1;
}

char *hex_symboltable_get_value(hex_context_t *ctx, uint16_t index)
{
    if (index >= ctx->symbol_table->count)
    {
        return NULL;
    }
    return ctx->symbol_table->symbols[index];
}

int hex_decode_bytecode_symboltable(hex_context_t *ctx, uint8_t **bytecode, size_t *size, size_t total)
{
    hex_symbol_table_t *table = ctx->symbol_table;
    table->count = 0;

    for (size_t i = 0; i < total; i++)
    {
        size_t len = (size_t)(*bytecode)[0];
        (*bytecode)++;
        *size -= 1;

        char *symbol = malloc(len + 1);
        if (symbol == NULL)
        {
            hex_error(ctx, "[decode symbol table] Memory allocation failed");
            // Handle memory allocation failure
            return -1;
        }
        memcpy(symbol, *bytecode, len);
        symbol[len] = '\0';
        hex_symboltable_set(ctx, symbol);
        free(symbol);
        *bytecode += len;
        *size -= len;
    }
    return 0;
}

uint8_t *hex_encode_bytecode_symboltable(hex_context_t *ctx, size_t *out_size)
{
    hex_symbol_table_t *table = ctx->symbol_table;
    size_t total_size = 0;

    for (uint16_t i = 0; i < table->count; ++i)
    {
        total_size += 1 + strlen(table->symbols[i]);
    }

    uint8_t *bytecode = malloc(total_size);
    size_t offset = 0;

    for (uint16_t i = 0; i < table->count; ++i)
    {
        size_t len = strlen(table->symbols[i]);
        bytecode[offset++] = (uint8_t)len;
        memcpy(bytecode + offset, table->symbols[i], len);
        offset += len;
    }

    *out_size = total_size;
    return bytecode;
}

hex_symbol_table_t *hex_symboltable_copy(hex_context_t *ctx)
{
    hex_symbol_table_t *original = ctx->symbol_table;
    hex_symbol_table_t *copy = malloc(sizeof(hex_symbol_table_t));
    if (copy == NULL)
    {
        hex_error(ctx, "[symbol table copy] Memory allocation failed");
        return NULL;
    }

    copy->symbols = malloc(sizeof(char *) * HEX_MAX_USER_SYMBOLS);
    if (copy->symbols == NULL)
    {
        hex_error(ctx, "[symbol table copy] Memory allocation failed for symbols array");
        free(copy);
        return NULL;
    }

    copy->count = original->count;
    for (uint16_t i = 0; i < original->count; ++i)
    {
        copy->symbols[i] = strdup(original->symbols[i]);
        if (copy->symbols[i] == NULL)
        {
            hex_error(ctx, "[symbol table copy] Memory allocation failed for symbol");
            // Free already allocated symbols
            for (uint16_t j = 0; j < i; ++j)
            {
                free(copy->symbols[j]);
            }
            free(copy->symbols);
            free(copy);
            return NULL;
        }
    }

    return copy;
}