2015-05-05 19:37:36 +03:00
|
|
|
require "./types"
|
2015-05-12 05:33:03 +03:00
|
|
|
require "./error"
|
2015-05-05 19:37:36 +03:00
|
|
|
|
|
|
|
module Mal
|
|
|
|
|
|
|
|
class Env
|
|
|
|
property data
|
|
|
|
|
|
|
|
def initialize(@outer)
|
|
|
|
@data = {} of String => Mal::Type
|
|
|
|
end
|
|
|
|
|
2015-05-07 21:04:42 +03:00
|
|
|
def initialize(@outer, binds, exprs : Array(Mal::Type))
|
|
|
|
@data = {} of String => Mal::Type
|
|
|
|
|
2015-05-12 05:33:03 +03:00
|
|
|
eval_error "binds must be list or vector" unless binds.is_a? Array
|
2015-05-07 21:04:42 +03:00
|
|
|
|
|
|
|
# Note:
|
|
|
|
# Array#zip() can't be used because overload resolution failed
|
2015-05-09 20:05:24 +03:00
|
|
|
(0...binds.size).each do |idx|
|
2015-05-13 17:03:03 +03:00
|
|
|
sym = binds[idx].unwrap
|
|
|
|
eval_error "bind name must be symbol" unless sym.is_a? Mal::Symbol
|
2015-05-12 21:16:36 +03:00
|
|
|
|
|
|
|
if sym.str == "&"
|
2015-05-13 17:03:03 +03:00
|
|
|
eval_error "missing variable parameter name" if binds.size == idx
|
|
|
|
next_param = binds[idx+1].unwrap
|
|
|
|
eval_error "bind name must be symbol" unless next_param.is_a? Mal::Symbol
|
|
|
|
var_args = Mal::List.new
|
|
|
|
exprs[idx..-1].each{|e| var_args << e} if idx < exprs.size
|
|
|
|
@data[next_param.str] = Mal::Type.new var_args
|
2015-05-12 21:16:36 +03:00
|
|
|
break
|
|
|
|
end
|
|
|
|
|
2015-05-13 17:03:03 +03:00
|
|
|
@data[sym.str] = exprs[idx]
|
2015-05-07 21:04:42 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-05 19:37:36 +03:00
|
|
|
def set(key, value)
|
|
|
|
@data[key] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
def find(key)
|
|
|
|
return self if @data.has_key? key
|
|
|
|
|
|
|
|
o = @outer
|
|
|
|
if o
|
|
|
|
o.find key
|
|
|
|
else
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(key)
|
|
|
|
e = find(key)
|
2015-05-12 05:33:03 +03:00
|
|
|
eval_error "#{key} not found" unless e
|
2015-05-05 19:37:36 +03:00
|
|
|
e.data[key]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|