Class: Twostroke::AST::UnsortedBinop
- Defined in:
- lib/twostroke/ast/unsorted_binop.rb
Instance Attribute Summary collapse
-
#left ⇒ Object
Returns the value of attribute left.
-
#op ⇒ Object
Returns the value of attribute op.
-
#right ⇒ Object
Returns the value of attribute right.
Class Method Summary collapse
Instance Method Summary collapse
Methods inherited from Base
Constructor Details
This class inherits a constructor from Twostroke::AST::Base
Instance Attribute Details
#left ⇒ Object
Returns the value of attribute left.
3 4 5 |
# File 'lib/twostroke/ast/unsorted_binop.rb', line 3 def left @left end |
#op ⇒ Object
Returns the value of attribute op.
3 4 5 |
# File 'lib/twostroke/ast/unsorted_binop.rb', line 3 def op @op end |
#right ⇒ Object
Returns the value of attribute right.
3 4 5 |
# File 'lib/twostroke/ast/unsorted_binop.rb', line 3 def right @right end |
Class Method Details
.operator_class ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/twostroke/ast/unsorted_binop.rb', line 5 def self.operator_class @@classes ||= { :ASTERISK => Multiplication, :SLASH => Division, :MOD => Modulus, :PLUS => Addition, :MINUS => Subtraction, :LEFT_SHIFT => LeftShift, :RIGHT_SHIFT => RightArithmeticShift, :RIGHT_TRIPLE_SHIFT => RightLogicalShift, :LT => LessThan, :LTE => LessThanEqual, :GT => GreaterThan, :GTE => GreaterThanEqual, :IN => In, :INSTANCEOF => InstanceOf, :DOUBLE_EQUALS => Equality, :NOT_EQUALS => Inequality, :TRIPLE_EQUALS => StrictEquality, :NOT_DOUBLE_EQUALS => StrictInequality, :AMPERSAND => BitwiseAnd, :CARET => BitwiseXor, :PIPE => BitwiseOr, :AND => And, :OR => Or } end |
.operator_precedence ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/twostroke/ast/unsorted_binop.rb', line 33 def self.operator_precedence @precedences ||= { :ASTERISK => 5, :SLASH => 5, :MOD => 5, :PLUS => 6, :MINUS => 6, :LEFT_SHIFT => 7, :RIGHT_SHIFT => 7, :RIGHT_TRIPLE_SHIFT => 7, :LT => 8, :LTE => 8, :GT => 8, :GTE => 8, :IN => 8, :INSTANCEOF => 8, :DOUBLE_EQUALS => 9, :NOT_EQUALS => 9, :TRIPLE_EQUALS => 9, :NOT_DOUBLE_EQUALS => 9, :AMPERSAND => 10, :CARET => 11, :PIPE => 12, :AND => 13, :OR => 14 } end |
Instance Method Details
#collapse(called_by_binop = false) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/twostroke/ast/unsorted_binop.rb', line 61 def collapse(called_by_binop = false) left_collapsed = left.is_a?(UnsortedBinop) ? left.collapse(true) : left.collapse right_collapsed = right.is_a?(UnsortedBinop) ? right.collapse(true) : right.collapse input = [*left_collapsed, op, *right_collapsed] unless called_by_binop stack = [] output = [] input.each do |token| if token.is_a? Symbol while stack.size > 0 && UnsortedBinop.operator_precedence[stack.last] <= UnsortedBinop.operator_precedence[token] output.push stack.pop end stack.push token else output.push token end end output.push stack.pop until stack.empty? output.each do |token| if token.is_a? Symbol r = stack.pop l = stack.pop stack.push UnsortedBinop.operator_class[token].new(left: l, right: r) else stack.push token end end stack.last else input end end |