cleaning, docs

This commit is contained in:
Torsten Rüger 2020-02-12 15:41:16 +07:00
parent 3c762c4fe7
commit 0e3a8bb859
6 changed files with 60 additions and 14 deletions

View File

@ -1,5 +1,11 @@
module SlotLanguage module SlotLanguage
# A LoadMaker makes SlotLoad. That means it stores the information
# to be able to create a SlotLoad
#
# Just like the SlotLoad stores two Slots, here we store two SlotMakers
#
class LoadMaker class LoadMaker
# The two SlotMakers that become Slots in to_slot
attr_reader :left , :right attr_reader :left , :right
def initialize(left , right) def initialize(left , right)
@ -9,9 +15,10 @@ module SlotLanguage
raise "No Slot #{right}" unless right.is_a?(SlotMaker) raise "No Slot #{right}" unless right.is_a?(SlotMaker)
end end
# create the SlotLoad, by creating the two Slots from the SlotMakers
def to_slot(compiler) def to_slot(compiler)
left_d = @left.slot_def(compiler) left_d = @left.to_slot(compiler)
right_d = @right.slot_def(compiler) right_d = @right.to_slot(compiler)
SlotMachine::SlotLoad.new("source" , left_d , right_d) SlotMachine::SlotLoad.new("source" , left_d , right_d)
end end
end end

View File

@ -1,21 +1,51 @@
module SlotLanguage module SlotLanguage
# MacroMaker instances represent Macros at the Slot level
#
# Macros are like SlotInstruction instances in that they transform to Risc
# But all SlotInstructions form a whole that lets us reduce Sol to Slot to risc.
# Each Macro on the other hand represents a functionality (kind of method)
# that can not be coded in sol. It transforms to a sequence of risc Instructions
# that can not be coded any other way. They are not Methods, as they have no
# scope, hence the name Macro.
#
# This MacroMaker is an attempt to code these kind of sequences in SlotLanguage
# The SlotCompiler is used to transform a file of SlotLanguage code into and
# array of SlotLanguage constructs, which in turn can be transformed into
# SlotInstructions.
# To start with we work backwards from existing large SlotInstructions, to
# get a list of constructs that will transform to the same SlotInstructions
# that transform to the same risc as the current large instruction (+ some redundandency)
class MacroMaker class MacroMaker
# an array of Makers
attr_reader :source attr_reader :source
# load slot code from a file, in a subdir code/ + filename
# use load_string to compile the content
def self.load_file(relative_name) def self.load_file(relative_name)
path = File.expand_path( "../code/#{relative_name}" , __FILE__) path = File.expand_path( "../code/#{relative_name}" , __FILE__)
load_string( File.read(path)) load_string( File.read(path))
end end
# compile the given SlotLanguage source
# the compiler returns an array of Makers which a new MacroMaker
# instance stores
# return the MacroMaker that represents the source
def self.load_string(source_code) def self.load_string(source_code)
MacroMaker.new( SlotCompiler.compile(source_code) ) MacroMaker.new( SlotCompiler.compile(source_code) )
end end
# must initialize with an array of Makers, which is stored
def initialize( source ) def initialize( source )
@source = source @source = source
raise "undefined source #{source}" unless source.is_a?(Array) raise "undefined source #{source}" unless source.is_a?(Array)
end end
# Basically calls to_slot on each Element of the source array
#
# Thus transforming an array of Makers into a linked list of
# SlotInstructions.
# Return the head of the linked list.
def to_slot(compiler) def to_slot(compiler)
chain = do_link( @source.first , compiler) chain = do_link( @source.first , compiler)
rest = @source.dup rest = @source.dup

View File

@ -9,6 +9,7 @@ module SlotLanguage
ast = Parser::CurrentRuby.parse( input ) ast = Parser::CurrentRuby.parse( input )
self.new.process(ast) self.new.process(ast)
end end
def not_implemented(node) def not_implemented(node)
raise "Not implemented #{node.type}" raise "Not implemented #{node.type}"
end end

View File

@ -1,28 +1,36 @@
module SlotLanguage module SlotLanguage
# A SlotMaker makes Slots. A Slot s the central SlotMachines description of a
# variable in an object. This Language level "Maker" holds the information
# (names of instance variables) to be able to create the Slot instance
#
# In the SlotLanguage this is used in the LoadMaker. Just as a Slotload stores
# two slots to define what is loaded where, the LoadMaker, that creates a SlotLoad,
# uses two SlotMakers.
class SlotMaker class SlotMaker
attr_reader :leaps # stores the (instance) names that allow us to create a Slot
attr_reader :names
def initialize(leaps) def initialize(names)
case leaps case names
when Array when Array
@leaps = leaps @names = names
when nil when nil
raise "No leaps given" raise "No names given"
else else
@leaps = [leaps] @names = [names]
end end
end end
def add_slot_name(name) def add_slot_name(name)
@leaps << name @names << name
end end
def slot_def(compiler) def to_slot(compiler)
SlotMachine::Slot.for(:message , leaps) SlotMachine::Slot.for(:message , names)
end end
def to_s def to_s
"message." + leaps.join(",") "message." + names.join(",")
end end
end end
end end

View File

@ -59,7 +59,7 @@ module SlotLanguage
def test_shift def test_shift
load = compile("word = name.member") load = compile("word = name.member")
assert_equal LoadMaker , load.class assert_equal LoadMaker , load.class
assert_equal :word , load.left.leaps.first assert_equal :word , load.left.names.first
assert_equal SlotMaker , load.right.class assert_equal SlotMaker , load.right.class
end end
end end

View File

@ -8,7 +8,7 @@ module SlotLanguage
@maker = SlotMaker.new(:hi ) @maker = SlotMaker.new(:hi )
end end
def test_slot def test_slot
@maker.slot_def(@compiler) @maker.to_slot(@compiler)
end end
end end
end end