File tree Expand file tree Collapse file tree 2 files changed +10
-8
lines changed
Expand file tree Collapse file tree 2 files changed +10
-8
lines changed Original file line number Diff line number Diff line change @@ -5,6 +5,11 @@ class BinaryExpression
55 attr_reader :operator
66 attr_accessor :left_node , :right_node
77
8+ # Helper method to simplify right-leaning tree construction
9+ def self . rtl ( right , operator , left )
10+ new ( left , operator , right )
11+ end
12+
813 def initialize ( left , operator , right )
914 @left_node = left
1015 @operator = operator
Original file line number Diff line number Diff line change @@ -62,14 +62,11 @@ def expression
6262 #
6363 # `a == b and b or c` is evaluated like (a and (b or c))
6464 def logical
65- expr = equality
66- while ( operator = id? ( 'and' ) || id? ( 'or' ) )
67- if expr . is_a? ( BinaryExpression ) && ( expr . operator == 'and' || expr . operator == 'or' )
68- expr . right_node = BinaryExpression . new ( expr . right_node , operator , equality )
69- else
70- expr = BinaryExpression . new ( expr , operator , equality )
71- end
72- end
65+ operator = nil
66+ exprs = [ equality ]
67+ exprs . push ( operator , equality ) while ( operator = id? ( 'and' ) || id? ( 'or' ) )
68+ expr = exprs . pop
69+ expr = BinaryExpression . rtl ( expr , exprs . pop , exprs . pop ) until exprs . empty?
7370 expr
7471 end
7572
You can’t perform that action at this time.
0 commit comments