Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. /* Interpreter for Malbolge. */
  2. /* <s>'98 Ben Olmstead.</s> modified by MilkyWay90 */
  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 <malloc.h>
  26. #include <string.h>
  27.  
  28. #ifdef __GNUC__
  29. static inline
  30. #endif
  31. void exec( unsigned short *mem );
  32.  
  33. #ifdef __GNUC__
  34. static inline
  35. #endif
  36. unsigned short op( unsigned short x, unsigned short y );
  37.  
  38. const char xlat1[] =
  39. "+b(29e*j1VMEKLyC})8&m#~W>qxdRp0wkrUo[D7,XTcA\"lI"
  40. ".v%{gJh4G\\-=O@5`_3i<?Z';FNQuY]szf$!BS/|t:Pn6^Ha";
  41.  
  42. const char xlat2[] =
  43. "5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1C"
  44. "B6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@";
  45.  
  46. static char normalized_malbolge[59049] = "";
  47.  
  48. int main( int argc, char **argv )
  49. {
  50. FILE *f;
  51. unsigned short i = 0, j;
  52. int x;
  53. unsigned short *mem;
  54. if ( argc != 2 )
  55. {
  56. fputs( "invalid command line\n", stderr );
  57. return ( 1 );
  58. }
  59. if ( ( f = fopen( argv[1], "r" ) ) == NULL )
  60. {
  61. fputs( "can't open file\n", stderr );
  62. return ( 1 );
  63. }
  64. #ifdef _MSC_VER
  65. mem = (unsigned short *)_halloc( 59049, sizeof(unsigned short) );
  66. #else
  67. mem = (unsigned short *)malloc( sizeof(unsigned short) * 59049 );
  68. #endif
  69. if ( mem == NULL )
  70. {
  71. fclose( f );
  72. fputs( "can't allocate memory\n", stderr );
  73. return ( 1 );
  74. }
  75. while ( ( x = getc( f ) ) != EOF )
  76. {
  77. if ( isspace( x ) ) continue;
  78. if ( x < 127 && x > 32 )
  79. {
  80. if ( strchr( "ji*p</vo", xlat1[( x - 33 + i ) % 94] ) == NULL )
  81. {
  82. fputs( "invalid character in source file\n", stderr );
  83. free( mem );
  84. fclose( f );
  85. return ( 1 );
  86. } else {
  87. normalized_malbolge[i] = xlat1[( x - 33 + i ) % 94];
  88. normalized_malbolge[i+1] = '\0';
  89. printf("%d\n", xlat1[( x - 33 + i ) % 94]);
  90. printf("%d\n", normalized_malbolge);
  91. }
  92. }
  93. if ( i == 59049 )
  94. {
  95. fputs( "input file too long\n", stderr );
  96. free( mem );
  97. fclose( f );
  98. return ( 1 );
  99. }
  100. mem[i++] = x;
  101. }
  102. fclose( f );
  103. while ( i < 59049 ) mem[i] = op( mem[i - 1], mem[i - 2] ), i++;
  104. exec( mem );
  105. free( mem );
  106. return ( 0 );
  107. }
  108.  
  109. #ifdef __GNUC__
  110. static inline
  111. #endif
  112. void exec( unsigned short *mem )
  113. {
  114. unsigned short a = 0, c = 0, d = 0;
  115. int x;
  116. for (;;)
  117. {
  118. //system("@cls||clear");
  119. char str[6];
  120. sprintf(str, "A: %d, ", a);
  121. printf("%s", str);
  122. sprintf(str, "C: %d, ", c);
  123. printf("%s", str);
  124. sprintf(str, "D: %d.\n", d);
  125. printf("%s", str);
  126. printf("Normalized Malbolge:\n %d \n", normalized_malbolge);
  127. printf("Press Enter to continue: ");
  128. getchar();
  129.  
  130. if ( mem[c] < 33 || mem[c] > 126 ) continue;
  131. switch ( xlat1[( mem[c] - 33 + c ) % 94] )
  132. {
  133. case 'j': d = mem[d]; break;
  134. case 'i': c = mem[d]; break;
  135. case '*': a = mem[d] = mem[d] / 3 + mem[d] % 3 * 19683; break;
  136. case 'p': a = mem[d] = op( a, mem[d] ); break;
  137. case '<':
  138. #if '\n' != 10
  139. if ( x == 10 ) putc( '\n', stdout ); else
  140. #endif
  141. putc( a, stdout );
  142. break;
  143. case '/':
  144. x = getc( stdin );
  145. #if '\n' != 10
  146. if ( x == '\n' ) a = 10; else
  147. #endif
  148. if ( x == EOF ) a = 59048; else a = x;
  149. break;
  150. case 'v': return;
  151. }
  152. mem[c] = xlat2[mem[c] - 33];
  153. if ( c == 59048 ) c = 0; else c++;
  154. if ( d == 59048 ) d = 0; else d++;
  155. }
  156. }
  157.  
  158. #ifdef __GNUC__
  159. static inline
  160. #endif
  161. unsigned short op( unsigned short x, unsigned short y )
  162. {
  163. unsigned short i = 0, j;
  164. static const unsigned short p9[5] =
  165. { 1, 9, 81, 729, 6561 };
  166. static const unsigned short o[9][9] =
  167. {
  168. { 4, 3, 3, 1, 0, 0, 1, 0, 0 },
  169. { 4, 3, 5, 1, 0, 2, 1, 0, 2 },
  170. { 5, 5, 4, 2, 2, 1, 2, 2, 1 },
  171. { 4, 3, 3, 1, 0, 0, 7, 6, 6 },
  172. { 4, 3, 5, 1, 0, 2, 7, 6, 8 },
  173. { 5, 5, 4, 2, 2, 1, 8, 8, 7 },
  174. { 7, 6, 6, 7, 6, 6, 4, 3, 3 },
  175. { 7, 6, 8, 7, 6, 8, 4, 3, 5 },
  176. { 8, 8, 7, 8, 8, 7, 5, 5, 4 },
  177. };
  178. for ( j = 0; j < 5; j++ )
  179. i += o[y / p9[j] % 9][x / p9[j] % 9] * p9[j];
  180. return ( i );
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement