namespace cleanup

remove unnecessary Virtual namespace prefix
which was a large motivation for the move
This commit is contained in:
Torsten Ruger 2015-05-08 15:19:30 +03:00
parent de8fe46fe0
commit d41676fdfd
7 changed files with 53 additions and 53 deletions

View File

@ -13,30 +13,30 @@ module Virtual
# attr_reader :value # attr_reader :value
def self.compile_integer expression , method def self.compile_integer expression , method
int = Virtual::IntegerConstant.new(expression.value) int = IntegerConstant.new(expression.value)
to = Virtual::Return.new(Virtual::Integer , int) to = Return.new(Integer , int)
method.add_code Virtual::Set.new( to , int) method.add_code Set.new( to , int)
to to
end end
def self.compile_true expression , method def self.compile_true expression , method
value = Virtual::TrueConstant.new value = TrueConstant.new
to = Virtual::Return.new(Virtual::Reference , value) to = Return.new(Reference , value)
method.add_code Virtual::Set.new( to , value ) method.add_code Set.new( to , value )
to to
end end
def self.compile_false expression , method def self.compile_false expression , method
value = Virtual::FalseConstant.new value = FalseConstant.new
to = Virtual::Return.new(Virtual::Reference , value) to = Return.new(Reference , value)
method.add_code Virtual::Set.new( to , value ) method.add_code Set.new( to , value )
to to
end end
def self.compile_nil expression , method def self.compile_nil expression , method
value = Virtual::NilConstant.new value = NilConstant.new
to = Virtual::Return.new(Virtual::Reference , value) to = Return.new(Reference , value)
method.add_code Virtual::Set.new( to , value ) method.add_code Set.new( to , value )
to to
end end
@ -45,7 +45,7 @@ module Virtual
# otherwise it's a method without args and a send is issued. # otherwise it's a method without args and a send is issued.
# whichever way this goes the result is stored in the return slot (as all compiles) # whichever way this goes the result is stored in the return slot (as all compiles)
def self.compile_name expression , method def self.compile_name expression , method
return Virtual::Self.new( Virtual::Mystery ) if expression.name == :self return Self.new( Mystery ) if expression.name == :self
name = expression.name name = expression.name
if method.has_var(expression.name) if method.has_var(expression.name)
# either an argument, so it's stored in message # either an argument, so it's stored in message
@ -62,19 +62,19 @@ module Virtual
def self.compile_module expression , method def self.compile_module expression , method
clazz = Virtual::BootSpace.space.get_or_create_class name clazz = BootSpace.space.get_or_create_class name
raise "uups #{clazz}.#{name}" unless clazz raise "uups #{clazz}.#{name}" unless clazz
to = Virtual::Return.new(Virtual::Reference , clazz ) to = Return.new(Reference , clazz )
method.add_code Virtual::Set.new( to , clazz ) method.add_code Set.new( to , clazz )
to to
end end
# attr_reader :string # attr_reader :string
def self.compile_string expression , method def self.compile_string expression , method
value = Virtual::StringConstant.new(expression.string) value = StringConstant.new(expression.string)
to = Virtual::Return.new(Virtual::Reference , value) to = Return.new(Reference , value)
Virtual::BootSpace.space.add_object value BootSpace.space.add_object value
method.add_code Virtual::Set.new( to , value ) method.add_code Set.new( to , value )
to to
end end
@ -85,17 +85,17 @@ module Virtual
raise "oh noo, nil from where #{expression.right.inspect}" unless r raise "oh noo, nil from where #{expression.right.inspect}" unless r
index = method.has_arg(name) index = method.has_arg(name)
if index if index
method.add_code Virtual::Set.new(Virtual::Return.new , Virtual::MessageSlot.new(index , r,type , r )) method.add_code Set.new(Return.new , MessageSlot.new(index , r,type , r ))
else else
index = method.ensure_local(expression.left.name) index = method.ensure_local(expression.left.name)
method.add_code Virtual::Set.new(Virtual::Return.new , Virtual::FrameSlot.new(index , r.type , r )) method.add_code Set.new(Return.new , FrameSlot.new(index , r.type , r ))
end end
r r
end end
def self.compile_variable expression, method def self.compile_variable expression, method
method.add_code Virtual::InstanceGet.new(expression.name) method.add_code InstanceGet.new(expression.name)
Virtual::Return.new( Virtual::Mystery ) Return.new( Mystery )
end end
end end
end end

View File

