~singpolyma/dhall-ruby

ref: bccb3dd8f6e4d0bc7c6267f1c6b404865a14f5f2 dhall-ruby/lib/dhall/visitor.rb -rw-r--r-- 386 bytes
bccb3dd8Stephen Paul Weber Refactor OperatorListConcatenate typecheck 3 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# frozen_string_literal: true

module Dhall
	class Visitor
		def initialize(callbacks)
			@callbacks = callbacks
		end

		def visit(expr)
			expr.to_h.each_with_object({}) do |(attr, value), h|
				if (callback = callback_for(value))
					h[attr] = callback.call(value)
				end
			end
		end

		protected

		def callback_for(x)
			@callbacks.find { |k, _| k === x }&.last
		end
	end
end