A fork of Crisp for HARP
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

68 rindas
1.5 KiB

  1. require "./types"
  2. require "./error"
  3. module Mal
  4. class Env
  5. property data
  6. def initialize(@outer)
  7. @data = {} of String => Mal::Type
  8. end
  9. def initialize(@outer, binds, exprs : Array(Mal::Type))
  10. @data = {} of String => Mal::Type
  11. eval_error "binds must be list or vector" unless binds.is_a? Array
  12. # Note:
  13. # Array#zip() can't be used because overload resolution failed
  14. (0...binds.size).each do |idx|
  15. sym = binds[idx].unwrap
  16. eval_error "bind name must be symbol" unless sym.is_a? Mal::Symbol
  17. if sym.str == "&"
  18. eval_error "missing variable parameter name" if binds.size == idx
  19. next_param = binds[idx+1].unwrap
  20. eval_error "bind name must be symbol" unless next_param.is_a? Mal::Symbol
  21. var_args = Mal::List.new
  22. exprs[idx..-1].each{|e| var_args << e} if idx < exprs.size
  23. @data[next_param.str] = Mal::Type.new var_args
  24. break
  25. end
  26. @data[sym.str] = exprs[idx]
  27. end
  28. end
  29. def dump
  30. puts "ENV BEGIN".colorize.red
  31. @data.each do |k, v|
  32. puts " #{k} -> #{print(v)}".colorize.red
  33. end
  34. puts "ENV END".colorize.red
  35. end
  36. def set(key, value)
  37. @data[key] = value
  38. end
  39. def find(key)
  40. return self if @data.has_key? key
  41. o = @outer
  42. if o
  43. o.find key
  44. else
  45. nil
  46. end
  47. end
  48. def get(key)
  49. e = find key
  50. eval_error "'#{key}' not found" unless e
  51. e.data[key]
  52. end
  53. end
  54. end