rpl.rb/lib/rpl/words/branch.rb

54 lines
2 KiB
Ruby
Raw Normal View History

2021-12-07 16:09:17 +01:00
# frozen_string_literal: true
module RplLang
module Words
module Branch
2022-02-26 18:53:39 +01:00
include Types
def populate_dictionary
super
@dictionary.add_word( ['ift'],
'Branch',
'( t pt -- … ) eval pt or not based on the value of boolean t',
2022-02-26 18:53:39 +01:00
RplProgram.new( '« « nop » ifte »' ) )
2022-02-11 16:06:57 +01:00
@dictionary.add_word( ['ifte'],
'Branch',
'( t pt pf -- … ) eval pt or pf based on the value of boolean t',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [:any, :any, [RplBoolean]] )
2022-02-26 18:53:39 +01:00
run( args[ args[2].value ? 1 : 0 ].value )
end )
2022-02-11 16:06:57 +01:00
@dictionary.add_word( ['times'],
'Branch',
2022-02-11 16:06:57 +01:00
'( p n -- … ) eval p n times while pushing counter on stack before',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplNumeric], :any] )
2022-02-26 18:53:39 +01:00
args[0].value.to_i.times do |counter|
@stack << RplNumeric.new( counter )
2022-02-26 18:53:39 +01:00
run( args[1].value )
end
2022-02-15 17:06:19 +01:00
end )
2022-02-11 16:06:57 +01:00
@dictionary.add_word( ['loop'],
'Branch',
2022-02-11 16:06:57 +01:00
'( p n1 n2 -- … ) eval p looping from n1 to n2 while pushing counter on stack before',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplNumeric], [RplNumeric], :any] )
2022-02-26 18:53:39 +01:00
((args[1].value.to_i)..(args[0].value.to_i)).each do |counter|
@stack << RplNumeric.new( counter )
2022-02-26 18:53:39 +01:00
run( args[2].value )
end
2022-02-15 17:06:19 +01:00
end )
2021-12-09 16:31:29 +01:00
end
2022-02-10 14:50:59 +01:00
end
2021-11-24 16:34:13 +01:00
end
end