Class: Peruby::Op::Postfix

Inherits:
Base
  • Object
show all
Defined in:
lib/peruby/op/expression.rb

Overview

Post-increment/decrement preserving the old expression value.

Instance Method Summary collapse

Methods inherited from Base

#argument_cells, #local_slot, #lvalue, #run_subroutine, #to_callable, #warning_directive?

Constructor Details

#initialize(operator, expression) ⇒ Postfix

Returns a new instance of Postfix.



58
59
60
61
# File 'lib/peruby/op/expression.rb', line 58

def initialize(operator, expression)
  @operator = operator
  @expression = expression
end

Instance Method Details

#run(env, context) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/peruby/op/expression.rb', line 63

def run(env, context)
  cell = @expression.lvalue(env, :scalar)
  old = cell.get
  value = @operator == :increment ? Conv.increment(old) : Conv.to_num(old) - 1
  cell.set(value)
  context == :list ? [Scalar.new(old)] : old
end