revert to symbols

Parfait::Words were nice, but endless problems with the fact that when
you write “String” you get a string.
Symbols take care of uniqueness at the same time
This commit is contained in:
Torsten Ruger 2015-05-31 18:34:18 +03:00
parent 5d870ef154
commit bee73801eb
18 changed files with 101 additions and 85 deletions

View File

@ -13,6 +13,3 @@ require "parfait/layout"
require "parfait/message" require "parfait/message"
require "parfait/frame" require "parfait/frame"
require "parfait/space" require "parfait/space"
#if we are in the ruby run-time / generating an executable
require "virtual/parfait_adapter"

View File

@ -30,9 +30,17 @@ module Parfait
@instance_methods @instance_methods
end end
def method_names
names = List.new
@instance_methods.each do |method|
names.push method.name
end
names
end
def add_instance_method method def add_instance_method method
raise "not a method #{method.class} #{method.inspect}" unless method.is_a? Method raise "not a method #{method.class} #{method.inspect}" unless method.is_a? Method
raise "syserr #{method.name.class}" unless method.name.is_a? Word raise "syserr #{method.name.class}" unless method.name.is_a? Symbol
found = get_instance_method( method.name ) found = get_instance_method( method.name )
if found if found
@instance_methods.delete(found) @instance_methods.delete(found)
@ -48,6 +56,7 @@ module Parfait
end end
def create_instance_method name , arg_names def create_instance_method name , arg_names
raise "uups #{name}.#{name.class}" unless name.is_a?(Symbol)
clazz = Space.object_space.get_class_by_name(self.name) clazz = Space.object_space.get_class_by_name(self.name)
raise "??? #{self.name}" unless clazz raise "??? #{self.name}" unless clazz
Method.new_object( clazz , name , arg_names ) Method.new_object( clazz , name , arg_names )
@ -61,7 +70,7 @@ module Parfait
end end
def get_instance_method fname def get_instance_method fname
raise "uups #{fname}.#{fname.class}" unless fname.is_a?(Word) or fname.is_a?(String) raise "uups #{fname}.#{fname.class}" unless fname.is_a?(Symbol)
#if we had a hash this would be easier. Detect or find would help too #if we had a hash this would be easier. Detect or find would help too
@instance_methods.each do |m| @instance_methods.each do |m|
return m if(m.name == fname ) return m if(m.name == fname )
@ -71,7 +80,7 @@ module Parfait
# get the method and if not found, try superclasses. raise error if not found # get the method and if not found, try superclasses. raise error if not found
def resolve_method m_name def resolve_method m_name
raise "uups #{m_name}.#{m_name.class}" unless m_name.is_a?(Word) or m_name.is_a?(String) raise "uups #{m_name}.#{m_name.class}" unless m_name.is_a?(Symbol)
method = get_instance_method(m_name) method = get_instance_method(m_name)
return method if method return method if method
if( @super_class ) if( @super_class )

View File

@ -56,14 +56,14 @@ module Parfait
end end
def get_main def get_main
kernel = get_class_by_name "Object" kernel = get_class_by_name :Object
kernel.get_instance_method "main" kernel.get_instance_method :main
end end
# this is the way to instantiate classes (not Parfait::Class.new) # this is the way to instantiate classes (not Parfait::Class.new)
# so we get and keep exactly one per name # so we get and keep exactly one per name
def get_class_by_name name def get_class_by_name name
raise "uups #{name}.#{name.class}" unless name.is_a?(Word) or name.is_a?(String) raise "uups #{name}.#{name.class}" unless name.is_a?(Symbol)
c = @classes[name] c = @classes[name]
puts "MISS, no class #{name} #{name.class}" unless c # " #{@classes}" puts "MISS, no class #{name} #{name.class}" unless c # " #{@classes}"
c c

View File