@ -6,23 +6,23 @@ module Virtual
def self.compile_callsite expession , method def self.compile_callsite expession , method
me = Compiler.compile( expession.receiver , method ) me = Compiler.compile( expession.receiver , method )
method.add_code Virtual::NewMessage.new method.add_code NewMessage.new
method.add_code Virtual::Set.new(Virtual::NewSelf.new(me.type), me) method.add_code Set.new(NewSelf.new(me.type), me)
method.add_code Virtual::Set.new(Virtual::NewName.new(), Virtual::StringConstant.new(expession.name)) method.add_code Set.new(NewName.new(), StringConstant.new(expession.name))
compiled_args = [] compiled_args = []
expession.args.each_with_index do |arg , i| expession.args.each_with_index do |arg , i|
#compile in the running method, ie before passing control #compile in the running method, ie before passing control
val = Compiler.compile( arg , method) val = Compiler.compile( arg , method)
# move the compiled value to it's slot in the new message # move the compiled value to it's slot in the new message
to = Virtual::NewMessageSlot.new(i ,val.type , val) to = NewMessageSlot.new(i ,val.type , val)
# (doing this immediately, not after the loop, so if it's a return it won't get overwritten) # (doing this immediately, not after the loop, so if it's a return it won't get overwritten)
method.add_code Virtual::Set.new(to , val ) method.add_code Set.new(to , val )
compiled_args << to compiled_args << to
end end
method.add_code Virtual::MessageSend.new(expession.name , me , compiled_args) #and pass control method.add_code MessageSend.new(expession.name , me , compiled_args) #and pass control
# the effect of the method is that the NewMessage Return slot will be filled, return it # the effect of the method is that the NewMessage Return slot will be filled, return it
# (this is what is moved _inside_ above loop for such expressions that are calls (or constants)) # (this is what is moved _inside_ above loop for such expressions that are calls (or constants))
Virtual::Return.new( method.return_type ) Return.new( method.return_type )
end end
end end
end end

View File

@ -6,17 +6,17 @@ module Virtual
raise "error, argument must be a identifier, not #{p}" unless p.is_a? Ast::NameExpression raise "error, argument must be a identifier, not #{p}" unless p.is_a? Ast::NameExpression
p.name p.name
end end
r = expression.receiver ? Compiler.compile(expression.receiver, method ) : Virtual::Self.new() r = expression.receiver ? Compiler.compile(expression.receiver, method ) : Self.new()
new_method = Virtual::CompiledMethod.new(expression.name , args , r ) new_method = CompiledMethod.new(expression.name , args , r )
new_method.class_name = r.is_a?(Virtual::BootClass) ? r.name : method.class_name new_method.class_name = r.is_a?(BootClass) ? r.name : method.class_name
clazz = Virtual::BootSpace.space.get_or_create_class(new_method.class_name) clazz = BootSpace.space.get_or_create_class(new_method.class_name)
clazz.add_instance_method new_method clazz.add_instance_method new_method
#frame = frame.new_frame #frame = frame.new_frame
return_type = nil return_type = nil
expression.body.each do |ex| expression.body.each do |ex|
return_type = Compiler.compile(ex,new_method ) return_type = Compiler.compile(ex,new_method )
raise return_type.inspect if return_type.is_a? Virtual::Instruction raise return_type.inspect if return_type.is_a? Instruction
end end
new_method.return_type = return_type new_method.return_type = return_type
new_method new_method
@ -26,13 +26,13 @@ module Virtual
locals = {} locals = {}
expression.params.each_with_index do |param , index| expression.params.each_with_index do |param , index|
arg = param.name arg = param.name
register = Virtual::RegisterReference.new(Virtual::RegisterMachine.instance.receiver_register).next_reg_use(index + 1) register = RegisterReference.new(RegisterMachine.instance.receiver_register).next_reg_use(index + 1)
arg_value = Virtual::Integer.new(register) arg_value = Integer.new(register)
locals[arg] = arg_value locals[arg] = arg_value
args << arg_value args << arg_value
end end
# class depends on receiver # class depends on receiver
me = Virtual::Integer.new( Virtual::RegisterMachine.instance.receiver_register ) me = Integer.new( RegisterMachine.instance.receiver_register )
if expression.receiver.nil? if expression.receiver.nil?
clazz = context.current_class clazz = context.current_class
else else
@ -40,7 +40,7 @@ module Virtual
clazz = c.meta_class clazz = c.meta_class
end end
function = Virtual::Function.new(name , me , args ) function = Function.new(name , me , args )
clazz.add_code_function function clazz.add_code_function function
parent_locals = context.locals parent_locals = context.locals
@ -52,11 +52,11 @@ module Virtual
expression.body.each do |b| expression.body.each do |b|
puts "compiling in function #{b}" puts "compiling in function #{b}"
last_compiled = b.compile(context) last_compiled = b.compile(context)
raise "alarm #{last_compiled} \n #{b}" unless last_compiled.is_a? Virtual::Word raise "alarm #{last_compiled} \n #{b}" unless last_compiled.is_a? Word
end end
return_reg = Virtual::Integer.new(Virtual::RegisterMachine.instance.return_register) return_reg = Integer.new(RegisterMachine.instance.return_register)
if last_compiled.is_a?(Virtual::IntegerConstant) or last_compiled.is_a?(Virtual::ObjectConstant) if last_compiled.is_a?(IntegerConstant) or last_compiled.is_a?(ObjectConstant)
return_reg.load function , last_compiled if last_compiled.register_symbol != return_reg.register_symbol return_reg.load function , last_compiled if last_compiled.register_symbol != return_reg.register_symbol
else else
return_reg.move( function, last_compiled ) if last_compiled.register_symbol != return_reg.register_symbol return_reg.move( function, last_compiled ) if last_compiled.register_symbol != return_reg.register_symbol

