2018-06-26 19:28:27 +02:00
|
|
|
module Vool
|
|
|
|
class BlockStatement < Statement
|
|
|
|
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.
|
|
|
|
#
|
|
|
|
# This means we do the compiler here (rather than to_mom, which is in
|
|
|
|
# fact never called)
|
2018-07-05 13:02:38 +02:00
|
|
|
def slot_definition(compiler)
|
2018-07-09 15:48:23 +02:00
|
|
|
return Mom::SlotDefinition.new(Mom::BlockConstant.new(parfait_block(compiler)) , [])
|
2018-06-26 19:28:27 +02:00
|
|
|
end
|
|
|
|
|
2018-07-09 15:48:23 +02:00
|
|
|
# create a block, a compiler for it, comile the bock and add the compiler(code)
|
|
|
|
# to the method compiler for further processing
|
2018-07-06 19:01:17 +02:00
|
|
|
def to_mom( compiler )
|
2018-07-09 15:48:23 +02:00
|
|
|
parfait_block = self.parfait_block(compiler)
|
2018-07-09 18:32:17 +02:00
|
|
|
block_compiler = Risc::BlockCompiler.new( parfait_block , compiler.get_method )
|
2018-07-09 15:48:23 +02:00
|
|
|
compiler.add_block_compiler(block_compiler)
|
|
|
|
head = body.to_mom( block_compiler )
|
2018-07-10 21:03:32 +02:00
|
|
|
block_compiler.add_mom(head)
|
2018-07-09 15:48:23 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
def normalize
|
|
|
|
BlockStatement.new( @args , @body.normalize)
|
|
|
|
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 }
|
|
|
|
Parfait::NamedList.type_for( type_hash )
|
|
|
|
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
|
|
|
|
Parfait::NamedList.type_for( type_hash )
|
2018-07-06 19:01:17 +02:00
|
|
|
end
|
2018-06-26 19:28:27 +02:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|