~singpolyma/dhall-ruby

bca4d8af659fa84f220a35ec9b7b2d040d374dc9 — Stephen Paul Weber 3 years ago 9e53452
Flatten/unfill applications and builtins when encoding
2 files changed, 29 insertions(+), 2 deletions(-)

M lib/dhall/ast.rb
M lib/dhall/builtins.rb
M lib/dhall/ast.rb => lib/dhall/ast.rb +14 -1
@@ 111,8 111,21 @@ module Dhall
			argument Expression
		end)

		def flatten
			f, args = if function.is_a?(Application)
				function.flatten
			elsif function.is_a?(Builtin) && (unfilled = function.unfill).is_a?(Application)
				unfilled.flatten
			else
				[function, []]
			end

			[f, args + [argument]]
		end

		def as_json
			[0, function.as_json, argument.as_json]
			function, arguments = flatten
			[0, function.as_json, *arguments.map(&:as_json)]
		end
	end


M lib/dhall/builtins.rb => lib/dhall/builtins.rb +15 -1
@@ 13,8 13,22 @@ module Dhall
			end
		end

		def unfill
			attributes.reduce(self.class.new) do |f, attr|
				if send(attr.name).nil?
					f
				else
					Application.new(function: f, argument: send(attr.name))
				end
			end
		end

		def as_json
			self.class.name.split(/::/).last.gsub(/_/, "/")
			if (unfilled = unfill).class != self.class
				unfilled.as_json
			else
				self.class.name.split(/::/).last.gsub(/_/, "/")
			end
		end

		protected