bfrb 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,6 +1,6 @@
1
1
  = bfrb
2
2
 
3
- bfrb is a simple brainfuck interpreter written in Ruby.
3
+ bfrb is a simple {brainfuck}[http://www.muppetlabs.com/~breadbox/bf] interpreter written in Ruby.
4
4
 
5
5
  == Usage
6
6
 
@@ -8,10 +8,25 @@ To install bfrb:
8
8
 
9
9
  % gem install bfrb
10
10
 
11
- After installation, you can use bfrb in two ways. First, you can pass brainfuck code directly into it, as long as it is wrapped in quotes.
11
+ Once bfrb is installed, you can use it to interpret your brainfuck code.
12
+ First, you can pass brainfuck code directly into it, as long as it is wrapped in quotes.
12
13
 
13
14
  % bfrb ",[.,]"
14
15
 
15
16
  Or, you can specify a file with the -f parameter:
16
17
 
17
18
  % bfrb -f test.bf
19
+
20
+ You can also start an interactive interpreter by running bfrb without any parameters
21
+
22
+ % bfrb
23
+
24
+ == Things To Do
25
+
26
+ * Compiler
27
+ * Unit Testing
28
+
29
+ == History
30
+
31
+ v 0.1.1 - Added REPL.
32
+ v 0.1.0 - Initial release. Allows running code passed in or from a file.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/bfrb.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{bfrb}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Chris ONeal"]
12
- s.date = %q{2011-06-03}
12
+ s.date = %q{2011-06-06}
13
13
  s.default_executable = %q{bfrb}
14
14
  s.description = %q{bfrb is a brainfuck interpreter written in Ruby.}
15
15
  s.email = %q{ctoneal@gmail.com}
data/bin/bfrb CHANGED
@@ -4,22 +4,31 @@ require File.expand_path(File.join(File.dirname(__FILE__), "..", "lib", "bfrb.rb
4
4
 
5
5
  def usage
6
6
  puts "Usage:"
7
+ puts "Start interactive evaluation: bfrb"
7
8
  puts "Pass code in as first command line parameter (in quotes): bfrb \",[.,]\""
8
9
  puts "Specify a file to read: bfrb -f test.bf"
10
+ puts "View this message: bfrb -u"
9
11
  end
10
12
 
11
13
  bfrb = BfRb::Base.new
12
14
  # if the command line params specify a file, run it
13
15
  # otherwise attempt to run code passed in as a param
14
- if ARGV.length > 1
15
- if ARGV[0] == "-f"
16
- bfrb.load_file(ARGV[1])
17
- else
18
- puts "Invalid arguments"
16
+ if ARGV.length == 0
17
+ puts "Starting interactive evaluation"
18
+ bfrb.repl
19
+ else
20
+ case ARGV[0]
21
+ when "-u"
19
22
  usage
23
+ when "-f"
24
+ if ARGV.length > 1
25
+ bfrb.load_file(ARGV[1])
26
+ else
27
+ puts "Invalid arguments"
28
+ usage
29
+ end
30
+ else
31
+ puts "Interpreting code"
32
+ bfrb.run(ARGV[0])
20
33
  end
21
- elsif ARGV.length > 0
22
- bfrb.run(ARGV[0])
23
- else
24
- usage
25
- end
34
+ end
@@ -4,6 +4,8 @@ module BfRb
4
4
  # interprets brainfuck code
5
5
  class Interpreter
6
6
 
7
+ attr_reader :program_counter, :memory_counter
8
+
7
9
  # initialize the interpreter
8
10
  def initialize
9
11
  @memory = Memory.new
@@ -19,17 +21,20 @@ module BfRb
19
21
  @loop_stack = []
20
22
  end
21
23
 
24
+ # run a given piece of code
22
25
  def run(code)
23
- @code = code
26
+ @code += code
24
27
  evaluate_code
25
28
  end
26
29
 
30
+ # evaluate each instruction in the current code
27
31
  def evaluate_code
28
32
  while ((0 <= @program_counter) and (@program_counter < @code.length))
29
33
  evaluate_instruction
30
34
  end
31
35
  end
32
36
 
37
+ # evaluate an individual instruction
33
38
  def evaluate_instruction
34
39
  case @code[@program_counter]
35
40
  when ">"
@@ -41,16 +46,16 @@ module BfRb
41
46
  @memory_counter = 0
42
47
  end
43
48
  when "+"
44
- @memory.set(@memory_counter, @memory.get(@memory_counter) + 1)
49
+ @memory.set(@memory_counter, current_memory + 1)
45
50
  when "-"
46
- @memory.set(@memory_counter, @memory.get(@memory_counter) - 1)
51
+ @memory.set(@memory_counter, current_memory - 1)
47
52
  when "."
48
- print @memory.get(@memory_counter).chr
53
+ print current_memory.chr
49
54
  when ","
50
55
  input = $stdin.gets.getbyte(0)
51
56
  @memory.set(@memory_counter, input)
52
57
  when "["
53
- if @memory.get(@memory_counter) != 0
58
+ if current_memory != 0
54
59
  @loop_stack.push @program_counter
55
60
  else
56
61
  bracket_counter = 1
@@ -65,11 +70,16 @@ module BfRb
65
70
  end
66
71
  when "]"
67
72
  matching_bracket = @loop_stack.pop
68
- if @memory.get(@memory_counter) != 0
73
+ if current_memory != 0
69
74
  @program_counter = matching_bracket - 1
70
75
  end
71
76
  end
72
77
  @program_counter += 1
73
78
  end
79
+
80
+ # returns the value in the current memory cell
81
+ def current_memory
82
+ return @memory.get(@memory_counter)
83
+ end
74
84
  end
75
85
  end
data/lib/bfrb.rb CHANGED
@@ -19,5 +19,28 @@ module BfRb
19
19
  code = File.open(file_path) { |f| f.read }
20
20
  run(code)
21
21
  end
22
+
23
+ # interactive read-evaluate-print loop
24
+ def repl
25
+ puts "'exit' leaves the REPL"
26
+ puts "'mem' displays the value in the current cell of memory"
27
+ puts "'clear' clears everything from memory"
28
+ while true
29
+ puts ""
30
+ print "bf> "
31
+ input = gets.chomp
32
+ case input
33
+ when "exit"
34
+ puts "Exiting"
35
+ break
36
+ when "mem"
37
+ puts "Cell: #{@interpreter.memory_counter} Value: #{@interpreter.current_memory}"
38
+ when "clear"
39
+ @interpreter.initialize_environment
40
+ else
41
+ @interpreter.run(input)
42
+ end
43
+ end
44
+ end
22
45
  end
23
46
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bfrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-06-03 00:00:00.000000000 -05:00
12
+ date: 2011-06-06 00:00:00.000000000 -05:00
13
13
  default_executable: bfrb
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: shoulda
17
- requirement: &8092368 !ruby/object:Gem::Requirement
17
+ requirement: &9389616 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :development
24
24
  prerelease: false
25
- version_requirements: *8092368
25
+ version_requirements: *9389616
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: bundler
28
- requirement: &8091288 !ruby/object:Gem::Requirement
28
+ requirement: &9388836 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 1.0.0
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *8091288
36
+ version_requirements: *9388836
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: jeweler
39
- requirement: &8089740 !ruby/object:Gem::Requirement
39
+ requirement: &9388188 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ~>
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 1.5.2
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *8089740
47
+ version_requirements: *9388188
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rcov
50
- requirement: &8073336 !ruby/object:Gem::Requirement
50
+ requirement: &9387504 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ! '>='
@@ -55,7 +55,7 @@ dependencies:
55
55
  version: '0'
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *8073336
58
+ version_requirements: *9387504
59
59
  description: bfrb is a brainfuck interpreter written in Ruby.
60
60
  email: ctoneal@gmail.com
61
61
  executables:
@@ -93,7 +93,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  segments:
95
95
  - 0
96
- hash: -476785
96
+ hash: 1070569779
97
97
  required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  none: false
99
99
  requirements: