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

View File

@ -1,36 +1,43 @@
module SlotLanguage
# 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
# (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
# two slots to define what is loaded where, the Assignment, that creates a SlotLoad,
# uses two Variables.
class Variable
# stores the (instance) names that allow us to create a Slot
attr_reader :names
attr_reader :name , :chain
def initialize(names)
case names
when Array
@names = names
when nil
raise "No names given"
else
@names = [names]
end
def initialize(name)
@name = name
raise "No name given #{name}" unless name.is_a?(Symbol)
end
def add_slot_name(name)
@names << name
def chained(to)
raise "Must chain to variable #{to}" unless to.is_a?(Variable)
if(@chain)
@chain.chained(to)
else
@chain = to
end
self
end
def to_slot(compiler)
SlotMachine::Slot.for(:message , names)
SlotMachine::Slot.for(:message , name)
end
def to_s
"message." + names.join(",")
str = "message.#{name}"
str += chain.to_s if @chain
str
end
end
class MessageVariable < Variable
end
class Constant < Variable
end
end

View File

@ -3,39 +3,53 @@ require_relative "helper"
module SlotLanguage
class TestAssignment < MiniTest::Test
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
assert_equal Assignment , compile_class("a = @b")
assign = compile_assign("a = @b")
end
def test_slot_load_linst
assert_equal Assignment , compile_class("@a = b")
assign = compile_assign("@a = b")
end
def test_slot_load_lrinst
assert_equal Assignment , compile_class("@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")
compile_assign("@a = @b")
end
def test_assign
assign = compile("c = d")
assign = compile_assign("a = b")
assert_equal Assignment , assign.class
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
assign = compile("c = c.next")
assert_equal Assignment , assign.class
end
def test_assign2
assign = compile("c.next = d")
assert_equal Assignment , assign.class
end
def test_shift
load = compile("word = name.member")
load = compile("a = b.c")
assert_equal Assignment , load.class
assert_equal :word , load.left.names.first
assert_equal :a , load.left.name
assert_equal Variable , load.right.class
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

View File

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

View File

@ -3,8 +3,33 @@ require_relative "helper"
module SlotLanguage
class TestVariable < MiniTest::Test
include SlotHelper
def test_basic_compile
assert_equal Variable , compile("a").class
def compile_var(str)
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