Skip to content

Commit

Permalink
Initial commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
sprang committed Feb 24, 2018
0 parents commit 3d110a3
Show file tree
Hide file tree
Showing 15 changed files with 727 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target
4 changes: 4 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]

name = "malbolge"
version = "0.0.1"
authors = ["Steve Sprang <[email protected]>"]
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 Steve Sprang

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Malbolge
========

This is an implementation of the
[Malbolge](http://en.wikipedia.org/wiki/Malbolge) interpreter in Rust.

Running
-------

Run the binary against the sample Malbolge programs in the `programs` directory:

`cargo run --release programs/99bottles.mb`

Malbolge Programs
-----------------
hello-world.mb by [Andrew Cooke](http://www.acooke.org/malbolge.html)

99bottles.mb by [Hisashi Iizawa](http://www.99-bottles-of-beer.net/language-malbolge-995.html)

copy.mb by [Lou Scheffer](http://www.lscheffer.com/malbolge.shtml)
246 changes: 246 additions & 0 deletions programs/99bottles.mb

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions programs/cat-wikipedia.mb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(=BA#9"=<;:3y7x54-21q/p-,+*)"!h%B0/.
~P<
<:(8&
66#"!~}|{zyxwvu
gJ%
1 change: 1 addition & 0 deletions programs/copy.mb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
D'BA@?>=<;:9876543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543210/.-,+*)('&%$#"!~}|{zyxwvutsrqponmlkjihgfedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDC&_������������������������������������佽�������������������������������������������������������������������������������������������������������������������������������������佽�������������������������������������������������������������������������
Empty file added programs/empty.mb
Empty file.
1 change: 1 addition & 0 deletions programs/hello-world-wikipedia.mb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(=<`#9]~6ZY32Vx/4Rs+0No-&Jk)"Fh}|Bcy?`=*z]Kw%oG4UUS0/@-ejc(:'8dc
1 change: 1 addition & 0 deletions programs/hello-world.mb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(=<`$9]7<5YXz7wT.3,+O/o'K%$H"'~D|#z@b=`{^Lx8%$Xmrkpohm-kNi;gsedcba`_^]\[ZYXWVUTSRQPONMLKJIHGFEDCBA@?>=<;:9876543s+O<oLm
2 changes: 2 additions & 0 deletions programs/invalid-char.mb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
b'`;$9!=IlXFiVwwvtPO0)pon%IHGFDV|dd@Q=+^:('&Y$#m!1S|.QOO=v('98$65aCB}0i.Tw+QPU'7qK#I20jiDVgG
S(bt<%@#!7~|4{y1xv.us+rp(om%lj"ig}fS"cx``uz]rwvYnslkTonPfOjiKgJeG]\EC_X]@[Z<R;VU7S6QP2N1LK-I
1 change: 1 addition & 0 deletions programs/too-short.mb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(
162 changes: 162 additions & 0 deletions reference/malbolge.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/* 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. */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <malloc.h>
#include <string.h>

#ifdef __GNUC__
static inline
#endif
void exec( unsigned short *mem );

#ifdef __GNUC__
static inline
#endif
unsigned short op( unsigned short x, unsigned short y );

const char xlat1[] =
"+b(29e*j1VMEKLyC})8&m#~W>qxdRp0wkrUo[D7,XTcA\"lI"
".v%{gJh4G\\-=O@5`_3i<?Z';FNQuY]szf$!BS/|t:Pn6^Ha";

const char xlat2[] =
"5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1C"
"B6v^=I_0/8|jsb9m<.TVac`uY*MK'X~xDl}REokN:#?G\"i@";

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++;
}
}

#ifdef __GNUC__
static inline
#endif
unsigned short op( unsigned short x, unsigned short y )
{
unsigned short i = 0, j;
static const unsigned short p9[5] =
{ 1, 9, 81, 729, 6561 };
static const unsigned short o[9][9] =
{
{ 4, 3, 3, 1, 0, 0, 1, 0, 0 },
{ 4, 3, 5, 1, 0, 2, 1, 0, 2 },
{ 5, 5, 4, 2, 2, 1, 2, 2, 1 },
{ 4, 3, 3, 1, 0, 0, 7, 6, 6 },
{ 4, 3, 5, 1, 0, 2, 7, 6, 8 },
{ 5, 5, 4, 2, 2, 1, 8, 8, 7 },
{ 7, 6, 6, 7, 6, 6, 4, 3, 3 },
{ 7, 6, 8, 7, 6, 8, 4, 3, 5 },
{ 8, 8, 7, 8, 8, 7, 5, 5, 4 },
};
for ( j = 0; j < 5; j++ )
i += o[y / p9[j] % 9][x / p9[j] % 9] * p9[j];
return ( i );
}
Loading

0 comments on commit 3d110a3

Please sign in to comment.