From 46e786db9d1b48b8fbc3502e36f093b755f3e09f Mon Sep 17 00:00:00 2001 From: kartofen Date: Tue, 26 Aug 2025 01:17:10 +0300 Subject: grammar for the grammar and lexing and parsing of a new language lbp --- util/queue.h | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 util/queue.h (limited to 'util/queue.h') 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 -- cgit v1.2.3