Programming · Uncategorized

Interpreter Bahasa Brainfuck Dalam Bahasa C

Brainfuck? Jangan salah paham dulu, Brainfuck adalah sebuah bahasa pemrograman esoteric yang minimalis. Bahasa Brainfuck hanya memiliki delapan perintah yaitu , . [ ] < > + -. Sebetulnya sudah banyak juga sih yang menulis compiler dan interpreter untuk bahasa ini, tapi saya ingin membuatnya sendiri karena tertarik perintahnya yang sedikit, hihi.

Untuk implementasi interpreter Brainfuck, saya memutuskan menggunakan Bahasa C sebagai permulaan. Di bawah ini adalah kode C untuk interpreter Brainfuck, kurang dari 170 baris, dan itu belum dioptimasi! 😛 Setelah di-compile interpreternya dapat menerima file program Brainfuck. Kalau malas compile sendiri, saya juga sudah siapkan executable untuk Windows di link github yang saya sediakan di bagian bawah. 🙂
https://github.com/Rosmianto/Brainfuck-interpreter

#include &lt;stdio.h&gt; // for basic I/O operation.
#include &lt;stdlib.h&gt; // for memory management.

typedef struct stack {
int value;
struct stack *next;
} stack_node_t;

typedef struct {
stack_node_t *top;
} stack_t;

int pop(stack_t *s);
void push(stack_t *s, int i);
char *bracemap(char *program);
void loadBFtoMemory(FILE *handle);
void process(char c);

const int memorySize = 30000;
char *cell;
int currentCell = 0;
int InstructionPointer = 0;
int i, charCount;
FILE *hInput; // Handle to File object.
char *programArray; // Cleaned up Brainfuck program.
char *braces; // Storage of braces pair location.
char *bracesStack; // temporary array for braces stack.
char _temp;

int main(int argc, char *argv[])
{
// Make sure file path always be specified.
if(argc &lt; 2)
return 0;

cell = calloc(memorySize, sizeof(char));

hInput = fopen(argv[1], &quot;r&quot;);

loadBFtoMemory(hInput);

braces = bracemap(programArray);

while(InstructionPointer &lt; charCount)
{
process(programArray[InstructionPointer]);
InstructionPointer++;
}

// printf(&quot;\n&quot;);

// for(i = 0; i &lt; 20; i++)
// printf(&quot;%d|&quot;, cell[i]);

// printf(&quot;\n&quot;);

free(programArray);
fclose(hInput);
return 0;
}

char *bracemap(char *program)
{
int i, start;
char *temp = calloc(charCount, sizeof(char));
stack_t tempStack = {NULL};

for(i = 0; i &lt; charCount; i++)
{
if(programArray[i] == '[')
{
push(&amp;tempStack, i);
start = i;
}
else if(programArray[i] == ']')
{
int start = pop(&amp;tempStack);
temp[start] = i;
temp[i] = start;
}
}
return temp;
}

void process(char c)
{
switch(c)
{
case '+':
cell[currentCell]++;
break;
case '-':
cell[currentCell]--;
break;
case '&gt;':
currentCell++;
break;
case '&lt;':
currentCell--;
break;
case '.':
printf(&quot;%c&quot;, cell[currentCell]);
break;
case ',':
cell[currentCell] = (char)getchar();
break;
case '[':
InstructionPointer = (cell[currentCell] == 0) ? braces[InstructionPointer] : (InstructionPointer);
break;
case ']':
InstructionPointer = (cell[currentCell] == 0) ? (InstructionPointer) : braces[InstructionPointer];
break;
default: // Ignore other characters.
break;
}
}

void loadBFtoMemory(FILE *handle){
char c;
charCount = 0;
programArray = (char*) malloc((charCount + 1) * sizeof(char));
if(programArray == NULL)
printf(&quot;Memory allocation failed.&quot;);
else
{
c = fgetc(hInput);
while(c != EOF)
{
switch(c)
{
case '+':
case '-':
case '&gt;':
case '&lt;':
case '.':
case ',':
case '[':
case ']':
programArray[charCount++] = c;
realloc(programArray, (charCount + 1) * sizeof(char));
default:
break;
}
c = (char)fgetc(hInput);
}
}
}

int pop(stack_t *s){
stack_node_t *free_node;
int c;

free_node = s-&gt;top;
c = free_node-&gt;value;
s-&gt;top = free_node-&gt;next;
free(free_node);

return c;
}

void push(stack_t *s, int c){
stack_node_t *new_node;
new_node = (stack_node_t *) malloc(sizeof(stack_node_t));
new_node-&gt;value = c;
new_node-&gt;next = s-&gt;top;

s-&gt;top = new_node;
}
Iklan

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s