brain_love 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/brain_love.gemspec +2 -0
- data/lib/brain_love/parser.rb +10 -3
- data/lib/brain_love/transformer.rb +8 -9
- data/lib/brain_love/version.rb +1 -1
- data/spec/brain_love/parser_spec.rb +1 -0
- data/spec/brain_love/transformer_spec.rb +32 -18
- metadata +3 -3
data/CHANGELOG.md
CHANGED
data/brain_love.gemspec
CHANGED
@@ -15,6 +15,8 @@ Gem::Specification.new do |gem|
|
|
15
15
|
gem.require_paths = ["lib"]
|
16
16
|
gem.version = BrainLove::VERSION
|
17
17
|
|
18
|
+
gem.required_ruby_version = '>= 1.9.2'
|
19
|
+
|
18
20
|
gem.add_development_dependency "rspec"
|
19
21
|
gem.add_development_dependency "rake"
|
20
22
|
gem.add_runtime_dependency "parslet"
|
data/lib/brain_love/parser.rb
CHANGED
@@ -20,9 +20,16 @@ module BrainLove
|
|
20
20
|
input_byte
|
21
21
|
end
|
22
22
|
|
23
|
-
rule
|
24
|
-
|
23
|
+
rule :comment do
|
24
|
+
(command | jump_forward | jump_backward).absnt? >> any.as(:comment)
|
25
|
+
end
|
26
|
+
|
27
|
+
rule :_loop do
|
28
|
+
(jump_forward >> root >> jump_backward).as(:loop)
|
29
|
+
end
|
25
30
|
|
26
|
-
rule
|
31
|
+
rule :root do
|
32
|
+
(comment | command | _loop).repeat.as(:statements)
|
33
|
+
end
|
27
34
|
end
|
28
35
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'pp'
|
2
|
+
|
1
3
|
module BrainLove
|
2
4
|
class Transformer < Parslet::Transform
|
3
5
|
rule(:increment_pointer => simple(:_)) { AST::IncrementPointer.new }
|
@@ -7,20 +9,17 @@ module BrainLove
|
|
7
9
|
rule(:output_byte => simple(:_)) { AST::OutputByte.new }
|
8
10
|
rule(:input_byte => simple(:_)) { AST::InputByte.new }
|
9
11
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
rule(:loop => sequence(:statements)) do
|
15
|
-
AST::Loop.new(statements.first)
|
12
|
+
# Hm, still feels ugly but better than was :)
|
13
|
+
rule(:comment => simple(:_)) do
|
14
|
+
nil
|
16
15
|
end
|
17
16
|
|
18
|
-
rule
|
19
|
-
AST::
|
17
|
+
rule(:loop => simple(:statements)) do
|
18
|
+
AST::Loop.new(statements)
|
20
19
|
end
|
21
20
|
|
22
21
|
rule :statements => sequence(:statements) do
|
23
|
-
AST::Statements.new(statements)
|
22
|
+
AST::Statements.new(statements.compact)
|
24
23
|
end
|
25
24
|
end
|
26
25
|
end
|
data/lib/brain_love/version.rb
CHANGED
@@ -6,33 +6,47 @@ end
|
|
6
6
|
|
7
7
|
describe BrainLove::Transformer do
|
8
8
|
describe 'simple commands' do
|
9
|
-
it { should transform('>').to(ast_statement([
|
10
|
-
it { should transform('<').to(ast_statement([
|
11
|
-
it { should transform('+').to(ast_statement([
|
12
|
-
it { should transform('-').to(ast_statement([
|
13
|
-
it { should transform('.').to(ast_statement([
|
14
|
-
it { should transform(',').to(ast_statement([
|
9
|
+
it { should transform('>').to(ast_statement([BrainLove::AST::IncrementPointer.new])) }
|
10
|
+
it { should transform('<').to(ast_statement([BrainLove::AST::DecrementPointer.new])) }
|
11
|
+
it { should transform('+').to(ast_statement([BrainLove::AST::IncrementByte.new])) }
|
12
|
+
it { should transform('-').to(ast_statement([BrainLove::AST::DecrementByte.new])) }
|
13
|
+
it { should transform('.').to(ast_statement([BrainLove::AST::OutputByte.new])) }
|
14
|
+
it { should transform(',').to(ast_statement([BrainLove::AST::InputByte.new])) }
|
15
15
|
end
|
16
16
|
|
17
17
|
describe 'loop' do
|
18
|
-
it { should transform('[]').to(ast_statement([
|
18
|
+
it { should transform('[]').to(ast_statement([BrainLove::AST::Loop.new(ast_statement([]))])) }
|
19
19
|
end
|
20
20
|
|
21
21
|
describe 'commands and loop' do
|
22
22
|
it do
|
23
23
|
should transform(',[.,]').to(
|
24
24
|
ast_statement([
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
25
|
+
BrainLove::AST::InputByte.new,
|
26
|
+
BrainLove::AST::Loop.new(
|
27
|
+
ast_statement([
|
28
|
+
BrainLove::AST::OutputByte.new,
|
29
|
+
BrainLove::AST::InputByte.new
|
30
|
+
])
|
31
|
+
)
|
32
|
+
])
|
33
|
+
)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'comments' do
|
38
|
+
it { should transform('[should not change ast]').to(ast_statement([BrainLove::AST::Loop.new(ast_statement([]))])) }
|
39
|
+
|
40
|
+
it do
|
41
|
+
should transform(",\n[\n.\n,\n]").to(
|
42
|
+
ast_statement([
|
43
|
+
BrainLove::AST::InputByte.new,
|
44
|
+
BrainLove::AST::Loop.new(
|
45
|
+
ast_statement([
|
46
|
+
BrainLove::AST::OutputByte.new,
|
47
|
+
BrainLove::AST::InputByte.new
|
48
|
+
])
|
49
|
+
)
|
36
50
|
])
|
37
51
|
)
|
38
52
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: brain_love
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-04-
|
12
|
+
date: 2012-04-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -102,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
102
102
|
requirements:
|
103
103
|
- - ! '>='
|
104
104
|
- !ruby/object:Gem::Version
|
105
|
-
version:
|
105
|
+
version: 1.9.2
|
106
106
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
107
|
none: false
|
108
108
|
requirements:
|