Class: Crisp::Functions::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/crisp/functions/core.rb

Class Method Summary collapse

Class Method Details

.load(env) ⇒ 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/crisp/functions/core.rb', line 4

def self.load(env)

  Function.new do
    print params_evaled.collect(&:to_s).join(' ') + "\n"
  end.bind('println', env)

  Function.new do
    validate_params_count(2, params.size)

    value = params_evaled[1]

    if value.class.name == "Crisp::Function"
      value.bind(params_values[0], env)
    else
      env[params_values[0]] = value
    end
  end.bind('def', env)

  Function.new do
    validate_params_count(2, params.size)

    if params[0].class.name != "Crisp::ArrayLiteral"
      raise ArgumentError, "no parameter list defined"
    end

    if params[1].class.name != "Crisp::Operation"
      raise ArgumentError, "no function body defined"
    end

    fn_param_list = params[0].raw_elements
    fn_operation = params[1]

    Function.new do
      validate_params_count(fn_param_list.size, params.size)

      local_env = Env.new
      fn_param_list.each_with_index do |key, idx|
        local_env[key.eval(env)] = params[idx]
      end

      chained_env = ChainedEnv.new(local_env, env)

      fn_operation.eval(chained_env)
    end
  end.bind('fn', env)

end