From 90cbeddc0a7a19259d9408c6d4e7d1caca8e8cb4 Mon Sep 17 00:00:00 2001 From: Torsten Ruger Date: Wed, 13 Aug 2014 20:05:00 +0300 Subject: [PATCH] premature start on the salama object files, just for reading the compiled code --- lib/sof/README.md | 30 +++++++++++++++++++++++++ lib/sof/all.rb | 3 +++ lib/sof/array.rb | 8 +++++++ lib/sof/members.rb | 53 ++++++++++++++++++++++++++++++++++++++++++++ lib/sof/occurence.rb | 13 +++++++++++ 5 files changed, 107 insertions(+) create mode 100644 lib/sof/README.md create mode 100644 lib/sof/all.rb create mode 100644 lib/sof/array.rb create mode 100644 lib/sof/members.rb create mode 100644 lib/sof/occurence.rb diff --git a/lib/sof/README.md b/lib/sof/README.md new file mode 100644 index 00000000..3971eb78 --- /dev/null +++ b/lib/sof/README.md @@ -0,0 +1,30 @@ +### Salama Object File + +Ok, so we all heard about object files, it's the things compilers create so we don't have to have huge compiles and +can link them later. + +Much few know what they include, and that is not because they are not very useful, but rather very complicated. + +An object machine must off course have it's own object files, because: + +- otherwise we'd have to express the object machine in c (nischt gut) +- we would be forced to read the source every time (slow) +- we would have no language independant format + +And i was going to get there, juust not now. I mean i think it's a great idea to have many languages compile and run +on the same object machine. Not neccessarily my idea, but i haven't seen it pulled off. Not that i will. + +I just want to be able to read my compiled code!! + +And so this is a little start, just some outputter. + +#### Direction + +The way this is meant to go (planned for 2020+) was a salama core with only a sof parser (as that is soo much simpler). + +Then to_ruby for all the ast classes to be able to roundtrip ruby code. + +Then go to storing sof in git, rather than ruby. + +Then write a python/java parser and respective runtime conversion. Extracting common features. With the respective +to_python on the ast's to roundtrip that too. Have to since by now we work on sof's. Etc . .. diff --git a/lib/sof/all.rb b/lib/sof/all.rb new file mode 100644 index 00000000..24967615 --- /dev/null +++ b/lib/sof/all.rb @@ -0,0 +1,3 @@ +require_relative "members" +require_relative "array" +require_relative "occurence" diff --git a/lib/sof/array.rb b/lib/sof/array.rb new file mode 100644 index 00000000..1de170f2 --- /dev/null +++ b/lib/sof/array.rb @@ -0,0 +1,8 @@ +Array.class_eval do + def attributes + [] + end + def to_sof + "" + end +end diff --git a/lib/sof/members.rb b/lib/sof/members.rb new file mode 100644 index 00000000..5ab51cb5 --- /dev/null +++ b/lib/sof/members.rb @@ -0,0 +1,53 @@ +module Sof + + class Members + def initialize root + @root = root + @counter = 1 + @objects = {} + add(root ,0 ) + end + + def add object , level + if( @objects.has_key?(object) ) + occurence = @objects.get(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) + end + end + end + + def write + string = "" + output string , @root + end + + def output string , object + occurence = @objects[object] + indent = " " * occurence.level + string += indent + if(object.respond_to? :to_sof) + string += object.to_sof + "\n" + else + string += "!" + object.class.name + "\n" + indent += " " + object.attributes.each do a + val = object.send a + output( string , val) + end + end + end + + def self.write object + members = Members.new object + members.write + end + end +end diff --git a/lib/sof/occurence.rb b/lib/sof/occurence.rb new file mode 100644 index 00000000..52322762 --- /dev/null +++ b/lib/sof/occurence.rb @@ -0,0 +1,13 @@ +module Sof + + class Occurence + def initialize object , level , number + @number = number + @object = object + @level = level + end + attr_reader :object , :number + attr_accessor :level + end + +end