remove unknown type

This commit is contained in:
Torsten Ruger 2015-09-23 18:35:37 +03:00
parent 4b613fb632
commit 9fe01c7b31
15 changed files with 49 additions and 38 deletions

View File

@ -8,13 +8,13 @@ module Bosl
if receiver if receiver
me = process( receiver.to_a.first ) me = process( receiver.to_a.first )
else else
me = Virtual::Self.new me = Virtual::Self.new :int
end end
## need two step process, compile and save to frame ## need two step process, compile and save to frame
# then move from frame to new message # then move from frame to new message
method.source.add_code Virtual::NewMessage.new method.source.add_code Virtual::NewMessage.new
method.source.add_code Virtual::Set.new( me , Virtual::NewSelf.new(me.type)) method.source.add_code Virtual::Set.new( me , Virtual::NewSelf.new(me.type))
method.source.add_code Virtual::Set.new( name.to_sym , Virtual::NewMessageName.new()) method.source.add_code Virtual::Set.new( name.to_sym , Virtual::NewMessageName.new(:int))
compiled_args = [] compiled_args = []
arguments.to_a.each_with_index do |arg , i| arguments.to_a.each_with_index do |arg , i|
#compile in the running method, ie before passing control #compile in the running method, ie before passing control

View File

@ -24,10 +24,10 @@ module Bosl
end end
end end
else else
r = Virtual::Self.new() r = Virtual::Self.new(:int)
class_name = method.for_class.name class_name = method.for_class.name
end end
new_method = Virtual::MethodSource.create_method(class_name, name , args ) new_method = Virtual::MethodSource.create_method(class_name, return_type, name , args )
new_method.source.receiver = r new_method.source.receiver = r
new_method.for_class.add_instance_method new_method new_method.for_class.add_instance_method new_method

View File

@ -9,12 +9,12 @@ module Bosl
name = expression.to_a.first name = expression.to_a.first
return Virtual::Self.new( Virtual::Reference.new(method.for_class)) if name == :self return Virtual::Self.new( Virtual::Reference.new(method.for_class)) if name == :self
# either an argument, so it's stored in message # either an argument, so it's stored in message
ret = Virtual::Return.new ret = Virtual::Return.new :int
if( index = method.has_arg(name)) if( index = method.has_arg(name))
method.source.add_code Virtual::Set.new( Virtual::ArgSlot.new(index ) , ret) method.source.add_code Virtual::Set.new( Virtual::ArgSlot.new(:int,index ) , ret)
else # or a local so it is in the frame else # or a local so it is in the frame
index = method.ensure_local( name ) index = method.ensure_local( name )
method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(index ) , ret ) method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(:int,index ) , ret )
end end
return ret return ret
end end

View File

@ -3,7 +3,7 @@ module Bosl
# operator attr_reader :operator, :left, :right # operator attr_reader :operator, :left, :right
def on_operator expression def on_operator expression
operator , left , right = *expression operator , left , right = *expression
Virtual::Return.new() Virtual::Return.new(:int)
end end
def on_assign expression def on_assign expression
@ -11,7 +11,7 @@ module Bosl
name = name.to_a.first name = name.to_a.first
v = process(value) v = process(value)
index = method.ensure_local( name ) index = method.ensure_local( name )
method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(index ) , v ) method.source.add_code Virtual::Set.new(Virtual::FrameSlot.new(:int,index ) , v )
end end
end end

View File

@ -4,7 +4,7 @@ module Register
module Integer module Integer
module ClassMethods module ClassMethods
def plus c def plus c
plus_function = Virtual::MethodSource.create_method(:Integer,:plus , [:Integer] ) plus_function = Virtual::MethodSource.create_method(:Integer,:int,:plus , [:Integer] )
plus_function.source.return_type = Virtual::Integer plus_function.source.return_type = Virtual::Integer
plus_function.source.receiver = Virtual::Integer plus_function.source.receiver = Virtual::Integer
@ -43,7 +43,7 @@ module Register
# end # end
def putint context def putint context
putint_function = Virtual::MethodSource.create_method(:Integer,:putint , [] ) putint_function = Virtual::MethodSource.create_method(:Integer,:int,:putint , [] )
putint_function.source.return_type = Virtual::Integer putint_function.source.return_type = Virtual::Integer
putint_function.source.receiver = Virtual::Integer putint_function.source.receiver = Virtual::Integer
return putint_function return putint_function
@ -72,7 +72,7 @@ module Register
# a hand coded version of the fibonachi numbers # a hand coded version of the fibonachi numbers
# not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html # not my hand off course, found in the net http://www.peter-cockerell.net/aalp/html/ch-5.html
def fibo context def fibo context
fibo_function = Virtual::MethodSource.create_method(:Integer,:fibo , [] ) fibo_function = Virtual::MethodSource.create_method(:Integer,:int,:fibo , [] )
fibo_function.source.return_type = Virtual::Integer fibo_function.source.return_type = Virtual::Integer
fibo_function.source.receiver = Virtual::Integer fibo_function.source.receiver = Virtual::Integer
return fibo_function return fibo_function

