finish typing variables
This commit is contained in:
@ -2,12 +2,12 @@ module Bosl
|
||||
Compiler.class_eval do
|
||||
|
||||
def on_class_field expression
|
||||
puts expression.inspect
|
||||
#puts expression.inspect
|
||||
type , name , value = *expression
|
||||
|
||||
for_class = self.method.for_class
|
||||
index = for_class.object_layout.variable_index(name)
|
||||
raise "class field already defined:#{name} for class #{for_class.name}" if index
|
||||
#raise "class field already defined:#{name} for class #{for_class.name}" if index
|
||||
puts "Define field #{name} on class #{for_class.name}"
|
||||
index = for_class.object_layout.add_instance_variable( name ) #TODO need typing
|
||||
|
||||
|
@ -2,16 +2,16 @@ module Bosl
|
||||
Compiler.class_eval do
|
||||
|
||||
def on_field_def expression
|
||||
# puts expression.inspect
|
||||
#puts expression.inspect
|
||||
type , name , value = *expression
|
||||
|
||||
index = method.ensure_local( name )
|
||||
index = method.ensure_local( name , type )
|
||||
|
||||
if value
|
||||
value = process( value )
|
||||
end
|
||||
|
||||
Virtual::Return.new( value )
|
||||
Virtual::Return.new( type , value )
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -2,12 +2,12 @@ module Bosl
|
||||
Compiler.class_eval do
|
||||
# function attr_reader :name, :params, :body , :receiver
|
||||
def on_function expression
|
||||
# puts expression.inspect
|
||||
#puts expression.inspect
|
||||
return_type , name , parameters, kids , receiver = *expression
|
||||
name = name.to_a.first
|
||||
args = parameters.to_a.collect do |p|
|
||||
raise "error, argument must be a identifier, not #{p}" unless p.type == :parameter
|
||||
Parfait::Variable.new( p.first , p[1])
|
||||
Parfait::Variable.new( *p)
|
||||
end
|
||||
|
||||
if receiver
|
||||
|
@ -13,8 +13,12 @@ module Bosl
|
||||
if( index = method.has_arg(name))
|
||||
method.source.add_code Virtual::Set.new( Virtual::ArgSlot.new(index,:int ) , ret)
|
||||
else # or a local so it is in the frame
|
||||
index = method.ensure_local( name )
|
||||
method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(index,:int ) , ret )
|
||||
index = method.has_local( name )
|
||||
if(index)
|
||||
method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(index,:int ) , ret )
|
||||
else
|
||||
raise "must define variable #{name} before using it"
|
||||
end
|
||||
end
|
||||
return ret
|
||||
end
|
||||
|
@ -10,8 +10,17 @@ module Bosl
|
||||
name , value = *expression
|
||||
name = name.to_a.first
|
||||
v = process(value)
|
||||
index = method.ensure_local( name )
|
||||
method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(:int,index ) , v )
|
||||
index = method.has_local( name )
|
||||
if(index)
|
||||
method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(:int,index ) , v )
|
||||
else
|
||||
index = method.has_arg( name )
|
||||
if(index)
|
||||
method.source.add_code Virtual::Set.new(Virtual::ArgSlot.new(:int,index ) , v )
|
||||
else
|
||||
raise "must define variable #{name} before using it in #{@method.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -34,46 +34,48 @@ module Parfait
|
||||
attributes [:name , :arguments , :for_class , :code , :locals ]
|
||||
|
||||
|
||||
# determine whether this method has a variable by the given name
|
||||
# variables are locals and and arguments
|
||||
# used to determine if a send must be issued
|
||||
# return index of the name into the message if so
|
||||
def has_var name
|
||||
raise "has_var #{name}.#{name.class}" unless name.is_a? Symbol
|
||||
index = has_arg(name)
|
||||
return index if index
|
||||
has_local(name)
|
||||
end
|
||||
|
||||
# determine whether this method has an argument by the name
|
||||
def has_arg name
|
||||
raise "has_arg #{name}.#{name.class}" unless name.is_a? Symbol
|
||||
self.arguments.index_of name
|
||||
max = self.arguments.get_length
|
||||
counter = 1
|
||||
while( counter <= max )
|
||||
if( self.arguments.get(counter).name == name)
|
||||
return counter
|
||||
end
|
||||
counter = counter + 1
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
# determine if method has a local variable or tmp (anonymous local) by given name
|
||||
def has_local name
|
||||
raise "has_local #{name}.#{name.class}" unless name.is_a? Symbol
|
||||
index = self.locals.index_of(name)
|
||||
index
|
||||
max = self.locals.get_length
|
||||
counter = 1
|
||||
while( counter <= max )
|
||||
if( self.locals.get(counter).name == name)
|
||||
return counter
|
||||
end
|
||||
counter = counter + 1
|
||||
end
|
||||
return nil
|
||||
end
|
||||
|
||||
def ensure_local name
|
||||
def ensure_local name , type
|
||||
index = has_local name
|
||||
return index if index
|
||||
self.locals.push name
|
||||
var = Variable.new( type , name)
|
||||
self.locals.push var
|
||||
self.locals.get_length
|
||||
end
|
||||
|
||||
def get_var name
|
||||
var = has_var name
|
||||
raise "no var #{name} in method #{self.name} , #{self.locals} #{self.arguments}" unless var
|
||||
var
|
||||
end
|
||||
|
||||
def sof_reference_name
|
||||
self.name
|
||||
end
|
||||
|
||||
def inspect
|
||||
"#{self.for_class.name}:#{name}(#{arguments.inspect})"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -93,6 +93,7 @@ module Virtual
|
||||
# Objects are data and get assembled after functions
|
||||
def add_object o
|
||||
return false if @objects[o.object_id]
|
||||
return if o.is_a? Fixnum
|
||||
raise "adding non parfait #{o.class}" unless o.is_a? Parfait::Object or o.is_a? Symbol
|
||||
@objects[o.object_id] = o
|
||||
true
|
||||
|
Reference in New Issue
Block a user