class ASTNormalizer
Attributes
complexity[RW]
Public Class Methods
new()
click to toggle source
# File lib/normalize_ast.rb, line 34 def initialize @track = NameTracker.new @complexity = Hash.new { |h,k| h[k] = [] } end
Public Instance Methods
pretty_complexity()
click to toggle source
# File lib/normalize_ast.rb, line 41 def pretty_complexity measures, out = [:int, :str, :send, :var, :float, :sym], {} @complexity.select { |k,v| measures.include?(k) }.each { |k,v| out[k] = v.size } out end
rewrite_ast(ast)
click to toggle source
# File lib/normalize_ast.rb, line 46 def rewrite_ast(ast) if ast.is_a? AST::Node type = ast.type case type # Variables when :lvar, :ivar, :gvar update_complexity(:var, ast.children.first) ast.updated(nil, ast.children.map { |child| @track.rename(:var, child) }) # Assignment when :lvasgn, :gvasgn, :ivasgn, :cvasgn update_complexity(:assignment, ast.children.first) ast.updated(nil, ast.children.map.with_index { |child,i| i == 0 ? @track.rename(:var,child) : rewrite_ast(child) }) # Primatives when :int, :float, :str, :sym, :arg, :restarg, :blockarg update_complexity(type, ast.children.first) ast.updated(nil, ast.children.map { |child| @track.rename(type, child) }) when :optarg update_complexity(:arg, ast.children.first) ast.updated(nil, ast.children.map.with_index { |child,i| if i == 0 @track.rename(:var, child) else rewrite_ast(child) end }) # Method definitions when :def update_complexity(:def, ast.children.first) ast.updated(nil, ast.children.map.with_index { |child,i| i == 0 ? :method : rewrite_ast(child) }) when :defs update_complexity(:def, ast.children.first) ast.updated(nil, ast.children.map.with_index { |child,i| i == 1 ? :method : rewrite_ast(child) }) when :send update_complexity(:send, ast.children[1]) ast.updated(nil, ast.children.map { |child| rewrite_ast(child) }) else ast.updated(nil, ast.children.map { |child| rewrite_ast(child) }) end else ast end end
update_complexity(type,val)
click to toggle source
# File lib/normalize_ast.rb, line 38 def update_complexity(type,val) @complexity[type].push(val) end