-
Notifications
You must be signed in to change notification settings - Fork 216
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce new heuristics for block types
- Loading branch information
Showing
6 changed files
with
229 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# frozen_string_literal: true | ||
|
||
module RBS | ||
module Prototype | ||
class NodeUsage | ||
include Helpers | ||
|
||
attr_reader :conditional_nodes | ||
|
||
def initialize(node) | ||
@node = node | ||
@conditional_nodes = Set[].compare_by_identity | ||
|
||
calculate(node, conditional: false) | ||
end | ||
|
||
def each_conditional_node(&block) | ||
if block | ||
conditional_nodes.each(&block) | ||
else | ||
conditional_nodes.each | ||
end | ||
end | ||
|
||
def calculate(node, conditional:) | ||
if conditional | ||
conditional_nodes << node | ||
end | ||
|
||
case node.type | ||
when :IF, :UNLESS | ||
cond_node, true_node, false_node = node.children | ||
calculate(cond_node, conditional: true) | ||
calculate(true_node, conditional: conditional) if true_node | ||
calculate(false_node, conditional: conditional) if false_node | ||
when :AND, :OR | ||
left, right = node.children | ||
calculate(left, conditional: true) | ||
calculate(right, conditional: conditional) | ||
when :QCALL | ||
receiver, _, args = node.children | ||
calculate(receiver, conditional: true) | ||
calculate(args, conditional: false) if args | ||
when :WHILE | ||
cond, body = node.children | ||
calculate(cond, conditional: true) | ||
calculate(body, conditional: false) if body | ||
when :OP_ASGN_OR, :OP_ASGN_AND | ||
var, _, asgn = node.children | ||
calculate(var, conditional: true) | ||
calculate(asgn, conditional: conditional) | ||
when :LASGN, :IASGN, :GASGN | ||
_, lhs = node.children | ||
calculate(lhs, conditional: conditional) if lhs | ||
when :MASGN | ||
lhs, _ = node.children | ||
calculate(lhs, conditional: conditional) | ||
when :CDECL | ||
if node.children.size == 2 | ||
_, lhs = node.children | ||
calculate(lhs, conditional: conditional) | ||
else | ||
const, _, lhs = node.children | ||
calculate(const, conditional: false) | ||
calculate(lhs, conditional: conditional) | ||
end | ||
when :SCOPE | ||
_, _, body = node.children | ||
calculate(body, conditional: conditional) | ||
when :CASE2 | ||
_, *branches = node.children | ||
branches.each do |branch| | ||
if branch.type == :WHEN | ||
list, body = branch.children | ||
list.children.each do |child| | ||
if child | ||
calculate(child, conditional: true) | ||
end | ||
end | ||
calculate(body, conditional: conditional) | ||
else | ||
calculate(branch, conditional: conditional) | ||
end | ||
end | ||
when :BLOCK | ||
*nodes, last = node.children | ||
nodes.each do |no| | ||
calculate(no, conditional: false) | ||
end | ||
calculate(last, conditional: conditional) if last | ||
else | ||
each_child(node) do |child| | ||
calculate(child, conditional: false) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module RBS | ||
module Prototype | ||
class NodeUsage | ||
include Helpers | ||
|
||
type node = RubyVM::AbstractSyntaxTree::Node | ||
|
||
attr_reader node: node | ||
|
||
attr_reader conditional_nodes: Set[node] | ||
|
||
def initialize: (node) -> void | ||
|
||
def calculate: (node, conditional: bool) -> void | ||
|
||
def each_conditional_node: () { (node) -> void } -> void | ||
| () -> Enumerator[node, void] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require "test_helper" | ||
|
||
class RBS::NodeUsageTest < Test::Unit::TestCase | ||
include RBS::Prototype | ||
|
||
def parse(string) | ||
RubyVM::AbstractSyntaxTree.parse(string) | ||
end | ||
|
||
def test_conditional | ||
usage = NodeUsage.new(parse(<<~RB)) | ||
if block | ||
yield | ||
end | ||
foo && bar || baz | ||
1&.+(2) | ||
begin | ||
bar | ||
end while baz | ||
a ||= b | ||
a += 123 | ||
x = 1 | ||
@y = 2 | ||
Z = 3 | ||
Z::Z1 = 4 | ||
x, y, z = foo | ||
puts unless foo | ||
case | ||
when foo | ||
else | ||
hello | ||
end | ||
[ | ||
(foo(); bar; baz) | ||
] | ||
RB | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters