diff --git a/Makefile b/Makefile index fe6f616..9e76042 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ bin_files = $(patsubst %.asm,%.bin,$(asm_files)) all: decode asm_files -decode: decode.c +decode: decode.c lib.h $(CC) $(CFLAGS) $< -o $@ asm_files: $(bin_files) diff --git a/lib.h b/lib.h index bebd135..6a1b625 100644 --- a/lib.h +++ b/lib.h @@ -15,12 +15,38 @@ typedef char sbyte; typedef ptrdiff_t size; typedef size_t usize; -#define OPTION(type, typeName) \ - struct { \ - enum {NONE, SOME} tag; \ - union { \ - u8 none; \ - type typeName; \ - } some; \ +enum OptionTag {NONE, SOME}; + +#define OPTION(type) \ + typedef struct \ + { \ + enum OptionTag tag; \ + union { \ + char none; \ + type some; \ + }; \ + } type##_opt; \ + \ + static inline type##_opt none_##type(void) \ + { \ + return (type##_opt){ .tag = NONE, .none = 0 }; \ + } \ + \ + static inline type##_opt some_##type(type value) \ + { \ + return (type##_opt){ .tag = SOME, .some = value }; \ + } \ + \ + static inline int get_some_##type(type##_opt opt, type* out_value) \ + { \ + if (opt.tag != SOME) return 0; \ + *out_value = opt.some; \ + return 1; \ } +#define IF_LET_SOME(type, var, opt) \ + type var; \ + if (get_some_##type(opt, &var)) + + +OPTION(u8)