View File

@ -6,7 +6,7 @@ module Register
# it isn't really a function, ie it is jumped to (not called), exits and may not return # it isn't really a function, ie it is jumped to (not called), exits and may not return
# so it is responsible for initial setup # so it is responsible for initial setup
def __init__ context def __init__ context
function = Virtual::MethodSource.create_method(:Kernel,:__init__ , []) function = Virtual::MethodSource.create_method(:Kernel,:int,:__init__ , [])
function.source.return_type = Virtual::Integer function.source.return_type = Virtual::Integer
# no method enter or return (automatically added), remove # no method enter or return (automatically added), remove
function.source.blocks.first.codes.pop # no Method enter function.source.blocks.first.codes.pop # no Method enter
@ -25,7 +25,7 @@ module Register
return function return function
end end
def exit context def exit context
function = Virtual::MethodSource.create_method(:Kernel,:exit , []) function = Virtual::MethodSource.create_method(:Kernel,:int,:exit , [])
function.source.return_type = Virtual::Integer function.source.return_type = Virtual::Integer
return function return function
ret = Virtual::RegisterMachine.instance.exit(function) ret = Virtual::RegisterMachine.instance.exit(function)
@ -33,7 +33,7 @@ module Register
function function
end end
def __send context def __send context
function = Virtual::MethodSource.create_method(:Kernel ,:__send , [] ) function = Virtual::MethodSource.create_method(:Kernel,:int ,:__send , [] )
function.source.return_type = Virtual::Integer function.source.return_type = Virtual::Integer
return function return function
end end

View File

@ -6,7 +6,7 @@ module Register
# main entry point, ie __init__ calls this # main entry point, ie __init__ calls this
# defined here as empty, to be redefined # defined here as empty, to be redefined
def main context def main context
function = Virtual::MethodSource.create_method(:Object,:main , []) function = Virtual::MethodSource.create_method(:Object, :int , :main , [])
return function return function
end end
@ -18,7 +18,7 @@ module Register
# The at_index is just "below" the api, something we need but don't want to expose, # The at_index is just "below" the api, something we need but don't want to expose,
# so we can't code the above in ruby # so we can't code the above in ruby
def _get_instance_variable context , name = Virtual::Integer def _get_instance_variable context , name = Virtual::Integer
get_function = Virtual::MethodSource.create_method(:Object,:_get_instance_variable , [ ] ) get_function = Virtual::MethodSource.create_method(:Object,:int, :_get_instance_variable , [ ] )
return get_function return get_function
# me = get_function.receiver # me = get_function.receiver
# var_name = get_function.args.first # var_name = get_function.args.first
@ -39,7 +39,7 @@ module Register
end end
def _set_instance_variable(context , name = Virtual::Integer , value = Virtual::Integer ) def _set_instance_variable(context , name = Virtual::Integer , value = Virtual::Integer )
set_function = Virtual::MethodSource.create_method(:Object,:_set_instance_variable ,[] ) set_function = Virtual::MethodSource.create_method(:Object,:int,:_set_instance_variable ,[] )
return set_function return set_function
# receiver set_function # receiver set_function
# me = set_function.receiver # me = set_function.receiver

View File

@ -3,7 +3,7 @@ module Register
module Word module Word
module ClassMethods module ClassMethods
def putstring context def putstring context
function = Virtual::MethodSource.create_method(:Word , :putstring , [] ) function = Virtual::MethodSource.create_method(:Word,:int , :putstring , [] )
Kernel.emit_syscall( function , :putstring ) Kernel.emit_syscall( function , :putstring )
function function
end end

View File

@ -34,17 +34,18 @@ module Virtual
# second, it creates MethodSource and attaches it to the method # second, it creates MethodSource and attaches it to the method
# #
# compile code then works with the method, but adds code tot the info # compile code then works with the method, but adds code tot the info
def self.create_method( class_name , method_name , args) def self.create_method( class_name , return_type , method_name , args)
raise "create_method #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol raise "create_method #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol
raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol raise "create_method #{method_name}.#{method_name.class}" unless method_name.is_a? Symbol
clazz = Virtual.machine.space.get_class_by_name class_name clazz = Virtual.machine.space.get_class_by_name class_name
raise "No such class #{class_name}" unless clazz raise "No such class #{class_name}" unless clazz
return_type = Virtual::Type.from_sym return_type
method = clazz.create_instance_method( method_name , Virtual.new_list(args)) method = clazz.create_instance_method( method_name , Virtual.new_list(args))
method.source = MethodSource.new(method) method.source = MethodSource.new(method , return_type)
method method
end end
# just passing the method object in for Instructions to make decisions (later) # just passing the method object in for Instructions to make decisions (later)
def initialize method , return_type = Virtual::Unknown def initialize method , return_type
# first block we have to create with .new , as new_block assumes a current # first block we have to create with .new , as new_block assumes a current
enter = Block.new( "enter" , method ).add_code(MethodEnter.new( method )) enter = Block.new( "enter" , method ).add_code(MethodEnter.new( method ))
@return_type = return_type @return_type = return_type

