Hola gente, les dejo este interpete de brainfuck que programe, espero sus criticas y consejos. Gracias.
Código (C) [Seleccionar]
#include <stdio.h>
#include <stdlib.h>
void Brainfuck(char *pcData, unsigned long dwData, unsigned char *ucStack, unsigned long dwStack, unsigned long *dwStackPtr);
int main()
{
char ucData[] = "++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++++++++++++++.>.+++.------.--------.>+.>.";
unsigned long dwStackPtr = 0;
unsigned long dwStack = 16384;
void *pvStack = malloc(dwStack);
memset(pvStack, 0, dwStack);
Brainfuck(ucData, sizeof(ucData) - 1, pvStack, dwStack, &dwStackPtr);
free(pvStack);
return 0;
}
void Brainfuck(char *pcData, unsigned long dwData, unsigned char *ucStack, unsigned long dwStack, unsigned long *dwStackPtr)
{
if(!pcData || !dwData || !ucStack || !dwStack || !dwStackPtr) {return;}
unsigned long i;
for(i = 0; i < dwData; i++)
{
switch(pcData[i])
{
case '>': if(*dwStackPtr < dwStack) {(*dwStackPtr)++;} break;
case '<': if(*dwStackPtr) {(*dwStackPtr)--;} break;
case '+': ucStack[*dwStackPtr]++; break;
case '-': ucStack[*dwStackPtr]--; break;
case '.': putchar(ucStack[*dwStackPtr]); break;
case ',': ucStack[*dwStackPtr] = getchar(); break;
case '[':
{
unsigned long j = 0;
int iLoop = 0;
do {
if(pcData[i + j] == '[') {iLoop++;} else if(pcData[i + j] == ']') {iLoop--;}
j++;
} while((j < dwData - i) && iLoop);
while(ucStack[(*dwStackPtr)]) {Brainfuck(&pcData[i + 1], j - 1, ucStack, dwStack, dwStackPtr);}
i += j;
break;
}
}
}
}