Rename check_maker to equal_goto
will have different classes for different checks, started that
This commit is contained in:
parent
0342df41c7
commit
99d2868400
@ -1,14 +0,0 @@
|
|||||||
module SlotLanguage
|
|
||||||
class CheckMaker
|
|
||||||
attr_reader :check , :left , :right, :goto
|
|
||||||
|
|
||||||
def initialize(check , left , right)
|
|
||||||
@check = check
|
|
||||||
@left = left
|
|
||||||
@right = right
|
|
||||||
end
|
|
||||||
def set_goto(go)
|
|
||||||
@goto = go
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
13
lib/slot_language/equal_goto.rb
Normal file
13
lib/slot_language/equal_goto.rb
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
module SlotLanguage
|
||||||
|
class EqualGoto
|
||||||
|
attr_reader :left , :right, :goto
|
||||||
|
|
||||||
|
def initialize( left , right)
|
||||||
|
@left = left
|
||||||
|
@right = right
|
||||||
|
end
|
||||||
|
def set_goto(go)
|
||||||
|
@goto = go
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -5,11 +5,16 @@ module SlotLanguage
|
|||||||
class SlotCompiler < AST::Processor
|
class SlotCompiler < AST::Processor
|
||||||
DEBUG = false
|
DEBUG = false
|
||||||
|
|
||||||
|
# conditionals supported, currently only equal
|
||||||
|
def self.checks
|
||||||
|
[:==]
|
||||||
|
end
|
||||||
|
|
||||||
def self.compile(input)
|
def self.compile(input)
|
||||||
ast = Parser::CurrentRuby.parse( input )
|
ast = Parser::CurrentRuby.parse( input )
|
||||||
self.new.process(ast)
|
self.new.process(ast)
|
||||||
end
|
end
|
||||||
|
attr_reader :labels
|
||||||
def initialize
|
def initialize
|
||||||
@labels = {}
|
@labels = {}
|
||||||
end
|
end
|
||||||
@ -27,7 +32,7 @@ module SlotLanguage
|
|||||||
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(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 #{name} , #{receiver} kids=#{kids}" if DEBUG
|
||||||
SlotMaker.new( name )
|
SlotMaker.new( name )
|
||||||
@ -78,12 +83,17 @@ module SlotLanguage
|
|||||||
Goto.new( label )
|
Goto.new( label )
|
||||||
end
|
end
|
||||||
def check(name , receiver , kids)
|
def check(name , receiver , kids)
|
||||||
raise "Only ==, not #{name}" unless name == :==
|
|
||||||
raise "Familiy too large #{kids}" if kids.length > 1
|
raise "Familiy too large #{kids}" if kids.length > 1
|
||||||
puts "Kids " + kids.to_s if DEBUG
|
puts "Kids " + kids.to_s if DEBUG
|
||||||
right = process(kids.first)
|
right = process(kids.first)
|
||||||
CheckMaker.new(name , receiver , right)
|
case name
|
||||||
|
when :==
|
||||||
|
return EqualGoto.new(receiver , right)
|
||||||
|
else
|
||||||
|
raise "Only ==, not #{name}" unless name == :==
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def assign(receiver , name , kids)
|
def assign(receiver , name , kids)
|
||||||
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)
|
||||||
@ -97,6 +107,6 @@ require_relative "named_slot"
|
|||||||
require_relative "message_slot"
|
require_relative "message_slot"
|
||||||
require_relative "slot_maker"
|
require_relative "slot_maker"
|
||||||
require_relative "load_maker"
|
require_relative "load_maker"
|
||||||
require_relative "check_maker"
|
|
||||||
require_relative "macro_maker"
|
require_relative "macro_maker"
|
||||||
require_relative "goto"
|
require_relative "goto"
|
||||||
|
require_relative "equal_goto"
|
||||||
|
60
test/slot_language/test_equal_goto.rb
Normal file
60
test/slot_language/test_equal_goto.rb
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
module SlotLanguage
|
||||||
|
class TestEqualGoto < MiniTest::Test
|
||||||
|
include SlotHelper
|
||||||
|
|
||||||
|
def do_check(check)
|
||||||
|
assert_equal EqualGoto , check.class
|
||||||
|
assert_equal Goto , check.goto.class
|
||||||
|
assert_equal SlotMaker , check.left.class
|
||||||
|
assert_equal SlotMaker , check.right.class
|
||||||
|
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
|
||||||
|
|
||||||
|
class TestEqualGotoFull < MiniTest::Test
|
||||||
|
include SlotHelper
|
||||||
|
def setup
|
||||||
|
@expr = compile("start_label;goto(start_label) if( b == c)")
|
||||||
|
end
|
||||||
|
def test_2
|
||||||
|
assert_equal Array , @expr.class
|
||||||
|
assert_equal 2 , @expr.length
|
||||||
|
end
|
||||||
|
def test_label
|
||||||
|
assert_equal SlotMachine::Label , @expr.first.class
|
||||||
|
assert_equal :start_label , @expr.first.name
|
||||||
|
end
|
||||||
|
def test_conditional
|
||||||
|
assert_equal EqualGoto , @expr.last.class
|
||||||
|
assert_equal :start_label , @expr.last.goto.label.name
|
||||||
|
end
|
||||||
|
def test_same_label
|
||||||
|
assert_equal @expr.first.object_id , @expr.last.goto.label.object_id
|
||||||
|
end
|
||||||
|
def test_expression_left
|
||||||
|
assert_equal SlotMaker , @expr.last.left.class
|
||||||
|
assert_equal [:b] , @expr.last.left.names
|
||||||
|
end
|
||||||
|
def test_expression_right
|
||||||
|
assert_equal SlotMaker , @expr.last.right.class
|
||||||
|
assert_equal [:c] , @expr.last.right.names
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -7,6 +7,9 @@ module SlotLanguage
|
|||||||
def test_init
|
def test_init
|
||||||
assert SlotCompiler.new
|
assert SlotCompiler.new
|
||||||
end
|
end
|
||||||
|
def test_labels
|
||||||
|
assert SlotCompiler.new.labels.empty?
|
||||||
|
end
|
||||||
def test_compile
|
def test_compile
|
||||||
assert_equal SlotMaker , compile("a").class
|
assert_equal SlotMaker , compile("a").class
|
||||||
end
|
end
|
||||||
@ -25,11 +28,6 @@ module SlotLanguage
|
|||||||
def test_slot_load_linst_trav2
|
def test_slot_load_linst_trav2
|
||||||
assert_equal LoadMaker , compile_class("@a.c = b.c")
|
assert_equal LoadMaker , compile_class("@a.c = b.c")
|
||||||
end
|
end
|
||||||
def test_if
|
|
||||||
check = compile("goto(exit_label) if(a == b)")
|
|
||||||
assert_equal CheckMaker , check.class
|
|
||||||
assert_equal Goto , check.goto.class
|
|
||||||
end
|
|
||||||
def test_assign
|
def test_assign
|
||||||
assign = compile("c = d")
|
assign = compile("c = d")
|
||||||
assert_equal LoadMaker , assign.class
|
assert_equal LoadMaker , assign.class
|
||||||
@ -42,12 +40,6 @@ module SlotLanguage
|
|||||||
assign = compile("c.next = d")
|
assign = compile("c.next = d")
|
||||||
assert_equal LoadMaker , assign.class
|
assert_equal LoadMaker , assign.class
|
||||||
end
|
end
|
||||||
def test_multiline
|
|
||||||
multi = compile("start_label;c = c.next;goto(start_label)")
|
|
||||||
assert_equal Array , multi.class
|
|
||||||
assert_equal SlotMachine::Label , multi.first.class
|
|
||||||
assert_equal Goto , multi.last.class
|
|
||||||
end
|
|
||||||
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
|
||||||
|
Loading…
x
Reference in New Issue
Block a user