Fix variables to chain recursively

This commit is contained in:
Torsten Rüger 2020-02-13 19:09:00 +07:00
parent 6194148fc5
commit a153fde3f0
5 changed files with 111 additions and 51 deletions

View File

@ -28,24 +28,30 @@ module SlotLanguage
end end
def on_send(statement) def on_send(statement)
kids = statement.children.dup kids = statement.children.dup
receiver = process(kids.shift) || MessageSlot.new receiver = kids.shift
name = kids.shift name = kids.shift
return label(name) if(name.to_s.end_with?("_label")) return label(name) if(name.to_s.end_with?("_label"))
return goto(name,kids) if(name == :goto) return goto(name,kids) if(name == :goto)
return check(name,receiver, kids) if SlotCompiler.checks.include?(name) return check(name,receiver, kids) if SlotCompiler.checks.include?(name)
return assign(receiver, name , kids) if(name.to_s.end_with?("=")) return assign(receiver, name , kids) if(name.to_s.end_with?("="))
puts "Send #{name} , #{receiver} kids=#{kids}" if DEBUG puts "Send: #{statement} " if DEBUG
Variable.new( name ) var = Variable.new( name )
if(receiver)
puts "receiver at #{name} #{receiver}" if DEBUG
process(receiver).chained(var)
else
var
end
end end
def on_lvar(lvar) def on_lvar(lvar)
puts "lvar #{lvar}" if DEBUG puts "lvar #{lvar}" if DEBUG
Variable.new(lvar.children.first ) Variable.new(lvar.children.first )
end end
def on_lvasgn( expression) def on_lvasgn( expression)
puts "lvasgn #{expression}" if DEBUG puts "i/lvasgn #{expression}" if DEBUG
name = expression.children[0] var = var_for(expression.children[0])
value = process(expression.children[1]) value = process(expression.children[1])
Assignment.new(Variable.new(name),value) Assignment.new(var , value)
end end
alias :on_ivasgn :on_lvasgn alias :on_ivasgn :on_lvasgn
@ -62,12 +68,21 @@ module SlotLanguage
process_all(exp) process_all(exp)
end end
end end
def on_ivar( expression) def on_ivar(expression)
puts "ivar #{expression}" if DEBUG puts "ivar #{expression}" if DEBUG
Variable.new(expression.children.first) var_for(expression.children.first)
end end
private private
def var_for( name )
name = name.to_s
if(name[0] == "@")
MessageVariable.new(name.to_s[1 .. -1].to_sym)
else
Variable.new(name.to_sym)
end
end
def label(name) def label(name)
raise "no label #{name}" unless(name.to_s.end_with?("_label")) raise "no label #{name}" unless(name.to_s.end_with?("_label"))
if @labels.has_key?(name) if @labels.has_key?(name)
@ -88,13 +103,14 @@ module SlotLanguage
right = process(kids.first) right = process(kids.first)
case name case name
when :== when :==
return EqualGoto.new(receiver , right) return EqualGoto.new(process(receiver) , right)
else else
raise "Only ==, not #{name}" unless name == :== raise "Only ==, not #{name}" unless name == :==
end end
end end
def assign(receiver , name , kids) def assign(receiver , name , kids)
raise name.to_s
name = name.to_s[0...-1].to_sym name = name.to_s[0...-1].to_sym
receiver.add_slot_name(name) receiver.add_slot_name(name)
right = process kids.shift right = process kids.shift

View File

@ -1,36 +1,43 @@
module SlotLanguage module SlotLanguage
# A Variable makes Slots. A Slot is the central SlotMachines description of a # A Variable makes Slots. A Slot is the central SlotMachines description of a
# variable in an object. At the Language level this holds the information # variable in an object. At the Language level this holds the information
# (names of instance variables) to be able to create the Slot instance # (names of variables) to be able to create the Slot instance
# #
# In the SlotLanguage this is used in the Assignment. Just as a Slotload stores # In the SlotLanguage this is used in the Assignment. Just as a Slotload stores
# two slots to define what is loaded where, the Assignment, that creates a SlotLoad, # two slots to define what is loaded where, the Assignment, that creates a SlotLoad,
# uses two Variables. # uses two Variables.
class Variable class Variable
# stores the (instance) names that allow us to create a Slot # stores the (instance) names that allow us to create a Slot
attr_reader :names attr_reader :name , :chain
def initialize(names) def initialize(name)
case names @name = name
when Array raise "No name given #{name}" unless name.is_a?(Symbol)
@names = names
when nil
raise "No names given"
else
@names = [names]
end
end end
def add_slot_name(name) def chained(to)
@names << name raise "Must chain to variable #{to}" unless to.is_a?(Variable)
if(@chain)
@chain.chained(to)
else
@chain = to
end
self
end end
def to_slot(compiler) def to_slot(compiler)
SlotMachine::Slot.for(:message , names) SlotMachine::Slot.for(:message , name)
end end
def to_s def to_s
"message." + names.join(",") str = "message.#{name}"
str += chain.to_s if @chain
str
end end
end end
class MessageVariable < Variable
end
class Constant < Variable
end
end end

