Class: Code::Object::Global

Inherits:
Code::Object show all
Defined in:
lib/code/object/global.rb

Instance Method Summary collapse

Methods inherited from Code::Object

#<=>, #==, #falsy?, #hash, #truthy?

Instance Method Details

#call(**args) ⇒ Object



4
5
6
7
8
9
10
11
12
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/code/object/global.rb', line 4

def call(**args)
  operator = args.fetch(:operator, nil)
  arguments = args.fetch(:arguments, [])
  context = args.fetch(:context)
  io = args.fetch(:io)
  globals = multi_fetch(args, *::Code::GLOBALS)
  value = arguments.first&.value

  if operator == "print"
    io.print(*arguments.map(&:value))
    ::Code::Object::Nothing.new
  elsif operator == "puts"
    io.puts(*arguments.map(&:value))
    ::Code::Object::Nothing.new
  elsif operator == "context"
    sig(arguments) { ::Code::Object::String }
    context[value] || ::Code::Object::Nothing.new
  elsif operator == "evaluate"
    sig(arguments) { ::Code::Object::String }
    Code.evaluate(value.raw)
  else
    result = context[operator]

    if result && result.is_a?(::Code::Object::Function)
      result.call(**args.merge(operator: nil))
    elsif result
      result
    else
      raise ::Code::Error::Undefined.new("#{operator} is not defined")
    end
  end
end

#to_sObject



37
38
39
# File 'lib/code/object/global.rb', line 37

def to_s
  "global"
end