Start nexr level p i SlotLanguage, calling it MacroMaker
Since we already have Macros. Macros are nothing more than a list of SlotMachine Instructions. This is what we are aiming to create (which is also what is created in Sol .to_slot) So the idea came to slot the MacroMaker in there after its done
This commit is contained in:
parent
cbbb0c2f07
commit
8cac5c064d
23
lib/slot_language/macro_maker.rb
Normal file
23
lib/slot_language/macro_maker.rb
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
module SlotLanguage
|
||||||
|
class MacroMaker
|
||||||
|
attr_reader :source
|
||||||
|
|
||||||
|
def self.load_file(relative_name)
|
||||||
|
path = File.expand_path( "../code/#{relative_name}" , __FILE__)
|
||||||
|
load_string( File.read(path))
|
||||||
|
end
|
||||||
|
def self.load_string(source_code)
|
||||||
|
MacroMaker.new( SlotCompiler.compile(source_code) )
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize( source )
|
||||||
|
@source = source
|
||||||
|
raise "undefined source #{source}" unless source.is_a?(Array)
|
||||||
|
end
|
||||||
|
|
||||||
|
def to_slot(compiler)
|
||||||
|
@source.first
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -29,27 +29,30 @@ module SlotLanguage
|
|||||||
def on_lvar(lvar)
|
def on_lvar(lvar)
|
||||||
SlotMaker.new(lvar.children.first , nil)
|
SlotMaker.new(lvar.children.first , nil)
|
||||||
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,nil),value)
|
||||||
end
|
end
|
||||||
|
alias :on_ivasgn :on_lvasgn
|
||||||
|
|
||||||
def on_if(expression)
|
def on_if(expression)
|
||||||
condition = process(expression.children[0])
|
condition = process(expression.children[0])
|
||||||
condition.set_goto( process(expression.children[1]) )
|
condition.set_goto( process(expression.children[1]) )
|
||||||
condition
|
condition
|
||||||
end
|
end
|
||||||
def on_begin(exp)
|
def on_begin(exp)
|
||||||
process(exp.first)
|
if( exp.children.length == 1)
|
||||||
|
process(exp.first)
|
||||||
|
else
|
||||||
|
process_all(exp)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
def on_ivar expression
|
def on_ivar expression
|
||||||
SlotMaker.new(instance_name(expression.children.first),nil)
|
SlotMaker.new(expression.children.first,nil)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def instance_name(sym)
|
|
||||||
sym.to_s[1 .. -1].to_sym
|
|
||||||
end
|
|
||||||
def label(name)
|
def label(name)
|
||||||
SlotMachine::Label.new(name.to_s , name)
|
SlotMachine::Label.new(name.to_s , name)
|
||||||
end
|
end
|
||||||
@ -78,3 +81,4 @@ 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 "check_maker"
|
||||||
|
require_relative "macro_maker"
|
||||||
|
@ -9,4 +9,10 @@ module SlotLanguage
|
|||||||
compile(input).class
|
compile(input).class
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
module SlotToHelper
|
||||||
|
def setup
|
||||||
|
Parfait.boot!({})
|
||||||
|
@compiler = SlotMachine::SlotCollection.compiler_for( :Space , :main,{},{})
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
2
test/slot_language/mini.slot
Normal file
2
test/slot_language/mini.slot
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
start_label
|
||||||
|
a = b
|
40
test/slot_language/test_macro_maker.rb
Normal file
40
test/slot_language/test_macro_maker.rb
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
module SlotLanguage
|
||||||
|
class TestMacroMakerLoad < MiniTest::Test
|
||||||
|
include SlotToHelper
|
||||||
|
def setup
|
||||||
|
super
|
||||||
|
@slot = MacroMaker.load_string( mini_file ).to_slot(@compiler)
|
||||||
|
end
|
||||||
|
def test_to_slot
|
||||||
|
assert @slot.is_a?(SlotMachine::Instruction) , @slot.class
|
||||||
|
end
|
||||||
|
def test_length
|
||||||
|
assert_equal 1 , @slot.length
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class TestMacroMakerLoad < MiniTest::Test
|
||||||
|
include SlotHelper
|
||||||
|
|
||||||
|
def check_mini(maker)
|
||||||
|
assert_equal MacroMaker , maker.class
|
||||||
|
assert_equal Array , maker.source.class
|
||||||
|
assert_equal SlotMachine::Label , maker.source.first.class
|
||||||
|
assert_equal 2 , maker.source.length
|
||||||
|
end
|
||||||
|
def mini_file
|
||||||
|
File.read(File.expand_path( "../mini.slot" , __FILE__))
|
||||||
|
end
|
||||||
|
def test_mini_file
|
||||||
|
check_mini MacroMaker.load_file("../../../test/slot_language/mini.slot")
|
||||||
|
end
|
||||||
|
def test_mini_string
|
||||||
|
check_mini MacroMaker.load_string( mini_file )
|
||||||
|
end
|
||||||
|
def test_mini_source
|
||||||
|
check_mini MacroMaker.new( SlotCompiler.compile(mini_file))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
@ -15,9 +15,21 @@ module SlotLanguage
|
|||||||
assert_equal SlotMachine::Label , label.class
|
assert_equal SlotMachine::Label , label.class
|
||||||
assert_equal :while_label , label.name
|
assert_equal :while_label , label.name
|
||||||
end
|
end
|
||||||
def test_slot_load
|
def test_slot_load_rinst
|
||||||
assert_equal LoadMaker , compile_class("a = @b")
|
assert_equal LoadMaker , compile_class("a = @b")
|
||||||
end
|
end
|
||||||
|
def test_slot_load_linst
|
||||||
|
assert_equal LoadMaker , compile_class("@a = b")
|
||||||
|
end
|
||||||
|
def test_slot_load_lrinst
|
||||||
|
assert_equal LoadMaker , compile_class("@a = @b")
|
||||||
|
end
|
||||||
|
def test_slot_load_linst_trav
|
||||||
|
assert_equal LoadMaker , compile_class("@a = b.c")
|
||||||
|
end
|
||||||
|
def test_slot_load_linst_trav2
|
||||||
|
assert_equal LoadMaker , compile_class("@a.c = b.c")
|
||||||
|
end
|
||||||
def test_goto
|
def test_goto
|
||||||
assert_equal SlotMachine::Jump , compile_class("goto(exit_label)")
|
assert_equal SlotMachine::Jump , compile_class("goto(exit_label)")
|
||||||
end
|
end
|
||||||
@ -38,5 +50,11 @@ 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 SlotMachine::Jump , multi.last.class
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
8
test/slot_language/test_slot_compiler2.rb
Normal file
8
test/slot_language/test_slot_compiler2.rb
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
require_relative "helper"
|
||||||
|
|
||||||
|
module SlotLanguage
|
||||||
|
class TestSlotCompiler < MiniTest::Test
|
||||||
|
include SlotHelper
|
||||||
|
|
||||||
|
end
|
||||||
|
end
|
Loading…
x
Reference in New Issue
Block a user