summaryrefslogtreecommitdiffstats
path: root/bytecode/slice.h
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2023-02-11 21:31:04 -0800
committerRose Hogenson <rhogenson@posteo.net>2023-02-11 21:31:04 -0800
commit5989629a7951e544ad4656672025a99ecd08c7c9 (patch)
tree89e2ca80f73d8dcf16a9fadcea41b2e141bdf4eb /bytecode/slice.h
parentSlightly simplify bytecode interpreter. (diff)
downloadsml-5989629a7951e544ad4656672025a99ecd08c7c9.tar.zst
Change how slices work.
I'm switching to a more macro-heavy solution. Hopefully it proves robust.
Diffstat (limited to 'bytecode/slice.h')
-rw-r--r--bytecode/slice.h46
1 files changed, 26 insertions, 20 deletions
diff --git a/bytecode/slice.h b/bytecode/slice.h
index 577befd..4f4624c 100644
--- a/bytecode/slice.h
+++ b/bytecode/slice.h
@@ -1,27 +1,33 @@
#ifndef _SLICE_H_
#define _SLICE_H_
+#include <assert.h>
#include <stddef.h>
-#include <stdint.h>
-#include "value.h"
-
-struct slice {
- size_t size;
- uint8_t *buf;
-};
-
-struct val_slice {
- size_t size;
- value *buf;
-};
-
-struct slice slice(struct slice, size_t, size_t);
-
-struct slice slice1(struct slice, size_t);
-
-struct val_slice val_slice(struct val_slice, size_t, size_t);
-
-struct val_slice val_slice1(struct val_slice, size_t);
+#define DEFINE_SLICE(t) \
+ typedef struct { \
+ size_t size; \
+ t *buf; \
+ } t##_slice; \
+ \
+ static inline t##_slice \
+ t##_slice2(t##_slice slice, size_t i, size_t j) \
+ { \
+ assert(i <= slice.size && j <= slice.size && i <= j); \
+ return (t##_slice) { \
+ .size = j - i, \
+ .buf = slice.buf + i, \
+ }; \
+ } \
+ \
+ static inline t##_slice \
+ t##_slice1(t##_slice slice, size_t i) \
+ { \
+ assert(i <= slice.size); \
+ return (t##_slice) { \
+ .size = slice.size - i, \
+ .buf = slice.buf + i, \
+ }; \
+ }
#endif