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 "members"
require_relative "array" require_relative "array"
require_relative "occurence" 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 Array.class_eval do
def attributes def add_sof(members , level)
[] each do |o|
members.add(o , level + 1)
end
end
def to_sof(io , members)
each do |object|
io.write("\n")
members.output(io , object)
end end
def to_sof
""
end end
end end

View File

@ -7,40 +7,62 @@ module Sof
@objects = {} @objects = {}
add(root ,0 ) add(root ,0 )
end end
attr_reader :objects
def add object , level def add object , level
if( @objects.has_key?(object) ) if( @objects.has_key?(object) )
occurence = @objects.get(object) occurence = @objects[object]
occurence.level = level if occurence.level > level occurence.level = level if occurence.level > level
else else
o = Occurence.new( object , @counter , level ) o = Occurence.new( object , @counter , level )
@objects[object] = o @objects[object] = o
c = @counter c = @counter
@counter = @counter + 1 @counter = @counter + 1
object.attributes.each do a if( object.respond_to?(:attributes))
object.attributes.each do |a|
val = object.send a val = object.send a
add(val , level + 1) add(val , level + 1)
end end
elsif not value?(object)
object.add_sof(self , level)
end
end 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 def write
string = "" io = StringIO.new
output string , @root output io , @root
io.string
end end
def output string , object def output io , object
occurence = @objects[object] occurence = @objects[object]
raise "no object #{object}" unless occurence
indent = " " * occurence.level indent = " " * occurence.level
string += indent io.write indent
if(object.respond_to? :to_sof) if(object.respond_to? :to_sof)
string += object.to_sof + "\n" object.to_sof(io , self)
else else
string += "!" + object.class.name + "\n" io.write object.class.name
indent += " " if( object.respond_to?(:attributes))
object.attributes.each do a object.attributes.each do |a|
val = object.send a val = object.send a
output( string , val) 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 end
end end

View File

@ -19,7 +19,11 @@ module Virtual
@codes = [] @codes = []
end end
attr_reader :name , :next , :codes , :method def attributes
[:name , :codes , :branch]
end
attr_reader :name , :codes , :method
attr_accessor :branch attr_accessor :branch
def reachable ret = [] 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 # This is partly because jumping over this layer and doing in straight in assember was too big a step
class Instruction < Virtual::Object 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 def == other
return false unless other.class == self.class return false unless other.class == self.class
attributes.each do |a| attributes.each do |a|
@ -27,7 +27,9 @@ module Virtual
end end
return true return true
end end
def attributes
[]
end
end end
module Named module Named

View File

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