2017-04-01 14:57:39 +02:00
|
|
|
module Vool
|
2019-08-08 11:19:27 +02:00
|
|
|
# This represents a class at the vool level. Vool is a syntax tree,
|
|
|
|
# so here the only child (or children) is a body.
|
|
|
|
# Body may either be a MethodStatement, or Statements (either empty or
|
|
|
|
# containing MethodStatement)
|
|
|
|
#
|
|
|
|
# We store the class name and the parfait class
|
|
|
|
#
|
|
|
|
# The Parfait class gets created lazily on the way down to mom, ie the clazz
|
|
|
|
# attribute will only be set after to_mom, or a direct call to create_class
|
2017-04-08 11:11:52 +02:00
|
|
|
class ClassStatement < Statement
|
2017-04-01 14:57:39 +02:00
|
|
|
attr_reader :name, :super_class_name , :body
|
2017-04-08 11:11:52 +02:00
|
|
|
attr_reader :clazz
|
2017-04-01 14:57:39 +02:00
|
|
|
|
|
|
|
def initialize( name , supe , body)
|
2018-06-29 22:29:10 +02:00
|
|
|
@name , @super_class_name = name , supe
|
|
|
|
case body
|
|
|
|
when MethodStatement
|
|
|
|
@body = Statements.new([body])
|
|
|
|
when Statements
|
|
|
|
@body = body
|
|
|
|
when nil
|
|
|
|
@body = Statements.new([])
|
|
|
|
else
|
|
|
|
raise "what body #{body}"
|
|
|
|
end
|
2017-04-08 11:11:52 +02:00
|
|
|
end
|
|
|
|
|
2019-08-08 11:19:27 +02:00
|
|
|
# This create the Parfait class, and then transforms every method
|
|
|
|
#
|
|
|
|
# As there is no class equivalnet in code, a MomCollection is returned,
|
|
|
|
# which is just a list of Mom::MethodCompilers
|
2017-04-12 10:52:23 +02:00
|
|
|
def to_mom( _ )
|
2018-06-26 19:46:58 +02:00
|
|
|
create_class_object
|
2018-07-06 19:01:17 +02:00
|
|
|
method_compilers = body.statements.collect do |node|
|
2019-02-17 13:37:50 +01:00
|
|
|
case node
|
|
|
|
when MethodStatement
|
|
|
|
node.to_mom(@clazz)
|
|
|
|
when ClassMethodStatement
|
|
|
|
node.to_mom(@clazz.meta_class)
|
|
|
|
else
|
2019-02-16 16:54:45 +01:00
|
|
|
raise "Only methods for now #{node.class}:#{node}"
|
|
|
|
end
|
2018-06-26 19:46:58 +02:00
|
|
|
end
|
2019-08-06 17:33:27 +02:00
|
|
|
Mom::MomCollection.new(method_compilers)
|
2017-04-12 10:52:23 +02:00
|
|
|
end
|
|
|
|
|
2018-03-15 12:52:56 +01:00
|
|
|
def each(&block)
|
|
|
|
block.call(self)
|
2018-03-16 06:33:29 +01:00
|
|
|
@body.each(&block) if @body
|
2017-04-08 11:11:52 +02:00
|
|
|
end
|
|
|
|
|
2019-08-08 11:19:27 +02:00
|
|
|
# This creates the Parfait class. But doesn not hadle reopening yet, so only new classes
|
|
|
|
# Creating the class involves creating the instance_type (or an initial version)
|
|
|
|
# which means knowing all used names. So we go through the code looking for
|
|
|
|
# InstanceVariables or InstanceVariable Assignments, to do that.
|
2018-03-14 13:11:09 +01:00
|
|
|
def create_class_object
|
2017-04-08 11:11:52 +02:00
|
|
|
@clazz = Parfait.object_space.get_class_by_name(@name )
|
|
|
|
if(@clazz)
|
|
|
|
#FIXME super class check with "sup"
|
2017-04-08 18:20:11 +02:00
|
|
|
#existing class, don't overwrite type (parfait only?)
|
|
|
|
else
|
2017-04-08 11:11:52 +02:00
|
|
|
@clazz = Parfait.object_space.create_class(@name , @super_class_name )
|
2019-02-12 21:36:37 +01:00
|
|
|
#TODO this should start from Object Type and add one name at a time.
|
|
|
|
# So the "trail" of types leading to this one exists.
|
|
|
|
# Also the Class always has a valid type.
|
2017-04-08 18:20:11 +02:00
|
|
|
ivar_hash = {}
|
2018-03-15 12:52:56 +01:00
|
|
|
self.each do |node|
|
|
|
|
next unless node.is_a?(InstanceVariable) or node.is_a?(IvarAssignment)
|
|
|
|
ivar_hash[node.name] = :Object
|
|
|
|
end
|
2017-04-08 18:20:11 +02:00
|
|
|
@clazz.set_instance_type( Parfait::Type.for_hash( @clazz , ivar_hash ) )
|
2019-08-08 11:19:27 +02:00
|
|
|
@clazz
|
2017-04-08 11:11:52 +02:00
|
|
|
end
|
2017-04-01 14:57:39 +02:00
|
|
|
end
|
2018-07-03 21:18:19 +02:00
|
|
|
|
|
|
|
def to_s(depth = 0)
|
|
|
|
at_depth(depth , "class #{name}" , @body.to_s(depth + 1) , "end")
|
|
|
|
end
|
2017-04-01 14:57:39 +02:00
|
|
|
end
|
|
|
|
end
|