Sha256: 917cb61f2b79a725abf98fe358644a36a5af0a956e58c855b5ef707abd686515

Contents?: true

Size: 865 Bytes

Versions: 1

Compression:

Stored size: 865 Bytes

Contents

require "lasp/parser"
require "lasp/env"

module Lasp
  module_function

  def eval(ast, env)
    case ast
    when Symbol then env.fetch(ast)
    when Array  then eval_form(ast, env)
    else ast
    end
  end

  def eval_form(form, env)
    head, *tail = *form

    if head == :def
      key, value = tail
      env[key] = Lasp::eval(value, env)
    elsif head == :fn
      params, func = tail
      -> (*args) { Lasp::eval(func, env.merge(Hash[params.zip(args)])) }
    elsif head == :do
      tail.map { |form| Lasp::eval(form, env) }.last
    elsif head == :if
      conditional, true_form, false_form = tail
      Lasp::eval(conditional, env) ? Lasp::eval(true_form, env) : Lasp::eval(false_form, env)
    elsif Proc === head
      head.(*tail)
    else
      fn = Lasp::eval(head, env)
      fn.(*tail.map { |form| Lasp::eval(form, env) })
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
lasp-0.6.0 lib/lasp/eval.rb