~singpolyma/dhall-ruby

8aaf7a2cbd120d8570eeadfe0f3409cd47e94655 — Stephen Paul Weber 5 years ago 1868c94
LetBlock is just sugar now and not part of the AST
5 files changed, 16 insertions(+), 47 deletions(-)

M lib/dhall/ast.rb
M lib/dhall/binary.rb
M lib/dhall/normalize.rb
M lib/dhall/parser.rb
M lib/dhall/typecheck.rb
M lib/dhall/ast.rb => lib/dhall/ast.rb +6 -22
@@ 1784,10 1784,10 @@ module Dhall

		def flatten
			flattened = body.is_a?(LetIn) ? body.flatten : body
			if flattened.is_a?(LetIn) || flattened.is_a?(LetBlock)
				LetBlock.for(lets: [let] + flattened.lets, body: flattened.body)
			if flattened.is_a?(LetBlock)
				LetBlock.new(lets: lets + flattened.lets, body: flattened.body)
			else
				self
				LetBlock.new(lets: lets, body: body)
			end
		end



@@ 1810,28 1810,16 @@ module Dhall
		end

		def as_json
			[25, *let.as_json, body.as_json]
			flatten.as_json
		end
	end

	class LetBlock < Expression
	class LetBlock
		include(ValueSemantics.for_attributes do
			lets Util::ArrayOf.new(Let, min: 2)
			lets Util::ArrayOf.new(Let)
			body Expression
		end)

		def self.for(lets:, body:)
			if lets.length == 1
				LetIn.new(let: lets.first, body: body)
			else
				new(lets: lets, body: body)
			end
		end

		def flatten
			unflatten.flatten
		end

		def unflatten
			lets.reverse.reduce(body) do |inside, let|
				letin = LetIn.new(let: let, body: inside)


@@ 1839,10 1827,6 @@ module Dhall
			end
		end

		def desugar
			unflatten(&:desugar)
		end

		def as_json
			[25, *lets.flat_map(&:as_json), body.as_json]
		end

M lib/dhall/binary.rb => lib/dhall/binary.rb +5 -5
@@ 268,18 268,18 @@ module Dhall
		end
	end

	class LetBlock
	class LetIn
		def self.decode(*parts)
			body = Dhall.decode(parts.pop)
			lets = parts.each_slice(3).map do |(var, type, assign)|
			parts.each_slice(3).map { |(var, type, assign)|
				Let.new(
					var:    var,
					assign: Dhall.decode(assign),
					type:   type.nil? ? nil : Dhall.decode(type)
				)
			}.reverse.reduce(body) do |inside, let|
				LetIn.new(let: let, body: inside)
			end

			self.for(lets: lets, body: body)
		end
	end



@@ 341,7 341,7 @@ module Dhall
		nil,
		nil,
		Import,
		LetBlock,
		LetIn,
		TypeAnnotation,
		ToMap,
		EmptyList

M lib/dhall/normalize.rb => lib/dhall/normalize.rb +0 -14
@@ 452,20 452,6 @@ module Dhall
		end
	end

	class LetBlock
		def normalize
			desugar.normalize
		end

		def shift(amount, name, min_index)
			unflatten.shift(amount, name, min_index)
		end

		def substitute(svar, with_expr)
			unflatten.substitute(svar, with_expr)
		end
	end

	class TypeAnnotation
		def normalize
			value.normalize

M lib/dhall/parser.rb => lib/dhall/parser.rb +5 -4
@@ 31,10 31,11 @@ module Dhall
			end

			def let_binding
				LetBlock.for(
					lets: captures(:let_binding).map(&:value),
					body: capture(:expression).value
				)
				captures(:let_binding).reverse.reduce(
					capture(:expression).value
				) do |inside, let|
					LetIn.new(let: let.value, body: inside)
				end
			end

			def lambda

M lib/dhall/typecheck.rb => lib/dhall/typecheck.rb +0 -2
@@ 931,8 931,6 @@ module Dhall
			end
		end

		TypeChecker.register ->(blk) { LetIn.for(blk.unflatten) }, Dhall::LetBlock

		class LetIn
			TypeChecker.register self, Dhall::LetIn