Iterative dumbification

This commit is contained in:
Mid 2024-02-13 22:43:04 +02:00
parent a1077f7c03
commit 55bfa2289e

View File

@ -185,10 +185,12 @@ static AST *xopify(AST *tlc, AST *chunk, AST *stmtPrev, AST *stmt, AST *e) {
return varify(tlc, chunk, stmtPrev, stmt, e); return varify(tlc, chunk, stmtPrev, stmt, e);
} }
static void dumben_chunk(AST *tlc, AST *chu) { static int dumben_chunk(AST *tlc, AST *chu) {
AST *sPrev = NULL; AST *sPrev = NULL;
AST *s = chu->chunk.statementFirst; AST *s = chu->chunk.statementFirst;
int effective = 0;
while(s) { while(s) {
if(s->nodeKind == AST_STMT_IF) { if(s->nodeKind == AST_STMT_IF) {
@ -199,25 +201,33 @@ static void dumben_chunk(AST *tlc, AST *chu) {
if(is_xop(e->exprBinOp.operands[0]) == XOP_NOT_XOP) { if(is_xop(e->exprBinOp.operands[0]) == XOP_NOT_XOP) {
e->exprBinOp.operands[0] = xopify(tlc, chu, sPrev, s, e->exprBinOp.operands[0]); e->exprBinOp.operands[0] = xopify(tlc, chu, sPrev, s, e->exprBinOp.operands[0]);
effective = 1;
} }
if(is_xop(e->exprBinOp.operands[1]) == XOP_NOT_XOP) { if(is_xop(e->exprBinOp.operands[1]) == XOP_NOT_XOP) {
e->exprBinOp.operands[1] = xopify(tlc, chu, sPrev, s, e->exprBinOp.operands[1]); e->exprBinOp.operands[1] = xopify(tlc, chu, sPrev, s, e->exprBinOp.operands[1]);
effective = 1;
} }
if(is_xop(e->exprBinOp.operands[0]) == XOP_MEM && is_xop(e->exprBinOp.operands[1]) == XOP_MEM) { if(is_xop(e->exprBinOp.operands[0]) == XOP_MEM && is_xop(e->exprBinOp.operands[1]) == XOP_MEM) {
// Can't have two mems; put one in var // Can't have two mems; put one in var
e->exprBinOp.operands[1] = varify(tlc, chu, sPrev, s, e->exprBinOp.operands[1]); e->exprBinOp.operands[1] = varify(tlc, chu, sPrev, s, e->exprBinOp.operands[1]);
effective = 1;
} }
} else {
s->stmtIf.expression = varify(tlc, chu, sPrev, s, e);
effective = 1;
} }
dumben_chunk(tlc, s->stmtIf.then); effective |= dumben_chunk(tlc, s->stmtIf.then);
} else if(s->nodeKind == AST_STMT_LOOP) { } else if(s->nodeKind == AST_STMT_LOOP) {
dumben_chunk(tlc, s->stmtLoop.body); effective |= dumben_chunk(tlc, s->stmtLoop.body);
} else if(s->nodeKind == AST_STMT_ASSIGN) { } else if(s->nodeKind == AST_STMT_ASSIGN) {
@ -225,6 +235,7 @@ static void dumben_chunk(AST *tlc, AST *chu) {
&& s->stmtAssign.to->nodeKind == AST_EXPR_UNARY_OP && s->stmtAssign.to->exprUnOp.operator == UNOP_DEREF) { && s->stmtAssign.to->nodeKind == AST_EXPR_UNARY_OP && s->stmtAssign.to->exprUnOp.operator == UNOP_DEREF) {
s->stmtAssign.to = varify(tlc, chu, sPrev, s, s->stmtAssign.to); s->stmtAssign.to = varify(tlc, chu, sPrev, s, s->stmtAssign.to);
effective = 1;
} }
} else if(s->nodeKind == AST_STMT_EXPR && s->stmtExpr.expr->nodeKind == AST_EXPR_CALL) { } else if(s->nodeKind == AST_STMT_EXPR && s->stmtExpr.expr->nodeKind == AST_EXPR_CALL) {
@ -234,6 +245,7 @@ static void dumben_chunk(AST *tlc, AST *chu) {
for(int i = 0; i < argCount; i++) { for(int i = 0; i < argCount; i++) {
if(is_xop(s->stmtExpr.expr->exprCall.args[i]) == XOP_NOT_XOP) { if(is_xop(s->stmtExpr.expr->exprCall.args[i]) == XOP_NOT_XOP) {
s->stmtExpr.expr->exprCall.args[i] = xopify(tlc, chu, sPrev, s, s->stmtExpr.expr->exprCall.args[i]); s->stmtExpr.expr->exprCall.args[i] = xopify(tlc, chu, sPrev, s, s->stmtExpr.expr->exprCall.args[i]);
effective = 1;
} }
} }
@ -242,8 +254,10 @@ static void dumben_chunk(AST *tlc, AST *chu) {
sPrev = s; sPrev = s;
s = s->statement.next; s = s->statement.next;
} }
return effective;
} }
void dumben_go(AST* tlc) { void dumben_go(AST* tlc) {
dumben_chunk(tlc, tlc); while(dumben_chunk(tlc, tlc));
} }