brainfuckrb 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +44 -0
- data/Rakefile +2 -0
- data/bin/brainfuck +11 -0
- data/brainfuckrb.gemspec +18 -0
- data/lib/brainfuckrb.rb +56 -0
- data/lib/brainfuckrb/version.rb +3 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Luka Dornhecker
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
# Brainfuckrb
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Isntall it with RubyGems:
|
6
|
+
|
7
|
+
$ gem install brainfuckrb
|
8
|
+
|
9
|
+
## Usage
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
#!/usr/bin/env ruby
|
13
|
+
# hello_brainfuck.rb
|
14
|
+
|
15
|
+
hello_brainfuck = <<-eos
|
16
|
+
++++++++++[>+++++++>++++++++++>+++>+<<<<-]>++.>+.+++++++..+++.>++.<<+++
|
17
|
+
++++++++++++.>.+++.------.--------.>+.>.
|
18
|
+
eos
|
19
|
+
bf Brainfuckrb::Brainfuck.new(hello_brainfuck)
|
20
|
+
bf.run
|
21
|
+
```
|
22
|
+
|
23
|
+
$ ./hello_brainfuck.rb
|
24
|
+
Hello World!
|
25
|
+
|
26
|
+
Or run brainfuck programs directly:
|
27
|
+
|
28
|
+
$ brainfuckrb hello.bf
|
29
|
+
$ Hello World!
|
30
|
+
|
31
|
+
Only the brainfuck control characters ```><+-.,[]``` are interpreted.
|
32
|
+
Others will be ignored.
|
33
|
+
|
34
|
+
## Known bugs
|
35
|
+
|
36
|
+
* Input does not work
|
37
|
+
|
38
|
+
## Contributing
|
39
|
+
|
40
|
+
1. Fork it
|
41
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
42
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
43
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
44
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/bin/brainfuck
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "brainfuckrb"
|
4
|
+
|
5
|
+
abort "No input" if ARGV.count < 1
|
6
|
+
abort "Input file not found" if not File.exist? ARGV[0]
|
7
|
+
abort "Input is not a file" if not File.file? ARGV[0]
|
8
|
+
|
9
|
+
File.open(ARGV[0], "r") do |file|
|
10
|
+
Brainfuckrb::Brainfuck.new(file.read).run
|
11
|
+
end
|
data/brainfuckrb.gemspec
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/brainfuckrb/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Luka Dornhecker"]
|
6
|
+
gem.email = ["luka.dornhecker@gmail.com"]
|
7
|
+
gem.description = %q{Brainfuck implementation in Ruby}
|
8
|
+
gem.summary = %q{Brainfuck implementation in Ruby}
|
9
|
+
gem.homepage = "http://lukad.github.com/brainfuckrb"
|
10
|
+
gem.license = "MIT"
|
11
|
+
|
12
|
+
gem.files = `git ls-files`.split($\)
|
13
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
14
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
15
|
+
gem.name = "brainfuckrb"
|
16
|
+
gem.require_paths = ["lib"]
|
17
|
+
gem.version = Brainfuckrb::VERSION
|
18
|
+
end
|
data/lib/brainfuckrb.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require "brainfuckrb/version"
|
2
|
+
require "io/console"
|
3
|
+
|
4
|
+
module Brainfuckrb
|
5
|
+
|
6
|
+
class Brainfuck
|
7
|
+
def initialize(program)
|
8
|
+
@program = program
|
9
|
+
reset
|
10
|
+
end
|
11
|
+
|
12
|
+
def reset
|
13
|
+
@mem = [0] * 65535
|
14
|
+
@p = 0
|
15
|
+
@pc = 0
|
16
|
+
end
|
17
|
+
|
18
|
+
def run(stop = nil)
|
19
|
+
_run(stop)
|
20
|
+
result = @mem[@p]
|
21
|
+
reset
|
22
|
+
return result
|
23
|
+
end
|
24
|
+
|
25
|
+
def _run(stop = nil)
|
26
|
+
stop ||= @program.length
|
27
|
+
|
28
|
+
while @pc < stop
|
29
|
+
case @program[@pc].chr
|
30
|
+
when ">" then @p += 1
|
31
|
+
when "<" then @p -= 1
|
32
|
+
when "+" then @mem[@p] += 1
|
33
|
+
when "-" then @mem[@p] -= 1
|
34
|
+
when "." then STDOUT.putc @mem[@p].chr
|
35
|
+
when "," then @mem[@p] = STDIN.readchar.ord
|
36
|
+
when "["
|
37
|
+
depth = 1
|
38
|
+
start = _end = @pc
|
39
|
+
while depth != 0
|
40
|
+
_end += 1
|
41
|
+
depth += 1 if @program[_end] == "["
|
42
|
+
depth -= 1 if @program[_end] == "]"
|
43
|
+
end
|
44
|
+
while @mem[@p] != 0
|
45
|
+
@pc = start + 1
|
46
|
+
_run(_end)
|
47
|
+
end
|
48
|
+
@pc = _end
|
49
|
+
when "]" then raise "x:y: not expecting \']\'"
|
50
|
+
end
|
51
|
+
@pc += 1
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: brainfuckrb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.2
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Luka Dornhecker
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-07-14 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Brainfuck implementation in Ruby
|
15
|
+
email:
|
16
|
+
- luka.dornhecker@gmail.com
|
17
|
+
executables:
|
18
|
+
- brainfuck
|
19
|
+
extensions: []
|
20
|
+
extra_rdoc_files: []
|
21
|
+
files:
|
22
|
+
- .gitignore
|
23
|
+
- Gemfile
|
24
|
+
- LICENSE
|
25
|
+
- README.md
|
26
|
+
- Rakefile
|
27
|
+
- bin/brainfuck
|
28
|
+
- brainfuckrb.gemspec
|
29
|
+
- lib/brainfuckrb.rb
|
30
|
+
- lib/brainfuckrb/version.rb
|
31
|
+
homepage: http://lukad.github.com/brainfuckrb
|
32
|
+
licenses:
|
33
|
+
- MIT
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
none: false
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
requirements: []
|
51
|
+
rubyforge_project:
|
52
|
+
rubygems_version: 1.8.15
|
53
|
+
signing_key:
|
54
|
+
specification_version: 3
|
55
|
+
summary: Brainfuck implementation in Ruby
|
56
|
+
test_files: []
|