all repos — hex @ c76da17c2ee93a3b4a20cdf33d840314003165ca

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

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

////////////////////////////////////////
// Stack Implementation               //
////////////////////////////////////////

// Free a token
void hex_free_token(hex_token_t *token)
{
    if (token)
    {
        free(token->value);
        free(token);
    }
}

// Push functions
int hex_push(hex_context_t *ctx, hex_item_t item)
{

    if (ctx->stack.top >= HEX_STACK_SIZE - 1)
    {
        hex_error(ctx, "Stack overflow");
        return 1;
    }
    hex_debug_item(ctx, "PUSH", item);
    int result = 0;
    if (item.type == HEX_TYPE_USER_SYMBOL)
    {
        hex_item_t value;
        if (hex_get_symbol(ctx, item.token->value, &value))
        {
            result = PUSH(ctx, value);
        }
        else
        {
            hex_error(ctx, "Undefined user symbol: %s", item.token->value);
            FREE(ctx, value);
            result = 1;
        }
    }
    else if (item.type == HEX_TYPE_NATIVE_SYMBOL)
    {
        hex_debug_item(ctx, "CALL", item);
        result = item.data.fn_value(ctx);
    }
    else
    {
        ctx->stack.entries[++ctx->stack.top] = item;
    }
    if (result == 0)
    {
        hex_debug_item(ctx, "DONE", item);
    }
    else
    {
        hex_debug_item(ctx, "FAIL", item);
    }
    return result;
}

char *hex_process_string(hex_context_t *ctx, const char *value)
{
    int len = strlen(value);
    char *processed_str = (char *)malloc(len + 1);
    if (!processed_str)
    {
        hex_error(ctx, "Memory allocation failed");
        return NULL;
    }

    char *dst = processed_str;
    const char *src = value;
    while (*src)
    {
        if (*src == '\\' && *(src + 1))
        {
            src++;
            switch (*src)
            {
            case 'n':
                *dst++ = '\n';
                break;
            case 't':
                *dst++ = '\t';
                break;
            case 'r':
                *dst++ = '\r';
                break;
            case 'b':
                *dst++ = '\b';
                break;
            case 'f':
                *dst++ = '\f';
                break;
            case 'v':
                *dst++ = '\v';
                break;
            // case '\\':
            //     *dst++ = '\\';
            //     break;
            case '\"':
                *dst++ = '\"';
                break;
            default:
                *dst++ = '\\';
                *dst++ = *src;
                break;
            }
        }
        else
        {
            *dst++ = *src;
        }
        src++;
    }
    *dst = '\0';
    return processed_str;
}

hex_item_t hex_string_item(hex_context_t *ctx, const char *value)
{
    hex_item_t item = {.type = HEX_TYPE_STRING, .data.str_value = hex_process_string(ctx, value)};
    return item;
}

hex_item_t hex_integer_item(hex_context_t *ctx, int value)
{
    (void)(ctx);
    hex_item_t item = {.type = HEX_TYPE_INTEGER, .data.int_value = value};
    return item;
}

hex_item_t hex_quotation_item(hex_context_t *ctx, hex_item_t **quotation, int size)
{
    (void)(ctx);
    hex_item_t item = {.type = HEX_TYPE_QUOTATION, .data.quotation_value = quotation, .quotation_size = size};
    return item;
}

int hex_push_string(hex_context_t *ctx, const char *value)
{
    return PUSH(ctx, hex_string_item(ctx, value));
}

int hex_push_integer(hex_context_t *ctx, int value)
{
    return PUSH(ctx, hex_integer_item(ctx, value));
}

int hex_push_quotation(hex_context_t *ctx, hex_item_t **quotation, int size)
{
    return PUSH(ctx, hex_quotation_item(ctx, quotation, size));
}

int hex_push_symbol(hex_context_t *ctx, hex_token_t *token)
{
    add_to_stack_trace(ctx, token);
    hex_item_t value;
    if (hex_get_symbol(ctx, token->value, &value))
    {
        value.token = token;
        return PUSH(ctx, value);
    }
    else
    {
        hex_error(ctx, "Undefined symbol: %s", token->value);
        return 1;
    }
}

// Pop function
hex_item_t hex_pop(hex_context_t *ctx)
{
    if (ctx->stack.top < 0)
    {
        hex_error(ctx, "Insufficient items on the stack");
        return (hex_item_t){.type = HEX_TYPE_INVALID};
    }
    hex_debug_item(ctx, " POP", ctx->stack.entries[ctx->stack.top]);
    return ctx->stack.entries[ctx->stack.top--];
}

// Free a stack item
void hex_free_item(hex_context_t *ctx, hex_item_t item)
{
    hex_debug_item(ctx, "FREE", item);
    if (item.type == HEX_TYPE_STRING && item.data.str_value != NULL)
    {
        hex_debug(ctx, "FREE: ** string start");
        item.data.str_value = NULL;
        free(item.data.str_value);
        hex_debug(ctx, "FREE: ** string end");
    }

    else if (item.type == HEX_TYPE_QUOTATION && item.data.quotation_value != NULL)
    {
        hex_debug(ctx, "FREE: ** quotation start");
        hex_free_list(ctx, item.data.quotation_value, item.quotation_size);
        item.data.quotation_value = NULL;
        hex_debug(ctx, "FREE: ** quotation end");
    }
    else if (item.type == HEX_TYPE_NATIVE_SYMBOL && item.token->value != NULL)
    {
        hex_debug(ctx, "FREE: ** native symbol start");
        item.token = NULL;
        hex_free_token(item.token);
        hex_debug(ctx, "FREE: ** native symbol end");
    }
    else if (item.type == HEX_TYPE_USER_SYMBOL && item.token->value != NULL)
    {
        hex_debug(ctx, "FREE: ** user symbol start");
        item.token = NULL;
        hex_free_token(item.token);
        hex_debug(ctx, "FREE: ** user symbol end");
    }
    else
    {
        hex_debug(ctx, "FREE: ** nothing to free");
    }
}

void hex_free_list(hex_context_t *ctx, hex_item_t **quotation, int size)
{
    for (int i = 0; i < size; i++)
    {
        FREE(ctx, *quotation[i]);
    }
}