<commit_before>#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include <stack_machine/common.h>
#include <stack_machine/context.h>
#include <stack_machine/entry.h>
#include <collections/hashtable.h>

state_t __REF(context_t *ctx)
{
    pushnum(ctx->ds, ctx->w.val);
    return OK;
}

state_t __EXEC(context_t *ctx)
{
    entry_t *entry;
    state_t retval;

    entry = (void *)*ctx->w.ptr;

    do
    {
        retval = entry->code_ptr(ctx);
        entry = (entry_t *)(*(ctx->ip)).addr;
        ctx->w = entry->param;
        ctx->ip++;

    } while (!stack_empty(ctx->rs) && retval == OK);

    return retval;
}

// TODO: this can be deleted once add_primitive converted to use ctx
int set_flags(hashtable_t *htbl, char *name, int flags)
{
    assert(htbl != NULL);
    assert(name != NULL);

    entry_t *entry;
    if (find_entry(htbl, name, &entry) == 0)
    {
        entry->flags = flags;
        return 0;
    }
    else
    {
        return -1;
    }

}


// TODO : convert to ctx->exe_tok
int add_primitive(hashtable_t *htbl, char *name, state_t (*code_ptr)(context_t *ctx), char *stack_effect, char *docstring)
{
    assert(htbl != NULL);
    assert(name != NULL);

    entry_t *entry = calloc(0, sizeof(entry_t));
    if (entry == NULL)
        return -1;<commit_msg>Rework __EXEC - fix bug where instruction pointer advanced improperly<commit_after>#include <stdio.h>
#include <ctype.h>
#include <string.h>

#include <stack_machine/common.h>
#include <stack_machine/context.h>
#include <stack_machine/entry.h>
#include <collections/hashtable.h>

state_t __REF(context_t *ctx)
{
    pushnum(ctx->ds, ctx->w.val);
    return OK;
}

state_t __EXEC(context_t *ctx)
{
    entry_t *entry;
    const int init_rs_size = stack_size(ctx->rs);

    entry = (void *)*ctx->w.ptr;

    for (;;)
    {
        state_t retval = entry->code_ptr(ctx);
        //printf("Executing: 0x%x: %s\n", entry, entry->name);

        if (stack_size(ctx->rs) == init_rs_size || retval != OK)
        {
            return retval;
        }

        entry = (entry_t *)(*(ctx->ip)).addr;
        ctx->w = entry->param;
        ctx->ip++;
    }
}

// TODO: this can be deleted once add_primitive converted to use ctx
int set_flags(hashtable_t *htbl, char *name, int flags)
{
    assert(htbl != NULL);
    assert(name != NULL);

    entry_t *entry;
    if (find_entry(htbl, name, &entry) == 0)
    {
        entry->flags = flags;
        return 0;
    }
    else
    {
        return -1;
    }

}


// TODO : convert to ctx->exe_tok
int add_primitive(hashtable_t *htbl, char *name, state_t (*code_ptr)(context_t *ctx), char *stack_effect, char *docstring)
{
    assert(htbl != NULL);
    assert(name != NULL);

    entry_t *entry = calloc(0, sizeof(entry_t));
    if (entry == NULL)
        return -1;
{'fraction_non_alphanumeric': 0.15384615384615385, 'fraction_numerical': 0.003935599284436494, 'mean_word_length': 2.7681940700808627, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4715649', 'n_tokens_mistral': 1039, 'n_tokens_neox': 1015, 'n_words': 238}