Refactor ALL the things!
This commit is contained in:
parent
6a2f10df0c
commit
f2def8fa05
28 changed files with 930 additions and 855 deletions
43
src/parsers/bits.c
Normal file
43
src/parsers/bits.c
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#include "parser_internal.h"
|
||||
|
||||
struct bits_env {
|
||||
uint8_t length;
|
||||
uint8_t signedp;
|
||||
};
|
||||
|
||||
static HParseResult* parse_bits(void* env, HParseState *state) {
|
||||
struct bits_env *env_ = env;
|
||||
HParsedToken *result = a_new(HParsedToken, 1);
|
||||
result->token_type = (env_->signedp ? TT_SINT : TT_UINT);
|
||||
if (env_->signedp)
|
||||
result->sint = h_read_bits(&state->input_stream, env_->length, true);
|
||||
else
|
||||
result->uint = h_read_bits(&state->input_stream, env_->length, false);
|
||||
return make_result(state, result);
|
||||
}
|
||||
|
||||
static const HParserVtable bits_vt = {
|
||||
.parse = parse_bits,
|
||||
};
|
||||
const HParser* h_bits(size_t len, bool sign) {
|
||||
struct bits_env *env = g_new(struct bits_env, 1);
|
||||
env->length = len;
|
||||
env->signedp = sign;
|
||||
HParser *res = g_new(HParser, 1);
|
||||
res->vtable = &bits_vt;
|
||||
res->env = env;
|
||||
return res;
|
||||
}
|
||||
|
||||
#define SIZED_BITS(name_pre, len, signedp) \
|
||||
const HParser* h_##name_pre##len () { \
|
||||
return h_bits(len, signedp); \
|
||||
}
|
||||
SIZED_BITS(int, 8, true)
|
||||
SIZED_BITS(int, 16, true)
|
||||
SIZED_BITS(int, 32, true)
|
||||
SIZED_BITS(int, 64, true)
|
||||
SIZED_BITS(uint, 8, false)
|
||||
SIZED_BITS(uint, 16, false)
|
||||
SIZED_BITS(uint, 32, false)
|
||||
SIZED_BITS(uint, 64, false)
|
||||
Loading…
Add table
Add a link
Reference in a new issue