View File

@ -3,39 +3,53 @@ require_relative "helper"
module SlotLanguage module SlotLanguage
class TestAssignment < MiniTest::Test class TestAssignment < MiniTest::Test
include SlotHelper include SlotHelper
def compile_assign(str)
assign = compile(str)
assert_equal Assignment , assign.class
assert_equal :a , assign.left.name
assert_equal :b , assign.right.name
assign
end
def test_slot_load_rinst def test_slot_load_rinst
assert_equal Assignment , compile_class("a = @b") assign = compile_assign("a = @b")
end end
def test_slot_load_linst def test_slot_load_linst
assert_equal Assignment , compile_class("@a = b") assign = compile_assign("@a = b")
end end
def test_slot_load_lrinst def test_slot_load_lrinst
assert_equal Assignment , compile_class("@a = @b") compile_assign("@a = @b")
end
def test_slot_load_linst_trav
assert_equal Assignment , compile_class("@a = b.c")
end
def test_slot_load_linst_trav2
assert_equal Assignment , compile_class("@a.c = b.c")
end end
def test_assign def test_assign
assign = compile("c = d") assign = compile_assign("a = b")
assert_equal Assignment , assign.class assert_equal Assignment , assign.class
end end
end
class TestAssignment2 < MiniTest::Test
include SlotHelper
def test_slot_load_linst_trav
assert_equal Assignment , compile_class("@a = b.c")
end
def test_assign1 def test_assign1
assign = compile("c = c.next") assign = compile("c = c.next")
assert_equal Assignment , assign.class assert_equal Assignment , assign.class
end end
def test_assign2
assign = compile("c.next = d")
assert_equal Assignment , assign.class
end
def test_shift def test_shift
load = compile("word = name.member") load = compile("a = b.c")
assert_equal Assignment , load.class assert_equal Assignment , load.class
assert_equal :word , load.left.names.first assert_equal :a , load.left.name
assert_equal Variable , load.right.class assert_equal Variable , load.right.class
end end
end end
class TestAssignment3 < MiniTest::Test
include SlotHelper
def est_slot_load_linst_trav2
assert_equal Assignment , compile_class("@a.c = b.c")
end
def est_assign2
assign = compile("c.next = d")
assert_equal Assignment , assign.class
end
end
end end

View File

@ -7,24 +7,22 @@ module SlotLanguage
def do_check(check) def do_check(check)
assert_equal EqualGoto , check.class assert_equal EqualGoto , check.class
assert_equal Goto , check.goto.class assert_equal Goto , check.goto.class
assert_equal Variable , check.left.class assert check.left.is_a?(Variable)
assert_equal Variable , check.right.class assert check.right.is_a?(Variable)
assert_equal :a , check.left.name
assert_equal :b , check.right.name
end end
def test_equal_local def test_equal_local
check = compile("goto(exit_label) if(a == b)") check = compile("goto(exit_label) if(a == b)")
do_check(check) do_check(check)
assert_equal :a , check.left.names[0]
assert_equal :b , check.right.names[0]
end end
def test_equal_inst_left def test_equal_inst_left
check = compile("goto(exit_label) if(@a == b)") check = compile("goto(exit_label) if(@a == b)")
do_check(check) do_check(check)
assert_equal :@a , check.left.names[0]
end end
def test_equal_inst_right def test_equal_inst_right
check = compile("goto(exit_label) if(a == @b)") check = compile("goto(exit_label) if(a == @b)")
do_check(check) do_check(check)
assert_equal :@b , check.right.names[0]
end end
end end
@ -50,11 +48,11 @@ module SlotLanguage
end end
def test_expression_left def test_expression_left
assert_equal Variable , @expr.last.left.class assert_equal Variable , @expr.last.left.class
assert_equal [:b] , @expr.last.left.names assert_equal :b , @expr.last.left.name
end end
def test_expression_right def test_expression_right
assert_equal Variable , @expr.last.right.class assert_equal Variable , @expr.last.right.class
assert_equal [:c] , @expr.last.right.names assert_equal :c , @expr.last.right.name
end end
end end
end end

View File

@ -3,8 +3,33 @@ require_relative "helper"
module SlotLanguage module SlotLanguage
class TestVariable < MiniTest::Test class TestVariable < MiniTest::Test
include SlotHelper include SlotHelper
def test_basic_compile def compile_var(str)
assert_equal Variable , compile("a").class var = compile(str)
assert var.is_a?(Variable)
assert_equal :a , var.name
var
end
def test_local
assert_equal Variable , compile_var("a").class
end
def test_inst
assert_equal MessageVariable , compile_var("@a").class
end
def test_local_chain
chain = compile_var("a.b")
assert_equal Variable , chain.chain.class
assert_equal :b , chain.chain.name
end
def test_local_chain2
chain = compile_var("a.b.c")
assert_equal Variable , chain.chain.chain.class
assert_equal :c , chain.chain.chain.name
end
def test_inst_chain
chain = compile_var("@a.b")
assert_equal MessageVariable , chain.class
assert_equal Variable , chain.chain.class
assert_equal :b , chain.chain.name
end end
end end
end end