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 |
#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;
}
|