subaltern 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.txt +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +32 -0
- data/Rakefile +85 -0
- data/TODO.txt +32 -0
- data/lib/subaltern.rb +39 -0
- data/lib/subaltern/context.rb +95 -0
- data/lib/subaltern/errors.rb +78 -0
- data/lib/subaltern/evaluator.rb +597 -0
- data/lib/subaltern/kernel.rb +64 -0
- data/lib/subaltern/version.rb +28 -0
- data/spec/array_spec.rb +74 -0
- data/spec/basics_spec.rb +125 -0
- data/spec/boolean_spec.rb +129 -0
- data/spec/context_spec.rb +23 -0
- data/spec/control_flow_spec.rb +251 -0
- data/spec/evil_spec.rb +106 -0
- data/spec/fixnum_spec.rb +20 -0
- data/spec/functions_spec.rb +123 -0
- data/spec/hash_spec.rb +88 -0
- data/spec/loops_spec.rb +108 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/string_spec.rb +26 -0
- data/spec/support/subaltern_helper.rb +9 -0
- data/subaltern.gemspec +34 -0
- metadata +122 -0
data/spec/hash_spec.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
|
5
|
+
describe Subaltern do
|
6
|
+
|
7
|
+
context Hash do
|
8
|
+
|
9
|
+
describe '{}' do
|
10
|
+
|
11
|
+
it 'returns the hash' do
|
12
|
+
|
13
|
+
Subaltern.eval('{}').should == {}
|
14
|
+
Subaltern.eval('{ 1 => 2, 3 => 4 }').should == { 1 => 2, 3 => 4 }
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '#[]' do
|
19
|
+
|
20
|
+
it 'works' do
|
21
|
+
|
22
|
+
Subaltern.eval('{}["a"]').should == nil
|
23
|
+
Subaltern.eval('{ "a" => 1 }["a"]').should == 1
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
describe '.{key} (javascripty)' do
|
28
|
+
|
29
|
+
it 'works' do
|
30
|
+
|
31
|
+
Subaltern.eval('{ "a" => 1 }.a').should == 1
|
32
|
+
Subaltern.eval('{ "length" => "zero" }.length').should == 1
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
describe '#each' do
|
37
|
+
|
38
|
+
it 'works' do
|
39
|
+
|
40
|
+
Subaltern.eval('''
|
41
|
+
s = []
|
42
|
+
{ "a" => 1, "b" => 2 }.each { |k, v| s << "#{k}:#{v}" }
|
43
|
+
s.join(", ")
|
44
|
+
''').should == 'a:1, b:2'
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
describe '#collect' do
|
49
|
+
|
50
|
+
it 'works' do
|
51
|
+
|
52
|
+
Subaltern.eval('''
|
53
|
+
{ "a" => 1, "b" => 2 }.collect do |k, v| "#{k}:#{v}"; end.join(", ")
|
54
|
+
''').should == 'a:1, b:2'
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe '#inject' do
|
59
|
+
|
60
|
+
it 'works' do
|
61
|
+
|
62
|
+
Subaltern.eval(%{
|
63
|
+
{ "a" => 1, "b" => 2 }.inject(0) { |sum, (k, v)| sum + v }
|
64
|
+
}).should == 3
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '#select' do
|
69
|
+
|
70
|
+
it 'works' do
|
71
|
+
|
72
|
+
if ruby19?
|
73
|
+
|
74
|
+
Subaltern.eval('''
|
75
|
+
{ "a" => 1, "b" => 2, "c" => 3 }.select { |k, v| v.odd? }
|
76
|
+
''').should == { 'a' => 1, 'c' => 3 }
|
77
|
+
|
78
|
+
else
|
79
|
+
|
80
|
+
Subaltern.eval('''
|
81
|
+
{ "a" => 1, "b" => 2, "c" => 3 }.select { |k, v| v.odd? }
|
82
|
+
''').should == [ [ 'a', 1 ], [ 'c', 3 ] ]
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
data/spec/loops_spec.rb
ADDED
@@ -0,0 +1,108 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
|
5
|
+
describe Subaltern do
|
6
|
+
|
7
|
+
describe 'while' do
|
8
|
+
|
9
|
+
it 'works' do
|
10
|
+
|
11
|
+
Subaltern.eval(%{
|
12
|
+
x = nil
|
13
|
+
while true do
|
14
|
+
x = :ok
|
15
|
+
break
|
16
|
+
end
|
17
|
+
x
|
18
|
+
}).should == :ok
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'works as modifier' do
|
22
|
+
|
23
|
+
Subaltern.eval(%{
|
24
|
+
x = nil
|
25
|
+
(x = :ok; break) while true
|
26
|
+
x
|
27
|
+
}).should == :ok
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe 'until' do
|
32
|
+
|
33
|
+
it 'works' do
|
34
|
+
|
35
|
+
Subaltern.eval(%{
|
36
|
+
x = nil
|
37
|
+
until false do
|
38
|
+
x = :ok
|
39
|
+
break
|
40
|
+
end
|
41
|
+
x
|
42
|
+
}).should == :ok
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'works as modifier' do
|
46
|
+
|
47
|
+
Subaltern.eval(%{
|
48
|
+
x = nil
|
49
|
+
(x = :ok; break) until false
|
50
|
+
x
|
51
|
+
}).should == :ok
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
describe 'for x in xs' do
|
56
|
+
|
57
|
+
it 'works' do
|
58
|
+
|
59
|
+
Subaltern.eval(%{
|
60
|
+
a = []
|
61
|
+
for e in [ 1, 2, 3, 4 ]
|
62
|
+
a << e
|
63
|
+
end
|
64
|
+
a
|
65
|
+
}).should == [ 1, 2, 3, 4 ]
|
66
|
+
end
|
67
|
+
|
68
|
+
it 'does not create a new context' do
|
69
|
+
|
70
|
+
Subaltern.eval(%{
|
71
|
+
for i in [ 1 ]
|
72
|
+
a = i
|
73
|
+
end
|
74
|
+
a
|
75
|
+
}).should == 1
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe 'for k,v in h' do
|
80
|
+
|
81
|
+
it 'works' do
|
82
|
+
|
83
|
+
Subaltern.eval(%{
|
84
|
+
a = []
|
85
|
+
for k, v in { 'a' => 'b', 'c' => 'd' }
|
86
|
+
a << k + ':' + v
|
87
|
+
end
|
88
|
+
a
|
89
|
+
}).should == %w[ a:b c:d ]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
describe 'loop' do
|
94
|
+
|
95
|
+
it 'works' do
|
96
|
+
|
97
|
+
Subaltern.eval(%{
|
98
|
+
a = 0
|
99
|
+
loop do
|
100
|
+
a = a + 1
|
101
|
+
break if a > 7
|
102
|
+
end
|
103
|
+
a
|
104
|
+
}).should == 8
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
|
2
|
+
require 'subaltern'
|
3
|
+
|
4
|
+
Dir[File.join(File.dirname(__FILE__), 'support/**/*.rb')].each do |f|
|
5
|
+
require(f)
|
6
|
+
end
|
7
|
+
|
8
|
+
RSpec.configure do |config|
|
9
|
+
|
10
|
+
# == Mock Framework
|
11
|
+
#
|
12
|
+
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
|
13
|
+
#
|
14
|
+
# config.mock_with :mocha
|
15
|
+
# config.mock_with :flexmock
|
16
|
+
# config.mock_with :rr
|
17
|
+
config.mock_with :rspec
|
18
|
+
|
19
|
+
config.include SubalternHelper
|
20
|
+
end
|
21
|
+
|
data/spec/string_spec.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
|
5
|
+
describe Subaltern do
|
6
|
+
|
7
|
+
context String do
|
8
|
+
|
9
|
+
describe 'a literal String' do
|
10
|
+
|
11
|
+
it 'is returned as is' do
|
12
|
+
|
13
|
+
Subaltern.eval('"123"').should == '123'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
describe 'string extrapolation' do
|
18
|
+
|
19
|
+
it 'happens' do
|
20
|
+
|
21
|
+
Subaltern.eval('"ab#{c}de"', 'c' => 'C').should == 'abCde'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
data/subaltern.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.join(File.dirname(__FILE__), 'lib/subaltern/version')
|
4
|
+
# bundler wants absolute path
|
5
|
+
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
|
9
|
+
s.name = 'subaltern'
|
10
|
+
s.version = Subaltern::VERSION
|
11
|
+
s.platform = Gem::Platform::RUBY
|
12
|
+
s.authors = [ 'John Mettraux' ]
|
13
|
+
s.email = [ 'jmettraux@gmail.com' ]
|
14
|
+
s.homepage = 'http://lambda.io'
|
15
|
+
s.rubyforge_project = 'ruote'
|
16
|
+
s.summary = 'an interpreter of a subset of Ruby, written in Ruby'
|
17
|
+
s.description = %{
|
18
|
+
an interpreter of a subset of Ruby, written in Ruby
|
19
|
+
}
|
20
|
+
|
21
|
+
#s.files = `git ls-files`.split("\n")
|
22
|
+
s.files = Dir[
|
23
|
+
'Rakefile',
|
24
|
+
'lib/**/*.rb', 'spec/**/*.rb', 'test/**/*.rb',
|
25
|
+
'*.gemspec', '*.txt', '*.rdoc', '*.md', '*.mdown'
|
26
|
+
]
|
27
|
+
|
28
|
+
s.add_runtime_dependency 'ruby_parser', '>= 2.0.6'
|
29
|
+
|
30
|
+
s.add_development_dependency 'rspec'
|
31
|
+
|
32
|
+
s.require_path = 'lib'
|
33
|
+
end
|
34
|
+
|
metadata
ADDED
@@ -0,0 +1,122 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: subaltern
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 23
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
- 0
|
10
|
+
version: 1.0.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- John Mettraux
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-02-05 00:00:00 +09:00
|
19
|
+
default_executable:
|
20
|
+
dependencies:
|
21
|
+
- !ruby/object:Gem::Dependency
|
22
|
+
name: ruby_parser
|
23
|
+
prerelease: false
|
24
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
30
|
+
segments:
|
31
|
+
- 2
|
32
|
+
- 0
|
33
|
+
- 6
|
34
|
+
version: 2.0.6
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: rspec
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
hash: 3
|
46
|
+
segments:
|
47
|
+
- 0
|
48
|
+
version: "0"
|
49
|
+
type: :development
|
50
|
+
version_requirements: *id002
|
51
|
+
description: "\n\
|
52
|
+
an interpreter of a subset of Ruby, written in Ruby\n "
|
53
|
+
email:
|
54
|
+
- jmettraux@gmail.com
|
55
|
+
executables: []
|
56
|
+
|
57
|
+
extensions: []
|
58
|
+
|
59
|
+
extra_rdoc_files: []
|
60
|
+
|
61
|
+
files:
|
62
|
+
- Rakefile
|
63
|
+
- lib/subaltern/context.rb
|
64
|
+
- lib/subaltern/errors.rb
|
65
|
+
- lib/subaltern/evaluator.rb
|
66
|
+
- lib/subaltern/kernel.rb
|
67
|
+
- lib/subaltern/version.rb
|
68
|
+
- lib/subaltern.rb
|
69
|
+
- spec/array_spec.rb
|
70
|
+
- spec/basics_spec.rb
|
71
|
+
- spec/boolean_spec.rb
|
72
|
+
- spec/context_spec.rb
|
73
|
+
- spec/control_flow_spec.rb
|
74
|
+
- spec/evil_spec.rb
|
75
|
+
- spec/fixnum_spec.rb
|
76
|
+
- spec/functions_spec.rb
|
77
|
+
- spec/hash_spec.rb
|
78
|
+
- spec/loops_spec.rb
|
79
|
+
- spec/spec_helper.rb
|
80
|
+
- spec/string_spec.rb
|
81
|
+
- spec/support/subaltern_helper.rb
|
82
|
+
- subaltern.gemspec
|
83
|
+
- CHANGELOG.txt
|
84
|
+
- LICENSE.txt
|
85
|
+
- TODO.txt
|
86
|
+
- README.md
|
87
|
+
has_rdoc: true
|
88
|
+
homepage: http://lambda.io
|
89
|
+
licenses: []
|
90
|
+
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
hash: 3
|
102
|
+
segments:
|
103
|
+
- 0
|
104
|
+
version: "0"
|
105
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
106
|
+
none: false
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
hash: 3
|
111
|
+
segments:
|
112
|
+
- 0
|
113
|
+
version: "0"
|
114
|
+
requirements: []
|
115
|
+
|
116
|
+
rubyforge_project: ruote
|
117
|
+
rubygems_version: 1.6.2
|
118
|
+
signing_key:
|
119
|
+
specification_version: 3
|
120
|
+
summary: an interpreter of a subset of Ruby, written in Ruby
|
121
|
+
test_files: []
|
122
|
+
|