rubyx/lib/virtual/parfait_adapter.rb

188 lines
4.8 KiB
Ruby
Raw Normal View History

2015-05-18 09:21:23 +02:00
# Below we define functions (in different classes) that are not part of the run-time
# They are used for the boot process, ie when this codes executes in the vm that builds salama
# To stay sane, we use the same classes that we use later, but "adapt" them to work in ruby
# This affects mainly memory layout
module FakeMem
def initialize
2015-05-31 10:07:49 +02:00
super()
2015-05-18 09:21:23 +02:00
@memory = [0,nil]
@position = nil
if Virtual.machine.class_mappings
2015-05-31 10:07:49 +02:00
init_layout
else
#puts "No init for #{self.class}:#{self.object_id}"
end
end
def init_layout
vm_name = self.class.name.split("::").last.to_sym
clazz = Virtual.machine.class_mappings[vm_name]
raise "Class not found #{vm_name}" unless clazz
2015-05-31 10:07:49 +02:00
raise "Layout not set #{vm_name}" unless clazz.object_layout
self.set_layout clazz.object_layout
2015-05-18 09:21:23 +02:00
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
2015-06-08 12:19:53 +02:00
include Padding
2015-06-19 12:11:40 +02:00
def init_layout; end
def has_layout?
true
end
def get_layout
Virtual.machine.class_mappings[:Word].object_layout
end
def word_length
2015-06-08 12:19:53 +02:00
padded to_s.length
2015-06-01 07:33:23 +02:00
end
# not the prettiest addition to the game, but it wasn't me who decided symbols are frozen in 2.x
def cache_positions
unless defined?(@@symbol_positions)
@@symbol_positions = {}
end
@@symbol_positions
end
def position
pos = cache_positions[self]
if pos == nil
str = "position accessed but not set, "
str += "Machine has object=#{Virtual.machine.objects.include?(self)} "
2015-06-01 07:33:23 +02:00
raise str + " for Symbol:#{self}"
end
pos
end
def set_position pos
# resetting of position used to be error, but since relink and dynamic instruction size it is ok.
# in measures (of 32)
old = cache_positions[self]
if old != nil and ((old - pos).abs > 32)
raise "position set again #{pos}!=#{old} for #{self}"
end
cache_positions[self] = pos
end
end
2015-05-18 09:21:23 +02:00
module Parfait
# Objects memory functions. Object memory is 1 based
# but we implement it with ruby array (0 based) and use 0 as type-word
# These are the same functions that Builtin implements at run-time
class Object
include FakeMem
include Padding
2015-06-08 11:37:20 +02:00
include Positioned
2015-06-06 18:46:53 +02:00
2015-05-18 09:21:23 +02:00
# these internal functions are _really_ internal
# they respresent the smallest code needed to build larger functionality
# but should _never_ be used outside parfait. in fact that should be impossible
def internal_object_get_typeword
raise "failed init for #{self.class}" unless @memory
2015-05-18 09:21:23 +02:00
@memory[0]
end
def internal_object_set_typeword w
raise "failed init for #{self.class}" unless @memory
2015-05-18 09:21:23 +02:00
@memory[0] = w
end
def internal_object_length
raise "failed init for #{self.class}" unless @memory
2015-05-18 09:21:23 +02:00
@memory.length - 1 # take of type-word
end
# 1 -based index
def internal_object_get(index)
@memory[index]
end
# 1 -based index
def internal_object_set(index , value)
raise "failed init for #{self.class}" unless @memory
2015-05-18 09:21:23 +02:00
@memory[index] = value
2015-06-19 12:11:40 +02:00
#shaddowing layout so we can ignore memory in Sof
if(index == LAYOUT_INDEX)
@layout = value
end
2015-05-18 09:21:23 +02:00
end
def internal_object_grow(length)
old_length = internal_object_length()
while( old_length < length )
internal_object_set( old_length + 1, nil)
old_length = old_length + 1
end
end
2015-05-30 13:22:33 +02:00
def internal_object_shrink(length)
old_length = internal_object_length()
while( length < old_length )
@memory.delete_at(old_length)
old_length = old_length - 1
end
end
2015-05-20 09:57:20 +02:00
def to_s
2015-06-19 11:29:41 +02:00
Sof.write(self)
2015-05-20 09:57:20 +02:00
end
2015-05-29 11:33:59 +02:00
def inspect
to_s
end
2015-05-18 09:21:23 +02:00
end
class List
def word_length
2015-05-25 17:48:35 +02:00
padded_words(get_length())
end
2015-05-20 09:57:20 +02:00
def to_sof_node(writer , level , ref )
Sof.array_to_sof_node(self , writer , level , ref )
end
def to_a
array = []
index = 1
while( index <= self.get_length)
array[index - 1] = get(index)
index = index + 1
end
array
end
2015-05-20 09:57:20 +02:00
end
class Dictionary
def to_sof_node(writer , level , ref)
Sof.hash_to_sof_node( self , writer , level , ref)
end
2015-05-18 09:21:23 +02:00
end
class Method
attr_accessor :info
end
2015-05-24 14:31:44 +02:00
class Word
2015-05-24 14:31:44 +02:00
def == other
return false unless other.is_a?(String) or other.is_a?(Word)
as_string = self.to_s
unless other.is_a? String
other = other.to_s
end
as_string == other
end
2015-06-03 09:01:59 +02:00
def to_string
string = ""
index = 1
while( index <= self.length)
string[index - 1] = get_char(index).chr
index = index + 1
end
string
end
end
end