Module: Malady::Evaluator

Defined in:
lib/malady/evaluator.rb

Class Method Summary collapse

Class Method Details

.eval_ast(env, ast) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/malady/evaluator.rb', line 27

def eval_ast(env, ast)
  type = ast.first
  rest = ast[1..-1]
  case type
  when :symbol
    env[ast[1]]
  when :list
    result = [:list]
    result + rest.map { |ast| evaluate(env, ast) }
  when :integer
    ast[1]
  else
    ast
  end
end

.eval_with_repl_env(ast) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/malady/evaluator.rb', line 17

def eval_with_repl_env(ast)
  repl_env = {
    '+' => lambda { |x, y| x + y },
    '-' => lambda { |x, y| x - y },
    '/' => lambda { |x, y| Integer(x / y) },
    '*' => lambda { |x, y| x * y }
  }
  evaluate(repl_env, ast)
end

.evaluate(env, ast) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/malady/evaluator.rb', line 4

def evaluate(env, ast)
  type = ast.first
  rest = ast[1..-1]
  if type == :list
    evaluated_list = eval_ast(env, ast)
    fn = evaluated_list[1]
    args = evaluated_list[2..-1]
    fn.call(*args)
  else
    eval_ast(env, ast)
  end
end