sysy-data/functional_c/sy/68_brainfk.sy

73 lines
1.7 KiB
Plaintext
Raw Normal View History

2024-06-02 22:04:54 +08:00
#include "sylib.h"
2024-04-14 22:20:29 +08:00
/*
a brainfuck interpreter
reference: https://gist.github.com/maxcountryman/1699708
*/
// tape, input buffer, and read/write pointer
#define TAPE_LEN 65535
#define BUFFER_LEN 32768
// const int TAPE_LEN = 65536, BUFFER_LEN = 32768;
int tape[TAPE_LEN], program[BUFFER_LEN], ptr = 0;
// read the input program
void read_program()
{
int i = 0, len = getint();
while (i < len) {
program[i] = getch();
i = i + 1;
}
program[i] = 0;
}
// interpret the input program
void interpret(int input[])
{
int cur_char, loop, i = 0;
while (input[i]) {
cur_char = input[i];
if (cur_char == 62) {
// '>'
ptr = ptr + 1;
} else if (cur_char == 60) {
// '<'
ptr = ptr - 1;
} else if (cur_char == 43) {
// '+'
tape[ptr] = tape[ptr] + 1;
} else if (cur_char == 45) {
// '-'
tape[ptr] = tape[ptr] - 1;
} else if (cur_char == 46) {
// '.'
putch(tape[ptr]);
} else if (cur_char == 44) {
// ','
tape[ptr] = getch();
} else if (cur_char == 93 && tape[ptr]) {
// ']'
loop = 1;
while (loop > 0) {
i = i - 1;
cur_char = input[i];
if (cur_char == 91) {
// '['
loop = loop - 1;
} else if (cur_char == 93) {
// ']'
loop = loop + 1;
}
}
}
i = i + 1;
}
}
int main()
{
read_program();
interpret(program);
return 0;
}