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:
parent
a8be4540b1
commit
e7f4773cba
@ -2,7 +2,7 @@ x := 1
|
|||||||
y := 1
|
y := 1
|
||||||
|
|
||||||
i := 60
|
i := 60
|
||||||
while i + 1 {
|
while i {
|
||||||
z := x + y
|
z := x + y
|
||||||
y = x
|
y = x
|
||||||
x = z
|
x = z
|
||||||
|
26
ir.c
26
ir.c
@ -15,12 +15,23 @@ const char *irinstr_str[IRInstrEnumSize] = {
|
|||||||
[IRJnz] = "jnz",
|
[IRJnz] = "jnz",
|
||||||
};
|
};
|
||||||
|
|
||||||
#define IRTOKS_INIT_CAP 4096
|
#define IRTOKS_INIT_CAP_LONG 4096
|
||||||
|
#define IRTOKS_INIT_CAP_SHORT 16
|
||||||
|
|
||||||
void irtoks_init(IRToks *v) {
|
static void irtoks_init_with_cap(IRToks *v, size_t cap);
|
||||||
v->toks = malloc(sizeof(IRTok) * IRTOKS_INIT_CAP);
|
static void irtoks_init_with_cap(IRToks *v, size_t cap) {
|
||||||
|
v->toks = malloc(sizeof(IRTok) * cap);
|
||||||
v->len = 0;
|
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) {
|
void irtoks_term(IRToks *v) {
|
||||||
@ -42,6 +53,13 @@ void irtoks_app(IRToks *v, IRTok t) {
|
|||||||
v->toks[v->len++] = 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_val(const Value *v);
|
||||||
static void print_irparam(const IRParam *p);
|
static void print_irparam(const IRParam *p);
|
||||||
|
|
||||||
|
4
ir.h
4
ir.h
@ -74,9 +74,11 @@ typedef struct IRToks {
|
|||||||
IRTok *toks;
|
IRTok *toks;
|
||||||
} IRToks;
|
} IRToks;
|
||||||
|
|
||||||
void irtoks_init(IRToks *v);
|
void irtoks_init_long(IRToks *v);
|
||||||
|
void irtoks_init_short(IRToks *v);
|
||||||
void irtoks_term(IRToks *v);
|
void irtoks_term(IRToks *v);
|
||||||
void irtoks_app(IRToks *v, IRTok t);
|
void irtoks_app(IRToks *v, IRTok t);
|
||||||
|
void irtoks_app_irtoks(IRToks *v, IRToks *other);
|
||||||
|
|
||||||
void print_ir(IRToks *v);
|
void print_ir(IRToks *v);
|
||||||
|
|
||||||
|
96
parse.c
96
parse.c
@ -5,11 +5,6 @@
|
|||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include "runtime.h"
|
#include "runtime.h"
|
||||||
|
|
||||||
typedef struct State {
|
|
||||||
TokList *toks;
|
|
||||||
IRToks *ir;
|
|
||||||
} State;
|
|
||||||
|
|
||||||
typedef struct Scope {
|
typedef struct Scope {
|
||||||
struct Scope *parent;
|
struct Scope *parent;
|
||||||
size_t mem_addr;
|
size_t mem_addr;
|
||||||
@ -35,8 +30,8 @@ static size_t get_ident_addr(const Scope *sc, const char *name, const Tok *errpo
|
|||||||
static IRParam tok_to_irparam(Scope *sc, Tok *t);
|
static IRParam tok_to_irparam(Scope *sc, Tok *t);
|
||||||
static Scope make_scope(Scope *parent, bool with_idents);
|
static Scope make_scope(Scope *parent, bool with_idents);
|
||||||
static void term_scope(Scope *sc);
|
static void term_scope(Scope *sc);
|
||||||
static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode);
|
static void expr(IRToks *out_ir, TokList *toks, Scope *parent_sc, TokListItem *t, ExprMode mode);
|
||||||
static void stmt(State *s, Scope *sc, TokListItem *t);
|
static void stmt(IRToks *out_ir, TokList *toks, Scope *sc, TokListItem *t);
|
||||||
|
|
||||||
static void mark_err(const Tok *t) {
|
static void mark_err(const Tok *t) {
|
||||||
err_ln = t->ln;
|
err_ln = t->ln;
|
||||||
@ -96,7 +91,7 @@ static void term_scope(Scope *sc) {
|
|||||||
map_term(&sc->ident_addrs);
|
map_term(&sc->ident_addrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
static void expr(IRToks *out_ir, TokList *toks, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
||||||
/* A simplified example of how the operator precedence parsing works:
|
/* A simplified example of how the operator precedence parsing works:
|
||||||
* ________________________________
|
* ________________________________
|
||||||
* Where t points to (between l_op and r_op in each step)
|
* Where t points to (between l_op and r_op in each step)
|
||||||
@ -148,14 +143,14 @@ static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
|||||||
|
|
||||||
/* Ignore newlines if told to do so. */
|
/* Ignore newlines if told to do so. */
|
||||||
if (mode.ignore_newln && t->next->tok.kind == TokOp && t->next->tok.Op == OpNewLn)
|
if (mode.ignore_newln && t->next->tok.kind == TokOp && t->next->tok.Op == OpNewLn)
|
||||||
toklist_del(s->toks, t->next, t->next);
|
toklist_del(toks, t->next, t->next);
|
||||||
|
|
||||||
/* Collapse negative factor. */
|
/* Collapse negative factor. */
|
||||||
if (negate) {
|
if (negate) {
|
||||||
bool is_last_operation = t->prev == start && t->next->tok.kind == TokOp && op_prec[t->next->tok.Op] == PREC_DELIM;
|
bool is_last_operation = t->prev == start && t->next->tok.kind == TokOp && op_prec[t->next->tok.Op] == PREC_DELIM;
|
||||||
Tok *v = &t->tok;
|
Tok *v = &t->tok;
|
||||||
t = t->prev;
|
t = t->prev;
|
||||||
toklist_del(s->toks, t->next, t->next);
|
toklist_del(toks, t->next, t->next);
|
||||||
|
|
||||||
if (v->kind == TokVal) {
|
if (v->kind == TokVal) {
|
||||||
/* immediately negate value */
|
/* immediately negate value */
|
||||||
@ -177,7 +172,7 @@ static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
|||||||
/* add IR instruction to negate the value */
|
/* add IR instruction to negate the value */
|
||||||
IRParam v_irparam;
|
IRParam v_irparam;
|
||||||
TRY(v_irparam = tok_to_irparam(&sc, v));
|
TRY(v_irparam = tok_to_irparam(&sc, v));
|
||||||
irtoks_app(s->ir, (IRTok){
|
irtoks_app(out_ir, (IRTok){
|
||||||
.ln = t->tok.ln,
|
.ln = t->tok.ln,
|
||||||
.col = t->tok.col,
|
.col = t->tok.col,
|
||||||
.instr = IRNeg,
|
.instr = IRNeg,
|
||||||
@ -189,7 +184,7 @@ static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
|||||||
|
|
||||||
if (mode.kind == ExprModeStorageAddr && is_last_operation) {
|
if (mode.kind == ExprModeStorageAddr && is_last_operation) {
|
||||||
/* done */
|
/* done */
|
||||||
toklist_del(s->toks, t, t);
|
toklist_del(toks, t, t);
|
||||||
return;
|
return;
|
||||||
} else {
|
} else {
|
||||||
/* leave new memory address as result */
|
/* leave new memory address as result */
|
||||||
@ -238,7 +233,7 @@ static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
|||||||
if (mode.kind == ExprModeStorageAddr) {
|
if (mode.kind == ExprModeStorageAddr) {
|
||||||
IRParam res;
|
IRParam res;
|
||||||
TRY(res = tok_to_irparam(&sc, &t->tok));
|
TRY(res = tok_to_irparam(&sc, &t->tok));
|
||||||
irtoks_app(s->ir, (IRTok){
|
irtoks_app(out_ir, (IRTok){
|
||||||
.ln = t->tok.ln,
|
.ln = t->tok.ln,
|
||||||
.col = t->tok.col,
|
.col = t->tok.col,
|
||||||
.instr = IRSet,
|
.instr = IRSet,
|
||||||
@ -247,7 +242,7 @@ static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
|||||||
.val = res,
|
.val = res,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
toklist_del(s->toks, t, t);
|
toklist_del(toks, t, t);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -276,7 +271,7 @@ static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
|||||||
/* delete the tokens that fall away from collapsing the expression
|
/* delete the tokens that fall away from collapsing the expression
|
||||||
* (NOTE: only their references are deleted here, that's important
|
* (NOTE: only their references are deleted here, that's important
|
||||||
* because we're still using their values later on) */
|
* because we're still using their values later on) */
|
||||||
toklist_del(s->toks, t->next, t->next->next);
|
toklist_del(toks, t->next, t->next->next);
|
||||||
|
|
||||||
IRInstr instr;
|
IRInstr instr;
|
||||||
switch (l_op->Op) {
|
switch (l_op->Op) {
|
||||||
@ -306,7 +301,7 @@ static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
|||||||
res_addr = sc.mem_addr++;
|
res_addr = sc.mem_addr++;
|
||||||
|
|
||||||
/* emit IR code to evaluate the non-constant expression */
|
/* emit IR code to evaluate the non-constant expression */
|
||||||
irtoks_app(s->ir, (IRTok){
|
irtoks_app(out_ir, (IRTok){
|
||||||
.ln = l_op->ln,
|
.ln = l_op->ln,
|
||||||
.col = l_op->col,
|
.col = l_op->col,
|
||||||
.instr = instr,
|
.instr = instr,
|
||||||
@ -319,7 +314,7 @@ static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
|||||||
|
|
||||||
if (mode.kind == ExprModeStorageAddr && is_last_operation) {
|
if (mode.kind == ExprModeStorageAddr && is_last_operation) {
|
||||||
/* done */
|
/* done */
|
||||||
toklist_del(s->toks, t, t);
|
toklist_del(toks, t, t);
|
||||||
break;
|
break;
|
||||||
} else {
|
} else {
|
||||||
/* leave new memory address as result */
|
/* leave new memory address as result */
|
||||||
@ -334,7 +329,7 @@ static void expr(State *s, Scope *parent_sc, TokListItem *t, ExprMode mode) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static void stmt(State *s, Scope *sc, TokListItem *t) {
|
static void stmt(IRToks *out_ir, TokList *toks, Scope *sc, TokListItem *t) {
|
||||||
TokListItem *start = t;
|
TokListItem *start = t;
|
||||||
if (t->tok.kind == TokIdent && t->tok.Ident.kind == IdentName) {
|
if (t->tok.kind == TokIdent && t->tok.Ident.kind == IdentName) {
|
||||||
char *name = t->tok.Ident.Name;
|
char *name = t->tok.Ident.Name;
|
||||||
@ -348,12 +343,12 @@ static void stmt(State *s, Scope *sc, TokListItem *t) {
|
|||||||
set_err("'%s' already declared in this scope", name);
|
set_err("'%s' already declared in this scope", name);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
TRY(expr(s, sc, t, (ExprMode){ .kind = ExprModeStorageAddr, .ignore_newln = false, .StorageAddr = addr }));
|
TRY(expr(out_ir, toks, sc, t, (ExprMode){ .kind = ExprModeStorageAddr, .ignore_newln = false, .StorageAddr = addr }));
|
||||||
} else if (t->tok.kind == TokAssign) {
|
} else if (t->tok.kind == TokAssign) {
|
||||||
t = t->next;
|
t = t->next;
|
||||||
size_t addr;
|
size_t addr;
|
||||||
TRY(addr = get_ident_addr(sc, name, &start->tok));
|
TRY(addr = get_ident_addr(sc, name, &start->tok));
|
||||||
TRY(expr(s, sc, t, (ExprMode){ .kind = ExprModeStorageAddr, .ignore_newln = false, .StorageAddr = addr }));
|
TRY(expr(out_ir, toks, sc, t, (ExprMode){ .kind = ExprModeStorageAddr, .ignore_newln = false, .StorageAddr = addr }));
|
||||||
}
|
}
|
||||||
} else if (t->tok.kind == TokOp && t->tok.Op == OpLCurl) {
|
} else if (t->tok.kind == TokOp && t->tok.Op == OpLCurl) {
|
||||||
Scope inner_sc = make_scope(sc, true);
|
Scope inner_sc = make_scope(sc, true);
|
||||||
@ -368,7 +363,7 @@ static void stmt(State *s, Scope *sc, TokListItem *t) {
|
|||||||
if (t->next->tok.Op == OpRCurl)
|
if (t->next->tok.Op == OpRCurl)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
TRY_ELSE(stmt(s, &inner_sc, t->next), term_scope(&inner_sc));
|
TRY_ELSE(stmt(out_ir, toks, &inner_sc, t->next), term_scope(&inner_sc));
|
||||||
}
|
}
|
||||||
term_scope(&inner_sc);
|
term_scope(&inner_sc);
|
||||||
t = t->next;
|
t = t->next;
|
||||||
@ -381,8 +376,9 @@ static void stmt(State *s, Scope *sc, TokListItem *t) {
|
|||||||
* 4: jmp to 1 if condition xyz is met
|
* 4: jmp to 1 if condition xyz is met
|
||||||
* */
|
* */
|
||||||
|
|
||||||
size_t jmp_instr_iaddr = s->ir->len;
|
/* add initial jmp instruction */
|
||||||
irtoks_app(s->ir, (IRTok){
|
size_t jmp_instr_iaddr = out_ir->len;
|
||||||
|
irtoks_app(out_ir, (IRTok){
|
||||||
.ln = t->tok.ln,
|
.ln = t->tok.ln,
|
||||||
.col = t->tok.col,
|
.col = t->tok.col,
|
||||||
.instr = IRJmp,
|
.instr = IRJmp,
|
||||||
@ -393,52 +389,46 @@ static void stmt(State *s, Scope *sc, TokListItem *t) {
|
|||||||
|
|
||||||
t = t->next;
|
t = t->next;
|
||||||
|
|
||||||
/* find beginning of while loop body */
|
/* parse condition */
|
||||||
TokListItem *lcurl;
|
IRToks cond_ir;
|
||||||
for (TokListItem *i = t;; i = i->next) {
|
irtoks_init_short(&cond_ir);
|
||||||
if (i == NULL) {
|
TRY_ELSE(expr(&cond_ir, toks, sc, t, (ExprMode){ .kind = ExprModeJustCollapse, .ignore_newln = false }), irtoks_term(&cond_ir));
|
||||||
mark_err(&start->tok);
|
IRParam cond_irparam;
|
||||||
set_err("Expected '{' after 'while' loop condition");
|
TRY_ELSE(cond_irparam = tok_to_irparam(sc, &t->tok), irtoks_term(&cond_ir));
|
||||||
return;
|
/* add conditional jump */
|
||||||
}
|
irtoks_app(&cond_ir, (IRTok){
|
||||||
if (i->tok.kind == TokOp && i->tok.Op == OpLCurl) {
|
|
||||||
lcurl = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* write loop body to IR stream */
|
|
||||||
TRY(stmt(s, sc, lcurl));
|
|
||||||
|
|
||||||
/* finally we know where the jmp from the beginning has to jump to */
|
|
||||||
s->ir->toks[jmp_instr_iaddr].Jmp.iaddr = s->ir->len;
|
|
||||||
|
|
||||||
TRY(expr(s, sc, t, (ExprMode){ .kind = ExprModeJustCollapse, .ignore_newln = false }));
|
|
||||||
IRParam condition;
|
|
||||||
TRY(condition = tok_to_irparam(sc, &t->tok));
|
|
||||||
|
|
||||||
irtoks_app(s->ir, (IRTok){
|
|
||||||
.ln = t->tok.ln,
|
.ln = t->tok.ln,
|
||||||
.col = t->tok.col,
|
.col = t->tok.col,
|
||||||
.instr = IRJnz,
|
.instr = IRJnz,
|
||||||
.CJmp = {
|
.CJmp = {
|
||||||
.iaddr = jmp_instr_iaddr + 1,
|
.iaddr = jmp_instr_iaddr + 1,
|
||||||
.condition = condition,
|
.condition = cond_irparam,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
t = t->next;
|
||||||
|
|
||||||
|
/* parse loop body */
|
||||||
|
TRY_ELSE(stmt(out_ir, toks, sc, t), irtoks_term(&cond_ir));
|
||||||
|
|
||||||
|
/* finally we know where the jmp from the beginning has to jump to */
|
||||||
|
out_ir->toks[jmp_instr_iaddr].Jmp.iaddr = out_ir->len;
|
||||||
|
|
||||||
|
/* append condition IR to program IR, then terminate condition IR stream */
|
||||||
|
irtoks_app_irtoks(out_ir, &cond_ir);
|
||||||
|
irtoks_term(&cond_ir);
|
||||||
}
|
}
|
||||||
toklist_del(s->toks, start, t);
|
toklist_del(toks, start, t);
|
||||||
}
|
}
|
||||||
|
|
||||||
IRToks parse(TokList *toks) {
|
IRToks parse(TokList *toks) {
|
||||||
IRToks ir;
|
IRToks ir;
|
||||||
irtoks_init(&ir);
|
irtoks_init_long(&ir);
|
||||||
State s = { .toks = toks, .ir = &ir };
|
|
||||||
Scope global_scope = make_scope(NULL, true);
|
Scope global_scope = make_scope(NULL, true);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
if (toks->begin->tok.kind == TokOp && toks->begin->tok.Op == OpEOF)
|
if (toks->begin->tok.kind == TokOp && toks->begin->tok.Op == OpEOF)
|
||||||
break;
|
break;
|
||||||
TRY_RET_ELSE(stmt(&s, &global_scope, toks->begin), ir, term_scope(&global_scope));
|
TRY_RET_ELSE(stmt(&ir, toks, &global_scope, toks->begin), ir, term_scope(&global_scope));
|
||||||
}
|
}
|
||||||
term_scope(&global_scope);
|
term_scope(&global_scope);
|
||||||
return ir;
|
return ir;
|
||||||
|
Loading…
Reference in New Issue
Block a user