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
|
|
|
|
|
2015-10-22 17:16:29 +02:00
|
|
|
module Register
|
2015-05-31 17:34:18 +02:00
|
|
|
def self.new_list array
|
2015-07-20 12:01:15 +02:00
|
|
|
list = Parfait::List.new
|
2015-05-31 17:34:18 +02:00
|
|
|
list.set_length array.length
|
|
|
|
index = 1
|
|
|
|
while index <= array.length do
|
|
|
|
list.set(index , array[index - 1])
|
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
list
|
|
|
|
end
|
2015-11-08 22:58:35 +01:00
|
|
|
# Functions to generate parfait objects
|
|
|
|
def self.new_word( string )
|
|
|
|
string = string.to_s if string.is_a? Symbol
|
|
|
|
word = Parfait::Word.new( string.length )
|
|
|
|
string.codepoints.each_with_index do |code , index |
|
|
|
|
word.set_char(index + 1 , code)
|
|
|
|
end
|
|
|
|
word
|
|
|
|
end
|
2015-05-31 17:34:18 +02:00
|
|
|
end
|
|
|
|
class Symbol
|
|
|
|
include Positioned
|
2015-06-08 12:19:53 +02:00
|
|
|
include Padding
|
2015-06-19 12:11:40 +02:00
|
|
|
|
2015-05-31 17:34:18 +02:00
|
|
|
def has_layout?
|
|
|
|
true
|
|
|
|
end
|
|
|
|
def get_layout
|
2015-10-22 17:16:29 +02:00
|
|
|
l = Register.machine.space.classes[:Word].object_layout
|
2015-07-21 18:41:30 +02:00
|
|
|
#puts "LL #{l.class}"
|
2015-07-21 14:40:25 +02:00
|
|
|
l
|
2015-05-31 17:34:18 +02:00
|
|
|
end
|
2015-11-04 09:33:10 +01:00
|
|
|
def padded_length
|
|
|
|
padded to_s.length + 4
|
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, "
|
2015-10-22 17:16:29 +02:00
|
|
|
str += "Machine has object=#{Register.machine.objects.has_key?(self.object_id)} "
|
2015-06-01 07:33:23 +02:00
|
|
|
raise str + " for Symbol:#{self}"
|
|
|
|
end
|
|
|
|
pos
|
|
|
|
end
|
2015-10-24 16:12:36 +02:00
|
|
|
def position= pos
|
2015-06-01 07:33:23 +02:00
|
|
|
# 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
|
|
|
|
|
2015-05-31 17:34:18 +02:00
|
|
|
end
|
2015-05-18 09:21:23 +02:00
|
|
|
|
|
|
|
module Parfait
|
|
|
|
# Objects memory functions. Object memory is 1 based
|
2015-11-08 22:58:35 +01:00
|
|
|
# but we implement it with ruby array (0 based) and don't use 0
|
|
|
|
# These are the same functions that Builtin implements for run-time
|
2015-05-18 09:21:23 +02:00
|
|
|
class Object
|
2015-06-04 07:22:38 +02:00
|
|
|
include Padding
|
2015-06-08 11:37:20 +02:00
|
|
|
include Positioned
|
2015-06-06 18:46:53 +02:00
|
|
|
|
2015-07-21 14:40:25 +02:00
|
|
|
def fake_init
|
2015-10-25 19:43:13 +01:00
|
|
|
@memory = Array.new(16)
|
2015-07-21 14:40:25 +02:00
|
|
|
@position = nil
|
|
|
|
self # for chaining
|
|
|
|
end
|
|
|
|
|
2015-05-18 09:21:23 +02:00
|
|
|
# 1 -based index
|
2015-11-07 16:40:59 +01:00
|
|
|
def get_internal(index)
|
2015-05-18 09:21:23 +02:00
|
|
|
@memory[index]
|
|
|
|
end
|
|
|
|
# 1 -based index
|
2015-11-07 16:40:59 +01:00
|
|
|
def set_internal(index , value)
|
2015-05-20 16:29:08 +02:00
|
|
|
raise "failed init for #{self.class}" unless @memory
|
2015-11-08 22:58:35 +01:00
|
|
|
raise "Word[#{index}] = " if((self.class == Parfait::Word) and value.nil? )
|
2015-07-02 09:26:48 +02:00
|
|
|
@memory[index] = value
|
2015-10-13 16:30:39 +02:00
|
|
|
value
|
2015-05-18 09:21:23 +02:00
|
|
|
end
|
|
|
|
end
|
2015-05-20 15:43:26 +02:00
|
|
|
class List
|
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
|
2015-05-20 15:43:26 +02:00
|
|
|
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
|
|
|
|
|
2015-05-20 15:43:26 +02:00
|
|
|
class Method
|
2015-07-03 19:13:03 +02:00
|
|
|
attr_accessor :source
|
2015-05-20 15:43:26 +02:00
|
|
|
end
|
2015-05-24 14:31:44 +02:00
|
|
|
|
2015-05-20 15:43:26 +02:00
|
|
|
class Word
|
2015-05-24 17:05:20 +02:00
|
|
|
|
2015-05-24 14:31:44 +02:00
|
|
|
def == other
|
|
|
|
return false unless other.is_a?(String) or other.is_a?(Word)
|
2015-07-02 09:26:48 +02:00
|
|
|
as_string = self.to_string
|
2015-05-24 14:31:44 +02:00
|
|
|
unless other.is_a? String
|
2015-07-02 09:26:48 +02:00
|
|
|
other = other.to_string
|
2015-05-24 14:31:44 +02:00
|
|
|
end
|
|
|
|
as_string == other
|
|
|
|
end
|
|
|
|
|
2015-06-03 09:01:59 +02:00
|
|
|
def to_string
|
2015-05-18 09:47:29 +02:00
|
|
|
string = ""
|
|
|
|
index = 1
|
2015-11-08 22:58:35 +01:00
|
|
|
while( index <= self.char_length)
|
2015-11-09 22:27:15 +01:00
|
|
|
string += get_char(index).chr
|
2015-05-18 09:47:29 +02:00
|
|
|
index = index + 1
|
|
|
|
end
|
|
|
|
string
|
|
|
|
end
|
|
|
|
end
|
2015-07-21 14:40:25 +02:00
|
|
|
|
|
|
|
## sof related stuff
|
|
|
|
class Object
|
|
|
|
# parfait versions are deliberately called different, so we "relay"
|
|
|
|
# have to put the "@" on the names for sof to take them off again
|
|
|
|
def instance_variables
|
|
|
|
get_instance_variables.to_a.collect{ |n| "@#{n}".to_sym }
|
|
|
|
end
|
|
|
|
# name comes in as a ruby @var name
|
|
|
|
def instance_variable_get name
|
|
|
|
var = get_instance_variable name.to_s[1 .. -1].to_sym
|
2015-07-21 18:41:30 +02:00
|
|
|
#puts "getting #{name} #{var}"
|
2015-07-21 14:40:25 +02:00
|
|
|
var
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-05-18 09:47:29 +02:00
|
|
|
end
|