normalize_ast 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +15 -0
- data/lib/normalize_ast.rb +112 -0
- metadata +45 -0
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
OThlN2UwNDUzYjVhMTBiODcwN2NhNjA5NmFiYTEzYTBiMTVhNTczNg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZmI2OTMzN2I1ZmMxZWI2YTBlMWQ4ZDYwOWU1OTk3Y2E2OGUyZTk2NQ==
|
7
|
+
!binary "U0hBNTEy":
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
MDU2YzUzMTE0MWYwOTE1MzRhNzlmYjM3YWJlYTU4ODJhMDAxMWU5NDQ0NDFm
|
10
|
+
MTUxZTI2ZGYzZTdlMjEzZjBiMjEyMDZmMDExNzQyY2JhMDE5OTVhNWU5N2Fm
|
11
|
+
MmU0MWFiMWFhYzBlOTEyOTUyMzgwMmY3ZTUxZDY5YWY2ODc0MDg=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
ZjJiODUxMmNmZTI0ODJkZWJhZDdkZDJhNTRhN2VhODhhODg1NDNkZjlhYzAz
|
14
|
+
NDlmMDgwNGFlMDk5NDAzYWIyNTNiMDVlMmIzZTJmZjYwN2YyZjJlOGMwOWEw
|
15
|
+
Y2FmNjYzYmZlNzhjZDYyYWQxZDk0NzExOTVmYjRlNTRlZGE2ZjM=
|
@@ -0,0 +1,112 @@
|
|
1
|
+
require 'parser/current'
|
2
|
+
require 'unparser'
|
3
|
+
require 'ast'
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
class NameTracker
|
7
|
+
def initialize
|
8
|
+
@var_hash = Hash.new { |h,k| h[k] = "var"+h.size.to_s }
|
9
|
+
@spt_hash = Hash.new { |h,k| h[k] = "*vr"+h.size.to_s }
|
10
|
+
@bar_hash = Hash.new { |h,k| h[k] = "&vr"+h.size.to_s }
|
11
|
+
@sym_hash = Hash.new { |h,k| h[k] = ("sym"+h.size.to_s).to_sym }
|
12
|
+
@str_hash = Hash.new { |h,k| h[k] = "str"+h.size.to_s }
|
13
|
+
@flt_hash = Hash.new { |h,k| h[k] = 0.0+h.size.to_f }
|
14
|
+
@int_hash = Hash.new { |h,k| h[k] = 0+h.size }
|
15
|
+
@mapping = {
|
16
|
+
:str => @str_hash,
|
17
|
+
:sym => @sym_hash,
|
18
|
+
:arg => @var_hash,
|
19
|
+
:float => @flt_hash,
|
20
|
+
:int => @int_hash,
|
21
|
+
:var => @var_hash,
|
22
|
+
:restarg => @spt_hash,
|
23
|
+
:blockarg => @bar_hash
|
24
|
+
}
|
25
|
+
end
|
26
|
+
def rename(type,id)
|
27
|
+
@mapping[type][id]
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
class ASTNormalizer
|
33
|
+
attr_accessor :complexity
|
34
|
+
def initialize
|
35
|
+
@track = NameTracker.new
|
36
|
+
@complexity = Hash.new { |h,k| h[k] = [] }
|
37
|
+
end
|
38
|
+
def update_complexity(type,val)
|
39
|
+
@complexity[type].push(val)
|
40
|
+
end
|
41
|
+
def pretty_complexity
|
42
|
+
measures, out = [:int, :str, :send, :var, :float, :sym], {}
|
43
|
+
@complexity.select { |k,v| measures.include?(k) }.each { |k,v| out[k] = v.size }
|
44
|
+
out
|
45
|
+
end
|
46
|
+
def rewrite_ast(ast)
|
47
|
+
if ast.is_a? AST::Node
|
48
|
+
type = ast.type
|
49
|
+
case type
|
50
|
+
# Variables
|
51
|
+
when :lvar, :ivar, :gvar
|
52
|
+
update_complexity(:var, ast.children.first)
|
53
|
+
ast.updated(nil, ast.children.map { |child| @track.rename(:var, child) })
|
54
|
+
# Assignment
|
55
|
+
when :lvasgn, :gvasgn, :ivasgn, :cvasgn
|
56
|
+
update_complexity(:assignment, ast.children.first)
|
57
|
+
ast.updated(nil, ast.children.map.with_index { |child,i|
|
58
|
+
i == 0 ? @track.rename(:var,child) : rewrite_ast(child)
|
59
|
+
})
|
60
|
+
# Primatives
|
61
|
+
when :int, :float, :str, :sym, :arg, :restarg, :blockarg
|
62
|
+
update_complexity(type, ast.children.first)
|
63
|
+
ast.updated(nil, ast.children.map { |child| @track.rename(type, child) })
|
64
|
+
when :optarg
|
65
|
+
update_complexity(:arg, ast.children.first)
|
66
|
+
ast.updated(nil, ast.children.map.with_index { |child,i|
|
67
|
+
if i == 0
|
68
|
+
@track.rename(:var, child)
|
69
|
+
else
|
70
|
+
rewrite_ast(child)
|
71
|
+
end
|
72
|
+
})
|
73
|
+
# Method definitions
|
74
|
+
when :def
|
75
|
+
update_complexity(:def, ast.children.first)
|
76
|
+
ast.updated(nil, ast.children.map.with_index { |child,i|
|
77
|
+
i == 0 ? :method : rewrite_ast(child)
|
78
|
+
})
|
79
|
+
when :defs
|
80
|
+
update_complexity(:def, ast.children.first)
|
81
|
+
ast.updated(nil, ast.children.map.with_index { |child,i|
|
82
|
+
i == 1 ? :method : rewrite_ast(child)
|
83
|
+
})
|
84
|
+
when :send
|
85
|
+
update_complexity(:send, ast.children[1])
|
86
|
+
ast.updated(nil, ast.children.map { |child| rewrite_ast(child) })
|
87
|
+
else
|
88
|
+
ast.updated(nil, ast.children.map { |child| rewrite_ast(child) })
|
89
|
+
end
|
90
|
+
else
|
91
|
+
ast
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
class ASTProcess
|
97
|
+
def store_nodes(ast, &block)
|
98
|
+
if ast.is_a? Parser::AST::Node
|
99
|
+
type = ast.type
|
100
|
+
yield ast, type
|
101
|
+
ast.children.each do |child|
|
102
|
+
store_nodes child, &block
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
# str = IO.read(ARGV[0])
|
109
|
+
# ast = Parser::CurrentRuby.parse(str)
|
110
|
+
# modified = ASTNormalizer.new.rewrite_ast(ast)
|
111
|
+
# puts Unparser.unparse(modified)
|
112
|
+
|
metadata
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: normalize_ast
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ethan Fast
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-31 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Uses whitequark's Ruby parser
|
14
|
+
email: ejhfast@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- lib/normalize_ast.rb
|
20
|
+
homepage: http://rubygems.org/gems/normalize_ast
|
21
|
+
licenses:
|
22
|
+
- MIT
|
23
|
+
metadata: {}
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
requirements: []
|
39
|
+
rubyforge_project:
|
40
|
+
rubygems_version: 2.0.5
|
41
|
+
signing_key:
|
42
|
+
specification_version: 4
|
43
|
+
summary: Code to normalize a Ruby AST
|
44
|
+
test_files: []
|
45
|
+
has_rdoc:
|