Execute Brain****/C: Difference between revisions

no edit summary
(Oops, this is incorrect too.)
No edit summary
Line 216:
//We are done. Give an awesome goodness signal back!
return FINISHED;
}
</lang>
 
==Version 3==
This is a modified code of Version 1. It happens to be Standard C.
 
<lang c>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
void bf_run(char *s) {
int len = strlen(s), d = 0;
char *tape = calloc(len, sizeof(char));
for (; d >= 0 && *s; s++) {
if (*s == '[') { //if there is a loop
char *p = ++s;
while(*p != ']') { //take care of the loop part
int c = *p;
switch(c) {
case '+': tape[d]++; break;
case '-': tape[d]--; break;
case '>': d++; break;
case '<': d--; break;
case '.': putchar(tape[d]); break;
case ',': tape[d] = getchar(); break;
default: break;
}
p++;
if(*p == ']' && tape[d] != 0) p = s; //the loop stops when the cell has value equal to 0
}
s = p + 1;
}
#define CASE(a, b) if (*s == a) { b; continue; }
CASE('+', tape[d]++);
CASE('-', tape[d]--);
CASE('>', d++);
CASE('<', d--);
CASE('.', putchar(tape[d]));
CASE(',', tape[d] = getchar());
}
free(tape);
}
 
int main(void) {
bf_run("--------[-->+++<]>.------------.---.--[--->+<]>-.----[->++++<]>+.++++."
"------------.------.++++++++.-[++>---<]>+.[->+++<]>++.[--->+<]>----.---."
"++++++++.---------.-[->+++++<]>-.++[->+++<]>.+++++++++.+++++++++.[---->+<]"
">++.-[--->++<]>.+++++++++++.--------.+++.+++.+[---->+<]>+++.+++++[->+++<]>"
".+++++++.+[->+++<]>.+++++++++++++.[-->+++++<]>+++.---[->++++<]>.----------"
"--.---.--[--->+<]>-.++[--->++<]>.-----------.+[----->+<]>.-.-[---->+<]>++."
"+[->+++<]>+.+++++++++++.--------.--[->+++<]>-.,"); //the quick brown fox jumps over the lazy dog.
return 0;
}
</lang>
Anonymous user