2019-02-14 18:24:12 +01:00
|
|
|
module Vool
|
2019-08-19 10:33:12 +02:00
|
|
|
class ClassMethodExpression < Expression
|
2019-02-14 18:24:12 +01:00
|
|
|
attr_reader :name, :args , :body
|
|
|
|
|
|
|
|
def initialize( name , args , body )
|
|
|
|
@name , @args , @body = name , args , body
|
|
|
|
raise "no bod" unless @body
|
|
|
|
end
|
|
|
|
|
2019-09-24 14:44:33 +02:00
|
|
|
# create the parfait VoolMethod to hold the code for this method
|
|
|
|
#
|
|
|
|
# Must pass in the actual Parfait class (default nil is just to conform to api)
|
|
|
|
def to_parfait( clazz = nil )
|
|
|
|
raise "No class given to class method #{name}" unless clazz
|
2019-09-29 21:37:28 +02:00
|
|
|
vool_m = clazz.single_class.create_instance_method_for(name , make_arg_type , make_frame , body )
|
|
|
|
vool_m.create_callable_method_for(clazz.single_class.instance_type)
|
|
|
|
vool_m
|
2019-09-24 14:44:33 +02:00
|
|
|
end
|
|
|
|
|
2019-10-03 19:55:41 +02:00
|
|
|
def to_slot(clazz)
|
2019-09-24 16:25:19 +02:00
|
|
|
raise "not singleton #{clazz.class}" unless clazz.class == Parfait::SingletonClass
|
2019-02-14 18:24:12 +01:00
|
|
|
raise( "no class in #{self}") unless clazz
|
2019-09-24 14:44:33 +02:00
|
|
|
method = clazz.get_instance_method(name )
|
|
|
|
raise( "no class method in #{@name} in #{clazz}") unless method
|
2019-09-21 17:07:58 +02:00
|
|
|
#puts "CLass method Class:#{clazz}:#{name}"
|
2019-02-14 18:24:12 +01:00
|
|
|
compiler = method.compiler_for(clazz.instance_type)
|
2019-08-19 13:33:02 +02:00
|
|
|
each {|node| raise "Blocks not implemented" if node.is_a?(LambdaExpression)}
|
2019-02-14 18:24:12 +01:00
|
|
|
compiler
|
|
|
|
end
|
|
|
|
|
|
|
|
def each(&block)
|
|
|
|
block.call(self)
|
|
|
|
@body.each(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
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 )
|
2019-02-14 18:24:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def to_s(depth = 0)
|
|
|
|
arg_str = @args.collect{|a| a.to_s}.join(', ')
|
2019-09-30 16:09:13 +02:00
|
|
|
at_depth(depth , "def self.#{name}(#{arg_str})\n#{@body.to_s(1)}\nend")
|
2019-02-14 18:24:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def make_frame
|
|
|
|
nodes = []
|
|
|
|
@body.each { |node| nodes << node }
|
|
|
|
type_hash = {}
|
|
|
|
nodes.each do |node|
|
|
|
|
next unless node.is_a?(LocalVariable) or node.is_a?(LocalAssignment)
|
|
|
|
type_hash[node.name] = :Object
|
|
|
|
end
|
2019-09-18 21:07:05 +02:00
|
|
|
Parfait::Type.for_hash( type_hash )
|
2019-02-14 18:24:12 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|