Class: Relisp::HashTable

Inherits:
Object show all
Defined in:
lib/relisp/type_conversion/programming_types.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_elisp(object) ⇒ Object



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# File 'lib/relisp/type_conversion/programming_types.rb', line 293

def self.from_elisp(object)
  slave = object[:slave]
  object_variable = slave.get_permanent_variable(object[:variable])

  unless slave.elisp_eval("(type-of #{object_variable})") == "hash-table".to_sym
    raise ArgumentError, "#{object_variable} isn't a hash-table"
  end
  keys_var = slave.new_elisp_variable
  vals_var = slave.new_elisp_variable
  slave.elisp_exec( "(setq #{keys_var} nil)" )
  slave.elisp_exec( "(setq #{vals_var} nil)" )
  slave.elisp_exec( "(maphash (lambda (key val)
             (setq #{keys_var} (append #{keys_var} (list key)))
             (setq #{vals_var} (append #{vals_var} (list val)))) 
           #{object_variable})" )
  keys = Cons.new(keys_var, slave)
  vals = Cons.new(vals_var, slave)
  keys = keys.to_list
  vals = vals.to_list
  hash = Hash.new
  keys.each_index do |i|
    hash[keys[i]] = vals[i]
  end

  [:object_variable, :keys_var, :vals_var]. each do |var|
    slave.elisp_exec "(makunbound '#{var})"
  end

  return hash
end

Instance Method Details

#to_elispObject



324
325
326
327
328
329
330
331
# File 'lib/relisp/type_conversion/programming_types.rb', line 324

def to_elisp
  lisp =  "(progn \n"
  lisp << "  (let ((--temp--relisp--variable (make-hash-table))) \n"
  each_pair do |key, val|
    lisp << "    (puthash #{key.to_elisp} #{val.to_elisp} --temp--relisp--variable) \n"
  end
  lisp << "--temp--relisp--variable))"
end