From 2c2ae149288055fd945853293fdea49af35b3e92 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Thu, 14 Aug 2014 17:40:56 +0300 Subject: [PATCH] getting some sof output and adding some tests. issues though. abound --- lib/sof/all.rb | 31 +++++++++++++++++++ lib/sof/array.rb | 13 +++++--- lib/sof/members.rb | 52 +++++++++++++++++++++++--------- lib/virtual/block.rb | 6 +++- lib/virtual/instruction.rb | 6 ++-- lib/virtual/method_definition.rb | 2 +- test/sof.rb | 17 +++++++++++ 7 files changed, 104 insertions(+), 23 deletions(-) create mode 100644 test/sof.rb diff --git a/lib/sof/all.rb b/lib/sof/all.rb index 24967615..873ea2c3 100644 --- a/lib/sof/all.rb +++ b/lib/sof/all.rb @@ -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 diff --git a/lib/sof/array.rb b/lib/sof/array.rb index 1de170f2..4ea037cc 100644 --- a/lib/sof/array.rb +++ b/lib/sof/array.rb @@ -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 diff --git a/lib/sof/members.rb b/lib/sof/members.rb index 5ab51cb5..cd0ba3ee 100644 --- a/lib/sof/members.rb +++ b/lib/sof/members.rb @@ -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 diff --git a/lib/virtual/block.rb b/lib/virtual/block.rb index 4e676e34..b92d0f17 100644 --- a/lib/virtual/block.rb +++ b/lib/virtual/block.rb @@ -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 = [] diff --git a/lib/virtual/instruction.rb b/lib/virtual/instruction.rb index a4049ca4..afb7dd81 100644 --- a/lib/virtual/instruction.rb +++ b/lib/virtual/instruction.rb @@ -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 diff --git a/lib/virtual/method_definition.rb b/lib/virtual/method_definition.rb index 15f615d7..9ebc5ff3 100644 --- a/lib/virtual/method_definition.rb +++ b/lib/virtual/method_definition.rb @@ -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 diff --git a/test/sof.rb b/test/sof.rb new file mode 100644 index 00000000..3b12cf44 --- /dev/null +++ b/test/sof.rb @@ -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 \ No newline at end of file