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