1 /* Interpreter for Malbolge. */
2 /* '98 Ben Olmstead. */
3 /* */
4 /* Malbolge is the name of Dante's Eighth circle of Hell. This */
5 /* interpreter isn't even Copylefted; I hereby place it in the public */
6 /* domain. Have fun... */
7 /* */
8 /* Note: in keeping with the idea that programming in Malbolge is */
9 /* meant to be hell, there is no debugger. */
10 /* */
11 /* By the way, this code assumes that short is 16 bits. I haven't */
12 /* seen any case where it isn't, but it might happen. If short is */
13 /* longer than 16 bits, it will still work, though it will take up */
14 /* considerably more memory. */
15 /* */
16 /* If you are compiling with a 16-bit Intel compiler, you will need */
17 /* >64K data arrays; this means using the HUGE memory model on most */
18 /* compilers, but MS C, as of 8.00, possibly earlier as well, allows */
19 /* you to specify a custom memory-model; the best model to choose in */
20 /* this case is /Ashd (near code, huge data), I think. */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <ctype.h>
25 #include <string.h>
26
27 #ifdef __GNUC__
28 static inline
29 #endif
30 void exec( unsigned short *mem );
31
32 #ifdef __GNUC__
33 static inline
34 #endif
35 unsigned short op( unsigned short x, unsigned short y );
36
37 const char xlat1[] =
38 "+b(29e*j1VMEKLyC})8&m#~W>qxdRp0wkrUo[D7,XTcA\"lI"
39 ".v%{gJh4G\\-=O@5`_3i<?Z';FNQuY]szf$!BS/|t:Pn6^Ha";
40
41 const char xlat2[] =
42 "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1C"
43 "B6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@";
44
main(int argc,char ** argv)45 int main( int argc, char **argv )
46 {
47 FILE *f;
48 unsigned short i = 0, j;
49 int x;
50 unsigned short *mem;
51 if ( argc != 2 )
52 {
53 fputs( "invalid command line\n", stderr );
54 return ( 1 );
55 }
56 if ( ( f = fopen( argv[1], "r" ) ) == NULL )
57 {
58 fputs( "can't open file\n", stderr );
59 return ( 1 );
60 }
61 #ifdef _MSC_VER
62 mem = (unsigned short *)_halloc( 59049, sizeof(unsigned short) );
63 #else
64 mem = (unsigned short *)malloc( sizeof(unsigned short) * 59049 );
65 #endif
66 if ( mem == NULL )
67 {
68 fclose( f );
69 fputs( "can't allocate memory\n", stderr );
70 return ( 1 );
71 }
72 while ( ( x = getc( f ) ) != EOF )
73 {
74 if ( isspace( x ) ) continue;
75 if ( x < 127 && x > 32 )
76 {
77 if ( strchr( "ji*p</vo", xlat1[( x - 33 + i ) % 94] ) == NULL )
78 {
79 fputs( "invalid character in source file\n", stderr );
80 free( mem );
81 fclose( f );
82 return ( 1 );
83 }
84 }
85 if ( i == 59049 )
86 {
87 fputs( "input file too long\n", stderr );
88 free( mem );
89 fclose( f );
90 return ( 1 );
91 }
92 mem[i++] = x;
93 }
94 fclose( f );
95 while ( i < 59049 ) mem[i] = op( mem[i - 1], mem[i - 2] ), i++;
96 exec( mem );
97 free( mem );
98 return ( 0 );
99 }
100
101 #ifdef __GNUC__
102 static inline
103 #endif
exec(unsigned short * mem)104 void exec( unsigned short *mem )
105 {
106 unsigned short a = 0, c = 0, d = 0;
107 int x;
108 for (;;)
109 {
110 if ( mem[c] < 33 || mem[c] > 126 ) continue;
111 switch ( xlat1[( mem[c] - 33 + c ) % 94] )
112 {
113 case 'j': d = mem[d]; break;
114 case 'i': c = mem[d]; break;
115 case '*': a = mem[d] = mem[d] / 3 + mem[d] % 3 * 19683; break;
116 case 'p': a = mem[d] = op( a, mem[d] ); break;
117 case '<':
118 #if '\n' != 10
119 if ( x == 10 ) putc( '\n', stdout ); else
120 #endif
121 putc( a, stdout );
122 break;
123 case '/':
124 x = getc( stdin );
125 #if '\n' != 10
126 if ( x == '\n' ) a = 10; else
127 #endif
128 if ( x == EOF ) a = 59048; else a = x;
129 break;
130 case 'v': return;
131 }
132 mem[c] = xlat2[mem[c] - 33];
133 if ( c == 59048 ) c = 0; else c++;
134 if ( d == 59048 ) d = 0; else d++;
135 }
136 }
137
138 #ifdef __GNUC__
139 static inline
140 #endif
op(unsigned short x,unsigned short y)141 unsigned short op( unsigned short x, unsigned short y )
142 {
143 unsigned short i = 0, j;
144 static const unsigned short p9[5] =
145 { 1, 9, 81, 729, 6561 };
146 static const unsigned short o[9][9] =
147 {
148 { 4, 3, 3, 1, 0, 0, 1, 0, 0 },
149 { 4, 3, 5, 1, 0, 2, 1, 0, 2 },
150 { 5, 5, 4, 2, 2, 1, 2, 2, 1 },
151 { 4, 3, 3, 1, 0, 0, 7, 6, 6 },
152 { 4, 3, 5, 1, 0, 2, 7, 6, 8 },
153 { 5, 5, 4, 2, 2, 1, 8, 8, 7 },
154 { 7, 6, 6, 7, 6, 6, 4, 3, 3 },
155 { 7, 6, 8, 7, 6, 8, 4, 3, 5 },
156 { 8, 8, 7, 8, 8, 7, 5, 5, 4 },
157 };
158 for ( j = 0; j < 5; j++ )
159 i += o[y / p9[j] % 9][x / p9[j] % 9] * p9[j];
160 return ( i );
161 }
162