rensei 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ module Rensei
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ require_relative 'lib/rensei/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "rensei"
5
+ spec.version = Rensei::VERSION
6
+ spec.authors = ["manga_osyo"]
7
+ spec.email = ["manga.osyo@gmail.com"]
8
+
9
+ spec.summary = %q{Unparse from `RubyVM::AbstractSyntaxTree::Node` to Ruby code.}
10
+ spec.description = %q{Unparse from `RubyVM::AbstractSyntaxTree::Node` to Ruby code.}
11
+ spec.homepage = ""
12
+ spec.license = "MIT"
13
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+ end
@@ -0,0 +1,55 @@
1
+ require "rensei"
2
+
3
+ class Hash
4
+ # ノードの Hash をネストして操作する拡張する
5
+ def each_node(&block)
6
+ return enum_for(:each_node) unless block
7
+ self[:children].each { |node|
8
+ block.call self
9
+ if Hash === node
10
+ node.each_node(&block)
11
+ end
12
+ }
13
+ end
14
+ end
15
+
16
+ # RubyVM::AbstractSyntaxTree::Node から Hash に変換する拡張
17
+ # RubyVM::AbstractSyntaxTree::Node#to_h で変換できる
18
+ using Rensei::NodeToHash
19
+
20
+
21
+ def bocchi &block
22
+ # ブロックの中身を AST に変換
23
+ ast = RubyVM::AbstractSyntaxTree.of(block)
24
+
25
+ # AST から必要な node だけ抽出
26
+ node = ast.children.last
27
+
28
+ # AST の node から Hash に変換
29
+ node_h = node.to_h
30
+
31
+ # AST の type の意味を変更
32
+ # hoge.foo から hoge&.foo に変換する
33
+ node_h.each_node { |node|
34
+ node[:type] = :QCALL if node[:type] == :CALL
35
+ }
36
+
37
+ # 変換した AST の Hash を Ruby のコードに戻す
38
+ code = Rensei.unparse(node_h)
39
+
40
+ # メソッドの呼び出し元のコンテキストで評価する
41
+ block.binding.eval(code)
42
+ end
43
+
44
+
45
+ hoge1 = nil
46
+ hoge2 = Struct.new(:foo).new(nil)
47
+ hoge3 = Struct.new(:foo).new(bar: "hogehoge")
48
+
49
+ # . を &. に変換して実行する
50
+ bocchi {
51
+ pp hoge1.foo[:bar] # => nil
52
+ pp hoge2.foo[:bar] # => nil
53
+ pp hoge3.foo[:bar] # => "hogehoge"
54
+ }
55
+
@@ -0,0 +1,12 @@
1
+ require "rensei"
2
+
3
+ code = <<~EOS
4
+ 10.times do |i|
5
+ puts i
6
+ end
7
+ EOS
8
+
9
+ ast = RubyVM::AbstractSyntaxTree.parse(code)
10
+ ruby = Rensei.unparse(ast)
11
+ puts ruby
12
+ # => 10.times() { |i| puts(i) }
metadata ADDED
@@ -0,0 +1,59 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rensei
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - manga_osyo
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-11-30 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: Unparse from `RubyVM::AbstractSyntaxTree::Node` to Ruby code.
14
+ email:
15
+ - manga.osyo@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".rspec"
22
+ - ".travis.yml"
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - lib/rensei.rb
30
+ - lib/rensei/node_to_hash.rb
31
+ - lib/rensei/unparser.rb
32
+ - lib/rensei/version.rb
33
+ - rensei.gemspec
34
+ - sample/bocchi.rb
35
+ - sample/sample.rb
36
+ homepage: ''
37
+ licenses:
38
+ - MIT
39
+ metadata: {}
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: 2.3.0
49
+ required_rubygems_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubygems_version: 3.1.4
56
+ signing_key:
57
+ specification_version: 4
58
+ summary: Unparse from `RubyVM::AbstractSyntaxTree::Node` to Ruby code.
59
+ test_files: []