view interps/egobf/src/bfi.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

/*
 * Copyright (c) 2005  Gregor Richards
 *
 * This file is part of egobfi.
 * 
 * egobfi is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * egobfi is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with egobfi; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include "../config.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef HAVE_WCHAR_H
#include <wchar.h>
#endif

#include "bfi.h"
#include "egobfi.h"

void bf_interpret()
{
    int input, cur, iseof;
    struct bfi *bfic;
    
    while (prog[pptr].cmd != FIN) {
        /*printf("%d\n", pptr);*/
        bfic = prog + pptr;
        switch (bfic->cmd) {
            case ADD:
                *mptr += bfic->arg1;
                pptr++;
                break;
                
            case RHT:
                mptr += bfic->arg1;
                CHKMSZ(mptr)
                pptr++;
                break;
                
            case LPO:
                if (*mptr) {
                    /* if it's true, go into the loop */
                    pptr++;
                } else {
                    /* if it's false, jump to the end */
                    pptr = bfic->arg1 + 1;
                }
                break;
                
            case LPC:
                /* just jump to the beginning */
                pptr = bfic->arg1;
                break;
                
            case OUT:
                /* output a character */
#ifdef HAVE_WCHAR_H
                if (unicode) {
                    putwchar(*mptr);
                } else {
                    putchar(*mptr);
                }
#else
                putchar(*mptr);
#endif
                
                fflush(stdout);
                pptr++;
                break;
                
            case INP:
                /* input a character */
                iseof = 0;
#ifdef HAVE_WCHAR_H
                if (unicode) {
                    input = (int) getwchar();
                    iseof = (input == (int) WEOF);
                } else {
                    input = getchar();
                    iseof = (input == EOF);
                }
#else
                input = getchar();
                iseof = (input == EOF);
#endif
                
                if (iseof) {
                    switch (eof) {
                        case 0:
                            *mptr = 0;
                            break;
                            
                        case -1:
                            *mptr = -1;
                            break;
                    }
                } else {
                    *mptr = input;
                }
                pptr++;
                break;                
            
            case DBG:
                pptr++;
                if (debug) {
                    cur = mptr - mem - 10;
                    if (cur < 0) cur = 0;
                    printf("%d:", cur);
                    for (; cur < mptr - mem + 40; cur++) {
                        if (cur == mptr - mem) putchar('*');
                        CHKMSZ(mem + cur)
                        printf("%d|", (int) mem[cur]);
                    }
                    putchar('\n');
                }
                break;
                
                /* INTERNAL COMMANDS */
            case ZRO:
                *mptr = 0;
                pptr++;
                break;
                
            case ADDTO:
                CHKMSZ(mptr + bfic->arg1)
                mptr[bfic->arg1] += bfic->arg2 * *mptr;
                *mptr = 0;
                pptr++;
                break;
                
            default:
                pptr++;
        }
    }
}