minruby 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 39f7f065132debe11242193f8be1fe7d820e17c4
4
+ data.tar.gz: 213eff7a9b7da0f576bf059b282145d5d334a0c1
5
+ SHA512:
6
+ metadata.gz: 121f1dc50cfd70035b8cdd7d93fb57302bc2d99ac65f554ed0d8ccd5dd8890698e1e28b87e7c8680bec9e2ed079c10b4c26b9930fc1bdde617fb8cec27868a9d
7
+ data.tar.gz: 314bdb65c1fefe2c03ca5029e0db7367456ae552ccc5679605a1c70fdd4f5cb2e86e9edfcd1f91ac10a98195b068bbf77de55bcac2b25b477d2026c397720c49
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Yusuke Endoh
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # 『Ruby で学ぶ Ruby』用ライブラリ
2
+
3
+ ASCII.jp の Web 連載『[Ruby で学ぶ Ruby](http://ascii.jp/elem/000/001/230/1230449/)』のための補助ライブラリです。
4
+
5
+ ## インストール方法
6
+
7
+ コンソールで
8
+
9
+ gem install minruby
10
+
11
+ というコマンドを実行してください。
12
+ また、あなたのプログラムの最初の行に
13
+
14
+ require "minruby"
15
+
16
+ という行を追加してください。
17
+
18
+ ## 別のインストール方法
19
+
20
+ `gem` でのインストールがうまく行かない場合は、[ライブラリのファイル](https://github.com/mame/minruby/lib/minruby.rb)をダウンロードしてあなたのプログラムと同じフォルダに置いてください。それから、あなたのプログラムの最初の行に
21
+
22
+ require "./minruby"
23
+
24
+ という行を追加してください。
25
+
26
+ ## 使い方
27
+
28
+ 連載記事を参照してください。第 4 回以降でこのライブラリを使用しています。
29
+
30
+ ## ライセンス
31
+
32
+ [MIT License](http://opensource.org/licenses/MIT)
33
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/lib/minruby.rb ADDED
@@ -0,0 +1,161 @@
1
+ require "pp"
2
+ require "ripper"
3
+
4
+ class MinRubyParser
5
+ def self.minruby_parse(program)
6
+ MinRubyParser.new.minruby_parse(program)
7
+ end
8
+
9
+ def minruby_parse(program)
10
+ simplify(Ripper.sexp(program))
11
+ end
12
+
13
+ def simplify(exp)
14
+ case exp[0]
15
+ when :program, :bodystmt
16
+ make_stmts(exp[1])
17
+ when :def
18
+ name = exp[1][1]
19
+ params = exp[2]
20
+ params = params[1] if params[0] == :paren
21
+ params = (params[1] || []).map {|a| a[1] }
22
+ body = simplify(exp[3])
23
+ ["func_def", name, params, body]
24
+ when :call
25
+ recv = simplify(exp[1])
26
+ name = exp[3][1]
27
+ ["method_call", recv, name, []]
28
+ when :fcall
29
+ name = exp[1][1]
30
+ ["func_call", name, []]
31
+ when :method_add_arg
32
+ call = simplify(exp[1])
33
+ e = exp[2]
34
+ e = e[1] || [] if e[0] == :arg_paren
35
+ e = e[1] || [] if e[0] == :args_add_block
36
+ e = e.map {|e_| simplify(e_) }
37
+ call[call[0] == "func_call" ? 2 : 3] = e
38
+ call
39
+ when :command
40
+ name = exp[1][1]
41
+ args = exp[2][1].map {|e_| simplify(e_) }
42
+ ["func_call", name, args]
43
+ when :if, :elsif
44
+ cond_exp = simplify(exp[1])
45
+ then_exp = make_stmts(exp[2])
46
+ if exp[3]
47
+ if exp[3][0] == :elsif
48
+ else_exp = simplify(exp[3])
49
+ else
50
+ else_exp = make_stmts(exp[3][1])
51
+ end
52
+ end
53
+ ["if", cond_exp, then_exp, else_exp]
54
+ when :if_mod
55
+ cond_exp = simplify(exp[1])
56
+ then_exp = make_stmts([exp[2]])
57
+ ["if", cond_exp, then_exp, nil]
58
+ when :while
59
+ cond_exp = simplify(exp[1])
60
+ body_exp = make_stmts(exp[2])
61
+ ["while", cond_exp, body_exp]
62
+ when :binary
63
+ exp1 = simplify(exp[1])
64
+ op = exp[2]
65
+ exp2 = simplify(exp[3])
66
+ [op.to_s, exp1, exp2]
67
+ when :var_ref
68
+ case exp[1][0]
69
+ when :@kw
70
+ case exp[1][1]
71
+ when "nil" then ["lit", nil]
72
+ when "true" then ["lit", true]
73
+ when "false" then ["lit", false]
74
+ else
75
+ raise
76
+ end
77
+ when :@ident
78
+ ["var_ref", exp[1][1]]
79
+ when :@const
80
+ ["const_ref", exp[1][1]]
81
+ end
82
+ when :@int
83
+ ["lit", exp[1].to_i]
84
+ when :string_literal
85
+ ["lit", exp[1][1][1]]
86
+ when :symbol_literal
87
+ ["lit", exp[1][1][1].to_sym]
88
+ when :assign
89
+ case exp[1][0]
90
+ when :var_field
91
+ ["var_assign", exp[1][1][1], simplify(exp[2])]
92
+ when :aref_field
93
+ ["ary_assign", simplify(exp[1][1]), simplify(exp[1][2][1][0]), simplify(exp[2])]
94
+ else
95
+ raise
96
+ end
97
+ when :case
98
+ arg = simplify(exp[1])
99
+ when_clauses = []
100
+ exp = exp[2]
101
+ while exp && exp[0] == :when
102
+ pat = exp[1].map {|e_| simplify(e_) }
103
+ when_clauses << [pat, make_stmts(exp[2])]
104
+ exp = exp[3]
105
+ end
106
+ else_clause = make_stmts(exp[1]) if exp
107
+ #["case", arg, when_clauses, else_clause]
108
+
109
+ exp = else_clause
110
+ when_clauses.reverse_each do |patterns, stmts|
111
+ patterns.each do |pattern|
112
+ exp = ["if", ["==", arg, pattern], stmts, exp]
113
+ end
114
+ end
115
+ exp
116
+ when :method_add_block
117
+ call = simplify(exp[1])
118
+ blk_params = exp[2][1][1][1].map {|a| a[1] }
119
+ blk_body = exp[2][2].map {|e_| simplify(e_) }
120
+ call << blk_params << blk_body
121
+ when :aref
122
+ ["ary_ref", simplify(exp[1]), *exp[2][1].map {|e_| simplify(e_) }]
123
+ when :array
124
+ ["ary_new", *(exp[1] ? exp[1].map {|e_| simplify(e_) } : [])]
125
+ when :hash
126
+ kvs = []
127
+ if exp[1]
128
+ exp[1][1].each do |e_|
129
+ key = simplify(e_[1])
130
+ val = simplify(e_[2])
131
+ kvs << [key, val]
132
+ end
133
+ end
134
+ ["hash_new", kvs]
135
+ when :void_stmt
136
+ ["lit", nil]
137
+ when :paren
138
+ simplify(exp[1][0])
139
+ else
140
+ pp exp
141
+ raise "unsupported node: #{ exp[0] }"
142
+ end
143
+ end
144
+
145
+ def make_stmts(exps)
146
+ exps = exps.map {|exp| simplify(exp) }
147
+ exps.size == 1 ? exps[0] : ["stmts", *exps]
148
+ end
149
+ end
150
+
151
+ def minruby_load()
152
+ File.read(ARGV.shift)
153
+ end
154
+
155
+ def minruby_parse(src)
156
+ MinRubyParser.minruby_parse(src)
157
+ end
158
+
159
+ def minruby_call(mhd, args)
160
+ send(mhd, *args)
161
+ end
data/minruby.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ Gem::Specification.new do |spec|
3
+ spec.name = "minruby"
4
+ spec.version = "1.0"
5
+ spec.authors = ["Yusuke Endoh"]
6
+ spec.email = ["mame@ruby-lang.org"]
7
+
8
+ spec.summary = %q{A helper library for "Ruby de manabu Ruby"}
9
+ spec.description = %q{This library provides some helper modules to implement a toy Ruby implementation. This is created for a series of articles, "Ruby de manabu Ruby (Learning Ruby by Ruby)", in ASCII.jp}
10
+ spec.homepage = "http://github.com/mame/minruby/"
11
+ spec.license = "MIT"
12
+
13
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
14
+ f.match(%r{^(test|spec|features)/})
15
+ end
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.13"
21
+ spec.add_development_dependency "rake", "~> 10.0"
22
+ end
metadata ADDED
@@ -0,0 +1,81 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minruby
3
+ version: !ruby/object:Gem::Version
4
+ version: '1.0'
5
+ platform: ruby
6
+ authors:
7
+ - Yusuke Endoh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-10-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: This library provides some helper modules to implement a toy Ruby implementation. This
42
+ is created for a series of articles, "Ruby de manabu Ruby (Learning Ruby by Ruby)",
43
+ in ASCII.jp
44
+ email:
45
+ - mame@ruby-lang.org
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".travis.yml"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - lib/minruby.rb
56
+ - minruby.gemspec
57
+ homepage: http://github.com/mame/minruby/
58
+ licenses:
59
+ - MIT
60
+ metadata: {}
61
+ post_install_message:
62
+ rdoc_options: []
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ">="
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ required_rubygems_version: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ requirements: []
76
+ rubyforge_project:
77
+ rubygems_version: 2.6.7
78
+ signing_key:
79
+ specification_version: 4
80
+ summary: A helper library for "Ruby de manabu Ruby"
81
+ test_files: []