Class: Keisan::AST::ListAssignment
- Inherits:
-
Object
- Object
- Keisan::AST::ListAssignment
- Defined in:
- lib/keisan/ast/list_assignment.rb
Instance Attribute Summary collapse
-
#assignment ⇒ Object
readonly
Returns the value of attribute assignment.
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#lhs ⇒ Object
readonly
Returns the value of attribute lhs.
-
#rhs ⇒ Object
readonly
Returns the value of attribute rhs.
Instance Method Summary collapse
- #evaluate ⇒ Object
-
#initialize(assignment, context, lhs, rhs) ⇒ ListAssignment
constructor
A new instance of ListAssignment.
Constructor Details
#initialize(assignment, context, lhs, rhs) ⇒ ListAssignment
Returns a new instance of ListAssignment.
6 7 8 9 10 11 |
# File 'lib/keisan/ast/list_assignment.rb', line 6 def initialize(assignment, context, lhs, rhs) @assignment = assignment @context = context @lhs = lhs @rhs = rhs end |
Instance Attribute Details
#assignment ⇒ Object (readonly)
Returns the value of attribute assignment.
4 5 6 |
# File 'lib/keisan/ast/list_assignment.rb', line 4 def assignment @assignment end |
#context ⇒ Object (readonly)
Returns the value of attribute context.
4 5 6 |
# File 'lib/keisan/ast/list_assignment.rb', line 4 def context @context end |
#lhs ⇒ Object (readonly)
Returns the value of attribute lhs.
4 5 6 |
# File 'lib/keisan/ast/list_assignment.rb', line 4 def lhs @lhs end |
#rhs ⇒ Object (readonly)
Returns the value of attribute rhs.
4 5 6 |
# File 'lib/keisan/ast/list_assignment.rb', line 4 def rhs @rhs end |
Instance Method Details
#evaluate ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/keisan/ast/list_assignment.rb', line 13 def evaluate rhs = @rhs.evaluate(context) if !rhs.is_a?(List) raise Exceptions::InvalidExpression.new("To do multiple assignment, RHS must be a list") end if lhs.children.size != rhs.children.size raise Exceptions::InvalidExpression.new("To do multiple assignment, RHS list must have same length as LHS list") end i = 0 while i < lhs.children.size lhs_variable = lhs.children[i] rhs_assignment = rhs.children[i] individual_assignment = Assignment.new( children = [lhs_variable, rhs_assignment], local: assignment.local, compound_operator: assignment.compound_operator ) individual_assignment.evaluate(context) i += 1 end end |