Hash :
2976db93
Author :
Thomas de Grivel
Date :
2023-01-26T09:50:25
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 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
/* c3
* Copyright 2022,2023 kmx.io <contact@kmx.io>
*
* Permission is hereby granted to use this software excepted
* on Apple computers granted the above copyright notice and
* this permission paragraph are included in all copies and
* substantial portions of this software.
*
* THIS SOFTWARE IS PROVIDED "AS-IS" WITHOUT ANY GUARANTEE OF
* PURPOSE AND PERFORMANCE. IN NO EVENT WHATSOEVER SHALL THE
* AUTHOR BE CONSIDERED LIABLE FOR THE USE AND PERFORMANCE OF
* THIS SOFTWARE.
*/
#ifndef TYPES_H
#define TYPES_H
#include <limits.h>
#include <setjmp.h>
#include <stdio.h>
#include <sys/types.h>
#include "config.h"
#include "sha1.h"
#include "../libtommath/tommath.h"
/* Basic integer types. */
typedef char s8;
typedef int16_t s16;
typedef int32_t s32;
typedef long sw;
typedef int64_t s64;
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef unsigned long uw;
typedef uint64_t u64;
#define S32_MAX LONG_MAX
#define S64_MAX LLONG_MAX
#define U64_MAX ULLONG_MAX
/* IEEE 754 floating point numbers. */
typedef float f32;
typedef double f64;
/* Boolean : true or false. */
typedef s8 bool;
/* enums */
typedef enum {
false = 0,
true = 1
} e_bool;
typedef enum {
TAG_VOID = 0,
TAG_BOOL = 1,
TAG_CALL,
TAG_CALL_FN,
TAG_CALL_MACRO,
TAG_CHARACTER,
TAG_F32,
TAG_F64,
TAG_FN,
TAG_IDENT,
TAG_INTEGER,
TAG_S64,
TAG_S32,
TAG_S16,
TAG_S8,
TAG_U8,
TAG_U16,
TAG_U32,
TAG_U64,
TAG_LIST,
TAG_PTAG,
TAG_QUOTE,
TAG_STR,
TAG_SYM,
TAG_TUPLE,
TAG_VAR
} e_tag_type;
/* structs */
typedef struct arg s_arg;
typedef struct binding s_binding;
typedef struct buf s_buf;
typedef struct buf_save s_buf_save;
typedef struct call s_call;
typedef struct env s_env;
typedef struct error_handler s_error_handler;
typedef struct fact s_fact;
typedef struct facts s_facts;
typedef struct facts_cursor s_facts_cursor;
typedef struct facts_spec_cursor s_facts_spec_cursor;
typedef struct facts_with_cursor s_facts_with_cursor;
typedef struct facts_with_cursor_level s_facts_with_cursor_level;
typedef struct fn s_fn;
typedef struct frame s_frame;
typedef struct ident s_ident;
typedef struct integer s_integer;
typedef struct list s_list;
typedef struct list s_list_map;
typedef struct log s_log;
typedef struct module s_module;
typedef struct str s_str;
typedef struct struct_ s_struct;
typedef struct sym s_sym;
typedef struct sym_list s_sym_list;
typedef struct tag s_tag;
typedef struct tuple s_tuple;
typedef struct unwind_protect s_unwind_protect;
/* unions */
typedef union ptr u_ptr;
typedef union ptr_w u_ptr_w;
typedef union tag_data u_tag_data;
typedef union tag_type u_tag_type;
/* typedefs */
typedef s32 character;
typedef s_tag **p_facts_spec;
typedef SHA1_CTX t_hash;
typedef s_tag *p_quote;
typedef const s_tag *p_tag;
typedef const s_tag *p_var;
typedef u64 t_skiplist_height;
#define CHARACTER_MAX S32_MAX
#define SKIPLIST_HEIGHT_MAX U64_MAX
/* 1 */
struct buf_save {
s_buf_save *next;
uw rpos;
uw wpos;
};
struct fact {
const s_tag *subject;
const s_tag *predicate;
const s_tag *object;
uw id; /* XXX random without collision */
};
struct frame {
s_binding *bindings;
s_frame *next;
};
struct fn {
uw arity;
s_arg *args;
s_binding *bindings;
s_list *algo;
};
struct ident {
const s_sym *module;
const s_sym *sym;
};
struct module {
const s_sym *name;
s_facts *facts;
};
union ptr {
const void *p;
const s8 *ps8;
const u8 *pu8;
};
union ptr_w {
void *p;
s8 *ps8;
u8 *pu8;
};
struct struct_ {
void *data;
uw count;
s_module *module;
s_ident *type;
};
struct sym_list {
s_sym *sym;
s_sym_list *next;
};
union tag_type {
e_tag_type type;
};
struct tuple {
uw count;
s_tag *tag;
};
struct unwind_protect {
jmp_buf buf;
jmp_buf *jmp;
s_unwind_protect *next;
};
/* 2 */
struct arg {
const s_sym *name;
s_ident type;
s_arg *next;
};
struct binding {
const s_sym *name;
const s_tag *value;
s_binding *next;
};
struct buf {
sw column;
sw (*flush) (s_buf *buf);
bool free;
sw line;
u_ptr_w ptr;
sw (*refill) (s_buf *buf);
uw rpos;
s_buf_save *save;
sw (*seek) (s_buf *buf, sw offset, u8 whence);
uw size;
void *user_ptr;
u64 wpos;
};
struct call {
/* key */
s_ident ident;
s_list *arguments;
s_list_map *keyword;
/* value */
s_fn *fn;
};
struct facts_spec_cursor {
p_facts_spec spec;
const s_tag *subject;
uw pos;
};
struct integer {
mp_int mp_int;
};
struct str {
u_ptr_w free; /**< Pointer to free or NULL. */
uw size; /**< Size in bytes. */
u_ptr ptr; /**< Pointer to memory. */
};
/* 3 */
struct env {
s_list *backtrace;
s_error_handler *error_handler;
s_frame *frame;
s_buf err;
s_buf in;
s_buf out;
s_unwind_protect *unwind_protect;
};
struct log {
s_buf buf;
u64 count;
t_hash hash;
};
struct sym {
s_str str;
};
union tag_data {
bool bool;
s_call call;
character character;
f32 f32;
f64 f64;
s_fn fn;
s_ident ident;
s_integer integer;
s_list *list;
p_tag ptag;
p_quote quote;
s_str str;
const s_sym *sym;
s8 s8;
s16 s16;
s32 s32;
s64 s64;
s_tuple tuple;
u8 u8;
u16 u16;
u32 u32;
u64 u64;
p_var var;
};
/* 4 */
struct tag {
u_tag_type type;
u_tag_data data;
};
/* 5 */
struct error_handler
{
s_list *backtrace;
jmp_buf jmp_buf;
s_error_handler *next;
s_tag tag;
};
struct list {
s_tag tag;
s_tag next;
};
#define TYPEDEF_SET_ITEM(name, type) \
typedef struct set_item__##name s_set_item__##name; \
\
struct set_item__##name { \
type data; \
uw hash; \
s_set_item__##name *next; \
uw usage; \
}
TYPEDEF_SET_ITEM(tag, s_tag);
TYPEDEF_SET_ITEM(fact, s_fact);
#define TYPEDEF_SET(name, type) \
typedef struct set__##name { \
uw collisions; \
uw count; \
s_set_item__##name **items; \
uw max; \
} s_set__##name
TYPEDEF_SET(tag, s_tag);
TYPEDEF_SET(fact, s_fact);
#define TYPEDEF_SET_CURSOR(name) \
typedef struct set_cursor__##name { \
s_set__##name *set; \
uw i; \
s_set_item__##name *item; \
uw count; \
} s_set_cursor__##name
TYPEDEF_SET_CURSOR(tag);
TYPEDEF_SET_CURSOR(fact);
#define TYPEDEF_SKIPLIST_NODE(name, type) \
typedef struct skiplist_node__##name { \
type name; \
u8 height; \
} s_skiplist_node__##name
TYPEDEF_SKIPLIST_NODE(fact, s_fact *);
#define TYPEDEF_SKIPLIST(name, type) \
typedef struct skiplist__##name { \
s_skiplist_node__##name *head; \
s8 (*compare) (const type a, const type b); \
uw length; \
u8 max_height; \
} s_skiplist__##name
TYPEDEF_SKIPLIST(fact, s_fact *);
/* 5 */
struct facts {
s_set__tag tags;
s_set__fact facts;
s_skiplist__fact *index_spo;
s_skiplist__fact *index_pos;
s_skiplist__fact *index_osp;
s_log *log;
};
struct facts_cursor {
s_skiplist__fact *index;
s_skiplist_node__fact *node;
s_fact start;
s_fact end;
s_tag *var_subject;
s_tag *var_predicate;
s_tag *var_object;
};
/* 6 */
struct facts_with_cursor_level {
s_facts_cursor cursor;
s_fact *fact;
p_facts_spec spec;
};
/* 7 */
struct facts_with_cursor {
const s_facts *facts;
s_binding *bindings;
size_t facts_count;
s_facts_with_cursor_level *levels;
size_t level;
p_facts_spec spec;
};
#endif /* TYPES_H */