@ -9,7 +9,7 @@ module Builtin
# As we write before we recurse (save a push) we write the number backwards # As we write before we recurse (save a push) we write the number backwards
# arguments: string address , integer # arguments: string address , integer
def utoa context def utoa context
utoa_function = Virtual::CompiledMethodInfo.create_method("Integer" ,"utoa" , [ Virtual::Integer ] ) utoa_function = Virtual::CompiledMethodInfo.create_method(:Integer ,:utoa , [ Virtual::Integer ] )
function.info.return_type = Virtual::Integer function.info.return_type = Virtual::Integer
function.info.receiver = Virtual::Integer function.info.receiver = Virtual::Integer
return utoa_function return utoa_function
@ -29,7 +29,7 @@ module Builtin
end end
def putint context def putint context
putint_function = Virtual::CompiledMethodInfo.create_method("Integer","putint" , [] ) putint_function = Virtual::CompiledMethodInfo.create_method(:Integer,:putint , [] )
putint_function.info.return_type = Virtual::Integer putint_function.info.return_type = Virtual::Integer
putint_function.info.receiver = Virtual::Integer putint_function.info.receiver = Virtual::Integer
return putint_function return putint_function
@ -58,7 +58,7 @@ module Builtin
# 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::CompiledMethodInfo.create_method("Integer","fibo" , [] ) fibo_function = Virtual::CompiledMethodInfo.create_method(:Integer,:fibo , [] )
fibo_function.info.return_type = Virtual::Integer fibo_function.info.return_type = Virtual::Integer
fibo_function.info.receiver = Virtual::Integer fibo_function.info.receiver = Virtual::Integer
return fibo_function return fibo_function

View File

@ -5,7 +5,7 @@ module Builtin
# 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 (and relocation) # so it is responsible for initial setup (and relocation)
def __init__ context def __init__ context
function = Virtual::CompiledMethodInfo.create_method("Kernel","__init__" , []) function = Virtual::CompiledMethodInfo.create_method(:Kernel,:__init__ , [])
# puts "INIT LAYOUT #{function.get_layout.get_layout}" # puts "INIT LAYOUT #{function.get_layout.get_layout}"
function.info.return_type = Virtual::Integer function.info.return_type = Virtual::Integer
main = Virtual::Machine.instance.space.get_main main = Virtual::Machine.instance.space.get_main
@ -16,14 +16,14 @@ module Builtin
return function return function
end end
def putstring context def putstring context
function = Virtual::CompiledMethodInfo.create_method("Kernel" , "putstring" , [] ) function = Virtual::CompiledMethodInfo.create_method(:Kernel , :putstring , [] )
return function return function
ret = Virtual::RegisterMachine.instance.write_stdout(function) ret = Virtual::RegisterMachine.instance.write_stdout(function)
function.set_return ret function.set_return ret
function function
end end
def exit context def exit context
function = Virtual::CompiledMethodInfo.create_method("Kernel","exit" , []) function = Virtual::CompiledMethodInfo.create_method(:Kernel,:exit , [])
function.info.return_type = Virtual::Integer function.info.return_type = Virtual::Integer
return function return function
ret = Virtual::RegisterMachine.instance.exit(function) ret = Virtual::RegisterMachine.instance.exit(function)
@ -31,7 +31,7 @@ module Builtin
function function
end end
def __send context def __send context
function = Virtual::CompiledMethodInfo.create_method("Kernel" ,"__send" , [] ) function = Virtual::CompiledMethodInfo.create_method(:Kernel ,:__send , [] )
function.info.return_type = Virtual::Integer function.info.return_type = Virtual::Integer
return function return function
end end

View File

@ -5,7 +5,7 @@ module Builtin
# 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::CompiledMethodInfo.create_method("Object","main" , []) function = Virtual::CompiledMethodInfo.create_method(:Object,:main , [])
return function return function
end end
@ -17,7 +17,7 @@ module Builtin
# 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::CompiledMethodInfo.create_method("Object","_get_instance_variable" , [ Virtual::Reference ] ) get_function = Virtual::CompiledMethodInfo.create_method(:Object,:_get_instance_variable , [ Virtual::Reference ] )
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
@ -38,7 +38,7 @@ module Builtin
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::CompiledMethodInfo.create_method("Object","_set_instance_variable" ,[Virtual::Reference ,Virtual::Reference] ) set_function = Virtual::CompiledMethodInfo.create_method(:Object,:_set_instance_variable ,[Virtual::Reference ,Virtual::Reference] )
return set_function return set_function
receiver set_function receiver set_function
me = set_function.receiver me = set_function.receiver

View File

@ -1,5 +1,8 @@
require "parfait" require "parfait"
require "virtual/machine" require "virtual/machine"
#if we are in the ruby run-time / generating an executable
require "virtual/positioned"
require "virtual/parfait_adapter"
require "virtual/compiler" require "virtual/compiler"
require "virtual/instruction" require "virtual/instruction"

View File

