all repos — hex @ e789e8e6d03cf2991713c71771d2f29ea061b8d2

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

src/hex.h

 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
#ifndef HEX_H
#define HEX_H

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <signal.h>
#include <stdarg.h>
#include <stdint.h>

#ifdef _WIN32
#include <windows.h>
#include <io.h>
int isatty(int fd);
#else
#include <unistd.h>
#include <sys/wait.h>
#endif

// Constants
#define HEX_VERSION "0.1.0"
#define HEX_STDIN_BUFFER_SIZE 256
#define HEX_REGISTRY_SIZE 1024
#define HEX_STACK_SIZE 128
#define HEX_STACK_TRACE_SIZE 16
#define HEX_NATIVE_SYMBOLS 64

// Type Definitions
typedef enum hex_item_type_t
{
    HEX_TYPE_INTEGER,
    HEX_TYPE_STRING,
    HEX_TYPE_QUOTATION,
    HEX_TYPE_NATIVE_SYMBOL,
    HEX_TYPE_USER_SYMBOL,
    HEX_TYPE_INVALID
} hex_item_type_t;

typedef enum hex_token_type_t
{
    HEX_TOKEN_INTEGER,
    HEX_TOKEN_STRING,
    HEX_TOKEN_SYMBOL,
    HEX_TOKEN_QUOTATION_START,
    HEX_TOKEN_QUOTATION_END,
    HEX_TOKEN_COMMENT,
    HEX_TOKEN_INVALID
} hex_token_type_t;

typedef struct hex_file_position_t
{
    const char *filename;
    int line;
    int column;
} hex_file_position_t;

typedef struct hex_token_t
{
    hex_token_type_t type;
    char *value;
    hex_file_position_t position;
} hex_token_t;

typedef struct hex_context_t hex_context_t;

typedef struct hex_item_t
{
    hex_item_type_t type;
    union
    {
        int32_t int_value;
        char *str_value;
        int (*fn_value)(hex_context_t *);
        struct hex_item_t **quotation_value;
    } data;
    hex_token_t *token; // Token containing stack information (valid for HEX_TYPE_NATIVE_SYMBOL and HEX_TYPE_USER_SYMBOL)
    int quotation_size; // Size of the quotation (valid for HEX_TYPE_QUOTATION)
} hex_item_t;

typedef struct hex_registry_entry
{
    char *key;
    hex_item_t value;
} hex_registry_entry_t;

typedef struct hex_stack_trace_t
{
    hex_token_t entries[HEX_STACK_TRACE_SIZE];
    int start; // Index of the oldest item
    int size;  // Current number of items in the buffer
} hex_stack_trace_t;

typedef struct hex_stack_t
{
    hex_item_t entries[HEX_STACK_SIZE];
    int top;
} hex_stack_t;

typedef struct hex_registry_t
{
    hex_registry_entry_t entries[HEX_REGISTRY_SIZE];
    int size;
} hex_registry_t;

typedef struct hex_doc_entry_t
{
    const char *name;
    const char *description;
    const char *input;
    const char *output;
} hex_doc_entry_t;

typedef struct hex_doc_dictionary_t
{
    hex_doc_entry_t entries[64];
    int size;
} hex_doc_dictionary_t;

typedef struct hex_settings_t
{
    int debugging_enabled;
    int errors_enabled;
    int stack_trace_enabled;
} hex_settings_t;

typedef struct hex_context_t
{
    hex_stack_t stack;
    hex_registry_t registry;
    hex_stack_trace_t stack_trace;
    hex_settings_t settings;
    hex_doc_dictionary_t docs;
    int hashbang;
    char error[256];
    int argc;
    char **argv;
} hex_context_t;

// Help System
void hex_doc(hex_doc_dictionary_t *docs, const char *name, const char *description, const char *input, const char *output);
int hex_get_doc(hex_doc_dictionary_t *docs, const char *key, hex_doc_entry_t *result);
void hex_create_docs(hex_doc_dictionary_t *docs);
void hex_print_help();
void hex_print_docs(hex_doc_dictionary_t *docs);

// Free data
void hex_free_item(hex_context_t *ctx, hex_item_t item);
void hex_free_token(hex_token_t *token);
void hex_free_list(hex_context_t *ctx, hex_item_t **quotation, int size);

// Symbol management
int hex_valid_user_symbol(hex_context_t *ctx, const char *symbol);
int hex_valid_native_symbol(hex_context_t *ctx, const char *symbol);
int hex_set_symbol(hex_context_t *ctx, const char *key, hex_item_t value, int native);
void hex_set_native_symbol(hex_context_t *ctx, const char *name, int (*func)());
int hex_get_symbol(hex_context_t *ctx, const char *key, hex_item_t *result);

// Errors and debugging
void hex_error(hex_context_t *ctx, const char *format, ...);
void hex_debug(hex_context_t *ctx, const char *format, ...);
void hex_debug_item(hex_context_t *ctx, const char *message, hex_item_t item);
void hex_print_item(FILE *stream, hex_item_t item);
void add_to_stack_trace(hex_context_t *ctx, hex_token_t *token);
void print_stack_trace(hex_context_t *ctx);

// Item constructors
hex_item_t hex_string_item(hex_context_t *ctx, const char *value);
hex_item_t hex_integer_item(hex_context_t *ctx, int value);
hex_item_t hex_quotation_item(hex_context_t *ctx, hex_item_t **quotation, int size);

