view interps/egobf/src/egobfi.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 <stdio.h>
#include <stdlib.h>
#include <string.h>

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

#ifndef EGOBFC
#include "bfi.h"
#else
#include "bfc.h"
#endif
#include "egobfi.h"
#ifndef EGOBFC2M
#include "optimize.h"
#else
#include "c2m.h"
#endif
/*#include "optim2.h"*/

int wrapping = 1;
int debug = 0;
int eof = 0;
int compile = 0;
int unicode = 0;

#ifdef EGOBFC
int bytewidth = 1;
#else
USEINT bwmax;
USEINT *mem;
int mlen;
USEINT *mend;
USEINT *mptr;
#endif

char *iprog;
int iplen;
int ipptr;

#ifndef EGOBFC2M
struct bfi *prog;
int plen;
int pptr;
#else
void (*prog)();
void (*tprog)();
int plen;
int pptr;
#endif

int main(int argc, char **argv)
{
    int i;
    FILE *bfin = stdin;
    
    for (i = 1; i < argc; i++) {
        if (argv[i][0] == '-') {
            if (!strcmp(argv[i], "-wrap")) {
                /* set wrapping on or off */
                i++;
                if (!strcmp(argv[i], "off"))
                    wrapping = 0;
            } else if (!strcmp(argv[i], "-debug")) {
                debug = 1;
            } else if (!strcmp(argv[i], "-eof")) {
                /* choose eof mode */
                i++;
                switch (argv[i][0]) {
                    case '-':
                        eof = -1;
                        break;
                        
                    case 'n':
                    case 'N':
                        eof = 1; /* 1 means no change */
                        break;
                }
#ifdef HAVE_WCHAR_H
            } else if (!strcmp(argv[i], "-unicode")) {
                i++;
                if (!strcmp(argv[i], "on")) {
                    /* make sure it's supported at this level */
                    int chk;
#ifndef EGOBFC
                    chk = sizeof(USEINT);
#else
                    chk = bytewidth;
#endif
                    if(sizeof(wint_t) > chk) {
                        fprintf(stderr, "Sorry, I can't support unicode with this bitwidth!\n");
                        exit(1);
                    }
                    unicode = 1;
                }
#endif
#ifdef EGOBFC
            } else if (!strcmp(argv[i], "-width")) {
                i++;
                bytewidth = atoi(argv[i]) >> 3;
#endif
            } else if(!strcmp(argv[i], "-h") ||
                      !strcmp(argv[i], "-help") ||
                      !strcmp(argv[i], "--help")) {
                /* display help */
                fprintf(stderr, "Use: %s [options] [file]\n"
                        "Options:\n"
                        " -eof {0|-|n}\n"
                        "   set EOF mode: 0, -1 or no-change (respectively)\n"
                        "   [default: 0]\n"
                        " -debug\n"
                        "   activate the # command [default off]\n"
#ifndef EGOBFC2M
                        " -unicode {on|off}\n"
                        "   set unicode mode on or off [default off]\n"
#ifndef EGOBFC
                        " -wrap {on|off}\n"
                        "   set wrappong on or off [default on]\n"
#endif
#endif

#ifdef EGOBFC
                        " -width {8|16|32}\n"
                        "   set the bitwidth [default 8]\n"
#endif
                        ,
#ifndef EGOBFC
#ifndef EGOBFC2M
                        /* egobfi */ "egobfi{width}"
#else
                        /* egobfc2m */ "egobfc2m"
#endif
#else
                        /* egobfc */ "egobfc"
#endif
                        );
                exit(0);
            } else {
                fprintf(stderr, "Argument not recognized: %s\n", argv[i]);
                exit(1);
            }
        } else {
            /* this is our input file */
            bfin = fopen(argv[i], "r");
            if (bfin == NULL) {
                perror(argv[i]);
                exit(1);
            }
        }
    }
    
    /* get our max */
#ifndef EGOBFC
    bwmax = (USEINT) -1;
#endif
    
    /* initialize our memory */
#ifndef EGOBFC
    mem = (USEINT *) malloc(30000 * sizeof(USEINT));
    mlen = 30000;
    mend = mem + mlen;
    mptr = mem;
#endif
    iprog = (char *) malloc(30000);
    iplen = 30000;
    ipptr = 0;
#ifndef EGOBFC2M
    prog = (struct bfi *) malloc(30000 * sizeof(struct bfi));
    plen = 30000;
    pptr = 0;
#else
    prog = (void *) malloc(250000);
    tprog = prog;
    plen = 250000;
    pptr = 0;
#endif
    
    /* 0 it all */
#ifndef EGOBFC
    memset(mem, 0, mlen * sizeof(USEINT));
#endif
    memset(iprog, 0, iplen);
#ifndef EGOBFC2M
    memset(prog, 0, plen * sizeof(struct bfi));
#endif
    
    /* read in our BF */
    while (!feof(bfin) && !ferror(bfin)) {
        iprog[ipptr] = getc(bfin);
        if (ISBF(iprog[ipptr])) {
            ipptr++;
            CHKIPSZ(ipptr)
        }
    }
    ipptr = 0;
    
#ifndef EGOBFC2M
    /* optimize */
    optimize();
    /*optim2(); This seems to slow it down more than anything ...*/
    
#ifndef EGOBFC
    /* and interpret */
    bf_interpret();
#else
    /* and compile */
    bf_compile();
#endif
#else
    /* compile to memory and run */
    c2m();
#endif
    
    return 0;
}