@ -21,23 +21,23 @@ module Virtual
# map from the vm - class_name to the Parfait class (which carries parfait name) # map from the vm - class_name to the Parfait class (which carries parfait name)
class_mappings = {} #will later become instance variable class_mappings = {} #will later become instance variable
values = [ "Value" , "Integer" , "Kernel" , "Object"].collect {|cl| Virtual.new_word(cl) } values = [ :Value , :Integer , :Kernel , :Object]
value_classes = values.collect { |cl| @space.create_class(cl,nil) } value_classes = values.collect { |cl| @space.create_class(cl,nil) }
layouts = { "Word" => [] , layouts = { :Word => [] ,
"List" => [] , :List => [] ,
"Message" => [], :Message => [],
"MetaClass" => [], :MetaClass => [],
"BinaryCode" => [], :BinaryCode => [],
"Space" => ["classes","frames","messages","next_message","next_frame"], :Space => [:classes ,:frames ,:messages ,:next_message ,:next_frame],
"Frame" => ["locals" , "tmps" ], :Frame => [:locals , :tmps ],
"Layout" => ["object_class"] , :Layout => [:object_class] ,
"Class" => ["object_layout"], :Class => [:object_layout ],
"Dictionary" => ["keys" , "values"] , :Dictionary => [:keys , :values ] ,
"Method" => ["name" , "code" ,"arg_names" , "locals" , "tmps"] , :Method => [:name , :code ,:arg_names , :locals , :tmps ] ,
"Module" => ["name" , "instance_methods", "super_class", "meta_class"] :Module => [:name , :instance_methods , :super_class , :meta_class ]
} }
layouts.each do |name , layout| layouts.each do |name , layout|
class_mappings[name] = @space.create_class(Virtual.new_word(name) , nil) class_mappings[name] = @space.create_class(name , nil)
end end
value_classes[1].set_super_class( value_classes[0] ) # #set superclass (value) for integer value_classes[1].set_super_class( value_classes[0] ) # #set superclass (value) for integer
value_classes[2].set_super_class( value_classes[0] ) # and kernel (TODO is module) value_classes[2].set_super_class( value_classes[0] ) # and kernel (TODO is module)
@ -49,11 +49,11 @@ module Virtual
class_mappings.each do |name , clazz| class_mappings.each do |name , clazz|
variables = layouts[name] variables = layouts[name]
variables.each do |var_name| variables.each do |var_name|
clazz.object_layout.add_instance_variable Virtual.new_word(var_name) clazz.object_layout.add_instance_variable var_name
end end
end end
# superclass and layout corrections # superclass and layout corrections
supers = { "BinaryCode" => "Word", "Layout" => "List", "Class" => "Module"} supers = { :BinaryCode => :Word , :Layout => :List , :Class => :Module }
supers.each do |classname , superclass_name| supers.each do |classname , superclass_name|
clazz = class_mappings[classname] clazz = class_mappings[classname]
super_class = class_mappings[superclass_name] super_class = class_mappings[superclass_name]
@ -70,9 +70,9 @@ module Virtual
# lookup half created class info # lookup half created class info
# but it must be done before going through the objects (next step) # but it must be done before going through the objects (next step)
@class_mappings = class_mappings @class_mappings = class_mappings
class_mappings["Integer"] = value_classes[1] #need for further booting class_mappings[:Integer ] = value_classes[1] #need for further booting
class_mappings["Kernel"] = value_classes[2] #need for further booting class_mappings[:Kernel ] = value_classes[2] #need for further booting
class_mappings["Object"] = value_classes[3] #need for further booting class_mappings[:Object ] = value_classes[3] #need for further booting
@space.late_init @space.late_init
@ -96,25 +96,25 @@ module Virtual
def boot_functions! def boot_functions!
# very fiddly chicken 'n egg problem. Functions need to be in the right order, and in fact we # very fiddly chicken 'n egg problem. Functions need to be in the right order, and in fact we
# have to define some dummies, just for the other to compile # have to define some dummies, just for the other to compile
# TODO: go through the virtual parfait layer and adjust function names to what they really are # TODO go through the virtual parfait layer and adjust function names to what they really are
obj = @class_mappings["Object"] obj = @class_mappings[:Object ]
[:main , :_get_instance_variable , :_set_instance_variable].each do |f| [:main , :_get_instance_variable , :_set_instance_variable].each do |f|
obj.add_instance_method Builtin::Object.send(f , nil) obj.add_instance_method Builtin::Object.send(f , nil)
end end
obj = @class_mappings["Kernel"] obj = @class_mappings[:Kernel ]
# create dummy main first, __init__ calls it # create dummy main first, __init__ calls it
[:putstring,:exit,:__send ].each do |f| [:putstring,:exit,:__send ].each do |f|
obj.add_instance_method Builtin::Kernel.send(f , nil) obj.add_instance_method Builtin::Kernel.send(f , nil)
end end
underscore_init = obj.add_instance_method Builtin::Kernel.send(:__init__, nil) underscore_init = obj.add_instance_method Builtin::Kernel.send(:__init__, nil)
obj = @class_mappings["Integer"] obj = @class_mappings[:Integer ]
[:putint,:fibo].each do |f| [:putint,:fibo].each do |f|
obj.add_instance_method Builtin::Integer.send(f , nil) obj.add_instance_method Builtin::Integer.send(f , nil)
end end
# and the @init block in turn _jumps_ to __init__ # and the @init block in turn _jumps_ to __init__
# the point of which is that by the time main executes, all is "normal" # the point of which is that by the time main executes, all is :normal:
@init = Block.new(:_init_ , nil ) @init = Block.new(:_init_ , nil )
@init.add_code(Register::RegisterMain.new(underscore_init)) @init.add_code(Register::RegisterMain.new(underscore_init))
end end

