Module: CommandSearch::Optimizer
- Defined in:
- lib/command_search/optimizer.rb
Class Method Summary collapse
- .ands_and_ors!(ast) ⇒ Object
- .denest_parens(ast, parent_type = :root) ⇒ Object
- .negate_negate(ast) ⇒ Object
- .optimize(ast) ⇒ Object
- .remove_empty_strings!(ast) ⇒ Object
Class Method Details
.ands_and_ors!(ast) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/command_search/optimizer.rb', line 5 def ands_and_ors!(ast) ast.map! do |node| next node unless node[:nest_type] == :paren || node[:nest_type] == :pipe ands_and_ors!(node[:value]) next node[:value].first if node[:value].length < 2 node[:value] = node[:value].flat_map do |kid| next kid[:value] if kid[:nest_type] == :pipe kid end node[:value].uniq! node end end |
.denest_parens(ast, parent_type = :root) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/command_search/optimizer.rb', line 31 def denest_parens(ast, parent_type = :root) ast.flat_map do |node| next node unless node[:nest_type] node[:value] = denest_parens(node[:value], node[:nest_type]) # valid_self && (valid_parent || valid_child) if node[:nest_type] == :paren && (parent_type != :pipe || node[:value].count < 2) next node[:value] end node end end |
.negate_negate(ast) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/command_search/optimizer.rb', line 19 def negate_negate(ast) ast.flat_map do |node| next node unless node[:nest_type] node[:value] = negate_negate(node[:value]) next [] if node[:value] == [] type = node[:nest_type] child_type = node[:value].first[:nest_type] next node unless type == :minus && child_type == :minus node[:value].first[:value] end end |
.optimize(ast) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/command_search/optimizer.rb', line 50 def optimize(ast) out = ast out = denest_parens(out) remove_empty_strings!(out) out = negate_negate(out) ands_and_ors!(out) out.uniq! out end |
.remove_empty_strings!(ast) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/command_search/optimizer.rb', line 43 def remove_empty_strings!(ast) ast.reject! do |node| remove_empty_strings!(node[:value]) if node[:nest_type] node[:type] == :quoted_str && node[:value] == '' end end |