diff options
| author | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-06-08 17:10:31 +0800 |
|---|---|---|
| committer | Richard-Qin-X <richard.qin.001@gmail.com> | 2026-06-08 17:10:31 +0800 |
| commit | 72af0dedf9e18911da65b9a5c4906689e868d9fc (patch) | |
| tree | 2ecbad93f96ab8ad7e26dc02cd02d802f853ab97 /rcc-legacy/tests/sysy_lvX/083_brainfk.sy | |
| parent | 8faf6d290d6703b1775e0ad7a8928a4ebea4b11b (diff) | |
| download | rcc-project-72af0dedf9e18911da65b9a5c4906689e868d9fc.tar.gz rcc-project-72af0dedf9e18911da65b9a5c4906689e868d9fc.tar.xz | |
[legacy] Add functional and performance test suites
Diffstat (limited to 'rcc-legacy/tests/sysy_lvX/083_brainfk.sy')
| -rw-r--r-- | rcc-legacy/tests/sysy_lvX/083_brainfk.sy | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/rcc-legacy/tests/sysy_lvX/083_brainfk.sy b/rcc-legacy/tests/sysy_lvX/083_brainfk.sy new file mode 100644 index 0000000..d1ee0c1 --- /dev/null +++ b/rcc-legacy/tests/sysy_lvX/083_brainfk.sy @@ -0,0 +1,73 @@ +/* +a brainfuck interpreter +reference: https://gist.github.com/maxcountryman/1699708 +*/ + +// tape, input buffer, and read/write pointer +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; +} |