View File

@ -35,10 +35,8 @@ module Virtual
# #
# 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 , method_name , args)
raise "uups #{class_name}.#{class_name.class}" if class_name.is_a? Symbol raise "uups #{class_name}.#{class_name.class}" unless class_name.is_a? Symbol
raise "uups #{method_name}.#{method_name.class}" if class_name.is_a? Symbol raise "uups #{method_name}.#{method_name.class}" unless class_name.is_a? Symbol
class_name = Virtual.new_word(class_name) if class_name.is_a? String
method_name = Virtual.new_word(method_name) if method_name.is_a? String
clazz = Machine.instance.space.get_class_by_name class_name clazz = Machine.instance.space.get_class_by_name class_name
raise "No such class #{class_name}" unless clazz raise "No such class #{class_name}" unless clazz
method = clazz.create_instance_method( method_name , Virtual.new_list(args)) method = clazz.create_instance_method( method_name , Virtual.new_list(args))

View File

@ -46,7 +46,7 @@ module Virtual
# 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 Self.new( Reference.new(method.for_class)) if expression.name == :self return Self.new( Reference.new(method.for_class)) if expression.name == :self
name = Virtual.new_word expression.name.to_s name = expression.name.to_sym
if method.has_var(name) if method.has_var(name)
# either an argument, so it's stored in message # either an argument, so it's stored in message
if( index = method.has_arg(name)) if( index = method.has_arg(name))
@ -71,7 +71,7 @@ module Virtual
# attr_reader :string # attr_reader :string
def self.compile_string expression , method def self.compile_string expression , method
value = Virtual.new_word(expression.string) value = expression.string.to_sym
to = Return.new(Reference , value) to = Return.new(Reference , value)
method.info.add_code Set.new( to , value ) method.info.add_code Set.new( to , value )
to to
@ -84,11 +84,11 @@ module Virtual
end end
r = Compiler.compile(expression.right , method ) r = Compiler.compile(expression.right , method )
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(Virtual.new_word name) index = method.has_arg(name)
if index if index
method.info.add_code Set.new(Return.new , MessageSlot.new(index , r,type , r )) method.info.add_code Set.new(Return.new , MessageSlot.new(index , r,type , r ))
else else
index = method.ensure_local(Virtual.new_word expression.left.name) index = method.ensure_local(expression.left.name.to_sym)
method.info.add_code Set.new(Return.new , FrameSlot.new(index , r.type , r )) method.info.add_code Set.new(Return.new , FrameSlot.new(index , r.type , r ))
end end
r r

View File

@ -8,7 +8,7 @@ module Virtual
me = Compiler.compile( expession.receiver , method ) me = Compiler.compile( expession.receiver , method )
method.info.add_code NewMessage.new method.info.add_code NewMessage.new
method.info.add_code Set.new(NewSelf.new(me.type), me) method.info.add_code Set.new(NewSelf.new(me.type), me)
method.info.add_code Set.new(NewName.new(), Virtual.new_word(expession.name)) method.info.add_code Set.new(NewName.new(), expession.name.to_sym)
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

View File

@ -1,4 +1,3 @@
require_relative "positioned"
module Virtual module Virtual

View File

@ -7,8 +7,6 @@ module Virtual
class Set < Instruction class Set < Instruction
def initialize to , from def initialize to , from
@to = to @to = to
# hard to find afterwards where it came from, so ensure it doesn't happen
raise "From must be slot or constant, not symbol #{from}" if from.is_a? Symbol
@from = from @from = from
end end
attr_reader :from , :to attr_reader :from , :to

View File

