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 #{right}" unless right.is_a?(SlotMaker)
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

View File

@ -17,7 +17,19 @@ module SlotLanguage
end
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

View File

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

View File

@ -1,22 +1,24 @@
module SlotLanguage
class SlotMaker
attr_reader :name , :leaps
attr_reader :leaps
def initialize(name , more)
@name = name
if(more.is_a?(Array))
@leaps = more
def initialize(leaps)
case leaps
when Array
@leaps = leaps
when nil
raise "No leaps given"
else
@leaps = [more] if more
@leaps = [leaps]
end
end
def add_slot_name(name)
if(@leaps)
@leaps << name
else
@leaps = [name]
end
@leaps << name
end
def slot_def(compiler)
SlotMachine::SlotDefinition.new(:message , leaps)
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
@slot = MacroMaker.load_string( mini_file ).to_slot(@compiler)
end
def test_to_slot
assert @slot.is_a?(SlotMachine::Instruction) , @slot.class
def test_label
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
def test_length
assert_equal 1 , @slot.length
assert_equal 2 , @slot.length
end
end

View File

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