Class: Peruby::Op::Prefix

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

Overview

Pre-increment/decrement returning the updated 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) ⇒ Prefix

Returns a new instance of Prefix.



74
75
76
77
# File 'lib/peruby/op/expression.rb', line 74

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

Instance Method Details

#run(env, context) ⇒ Object



79
80
81
82
83
84
# File 'lib/peruby/op/expression.rb', line 79

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