brainfuck 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile.lock +8 -23
- data/Rakefile +12 -10
- data/Readme.md +17 -11
- data/bin/brainfuck +1 -1
- data/brainfuck.gemspec +5 -6
- data/lib/brainfuck.rb +5 -18
- data/lib/brainfuck/ast.rb +126 -18
- data/lib/brainfuck/code_loader.rb +31 -0
- data/lib/brainfuck/compiler.rb +81 -0
- data/lib/brainfuck/lexer.rb +32 -0
- data/lib/brainfuck/main.rb +99 -0
- data/lib/brainfuck/parser.rb +9 -24
- data/lib/brainfuck/stages.rb +170 -0
- data/lib/brainfuck/version.rb +1 -1
- data/test/acceptance/acceptance_test.rb +75 -0
- data/test/brainfuck/ast_test.rb +7 -0
- data/test/brainfuck/lexer_test.rb +21 -0
- data/test/brainfuck/parser_test.rb +16 -0
- data/test/test_helper.rb +10 -0
- metadata +32 -53
- data/lib/brainfuck/interpreter.rb +0 -20
- data/lib/brainfuck/stack.rb +0 -51
- data/spec/acceptance/acceptance_spec.rb +0 -105
- data/spec/brainfuck/ast_spec.rb +0 -36
- data/spec/brainfuck/interpreter_spec.rb +0 -25
- data/spec/brainfuck/parser_spec.rb +0 -27
- data/spec/brainfuck/stack_spec.rb +0 -81
- data/spec/brainfuck_spec.rb +0 -23
- data/spec/spec_helper.rb +0 -18
@@ -1,81 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Brainfuck
|
4
|
-
describe Stack do
|
5
|
-
|
6
|
-
describe "#current" do
|
7
|
-
it 'returns the current value of the stack' do
|
8
|
-
subject.current.should == 0
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
describe "#fwd" do
|
13
|
-
it 'advanced the pointer' do
|
14
|
-
subject.fwd
|
15
|
-
subject.instance_variable_get(:@pointer).should == 1
|
16
|
-
end
|
17
|
-
end
|
18
|
-
|
19
|
-
describe "#bwd" do
|
20
|
-
it 'decreases the pointer' do
|
21
|
-
subject.instance_variable_set(:@pointer, 4)
|
22
|
-
subject.bwd
|
23
|
-
subject.instance_variable_get(:@pointer).should == 3
|
24
|
-
end
|
25
|
-
context "if the pointer is trying to get below zero" do
|
26
|
-
it 'raises' do
|
27
|
-
subject.instance_variable_set(:@pointer, 0)
|
28
|
-
expect {
|
29
|
-
subject.bwd
|
30
|
-
}.to raise_error(RuntimeError, "Tried to access cell -1.")
|
31
|
-
end
|
32
|
-
end
|
33
|
-
end
|
34
|
-
|
35
|
-
describe "#inc" do
|
36
|
-
it 'increases the current cell' do
|
37
|
-
subject.instance_variable_set(:@stack, [4])
|
38
|
-
subject.inc
|
39
|
-
subject.current.should == 5
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
describe "#dec" do
|
44
|
-
it 'decreases the current cell' do
|
45
|
-
subject.instance_variable_set(:@stack, [4])
|
46
|
-
subject.dec
|
47
|
-
subject.current.should == 3
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
describe "#puts" do
|
52
|
-
it 'prints the current character' do
|
53
|
-
subject.stub_chain('current.chr').and_return 'A'
|
54
|
-
$stdout.should_receive(:print).with 'A'
|
55
|
-
subject.puts
|
56
|
-
end
|
57
|
-
end
|
58
|
-
|
59
|
-
describe "#gets" do
|
60
|
-
it 'prints the current character' do
|
61
|
-
subject.should_receive(:get_character).and_return 97
|
62
|
-
subject.gets
|
63
|
-
subject.current.should == 97
|
64
|
-
end
|
65
|
-
end
|
66
|
-
|
67
|
-
describe "#to_a" do
|
68
|
-
it 'returns the stack array' do
|
69
|
-
subject.to_a.should == [0]
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
describe "#clear" do
|
74
|
-
it 'clears the stack' do
|
75
|
-
subject.should_receive(:initialize)
|
76
|
-
subject.clear
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
end
|
data/spec/brainfuck_spec.rb
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe Brainfuck do
|
4
|
-
|
5
|
-
describe ".run" do
|
6
|
-
it 'runs the code and returns the stack' do
|
7
|
-
code = double('code')
|
8
|
-
parsed = double('parsed')
|
9
|
-
ast = [double('node'), double('node')]
|
10
|
-
|
11
|
-
Brainfuck::Interpreter.stack.should_receive(:clear)
|
12
|
-
|
13
|
-
Brainfuck::Parser.should_receive(:clean).with("code").and_return(code)
|
14
|
-
Brainfuck::Parser.stub_chain('new.parse').with(code).and_return parsed
|
15
|
-
Brainfuck::Interpreter.stub_chain('new.apply').with(parsed).and_return ast
|
16
|
-
|
17
|
-
ast.each { |n| n.should_receive(:eval) }
|
18
|
-
|
19
|
-
subject.run("code").should === Brainfuck::Interpreter.stack.to_a
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
end
|
data/spec/spec_helper.rb
DELETED
@@ -1,18 +0,0 @@
|
|
1
|
-
require 'bundler'
|
2
|
-
require 'simplecov'
|
3
|
-
SimpleCov.start do
|
4
|
-
add_group "Lib", "lib"
|
5
|
-
end
|
6
|
-
|
7
|
-
begin
|
8
|
-
Bundler.setup(:default, :development)
|
9
|
-
rescue Bundler::BundlerError => e
|
10
|
-
$stderr.puts e.message
|
11
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
12
|
-
exit e.status_code
|
13
|
-
end
|
14
|
-
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
15
|
-
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
16
|
-
|
17
|
-
require 'brainfuck'
|
18
|
-
require 'rspec'
|