Class: AbstractSyntaxTreeBuilder
- Inherits:
-
Object
- Object
- AbstractSyntaxTreeBuilder
- Defined in:
- lib/lamep/abstract_syntax_tree_builder.rb
Instance Method Summary collapse
- #build_tree ⇒ Object
-
#initialize(postfix) ⇒ AbstractSyntaxTreeBuilder
constructor
A new instance of AbstractSyntaxTreeBuilder.
Constructor Details
#initialize(postfix) ⇒ AbstractSyntaxTreeBuilder
Returns a new instance of AbstractSyntaxTreeBuilder.
3 4 5 |
# File 'lib/lamep/abstract_syntax_tree_builder.rb', line 3 def initialize(postfix) @postfix = postfix end |
Instance Method Details
#build_tree ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/lamep/abstract_syntax_tree_builder.rb', line 7 def build_tree until @postfix.length == 1 operator_index = first_operator_index fail NotEnoughOperatorsException if operator_index.nil? exp = Operator.factory!(@postfix.slice!(operator_index)) most_left_child = operator_index - exp::ARITY fail NotEnoughOperandsException, expression: exp if most_left_child < 0 children = @postfix.slice!(most_left_child, exp::ARITY) @postfix.insert(most_left_child, exp.new(*children)) end @postfix.first end |