brainfuck_ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: aab2eca7a1517f1379a14eab6f7a6c82b6bb0c35e32aaac0c7fa375014a83fdd
4
+ data.tar.gz: 890127ded5ceb4153732b40d1c67d0c0a4e667c51424bffbe431b1449126bb5f
5
+ SHA512:
6
+ metadata.gz: 11dfa2271fede9934b4ea66d8bf6262f4bfe6bbd9bd0a5c8b4b290f55776dda2744102ed4938a7961cef552d85875f5c658f17b02ee44b493d2213aed156d052
7
+ data.tar.gz: 290c20f7c32ce17745baa5456bd77db5698873b4d9fff250f36ddc811a56670e4890bb03116c9ddfccfb84caed686094098320c7cdaf930f27150a8b6fb2f4c7
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,18 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.7.3
3
+ NewCops: enable
4
+
5
+ Layout/LineLength:
6
+ Max: 120
7
+
8
+ Metrics/CyclomaticComplexity:
9
+ Max: 15
10
+
11
+ Style/RescueModifier:
12
+ Enabled: false
13
+
14
+ Style/StringLiterals:
15
+ Enabled: false
16
+
17
+ Style/StringLiteralsInInterpolation:
18
+ Enabled: false
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.7.3
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-09-01
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in brainfuck_ruby.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ gem "rspec", "~> 3.0"
11
+
12
+ gem "rubocop", "~> 1.7"
data/Gemfile.lock ADDED
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ brainfuck_ruby (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ diff-lcs (1.4.4)
11
+ parallel (1.20.1)
12
+ parser (3.0.2.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.0.0)
15
+ rake (13.0.6)
16
+ regexp_parser (2.1.1)
17
+ rexml (3.2.5)
18
+ rspec (3.10.0)
19
+ rspec-core (~> 3.10.0)
20
+ rspec-expectations (~> 3.10.0)
21
+ rspec-mocks (~> 3.10.0)
22
+ rspec-core (3.10.1)
23
+ rspec-support (~> 3.10.0)
24
+ rspec-expectations (3.10.1)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.10.0)
27
+ rspec-mocks (3.10.2)
28
+ diff-lcs (>= 1.2.0, < 2.0)
29
+ rspec-support (~> 3.10.0)
30
+ rspec-support (3.10.2)
31
+ rubocop (1.20.0)
32
+ parallel (~> 1.10)
33
+ parser (>= 3.0.0.0)
34
+ rainbow (>= 2.2.2, < 4.0)
35
+ regexp_parser (>= 1.8, < 3.0)
36
+ rexml
37
+ rubocop-ast (>= 1.9.1, < 2.0)
38
+ ruby-progressbar (~> 1.7)
39
+ unicode-display_width (>= 1.4.0, < 3.0)
40
+ rubocop-ast (1.11.0)
41
+ parser (>= 3.0.1.1)
42
+ ruby-progressbar (1.11.0)
43
+ unicode-display_width (2.0.0)
44
+
45
+ PLATFORMS
46
+ x86_64-linux
47
+
48
+ DEPENDENCIES
49
+ brainfuck_ruby!
50
+ rake (~> 13.0)
51
+ rspec (~> 3.0)
52
+ rubocop (~> 1.7)
53
+
54
+ BUNDLED WITH
55
+ 2.2.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Keegan Leitz
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ # BrainfuckRuby
2
+
3
+ A brainfuck interpreter written in Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'brainfuck_ruby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install brainfuck_ruby
20
+
21
+ ## Usage
22
+
23
+ ```rb
24
+ hello_world_in_brainfuck = <<~BRAINFUCK
25
+ +++++ +++
26
+ [
27
+ > ++++
28
+ [
29
+ > ++
30
+ > +++
31
+ > +++
32
+ > +
33
+ <<<< -
34
+ ]
35
+ > +
36
+ > +
37
+ > -
38
+ >> +
39
+ [
40
+ <
41
+ ]
42
+ < -
43
+ ]
44
+ >> .
45
+ > --- . +++++ ++ . . +++ .
46
+ >> .
47
+ < - .
48
+ < . +++ . ----- - . ----- --- .
49
+ >> + .
50
+ > ++ .
51
+ BRAINFUCK
52
+
53
+ BrainfuckRuby::Interpreter.new(hello_world_in_brainfuck).execute!
54
+ # Hello World!
55
+ # => nil
56
+ ```
57
+
58
+ ## Development
59
+
60
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
61
+
62
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
63
+
64
+ ## Contributing
65
+
66
+ Bug reports and pull requests are welcome on GitHub at https://github.com/kjleitz/brainfuck_ruby.
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "brainfuck_ruby"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/brainfuck_ruby/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "brainfuck_ruby"
7
+ spec.version = BrainfuckRuby::VERSION
8
+ spec.authors = ["Keegan Leitz"]
9
+ spec.email = ["kjleitz@gmail.com"]
10
+
11
+ spec.summary = "brainfuck interpreter written in ruby"
12
+ spec.description = "brainfuck interpreter written in ruby"
13
+ spec.homepage = "https://github.com/kjleitz/brainfuck_ruby"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.7.3"
16
+
17
+ spec.metadata["allowed_push_host"] = "https://rubygems.org"
18
+
19
+ spec.metadata["homepage_uri"] = spec.homepage
20
+ spec.metadata["source_code_uri"] = "https://github.com/kjleitz/brainfuck_ruby"
21
+
22
+ # Specify which files should be added to the gem when it is released.
23
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
24
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
25
+ `git ls-files -z`.split("\x0").reject do |f|
26
+ (f == __FILE__) || f.match(%r{\A(?:(?:test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
27
+ end
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ # Uncomment to register a new dependency of your gem
34
+ # spec.add_dependency "example-gem", "~> 1.0"
35
+
36
+ # For more information and examples about making a new gem, checkout our
37
+ # guide at: https://bundler.io/guides/creating_gem.html
38
+ end
@@ -0,0 +1,130 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrainfuckRuby
4
+ # Interprets brainfuck code. Initialize with a string containing some
5
+ # brainfuck, and then call `#execute!` on the instance to execute that code.
6
+ class Interpreter
7
+ attr_reader :options, :cell_values, :code, :cursor
8
+ attr_accessor :current_cell, :nesting
9
+
10
+ def initialize(brainfuck_code)
11
+ @code = brainfuck_code
12
+ reset!
13
+ end
14
+
15
+ def reset!
16
+ @cell_values = Hash.new(0)
17
+ @current_cell = 0
18
+ @cursor = 0
19
+ @nesting = 0
20
+ end
21
+
22
+ def execute!
23
+ loop do
24
+ instruction = current_instruction
25
+ break if instruction.nil?
26
+
27
+ handle_instruction!(instruction)
28
+ next_instruction!
29
+ end
30
+
31
+ reset!
32
+ nil
33
+ end
34
+
35
+ def handle_instruction!(instruction)
36
+ case instruction
37
+ when ">" then right!
38
+ when "<" then left!
39
+ when "+" then increment!
40
+ when "-" then decrement!
41
+ when "[" then goto_closing_brace! if current_cell_value.zero?
42
+ when "]" then goto_opening_brace! if current_cell_value.positive?
43
+ when "." then print!
44
+ when "," then read!
45
+ end
46
+ end
47
+
48
+ def cursor=(value)
49
+ raise "Cursor position cannot go below zero" if value.negative?
50
+
51
+ @cursor = value
52
+ end
53
+
54
+ def current_instruction
55
+ code[cursor]
56
+ end
57
+
58
+ def current_cell_value
59
+ cell_values[current_cell]
60
+ end
61
+
62
+ def current_cell_value=(value)
63
+ cell_values[current_cell] = value % 256
64
+ end
65
+
66
+ def current_cell_character
67
+ current_cell_value.chr
68
+ end
69
+
70
+ def next_instruction!
71
+ self.cursor += 1
72
+ end
73
+
74
+ def right!
75
+ self.current_cell += 1
76
+ end
77
+
78
+ def left!
79
+ self.current_cell -= 1
80
+ end
81
+
82
+ def increment!
83
+ self.current_cell_value += 1
84
+ end
85
+
86
+ def decrement!
87
+ self.current_cell_value -= 1
88
+ end
89
+
90
+ def goto_closing_brace!
91
+ goto_matching_brace_to_the :right
92
+ end
93
+
94
+ def goto_opening_brace!
95
+ goto_matching_brace_to_the :left
96
+ end
97
+
98
+ def print!
99
+ print(current_cell_character)
100
+ end
101
+
102
+ def read!
103
+ input = gets.chomp
104
+ self.current_cell_value = numeric?(input) ? input.to_i : input.ord
105
+ end
106
+
107
+ def numeric?(value)
108
+ !!Float(value) rescue false
109
+ end
110
+
111
+ private
112
+
113
+ # `direction` should be `:right` or `:left`
114
+ def goto_matching_brace_to_the(direction)
115
+ cursor_modifier = { right: 1, left: -1 }[direction]
116
+ nesting = 0
117
+
118
+ loop do
119
+ case current_instruction
120
+ when "[" then nesting += 1
121
+ when "]" then nesting -= 1
122
+ end
123
+
124
+ break if nesting.zero?
125
+
126
+ self.cursor += cursor_modifier
127
+ end
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module BrainfuckRuby
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "brainfuck_ruby/version"
4
+ require_relative "brainfuck_ruby/interpreter"
5
+
6
+ module BrainfuckRuby
7
+ class Error < StandardError; end
8
+ # Your code goes here...
9
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: brainfuck_ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Keegan Leitz
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-02 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: brainfuck interpreter written in ruby
14
+ email:
15
+ - kjleitz@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".rspec"
21
+ - ".rubocop.yml"
22
+ - ".ruby-version"
23
+ - CHANGELOG.md
24
+ - Gemfile
25
+ - Gemfile.lock
26
+ - LICENSE.txt
27
+ - README.md
28
+ - Rakefile
29
+ - bin/console
30
+ - bin/setup
31
+ - brainfuck_ruby.gemspec
32
+ - lib/brainfuck_ruby.rb
33
+ - lib/brainfuck_ruby/interpreter.rb
34
+ - lib/brainfuck_ruby/version.rb
35
+ homepage: https://github.com/kjleitz/brainfuck_ruby
36
+ licenses:
37
+ - MIT
38
+ metadata:
39
+ allowed_push_host: https://rubygems.org
40
+ homepage_uri: https://github.com/kjleitz/brainfuck_ruby
41
+ source_code_uri: https://github.com/kjleitz/brainfuck_ruby
42
+ post_install_message:
43
+ rdoc_options: []
44
+ require_paths:
45
+ - lib
46
+ required_ruby_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 2.7.3
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ requirements: []
57
+ rubygems_version: 3.1.6
58
+ signing_key:
59
+ specification_version: 4
60
+ summary: brainfuck interpreter written in ruby
61
+ test_files: []