Class: Peruby::Op::Unary
Overview
Unary scalar expression.
Instance Method Summary collapse
-
#initialize(operator, expression) ⇒ Unary
constructor
A new instance of Unary.
- #reference(env, context) ⇒ Object
- #run(env, context) ⇒ Object
Methods inherited from Base
#argument_cells, #local_slot, #lvalue, #run_subroutine, #to_callable, #warning_directive?
Constructor Details
#initialize(operator, expression) ⇒ Unary
Returns a new instance of Unary.
7 8 9 10 |
# File 'lib/peruby/op/expression.rb', line 7 def initialize(operator, expression) @operator = operator @expression = expression end |
Instance Method Details
#reference(env, context) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/peruby/op/expression.rb', line 26 def reference(env, context) return @expression.references(env, context) if @expression.respond_to?(:references) target = begin @expression.lvalue(env, :scalar) rescue PerlError Scalar.new(@expression.run(env, :scalar)) end value = Ref.new(target) context == :list ? [Scalar.new(value)] : value end |
#run(env, context) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/peruby/op/expression.rb', line 12 def run(env, context) return reference(env, context) if @operator == :reference value = @expression.run(env, :scalar) if @operator == :negate && value.is_a?(Ref) found, overloaded = env.runtime.overloaded(value, 'neg') return context == :list ? [Scalar.new(overloaded)] : overloaded if found end operations = { negate: -> { -Conv.to_num(value) }, positive: -> { Conv.to_num(value) }, not: -> { Conv.truthy?(value) ? '' : 1 }, bit_not: -> { Conv.bit_not(value) } } result = operations.fetch(@operator).call context == :list ? [Scalar.new(result)] : result end |