2021-12-07 16:09:17 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-02-11 15:46:47 +01:00
|
|
|
module RplLang
|
2022-02-10 14:50:59 +01:00
|
|
|
module Core
|
2022-02-11 15:46:47 +01:00
|
|
|
module Branch
|
|
|
|
def populate_dictionary
|
|
|
|
super
|
|
|
|
|
|
|
|
@dictionary.add_word( ['ift'],
|
|
|
|
'Branch',
|
|
|
|
'( t pt -- … ) eval pt or not based on the value of boolean t',
|
|
|
|
proc do
|
|
|
|
run( '« nop » ifte' )
|
|
|
|
end )
|
|
|
|
@dictionary.add_word( ['ifte'],
|
|
|
|
'Branch',
|
|
|
|
'( t pt pf -- … ) eval pt or pf based on the value of boolean t',
|
|
|
|
proc do
|
|
|
|
args = stack_extract( [:any, :any, %i[boolean]] )
|
|
|
|
|
|
|
|
run( args[ args[2][:value] ? 1 : 0 ][:value] )
|
|
|
|
end )
|
|
|
|
@dictionary.add_word( ['times'],
|
|
|
|
'Branch',
|
|
|
|
'( n p -- … ) eval p n times while pushing counter on stack before',
|
|
|
|
proc do
|
|
|
|
args = stack_extract( [:any, %i[numeric]] )
|
|
|
|
|
|
|
|
args[1][:value].to_i.times do |i|
|
|
|
|
counter = { value: BigDecimal( i, @precision ), type: :numeric, base: 10 }
|
|
|
|
@stack << counter
|
|
|
|
|
|
|
|
run( args[0][:value] )
|
|
|
|
end
|
|
|
|
|
|
|
|
end ) # specific
|
|
|
|
@dictionary.add_word( ['loop'],
|
|
|
|
'Branch',
|
|
|
|
'( n1 n2 p -- … ) eval p looping from n1 to n2 while pushing counter on stack before',
|
|
|
|
proc do
|
|
|
|
args = stack_extract( [:any, %i[numeric], %i[numeric]] )
|
|
|
|
|
|
|
|
((args[2][:value].to_i)..(args[1][:value].to_i)).each do |i|
|
|
|
|
counter = { value: BigDecimal( i, @precision ), type: :numeric, base: 10 }
|
|
|
|
@stack << counter
|
|
|
|
|
|
|
|
run( args[0][:value] )
|
|
|
|
end
|
|
|
|
end ) # specific
|
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
|