@ -16,7 +16,7 @@ module FakeMem
end end
end end
def init_layout def init_layout
vm_name = self.class.name.split("::").last vm_name = self.class.name.split("::").last.to_sym
clazz = Virtual::Machine.instance.class_mappings[vm_name] clazz = Virtual::Machine.instance.class_mappings[vm_name]
raise "Class not found #{vm_name}" unless clazz raise "Class not found #{vm_name}" unless clazz
raise "Layout not set #{vm_name}" unless clazz.object_layout raise "Layout not set #{vm_name}" unless clazz.object_layout
@ -52,6 +52,28 @@ module FakeMem
padded(words*4) # 4 == word length, a constant waiting for a home padded(words*4) # 4 == word length, a constant waiting for a home
end end
end end
module Virtual
def self.new_list array
list = Parfait::List.new_object
list.set_length array.length
index = 1
while index <= array.length do
list.set(index , array[index - 1])
index = index + 1
end
list
end
end
class Symbol
include Positioned
def init_layout; end
def has_layout?
true
end
def get_layout
Virtual::Machine.instance.class_mappings[:Word].object_layout
end
end
module Parfait module Parfait
# Objects memory functions. Object memory is 1 based # Objects memory functions. Object memory is 1 based
@ -158,26 +180,3 @@ module Parfait
end end
end end
end end
module Virtual
# Functions to generate parfait objects
def self.new_word( string )
string = string.to_s if string.is_a? Symbol
word = Parfait::Word.new_object( string.length )
string.codepoints.each_with_index do |code , index |
word.set_char(index + 1 , code)
end
word
end
def self.new_list array
list = Parfait::List.new_object
list.set_length array.length
index = 1
while index <= array.length do
list.set(index , array[index - 1])
index = index + 1
end
list
end
end

View File

@ -11,7 +11,7 @@ module Virtual
@gonners << f @gonners << f
end end
end end
init= Parfait::Space.object_space.get_class_by_name("Kernel").get_instance_method "__init__" init= Parfait::Space.object_space.get_class_by_name(:Kernel).get_instance_method :__init__
remove init remove init
dump_remaining dump_remaining
end end

View File

@ -39,8 +39,11 @@ module Virtual
if ref.type.is_a?(Reference) and ref.type.of_class if ref.type.is_a?(Reference) and ref.type.of_class
#find method and call #find method and call
clazz = ref.type.of_class clazz = ref.type.of_class
method = clazz.resolve_method code.name.to_s begin
raise "No method found #{code.name}" unless method method = clazz.resolve_method code.name
rescue
raise "No method found #{code.name} for #{clazz.name} in #{clazz.method_names}" unless method
end
new_codes << MethodCall.new( method ) new_codes << MethodCall.new( method )
else else
# must defer send to run-time # must defer send to run-time

View File

@ -33,7 +33,7 @@ class TestMoves < MiniTest::Test
assert_code code , :mvn , [0x05,0x10,0xe0,0xe3] #e3 e0 10 05 assert_code code , :mvn , [0x05,0x10,0xe0,0xe3] #e3 e0 10 05
end end
def test_constant_small # like test_mov def test_constant_small # like test_mov
const = Virtual.new_word "harvey" const = :harvey
const.set_position( 13 ) # 13 = 5 + 8 , 8 for the arm pipeline offset, gets subtracted const.set_position( 13 ) # 13 = 5 + 8 , 8 for the arm pipeline offset, gets subtracted
code = @machine.mov :r1 , 5 code = @machine.mov :r1 , 5
code.set_position(0) code.set_position(0)
@ -41,7 +41,7 @@ class TestMoves < MiniTest::Test
assert_code code , :mov , [0x05,0x10,0xa0,0xe3] #e3 ef 10 05 assert_code code , :mov , [0x05,0x10,0xa0,0xe3] #e3 ef 10 05
end end
def test_constant_big # like test_mov_big def test_constant_big # like test_mov_big
const = Virtual.new_word "harvey" const = :harvey
const.set_position( 0x222 ) const.set_position( 0x222 )
code = @machine.mov :r0 , 0x222 code = @machine.mov :r0 , 0x222
code.set_position(0) code.set_position(0)

View File

@ -28,7 +28,17 @@ module VirtualHelper
end end
end end
module Virtual
# Functions to generate parfait objects
def self.new_word( string )
string = string.to_s if string.is_a? Symbol
word = Parfait::Word.new_object( string.length )
string.codepoints.each_with_index do |code , index |
word.set_char(index + 1 , code)
end
word
end
end
class UnusedSofEquality class UnusedSofEquality
# simple thought: don't recurse for Blocks, just check their names # simple thought: don't recurse for Blocks, just check their names
def == other def == other