// Stack management
int hex_push(hex_context_t *ctx, hex_item_t item);
int hex_push_integer(hex_context_t *ctx, int value);
int hex_push_string(hex_context_t *ctx, const char *value);
int hex_push_quotation(hex_context_t *ctx, hex_item_t **quotation, int size);
int hex_push_symbol(hex_context_t *ctx, hex_token_t *token);
hex_item_t hex_pop(hex_context_t *ctx);

// Parser and interpreter
char *hex_process_string(hex_context_t *ctx, const char *value);
hex_token_t *hex_next_token(hex_context_t *ctx, const char **input, hex_file_position_t *position);
int32_t hex_parse_integer(const char *hex_str);
int hex_parse_quotation(hex_context_t *ctx, const char **input, hex_item_t *result, hex_file_position_t *position);
int hex_interpret(hex_context_t *ctx, const char *code, const char *filename, int line, int column);

// Helpers
char *hex_itoa(int num, int base);
char *hex_itoa_dec(int num);
char *hex_itoa_hex(int num);
void hex_raw_print_item(FILE *stream, hex_item_t item);
int hex_is_symbol(hex_token_t *token, char *value);
char *hex_type(hex_item_type_t type);

// Native symbols
int hex_symbol_store(hex_context_t *ctx);
int hex_symbol_free(hex_context_t *ctx);
int hex_symbol_type(hex_context_t *ctx);
int hex_symbol_i(hex_context_t *ctx);
int hex_symbol_eval(hex_context_t *ctx);
int hex_symbol_puts(hex_context_t *ctx);
int hex_symbol_warn(hex_context_t *ctx);
int hex_symbol_print(hex_context_t *ctx);
int hex_symbol_gets(hex_context_t *ctx);
int hex_symbol_add(hex_context_t *ctx);
int hex_symbol_subtract(hex_context_t *ctx);
int hex_symbol_multiply(hex_context_t *ctx);
int hex_symbol_divide(hex_context_t *ctx);
int hex_symbol_modulo(hex_context_t *ctx);
int hex_symbol_bitand(hex_context_t *ctx);
int hex_symbol_bitor(hex_context_t *ctx);
int hex_symbol_bitxor(hex_context_t *ctx);
int hex_symbol_bitnot(hex_context_t *ctx);
int hex_symbol_shiftleft(hex_context_t *ctx);
int hex_symbol_shiftright(hex_context_t *ctx);
int hex_symbol_int(hex_context_t *ctx);
int hex_symbol_str(hex_context_t *ctx);
int hex_symbol_dec(hex_context_t *ctx);
int hex_symbol_hex(hex_context_t *ctx);
int hex_symbol_equal(hex_context_t *ctx);
int hex_symbol_notequal(hex_context_t *ctx);
int hex_symbol_greater(hex_context_t *ctx);
int hex_symbol_less(hex_context_t *ctx);
int hex_symbol_greaterequal(hex_context_t *ctx);
int hex_symbol_lessequal(hex_context_t *ctx);
int hex_symbol_and(hex_context_t *ctx);
int hex_symbol_or(hex_context_t *ctx);
int hex_symbol_not(hex_context_t *ctx);
int hex_symbol_xor(hex_context_t *ctx);
int hex_symbol_cat(hex_context_t *ctx);
int hex_symbol_slice(hex_context_t *ctx);
int hex_symbol_len(hex_context_t *ctx);
int hex_symbol_get(hex_context_t *ctx);
int hex_symbol_insert(hex_context_t *ctx);
int hex_symbol_index(hex_context_t *ctx);
int hex_symbol_join(hex_context_t *ctx);
int hex_symbol_split(hex_context_t *ctx);
int hex_symbol_replace(hex_context_t *ctx);
int hex_symbol_read(hex_context_t *ctx);
int hex_symbol_write(hex_context_t *ctx);
int hex_symbol_append(hex_context_t *ctx);
int hex_symbol_args(hex_context_t *ctx);
int hex_symbol_exit(hex_context_t *ctx);
int hex_symbol_exec(hex_context_t *ctx);
int hex_symbol_run(hex_context_t *ctx);
int hex_symbol_if(hex_context_t *ctx);
int hex_symbol_when(hex_context_t *ctx);
int hex_symbol_while(hex_context_t *ctx);
int hex_symbol_each(hex_context_t *ctx);
int hex_symbol_error(hex_context_t *ctx);
int hex_symbol_try(hex_context_t *ctx);
int hex_symbol_q(hex_context_t *ctx);
int hex_symbol_map(hex_context_t *ctx);
int hex_symbol_filter(hex_context_t *ctx);
int hex_symbol_swap(hex_context_t *ctx);
int hex_symbol_dup(hex_context_t *ctx);
int hex_symbol_stack(hex_context_t *ctx);
int hex_symbol_clear(hex_context_t *ctx);
int hex_symbol_pop(hex_context_t *ctx);

// REPL and initialization
void hex_register_symbols(hex_context_t *ctx);
hex_context_t hex_init();
void hex_repl(hex_context_t *ctx);
void hex_process_stdin(hex_context_t *ctx);
void hex_handle_sigint(int sig);
char *hex_read_file(hex_context_t *ctx, const char *filename);

#endif // HEX_H