make while loop parsing more elegant

It is now possible to parse parts of the program and save the IR for
later addition. This is currently used for while loops but will also make other
control flow elements easier to implement in the future.
This commit is contained in:
r4
2021-12-22 12:52:16 +01:00
parent a8be4540b1
commit e7f4773cba
4 changed files with 69 additions and 59 deletions

26
ir.c
View File

@@ -15,12 +15,23 @@ const char *irinstr_str[IRInstrEnumSize] = {
[IRJnz] = "jnz",
};
#define IRTOKS_INIT_CAP 4096
#define IRTOKS_INIT_CAP_LONG 4096
#define IRTOKS_INIT_CAP_SHORT 16
void irtoks_init(IRToks *v) {
v->toks = malloc(sizeof(IRTok) * IRTOKS_INIT_CAP);
static void irtoks_init_with_cap(IRToks *v, size_t cap);
static void irtoks_init_with_cap(IRToks *v, size_t cap) {
v->toks = malloc(sizeof(IRTok) * cap);
v->len = 0;
v->cap = IRTOKS_INIT_CAP;
v->cap = cap;
}
void irtoks_init_long(IRToks *v) {
irtoks_init_with_cap(v, IRTOKS_INIT_CAP_LONG);
}
void irtoks_init_short(IRToks *v) {
irtoks_init_with_cap(v, IRTOKS_INIT_CAP_SHORT);
}
void irtoks_term(IRToks *v) {
@@ -42,6 +53,13 @@ void irtoks_app(IRToks *v, IRTok t) {
v->toks[v->len++] = t;
}
void irtoks_app_irtoks(IRToks *v, IRToks *other) {
if (v->len+other->len > v->cap)
v->toks = realloc(v->toks, sizeof(IRTok) * (other->len + (v->cap *= 2)));
for (size_t i = 0; i < other->len; i++)
v->toks[v->len++] = other->toks[i];
}
static void print_val(const Value *v);
static void print_irparam(const IRParam *p);