https://github.com/mozamimy/kaguya
Kaguya is an implementation of Brainf**k by Ruby
https://github.com/mozamimy/kaguya
brainfuck programming-language ruby
Last synced: about 1 year ago
JSON representation
Kaguya is an implementation of Brainf**k by Ruby
- Host: GitHub
- URL: https://github.com/mozamimy/kaguya
- Owner: mozamimy
- License: mit
- Created: 2017-04-02T10:04:31.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-04-02T11:22:02.000Z (about 9 years ago)
- Last Synced: 2024-04-24T00:43:58.543Z (about 2 years ago)
- Topics: brainfuck, programming-language, ruby
- Language: Ruby
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Kaguya
Kaguya is an implementation of Brainf\*\*k by Ruby.
This implementation consits of following elements,
- Tiny parser
- Tiny compiler
- Tiny virtual stack machine
## Installation
```
$ gem install kaguya
```
## Usage
```
$ kaguya helloworld.bf
```
An option `--debug` helps you to see AST and instruction sequence.
```
$ kaguya --debug helloworld.bf
```
## Technical components
### Parser
https://github.com/mozamimy/kaguya/blob/master/lib/kaguya/parser.rb
The parser parses a BF script one charactor at a time. Errors can be raised by the parser if the script contains invalid corresponding of `[` and `]` or invalid charactors.
Only the root node and `while` nodes have children.
### Virtual machine
https://github.com/mozamimy/kaguya/blob/master/lib/kaguya/vm.rb
Kaguya VM has a two stacks (left\_stack, right\_stack) and has following 9 instructions,
- `forward`: Forward stack, if right\_stack is empty then push 0 to left\_stack else move top of right\_stack to left\_stack.
- `backward`: Backward stack, top data of left\_stack is poped and pushed to right\_stack.
- `increment`: Increment top of left\_stack.
- `decrement`: Decrement top of left\_stack.
- `output`: Output top of left\_stack to standard I/O.
- `input`: Input a value from standard I/O and push it to left\_stack.
- `branch_ifzero N`: Jump to an instruction at `PC - N` if top of left\_stack is 0.
- `branch_unlesszero N`: Jump to an instruction at `PC - N` unless top of left\_stack is 0.
- `leave`: Exit
### Compiler
https://github.com/mozamimy/kaguya/blob/master/lib/kaguya/compiler.rb
Kaguya compiler compiles an AST generated by the parser to an instruction sequence for Kaguya VM.
### Example
```
[20:17:51]mozamimy@queen:kaguya (master) (-'x'-).oO(
> cat example/useless.bf
>>+[<>[+-]]
[20:17:53]mozamimy@queen:kaguya (master) (-'x'-).oO(
> be exe/kaguya --debug example/useless.bf
=== AST ===
#,
@type=:forward>,
#,
@type=:forward>,
#,
@type=:increment>,
#,
@type=:backward>,
#,
@type=:forward>,
#,
@type=:increment>,
#,
@type=:decrement>],
@parent=#,
@type=:while>],
@parent=#,
@type=:while>],
@parent=nil,
@type=:root>
=== ISEQ ===
[[:forward, nil],
[:forward, nil],
[:increment, nil],
[:branch_ifzero, 8],
[:backward, nil],
[:forward, nil],
[:branch_ifzero, 4],
[:increment, nil],
[:decrement, nil],
[:branch_unlesszero, -2],
[:branch_unlesszero, -6],
[:leave, nil]]
=== RUN ===
```
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/mozamimy/kaguya.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).