/* Interpreter for Malbolge. */ /* '98 Ben Olmstead. */ /* */ /* Malbolge is the name of Dante's Eighth circle of Hell. This */ /* interpreter isn't even Copylefted; I hereby place it in the public */ /* domain. Have fun... */ /* */ /* Note: in keeping with the idea that programming in Malbolge is */ /* meant to be hell, there is no debugger. */ /* */ /* By the way, this code assumes that short is 16 bits. I haven't */ /* seen any case where it isn't, but it might happen. If short is */ /* longer than 16 bits, it will still work, though it will take up */ /* considerably more memory. */ /* */ /* If you are compiling with a 16-bit Intel compiler, you will need */ /* >64K data arrays; this means using the HUGE memory model on most */ /* compilers, but MS C, as of 8.00, possibly earlier as well, allows */ /* you to specify a custom memory-model; the best model to choose in */ /* this case is /Ashd (near code, huge data), I think. */
int main( int argc, char **argv ) { FILE *f; unsigned short i = 0, j; int x; unsigned short *mem; if ( argc != 2 ) { fputs( "invalid command line\n", stderr ); return ( 1 ); } if ( ( f = fopen( argv[1], "r" ) ) == NULL ) { fputs( "can't open file\n", stderr ); return ( 1 ); } #ifdef _MSC_VER mem = (unsigned short *)_halloc( 59049, sizeof(unsigned short) ); #else mem = (unsigned short *)malloc( sizeof(unsigned short) * 59049 ); #endif if ( mem == NULL ) { fclose( f ); fputs( "can't allocate memory\n", stderr ); return ( 1 ); } while ( ( x = getc( f ) ) != EOF ) { if ( isspace( x ) ) continue; if ( x < 127 && x > 32 ) { if ( strchr( "ji*p</vo", xlat1[( x - 33 + i ) % 94] ) == NULL ) { fputs( "invalid character in source file\n", stderr ); free( mem ); fclose( f ); return ( 1 ); } } if ( i == 59049 ) { fputs( "input file too long\n", stderr ); free( mem ); fclose( f ); return ( 1 ); } mem[i++] = x; } fclose( f ); while ( i < 59049 ) mem[i] = op( mem[i - 1], mem[i - 2] ), i++; exec( mem ); free( mem ); return ( 0 ); }
#ifdef __GNUC__ static inline #endif void exec( unsigned short *mem ) { unsigned short a = 0, c = 0, d = 0; int x; for (;;) { if ( mem[c] < 33 || mem[c] > 126 ) continue; switch ( xlat1[( mem[c] - 33 + c ) % 94] ) { case 'j': d = mem[d]; break; case 'i': c = mem[d]; break; case '*': a = mem[d] = mem[d] / 3 + mem[d] % 3 * 19683; break; case 'p': a = mem[d] = op( a, mem[d] ); break; case '<': #if '\n' != 10 if ( x == 10 ) putc( '\n', stdout ); else #endif putc( a, stdout ); break; case '/': x = getc( stdin ); #if '\n' != 10 if ( x == '\n' ) a = 10; else #endif if ( x == EOF ) a = 59048; else a = x; break; case 'v': return; } mem[c] = xlat2[mem[c] - 33]; if ( c == 59048 ) c = 0; else c++; if ( d == 59048 ) d = 0; else d++; } }