code / tomo

Lines41.3K C23.7K Markdown9.7K YAML5.0K Tomo2.3K
7 others 763
Python231 Shell230 make212 INI47 Text21 SVG16 Lua6
(162 lines)
1 // Common datastructures (lists, tables, closures)
3 #pragma once
5 #include <gmp.h>
6 #include <stdbool.h>
7 #include <stdint.h>
9 #define LIST_LENGTH_BITS 64
10 #define LIST_FREE_BITS 48
11 #define LIST_ATOMIC_BITS 1
12 #define LIST_REFCOUNT_BITS 3
13 #define LIST_STRIDE_BITS 12
15 #define MAX_FOR_N_BITS(N) ((1L << (N)) - 1L)
16 #define LIST_MAX_STRIDE MAX_FOR_N_BITS(LIST_STRIDE_BITS - 1)
17 #define LIST_MIN_STRIDE (~MAX_FOR_N_BITS(LIST_STRIDE_BITS - 1))
18 #define LIST_MAX_DATA_REFCOUNT MAX_FOR_N_BITS(LIST_REFCOUNT_BITS)
19 #define LIST_MAX_FREE_ENTRIES MAX_FOR_N_BITS(LIST_FREE_BITS)
21 #define Num_t double
22 #define Num32_t float
24 #define Int64_t int64_t
25 #define Int32_t int32_t
26 #define Int16_t int16_t
27 #define Int8_t int8_t
28 #define Byte_t uint8_t
29 #define Bool_t bool
31 typedef union {
32 int64_t small;
33 __mpz_struct *big;
34 } Int_t;
36 #define OptionalInt_t Int_t
38 typedef struct {
39 void *data;
40 // All of the following fields add up to 64 bits, which means that list
41 // structs can be passed in two 64-bit registers. C will handle doing the
42 // bit arithmetic to extract the necessary values, which is cheaper than
43 // spilling onto the stack and needing to retrieve data from the stack.
44 uint64_t length : LIST_LENGTH_BITS;
45 uint64_t free : LIST_FREE_BITS;
46 bool atomic : LIST_ATOMIC_BITS;
47 uint8_t data_refcount : LIST_REFCOUNT_BITS;
48 int16_t stride : LIST_STRIDE_BITS;
49 } List_t;
51 typedef struct {
52 uint32_t occupied : 1, index : 31;
53 uint32_t next_bucket;
54 } bucket_t;
56 // Maximum bucket size is determined by the maximum value for `index` in the `bucket_t` struct
57 #define TABLE_MAX_BUCKETS 0x7fffffff
58 #define TABLE_MAX_DATA_REFCOUNT 3
60 typedef struct {
61 uint32_t count : 31, last_free : 31;
62 uint8_t data_refcount : 2;
63 bucket_t buckets[];
64 } bucket_info_t;
66 typedef struct table_s {
67 List_t entries;
68 uint64_t hash;
69 bucket_info_t *bucket_info;
70 struct table_s *fallback;
71 } Table_t;
73 typedef struct Present$$struct {
74 } Present$$type;
76 #define PRESENT_STRUCT ((Present$$type){})
78 typedef struct {
79 Present$$type value;
80 bool has_value;
81 } $OptionalPresent$$type;
83 #define NONE_PRESENT_STRUCT (($OptionalPresent$$type){.has_value = false})
84 #define OPTIONAL_PRESENT_STRUCT (($OptionalPresent$$type){.has_value = true})
86 typedef struct {
87 void *fn, *userdata;
88 } Closure_t;
90 enum text_type { TEXT_NONE, TEXT_ASCII, TEXT_GRAPHEMES, TEXT_CONCAT, TEXT_BLOB };
92 typedef struct Text_s {
93 uint64_t length : 53; // Number of grapheme clusters
94 uint8_t tag : 3;
95 uint8_t depth : 8;
96 union {
97 struct {
98 const char *ascii;
99 // char ascii_buf[8];
101 struct {
102 const int32_t *graphemes;
103 // int32_t grapheme_buf[2];
105 struct {
106 const struct Text_s *left, *right;
108 struct {
109 const int32_t *map;
110 const uint8_t *bytes;
111 } blob;
113 } Text_t;
115 typedef const char *Path_t;
117 #define OptionalPath_t Path_t
119 typedef struct Result$Success$$struct {
120 } Result$Success$$type;
122 typedef struct {
123 Result$Success$$type value;
124 bool has_value;
125 } $OptionalResult$Success$$type;
127 typedef struct Result$Failure$$struct {
128 Text_t reason;
129 } Result$Failure$$type;
131 typedef struct {
132 Result$Failure$$type value;
133 bool has_value;
134 } $OptionalResult$Failure$$type;
136 #define Result$Success ((Result$$type){.$tag = Result$tag$Success})
137 #define SuccessResult Result$Success
138 #define Result$tagged$Failure(msg) ((Result$$type){.$tag = Result$tag$Failure, .Failure.reason = msg})
139 #define FailureResult(...) Result$tagged$Failure(Texts(__VA_ARGS__))
141 typedef struct Result$$struct {
142 enum { Result$tag$none, Result$tag$Success, Result$tag$Failure } $tag;
143 union {
144 Result$Success$$type Success;
145 Result$Failure$$type Failure;
147 } Result$$type;
149 #define Result_t Result$$type
151 #define OptionalBool_t uint8_t
152 #define OptionalList_t List_t
153 #define OptionalTable_t Table_t
154 #define OptionalText_t Text_t
155 #define OptionalClosure_t Closure_t
157 typedef struct {
158 Byte_t value;
159 bool has_value : 1;
160 } OptionalByte_t;
162 #define NONE_BYTE ((OptionalByte_t){.has_value = false})