From bca4d8af659fa84f220a35ec9b7b2d040d374dc9 Mon Sep 17 00:00:00 2001 From: Stephen Paul Weber Date: Tue, 19 Mar 2019 22:10:56 -0500 Subject: [PATCH] Flatten/unfill applications and builtins when encoding --- lib/dhall/ast.rb | 15 ++++++++++++++- lib/dhall/builtins.rb | 16 +++++++++++++++- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/dhall/ast.rb b/lib/dhall/ast.rb index 38f1021..823aa42 100644 --- a/lib/dhall/ast.rb +++ b/lib/dhall/ast.rb @@ -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 diff --git a/lib/dhall/builtins.rb b/lib/dhall/builtins.rb index 25844ce..f1ee321 100644 --- a/lib/dhall/builtins.rb +++ b/lib/dhall/builtins.rb @@ -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 -- 2.34.5