View File

@ -12,8 +12,8 @@ module Virtual
is = Compiler.compile(expression.cond, method ) is = Compiler.compile(expression.cond, method )
# TODO should/will use different branches for different conditions. # TODO should/will use different branches for different conditions.
# just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? Virtual::BranchCondition # just a scetch : cond_val = cond_val.is_true?(method) unless cond_val.is_a? BranchCondition
method.add_code Virtual::IsTrueBranch.new( true_block ) method.add_code IsTrueBranch.new( true_block )
# compile the true block (as we think of it first, even it is second in sequential order) # compile the true block (as we think of it first, even it is second in sequential order)
method.current true_block method.current true_block
@ -30,7 +30,7 @@ module Virtual
last = Compiler.compile(part,method ) last = Compiler.compile(part,method )
raise part.inspect if last.nil? raise part.inspect if last.nil?
end end
method.add_code Virtual::UnconditionalBranch.new( merge_block ) method.add_code UnconditionalBranch.new( merge_block )
puts "compiled if: end" puts "compiled if: end"
method.current merge_block method.current merge_block

View File

@ -6,7 +6,7 @@ module Virtual
end end
def self.compile_class expression , method def self.compile_class expression , method
clazz = ::Virtual::BootSpace.space.get_or_create_class expression.name clazz = ::BootSpace.space.get_or_create_class expression.name
puts "Created class #{clazz.name.inspect}" puts "Created class #{clazz.name.inspect}"
expression.expressions.each do |expr| expression.expressions.each do |expr|
# check if it's a function definition and add # check if it's a function definition and add

View File

@ -3,7 +3,7 @@ module Virtual
# return attr_reader :expression # return attr_reader :expression
def self.compile_return expression, scope ,method def self.compile_return expression, scope ,method
Virtual::Reference.new Reference.new
end end
def old def old
into = context.function into = context.function
@ -11,8 +11,8 @@ module Virtual
expression_value = expression.compile(context) expression_value = expression.compile(context)
# copied from function expression: TODO make function # copied from function expression: TODO make function
return_reg = Virtual::Integer.new(Virtual::RegisterMachine.instance.return_register) return_reg = Integer.new(RegisterMachine.instance.return_register)
if expression_value.is_a?(Virtual::IntegerConstant) or expression_value.is_a?(Virtual::ObjectConstant) if expression_value.is_a?(IntegerConstant) or expression_value.is_a?(ObjectConstant)
return_reg.load into , expression_value return_reg.load into , expression_value
else else
return_reg.move( into, expression_value ) if expression_value.register_symbol != return_reg.register_symbol return_reg.move( into, expression_value ) if expression_value.register_symbol != return_reg.register_symbol

View File

@ -3,11 +3,11 @@ module Virtual
# while- attr_reader :condition, :body # while- attr_reader :condition, :body
def self.compile_while expression, method def self.compile_while expression, method
start = Virtual::Label.new("while_start") start = Label.new("while_start")
method.add_code start method.add_code start
is = expression.condition.compile(method ) is = expression.condition.compile(method )
branch = Virtual::IsTrueBranch.new "while" branch = IsTrueBranch.new "while"
merge = Virtual::Label.new(branch.name) merge = Label.new(branch.name)
branch.other = merge #false jumps to end of while branch.other = merge #false jumps to end of while
method.add_code branch method.add_code branch
last = is last = is