2019-10-03 23:36:49 +02:00
|
|
|
module Sol
|
2019-08-19 10:33:12 +02:00
|
|
|
class LambdaExpression < Expression
|
2018-06-26 19:28:27 +02:00
|
|
|
attr_reader :args , :body , :clazz
|
|
|
|
|
|
|
|
def initialize( args , body , clazz = nil)
|
|
|
|
@args , @body = args , body
|
|
|
|
raise "no bod" unless @body
|
|
|
|
@clazz = clazz
|
|
|
|
end
|
|
|
|
|
2018-07-06 19:01:17 +02:00
|
|
|
# because of normalization (of send), slot_definition is called first,
|
|
|
|
# to assign the block to a variable.
|
|
|
|
#
|
2019-10-03 19:55:41 +02:00
|
|
|
# This means we do the compiler here (rather than to_slot, which is in
|
2018-07-06 19:01:17 +02:00
|
|
|
# fact never called)
|
2020-02-17 08:29:45 +01:00
|
|
|
def to_slotted(compiler)
|
2019-08-19 13:33:02 +02:00
|
|
|
compile(compiler) unless @parfait_block
|
2020-02-17 08:45:54 +01:00
|
|
|
return SlotMachine::Slotted.for(SlotMachine::LambdaConstant.new(parfait_block(compiler)) )
|
2018-06-26 19:28:27 +02:00
|
|
|
end
|
|
|
|
|
2019-12-01 11:09:24 +01:00
|
|
|
# create a block, a compiler for it, compile the block and add the compiler(code)
|
2018-07-09 15:48:23 +02:00
|
|
|
# to the method compiler for further processing
|
2019-08-19 13:33:02 +02:00
|
|
|
def compile( compiler )
|
2018-07-09 15:48:23 +02:00
|
|
|
parfait_block = self.parfait_block(compiler)
|
2019-10-03 19:55:41 +02:00
|
|
|
block_compiler = SlotMachine::BlockCompiler.new( parfait_block , compiler.get_method )
|
|
|
|
head = body.to_slot( block_compiler )
|
2019-08-07 14:08:45 +02:00
|
|
|
block_compiler.add_code(head)
|
2019-09-28 16:24:10 +02:00
|
|
|
compiler.add_method_compiler(block_compiler)
|
2018-07-06 19:01:17 +02:00
|
|
|
end
|
|
|
|
|
2018-06-26 19:28:27 +02:00
|
|
|
def each(&block)
|
|
|
|
block.call(self)
|
|
|
|
@body.each(&block)
|
|
|
|
end
|
|
|
|
|
2019-08-19 09:40:22 +02:00
|
|
|
def to_s(depth=0)
|
|
|
|
"Block #{args} #{body}"
|
|
|
|
end
|
2018-07-09 15:48:23 +02:00
|
|
|
# create the parfait block (parfait representation of the block, a Callable similar
|
|
|
|
# to CallableMethod)
|
|
|
|
def parfait_block(compiler)
|
|
|
|
return @parfait_block if @parfait_block
|
2018-07-09 18:32:17 +02:00
|
|
|
@parfait_block = compiler.create_block( make_arg_type , make_frame(compiler))
|
2018-07-07 16:34:48 +02:00
|
|
|
end
|
2018-07-09 15:48:23 +02:00
|
|
|
|
|
|
|
private
|
2018-07-07 16:34:48 +02:00
|
|
|
def make_arg_type( )
|
|
|
|
type_hash = {}
|
|
|
|
@args.each {|arg| type_hash[arg] = :Object }
|
2019-09-18 21:07:05 +02:00
|
|
|
Parfait::Type.for_hash( type_hash )
|
2018-07-07 16:34:48 +02:00
|
|
|
end
|
2018-07-09 16:55:45 +02:00
|
|
|
def make_frame(compiler)
|
2018-07-07 16:34:48 +02:00
|
|
|
type_hash = {}
|
|
|
|
@body.each do |node|
|
|
|
|
next unless node.is_a?(LocalVariable) or node.is_a?(LocalAssignment)
|
2018-07-09 16:55:45 +02:00
|
|
|
next if compiler.in_scope?(node.name)
|
2018-07-07 16:34:48 +02:00
|
|
|
type_hash[node.name] = :Object
|
|
|
|
end
|
2019-09-18 21:07:05 +02:00
|
|
|
Parfait::Type.for_hash( type_hash )
|
2018-07-06 19:01:17 +02:00
|
|
|
end
|
2018-06-26 19:28:27 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|