View File

@ -4,7 +4,7 @@ module Virtual
# Slots in the Frame are local or temporary variables in a message # Slots in the Frame are local or temporary variables in a message
class FrameSlot < Slot class FrameSlot < Slot
def initialize index , type = Unknown, value = nil def initialize index , type , value = nil
super(type, value) super(type, value)
@index = index @index = index
end end

View File

@ -7,7 +7,7 @@ module Virtual
# The Message has a layout as per the constant above # The Message has a layout as per the constant above
class MessageSlot < Slot class MessageSlot < Slot
def initialize type = Unknown , value = nil def initialize type , value = nil
super(type , value ) super(type , value )
end end
def object_name def object_name
@ -19,28 +19,28 @@ module Virtual
# Return is the return of MessageSlot # Return is the return of MessageSlot
class Return < MessageSlot class Return < MessageSlot
def initialize type = Unknown, value = nil def initialize type , value = nil
super( type , value ) super( type , value )
end end
end end
# Self is the self in MessageSlot # Self is the self in MessageSlot
class Self < MessageSlot class Self < MessageSlot
def initialize type = Unknown, value = nil def initialize type , value = nil
super( type , value ) super( type , value )
end end
end end
# MessageName of the current message # MessageName of the current message
class MessageName < MessageSlot class MessageName < MessageSlot
def initialize type = Unknown, value = nil def initialize type , value = nil
super( type , value ) super( type , value )
end end
end end
# NewMessageName of the next message # NewMessageName of the next message
class ArgSlot < MessageSlot class ArgSlot < MessageSlot
def initialize index , type = Unknown, value = nil def initialize index , type , value = nil
@index = index @index = index
super( type , value ) super( type , value )
end end

View File

@ -7,7 +7,7 @@ module Virtual
# The Message has a layout as per the constant above # The Message has a layout as per the constant above
class NewMessageSlot < Slot class NewMessageSlot < Slot
def initialize type = Unknown, value = nil def initialize type , value = nil
super( type , value ) super( type , value )
end end
def object_name def object_name
@ -19,28 +19,28 @@ module Virtual
# NewReturn is the return of NewMessageSlot # NewReturn is the return of NewMessageSlot
class NewReturn < NewMessageSlot class NewReturn < NewMessageSlot
def initialize type = Unknown, value = nil def initialize type , value = nil
super( type , value ) super( type , value )
end end
end end
# NewSelf is the self of NewMessageSlot # NewSelf is the self of NewMessageSlot
class NewSelf < NewMessageSlot class NewSelf < NewMessageSlot
def initialize type = Unknown, value = nil def initialize type , value = nil
super( type , value ) super( type , value )
end end
end end
# NewMessageName of the next message # NewMessageName of the next message
class NewMessageName < NewMessageSlot class NewMessageName < NewMessageSlot
def initialize type = Unknown, value = nil def initialize type , value = nil
super( type , value ) super( type , value )
end end
end end
# NewMessageName of the next message # NewMessageName of the next message
class NewArgSlot < NewMessageSlot class NewArgSlot < NewMessageSlot
def initialize index , type = Unknown, value = nil def initialize index , type , value = nil
@index = index @index = index
super( type , value ) super( type , value )
end end

View File

@ -14,7 +14,7 @@ module Virtual
# that object # that object
# #
class SelfSlot < Slot class SelfSlot < Slot
def initialize type = Unknown, value = nil def initialize type , value = nil
super super
end end
def object_name def object_name

View File

@ -7,6 +7,19 @@ module Virtual
return false unless other.class == self.class return false unless other.class == self.class
return true return true
end end
# map from a type sym (currently :int/:ref) to a class of subtype of Type
# TODO needs to be made extensible in a defined way.
def self.from_sym type
case type
when :int
Virtual::Integer
when :ref
Virtual::Reference
else
raise "No type maps to:#{type}"
end
end
end end
class Integer < Type class Integer < Type
@ -20,7 +33,4 @@ module Virtual
attr_reader :of_class attr_reader :of_class
end end
class Unknown < Type
end
end end

View File

@ -36,7 +36,7 @@ class TestBasic < MiniTest::Test
def test_name def test_name
@string_input = 'foo ' @string_input = 'foo '
@output = "- Virtual::Return(:type => Virtual::Unknown)" @output = "- Virtual::Return(:type => :int)"
check check
end end