aboutsummaryrefslogtreecommitdiff
path: root/util
diff options
context:
space:
mode:
authorkartofen <kartofen.mail.0@protonmail.com>2025-08-26 01:17:10 +0300
committerkartofen <kartofen.mail.0@protonmail.com>2025-08-26 01:17:10 +0300
commit46e786db9d1b48b8fbc3502e36f093b755f3e09f (patch)
tree9e279216e68f3fe4b0849d1e07184fe674dc551f /util
parent1c83c514c8108fccfec9764da5e4563b98eb871b (diff)
grammar for the grammar and lexing and parsing of a new language lbp
Diffstat (limited to 'util')
-rw-r--r--util/dict.h2
-rw-r--r--util/queue.h46
2 files changed, 48 insertions, 0 deletions
diff --git a/util/dict.h b/util/dict.h
index 109c07a..2da8e6f 100644
--- a/util/dict.h
+++ b/util/dict.h
@@ -28,6 +28,8 @@ struct dict {
size_t num_levels;
};
+#define DICT_INIT(strings_, nstrings_, char_to_bit_) (struct dict){.strings = strings_, .nstrings = nstrings_, .char_to_bit = char_to_bit_}
+
int dict_compile(struct dict *d);
void dict_free(struct dict *d);
void dict_print(struct dict *d);
diff --git a/util/queue.h b/util/queue.h
new file mode 100644
index 0000000..31236f6
--- /dev/null
+++ b/util/queue.h
@@ -0,0 +1,46 @@
+#ifndef QUEUE_H
+#define QUEUE_H
+
+#define QUEUE_GENERATE(id, type, cap) \
+ static struct \
+ { type buf[cap]; size_t start; size_t end; } _##id##_queue; \
+ \
+ static int id##_enqueue(type *m) \
+ { \
+ if(_##id##_queue.end >= _##id##_queue.start + cap) { \
+ fprintf(stderr, \
+ "ERROR: Queue capacity of %d reached\n", cap); \
+ return 1; \
+ } \
+ \
+ _##id##_queue.buf[_##id##_queue.end++ % cap] = *m; \
+ return 0; \
+ } \
+ \
+ static int id##_dequeue(type *m) \
+ { \
+ if(_##id##_queue.start >= _##id##_queue.end) { \
+ fprintf(stderr, "ERROR: Trying to dequeue empty queue\n"); \
+ return 1; \
+ } \
+ \
+ *m = _##id##_queue.buf[_##id##_queue.start++ % cap]; \
+ return 0; \
+ } \
+ \
+ static int id##_empty() \
+ { return _##id##_queue.start == _##id##_queue.end; } \
+ \
+ static int id##_peek(type *m) \
+ { \
+ if(id##_empty()) { \
+ fprintf(stderr, \
+ "ERROR: Trying to peek into empty queue\n"); \
+ return 1; \
+ } \
+ \
+ *m = _##id##_queue.buf[_##id##_queue.start % cap]; \
+ return 0; \
+ }
+
+#endif