nbp / holyjit

Generic purpose Just-In-time compiler for Rust.
https://holyjit.org/
Mozilla Public License 2.0
1.51k stars 26 forks source link

Brainfuck compiler is incorrect #11

Closed mateon1 closed 7 years ago

mateon1 commented 7 years ago

Ignoring the unimplemented opcodes, I believe you got the memory pointer backwards.

Currently, you index memory on the singular value cell, and never actually access memory for other operations (here, the naïve looping implementation).

How does the interpreter look like now: memory = [0u8; 256]; value = 0; +, - => memory[value] = memory[value] +- 1, >, < => value = value +- 1, [ => if value != 0 { continue; } else { /* Complex forwards scan */ }, ] => if value == 0 { continue; } else { /* Complex backwards scan */ }

How it should look like: memory = Vec<u8>::new(); ptr = 0 +, - => memory[ptr] = memory[ptr] +- 1, >, < => ptr = ptr +- 1 [And handle Vec resizing], [ => if memory[ptr] != 0 { continue; } else { /* Complex forwards scan */ }, ] => if memory[ptr] == 0 { continue; } else { /* Complex backwards scan */ }

nbp commented 7 years ago

Thanks for reporting this issue. Unless you want to provide a fix and try HolyJit by your-self, I can make a patch for it next week.

nbp commented 7 years ago

This issue is fixed by commit b37f074. Thanks to @yblein 's changes in #16