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 CHANGED
@@ -1,5 +1,9 @@
1
1
  # Changelog
2
2
 
3
+ ## v0.0.3
4
+
5
+ * Parser and transformer were rewritten. All unknown characters are ignored and considered as comments.
6
+
3
7
  ## v0.0.2
4
8
 
5
9
  * Data cells now are unsigned byte and wrap around on decrement and increment
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"
@@ -20,9 +20,16 @@ module BrainLove
20
20
  input_byte
21
21
  end
22
22
 
23
- rule(:commands) { command.repeat(1) }
24
- rule(:_loop) { (jump_forward >> (commands | _loop).as(:statements).repeat >> jump_backward).as(:loop) }
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(:root) { (commands | _loop).as(:statements).repeat.as(:statements) }
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
- rule(:loop => simple(:_)) do
11
- AST::Loop.new
12
- end
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 :statements => simple(:statements) do
19
- AST::Statements.new([statements])
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
@@ -1,3 +1,3 @@
1
1
  module BrainLove
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -55,6 +55,7 @@ describe BrainLove::Parser do
55
55
  it { should parse('++[]') }
56
56
  it { should parse('[[]]') }
57
57
  it { should parse('[[++]++]>.') }
58
+ it { should parse('this is comments and produces no error') }
58
59
 
59
60
  it { should_not parse('[') }
60
61
  it { should_not parse(']') }
@@ -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([ast_statement([BrainLove::AST::IncrementPointer.new])])) }
10
- it { should transform('<').to(ast_statement([ast_statement([BrainLove::AST::DecrementPointer.new])])) }
11
- it { should transform('+').to(ast_statement([ast_statement([BrainLove::AST::IncrementByte.new])])) }
12
- it { should transform('-').to(ast_statement([ast_statement([BrainLove::AST::DecrementByte.new])])) }
13
- it { should transform('.').to(ast_statement([ast_statement([BrainLove::AST::OutputByte.new])])) }
14
- it { should transform(',').to(ast_statement([ast_statement([BrainLove::AST::InputByte.new])])) }
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([ast_statement([BrainLove::AST::Loop.new()])])) }
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
- ast_statement([
26
- BrainLove::AST::InputByte.new
27
- ]),
28
- ast_statement([
29
- BrainLove::AST::Loop.new(
30
- ast_statement([
31
- BrainLove::AST::OutputByte.new,
32
- BrainLove::AST::InputByte.new
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.2
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-08 00:00:00.000000000 Z
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: '0'
105
+ version: 1.9.2
106
106
  required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  none: false
108
108
  requirements: