Class: Peruby::Op::Eval

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

Overview

Block and string eval sharing the current lexical environment.

Instance Method Summary collapse

Methods inherited from Base

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

Constructor Details

#initialize(body, string, file: '-e') ⇒ Eval

Returns a new instance of Eval.



229
230
231
232
233
# File 'lib/peruby/op/control.rb', line 229

def initialize(body, string, file: '-e')
  @body = body
  @string = string
  @file = file
end

Instance Method Details

#run(env, context) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/peruby/op/control.rb', line 235

def run(env, context)
  env.fetch(:scalar, '@').set('')
  result = if @string
             env.with_scope do
               source = Conv.to_str(@body.run(env, :scalar))
               operation = CompileUnit.new(runtime: env.runtime, file: @file).compile(source, env:)
               operation.run_subroutine(env, context)
             end
           else
             @body.run_subroutine(env, context)
           end
  env.fetch(:scalar, '@').set('')
  result
rescue Error => e
  value = e.is_a?(PerlError) ? e.value : e.message
  value = env.runtime.error_message(value) unless value.is_a?(Ref)
  env.fetch(:scalar, '@').set(value)
  context == :list ? [] : nil
end