Start to transform SL into SM

This commit is contained in:
Torsten Rüger 2019-10-07 20:14:40 +03:00
parent 8cac5c064d
commit 308670b90c
7 changed files with 69 additions and 25 deletions

View File

@ -8,5 +8,11 @@ module SlotLanguage
raise "No Slot #{left}" unless left.is_a?(SlotMaker) raise "No Slot #{left}" unless left.is_a?(SlotMaker)
raise "No Slot #{right}" unless right.is_a?(SlotMaker) raise "No Slot #{right}" unless right.is_a?(SlotMaker)
end end
def to_slot(compiler)
left_d = @left.slot_def(compiler)
right_d = @right.slot_def(compiler)
SlotMachine::SlotLoad.new("source" , left_d , right_d)
end
end end
end end

View File

@ -17,7 +17,19 @@ module SlotLanguage
end end
def to_slot(compiler) def to_slot(compiler)
@source.first chain = do_link( @source.first , compiler)
rest = @source.dup
rest.shift
rest.each do |link|
chain << do_link(link , compiler)
end
chain
end
private
def do_link(link,compiler)
return link if link.is_a?(SlotMachine::Instruction)
link.to_slot(compiler)
end end
end end
end end

View File

@ -24,15 +24,15 @@ module SlotLanguage
return goto(name,kids) if(name == :goto) return goto(name,kids) if(name == :goto)
return check(name,receiver, kids) if(name == :==) return check(name,receiver, kids) if(name == :==)
return assign(receiver, name , kids) if(name.to_s.end_with?("=")) return assign(receiver, name , kids) if(name.to_s.end_with?("="))
SlotMaker.new( name , receiver ) SlotMaker.new( name )
end end
def on_lvar(lvar) def on_lvar(lvar)
SlotMaker.new(lvar.children.first , nil) SlotMaker.new(lvar.children.first )
end end
def on_lvasgn( expression) def on_lvasgn( expression)
name = expression.children[0] name = expression.children[0]
value = process(expression.children[1]) value = process(expression.children[1])
LoadMaker.new(SlotMaker.new(name,nil),value) LoadMaker.new(SlotMaker.new(name),value)
end end
alias :on_ivasgn :on_lvasgn alias :on_ivasgn :on_lvasgn
@ -49,7 +49,7 @@ module SlotLanguage
end end
end end
def on_ivar expression def on_ivar expression
SlotMaker.new(expression.children.first,nil) SlotMaker.new(expression.children.first)
end end
private private

View File

@ -1,22 +1,24 @@
module SlotLanguage module SlotLanguage
class SlotMaker class SlotMaker
attr_reader :name , :leaps attr_reader :leaps
def initialize(name , more) def initialize(leaps)
@name = name case leaps
if(more.is_a?(Array)) when Array
@leaps = more @leaps = leaps
when nil
raise "No leaps given"
else else
@leaps = [more] if more @leaps = [leaps]
end end
end end
def add_slot_name(name) def add_slot_name(name)
if(@leaps)
@leaps << name @leaps << name
else end
@leaps = [name]
end def slot_def(compiler)
SlotMachine::SlotDefinition.new(:message , leaps)
end end
end end
end end

View File

@ -0,0 +1,16 @@
require_relative "helper"
module SlotLanguage
class TestLoadMaker < MiniTest::Test
include SlotToHelper
def setup
super
left = SlotMaker.new(:hi )
right = SlotMaker.new(:hi )
@slot = LoadMaker.new( left,right ).to_slot(@compiler)
end
def test_to_slot
assert_equal SlotMachine::SlotLoad , @slot.class
end
end
end

View File

@ -7,11 +7,18 @@ module SlotLanguage
super super
@slot = MacroMaker.load_string( mini_file ).to_slot(@compiler) @slot = MacroMaker.load_string( mini_file ).to_slot(@compiler)
end end
def test_to_slot def test_label
assert @slot.is_a?(SlotMachine::Instruction) , @slot.class assert_equal SlotMachine::Label , @slot.class
end
def test_assign
assert_equal SlotMachine::SlotLoad , @slot.next.class
assert_equal :message , @slot.next.left.known_object
assert_equal [:a] , @slot.next.left.slots
assert_equal :message , @slot.next.right.known_object
assert_equal [:b] , @slot.next.right.slots
end end
def test_length def test_length
assert_equal 1 , @slot.length assert_equal 2 , @slot.length
end end
end end

View File

@ -2,12 +2,13 @@ require_relative "helper"
module SlotLanguage module SlotLanguage
class TestSlotMaker < MiniTest::Test class TestSlotMaker < MiniTest::Test
include SlotHelper include SlotToHelper
def setup
def test_label super
label = compile("while_label") @maker = SlotMaker.new(:hi )
assert_equal SlotMachine::Label , label.class end
assert_equal :while_label , label.name def test_slot
@maker.slot_def(@compiler)
end end
end end
end end