view interps/egobf/src/optim2.c @ 12518:2d8fe55c6e65 draft default tip

<int-e> learn The password of the month is release incident pilot.
author HackEso <hackeso@esolangs.org>
date Sun, 03 Nov 2024 00:31:02 +0000
parents 859f9b4339e6
children
line wrap: on
line source

#include "egobfi.h"
#include "optim2.h"

void optim2()
{
    struct bfi *cur;
    
    /* second-level optimization, searches for add-to loops */
    for (pptr = 0; prog[pptr].cmd != FIN; pptr++) {
        if (prog[pptr].cmd == LPO) {
            cur = prog + pptr;
            /* two types of add-tos:
               >x +x <x -x
               -x >x +x <x
               */
            if (cur[1].cmd == RHT &&
                cur[3].cmd == RHT &&
                cur[1].arg1 == cur[3].arg1 * -1 &&
                cur[5].cmd == LPC) {
                if (cur[2].cmd == ADD &&
                    cur[4].cmd == ADD &&
                    cur[4].arg1 == -1) {
                    /* YAY! */
                    cur->cmd = ADDTO;
                    cur->arg1 = cur[1].arg1;
                    cur->arg2 = cur[2].arg1;
                    
                    cur[1].cmd = NIL;
                    cur[2].cmd = NIL;
                    cur[3].cmd = NIL;
                    cur[4].cmd = NIL;
                    cur[5].cmd = NIL;
                    pptr += 5;
                }
            } else if (cur[2].cmd == RHT &&
                       cur[4].cmd == RHT &&
                       cur[2].arg1 == cur[4].arg1 * -1 &&
                       cur[5].cmd == LPC) {
                if (cur[1].cmd == ADD &&
                    cur[3].cmd == ADD &&
                    cur[1].arg1 == -1) {
                    /* YAY! */
                    cur->cmd = ADDTO;
                    cur->arg1 = cur[2].arg1;
                    cur->arg2 = cur[3].arg1;
                    
                    cur[1].cmd = NIL;
                    cur[2].cmd = NIL;
                    cur[3].cmd = NIL;
                    cur[4].cmd = NIL;
                    cur[5].cmd = NIL;
                    pptr += 5;
                }
            }
        }
    }
    pptr = 0;
}