getting some sof output and adding some tests. issues though. abound

This commit is contained in:
Torsten Ruger 2014-08-14 17:40:56 +03:00
parent 7e60827dd3
commit 2c2ae14928
7 changed files with 104 additions and 23 deletions

View File

@ -1,3 +1,34 @@
require_relative "members"
require_relative "array"
require_relative "occurence"
Symbol.class_eval do
def to_sof(io, members)
io.write ":#{to_s}"
end
end
NilClass.class_eval do
def to_sof(io,members)
io.write "nil"
end
end
TrueClass.class_eval do
def to_sof(io , members)
io.write "true"
end
end
FalseClass.class_eval do
def to_sof(io , members)
io.write "false"
end
end
String.class_eval do
def to_sof(io, members)
io.write self
end
end
Fixnum.class_eval do
def to_sof(io , members)
io.write to_s
end
end

View File

@ -1,8 +1,13 @@
Array.class_eval do
def attributes
[]
def add_sof(members , level)
each do |o|
members.add(o , level + 1)
end
end
def to_sof
""
def to_sof(io , members)
each do |object|
io.write("\n")
members.output(io , object)
end
end
end

View File

@ -7,40 +7,62 @@ module Sof
@objects = {}
add(root ,0 )
end
attr_reader :objects
def add object , level
if( @objects.has_key?(object) )
occurence = @objects.get(object)
occurence = @objects[object]
occurence.level = level if occurence.level > level
else
o = Occurence.new( object , @counter , level )
@objects[object] = o
c = @counter
@counter = @counter + 1
object.attributes.each do a
val = object.send a
add(val , level + 1)
if( object.respond_to?(:attributes))
object.attributes.each do |a|
val = object.send a
add(val , level + 1)
end
elsif not value?(object)
object.add_sof(self , level)
end
end
end
def value? o
return true if o == true
return true if o == false
return true if o == nil
return true if o.class == Fixnum
return true if o.class == Symbol
return true if o.class == String
return false
end
def write
string = ""
output string , @root
io = StringIO.new
output io , @root
io.string
end
def output string , object
def output io , object
occurence = @objects[object]
raise "no object #{object}" unless occurence
indent = " " * occurence.level
string += indent
io.write indent
if(object.respond_to? :to_sof)
string += object.to_sof + "\n"
object.to_sof(io , self)
else
string += "!" + object.class.name + "\n"
indent += " "
object.attributes.each do a
val = object.send a
output( string , val)
io.write object.class.name
if( object.respond_to?(:attributes))
object.attributes.each do |a|
val = object.send a
io.write( a )
io.write( " " )
output( io , val)
end
io.puts ""
else
raise "General object not supported (yet), need attribute method #{object}"
end
end
end

View File

@ -19,7 +19,11 @@ module Virtual
@codes = []
end
attr_reader :name , :next , :codes , :method
def attributes
[:name , :codes , :branch]
end
attr_reader :name , :codes , :method
attr_accessor :branch
def reachable ret = []

View File

@ -12,7 +12,7 @@ module Virtual
# This is partly because jumping over this layer and doing in straight in assember was too big a step
class Instruction < Virtual::Object
# simple thought: don't recurse for labels, just check their names
# simple thought: don't recurse for Blocks, just check their names
def == other
return false unless other.class == self.class
attributes.each do |a|
@ -27,7 +27,9 @@ module Virtual
end
return true
end
def attributes
[]
end
end
module Named

View File

@ -35,7 +35,7 @@ module Virtual
MethodDefinition.new(:main , [] , Virtual::SelfReference )
end
def attributes
[:name , :args , :receiver , :return_type , :start]
[:name , :args , :receiver , :return_type , :blocks]
end
def initialize name , args , receiver = Virtual::SelfReference.new , return_type = Virtual::Mystery , start = MethodEnter.new()
@name = name.to_sym

17
test/sof.rb Normal file
View File

@ -0,0 +1,17 @@
require_relative "helper"
class SimpleObjectWithAttributes
end
class BasicSof < MiniTest::Test
def test_true
out = Sof::Members.write(true)
assert_equal " true\n" , out
end
def test_num
out = Sof::Members.write(124)
assert_equal " 124\n" , out
end
end