rpl.rb/lib/rpl/words/string-list.rb

134 lines
5.4 KiB
Ruby
Raw Normal View History

2021-12-07 16:09:17 +01:00
# frozen_string_literal: true
module RplLang
module Words
module StringAndList
2022-02-26 18:53:39 +01:00
include Types
def populate_dictionary
super
@dictionary.add_word( ['→str', '->str'],
'String',
'( a -- s ) convert element to string',
proc do
args = stack_extract( [:any] )
2022-02-26 18:53:39 +01:00
@stack << RplString.new( "\"#{args[0]}\"" )
end )
@dictionary.add_word( ['str→', 'str->'],
'String',
'( s -- a ) convert string to element',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplString]] )
2022-02-26 18:53:39 +01:00
@stack += Parser.parse( args[0].value )
end )
@dictionary.add_word( ['→list', '->list'],
'Lists',
'( … x -- […] ) pack x stacks levels into a list',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplNumeric]] )
args = stack_extract( %i[any] * args[0].value )
2022-02-26 18:53:39 +01:00
@stack << RplList.new( args.reverse )
end )
@dictionary.add_word( ['list→', 'list->'],
'Lists',
'( […] -- … ) unpack list on stack',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplList]] )
2022-02-26 18:53:39 +01:00
args[0].value.each do |elt|
@stack << elt
end
end )
@dictionary.add_word( ['chr'],
'String',
'( n -- c ) convert ASCII character code in stack level 1 into a string',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplNumeric]] )
2022-02-26 18:53:39 +01:00
@stack << RplString.new( "\"#{args[0].value.to_i.chr}\"" )
end )
@dictionary.add_word( ['num'],
'String',
'( s -- n ) return ASCII code of the first character of the string in stack level 1 as a real number',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplString]] )
2022-02-26 18:53:39 +01:00
@stack << RplNumeric.new( args[0].value.ord )
end )
@dictionary.add_word( ['size'],
'String',
'( s -- n ) return the length of the string or list',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplString, RplList]] )
2022-02-26 18:53:39 +01:00
@stack << RplNumeric.new( args[0].value.length )
end )
@dictionary.add_word( ['pos'],
'String',
'( s s -- n ) search for the string in level 1 within the string in level 2',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplString], [RplString]] )
2022-02-26 18:53:39 +01:00
@stack << RplNumeric.new( args[1].value.index( args[0].value ) )
end )
@dictionary.add_word( ['sub'],
'String',
'( s n n -- s ) return a substring of the string in level 3',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplNumeric], [RplNumeric], [RplString]] )
2022-02-26 18:53:39 +01:00
@stack << RplString.new( "\"#{args[2].value[ (args[1].value - 1)..(args[0].value - 1) ]}\"" )
end )
@dictionary.add_word( ['rev'],
'String',
'( s -- s ) reverse string or list',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplString, RplList]] )
2022-02-26 18:53:39 +01:00
args[0].value.reverse!
2022-02-26 18:53:39 +01:00
@stack << args[0]
2022-02-15 17:06:19 +01:00
end )
@dictionary.add_word( ['split'],
'String',
'( s c -- … ) split string s on character c',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplString], [RplString]] )
2022-02-26 18:53:39 +01:00
args[1].value.split( args[0].value ).each do |elt|
@stack << RplString.new( "\"#{elt}\"" )
end
2022-02-15 17:06:19 +01:00
end )
@dictionary.add_word( ['dolist'],
'Lists',
'( […] prg -- … ) run prg on each element of a list',
proc do
2022-02-26 18:53:39 +01:00
args = stack_extract( [[RplProgram], [RplList]] )
2022-02-26 18:53:39 +01:00
args[1].value.each do |elt|
@stack << elt
2022-02-26 18:53:39 +01:00
run( args[0].value )
end
2022-02-26 18:53:39 +01:00
run( "#{args[1].value.length} →list" )
end )
2021-12-15 13:32:48 +01:00
end
end
end
end