~singpolyma/dhall-ruby

feb773cff1da6de19999e499e8d08d5f215be7da — Stephen Paul Weber 3 years ago d2bfbab
Refactor TextLiteral typecheck
1 files changed, 25 insertions(+), 11 deletions(-)

M lib/dhall/typecheck.rb
M lib/dhall/typecheck.rb => lib/dhall/typecheck.rb +25 -11
@@ 183,20 183,34 @@ module Dhall
				@lit = lit
			end

			def annotate(context)
				chunks = @lit.chunks.map do |c|
					if c.is_a?(Dhall::Text)
						c
					else
						annotated = TypeChecker.for(c).annotate(context)
						if annotated.type != Dhall::Variable["Text"]
							raise TypeError, "Cannot interpolate non-Text: " \
							                 "#{annotated.type}"
			class Chunks
				def initialize(chunks)
					@chunks = chunks
				end

				def map
					self.class.new(@chunks.map { |c|
						if c.is_a?(Dhall::Text)
							c
						else
							yield c
						end
						annotated
					end
					})
				end

				def to_a
					@chunks
				end
			end

			def annotate(context)
				chunks = Chunks.new(@lit.chunks).map { |c|
					TypeChecker.for(c).annotate(context).tap do |annotated|
						TypeChecker.assert annotated.type, Dhall::Variable["Text"],
						                   "Cannot interpolate #{annotated.type}"
					end
				}.to_a

				Dhall::TypeAnnotation.new(
					value: @lit.with(chunks: chunks),
					type:  Dhall::Variable["Text"]