IR jmp and scopes

This commit is contained in:
r4 2021-12-21 13:59:08 +01:00
parent 6080901842
commit 005309d1eb
5 changed files with 33 additions and 7 deletions

View File

@ -1,8 +1,8 @@
a := 1
a = a + 1
b := a - 2 * 3
b = 2 + b * a
c := -b
a := 0
{
a := 1
b := a
}
//a := 1
//b := 1 - 2 * 2 + 5

5
ir.c
View File

@ -11,6 +11,7 @@ const char *irinstr_str[IRInstrEnumSize] = {
[IRMul] = "mul",
[IRDiv] = "div",
[IRPrint] = "print",
[IRJmp] = "jmp",
[IRJnz] = "jnz",
};
@ -90,6 +91,10 @@ void print_ir(IRToks *v) {
print_irparam(&a->param);
}
break;
case IRJmp:
printf(" ");
printf(" %zu", v->toks[i].Jmp.iaddr);
break;
case IRJnz:
printf(" ");
print_irparam(&v->toks[i].CJmp.condition);

5
ir.h
View File

@ -11,6 +11,7 @@ enum IRInstr {
IRMul,
IRDiv,
IRPrint,
IRJmp,
IRJnz,
IRInstrEnumSize,
};
@ -57,6 +58,10 @@ typedef struct IRTok {
size_t args_size;
} Print;
struct {
size_t iaddr;
} Jmp;
struct {
size_t iaddr;
IRParam condition;

16
parse.c
View File

@ -343,6 +343,22 @@ static void stmt(State *s, Scope *sc, TokListItem *t) {
TRY(addr = get_ident_addr(sc, name, &start->tok));
TRY(expr(s, sc, t, true, true, addr));
}
} else if (t->tok.kind == TokOp && t->tok.Op == OpLCurl) {
Scope inner_sc = make_scope(sc, sc->mem_addr, true);
for (;;) {
if (t->next->tok.kind == TokOp) {
if (t->next->tok.Op == OpEOF) {
term_scope(&inner_sc);
mark_err(&start->tok);
set_err("Unclosed '{'");
return;
}
if (t->next->tok.Op == OpRCurl)
break;
}
TRY(stmt(s, &inner_sc, t->next));
}
term_scope(&inner_sc);
}
toklist_del(s->toks, start, t);
}

View File

@ -15,7 +15,7 @@ Value eval_arith(IRInstr instr, const Value *lhs, const Value *rhs) {
case IRSub: res = lhs->Int - rhs->Int; break;
case IRMul: res = lhs->Int * rhs->Int; break;
case IRDiv: res = lhs->Int / rhs->Int; break;
default: break;
default: ASSERT_UNREACHED();
}
return (Value){
.type.kind = TypeInt,
@ -28,7 +28,7 @@ Value eval_arith(IRInstr instr, const Value *lhs, const Value *rhs) {
case IRSub: res = lhs->Float - rhs->Float; break;
case IRMul: res = lhs->Float * rhs->Float; break;
case IRDiv: res = lhs->Float / rhs->Float; break;
default: break;
default: ASSERT_UNREACHED();
}
return (Value){
.type.kind = TypeFloat,