8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/rpl/words/string-list.rb', line 8
def populate_dictionary
super
@dictionary.add_word( ['→str', '->str'],
'String',
'( a -- s ) convert element to string',
proc do
args = ( [:any] )
@stack << Types.new_object( RplString, "\"#{args[0]}\"" )
end )
@dictionary.add_word( ['str→', 'str->'],
'String',
'( s -- a ) convert string to element',
proc do
args = ( [[RplString]] )
@stack += Parser.parse( args[0].value )
end )
@dictionary.add_word( ['→list', '->list'],
'Lists',
'( … x -- […] ) pack x stacks levels into a list',
proc do
args = ( [[RplNumeric]] )
args = ( %i[any] * args[0].value )
@stack << Types.new_object( RplList, args.reverse )
end )
@dictionary.add_word( ['list→', 'list->'],
'Lists',
'( […] -- … ) unpack list on stack',
proc do
args = ( [[RplList]] )
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
args = ( [[RplNumeric]] )
@stack << Types.new_object( RplString, "\"#{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
args = ( [[RplString]] )
@stack << Types.new_object( RplNumeric, args[0].value.ord )
end )
@dictionary.add_word( ['size'],
'String',
'( s -- n ) return the length of the string or list',
proc do
args = ( [[RplString, RplList]] )
@stack << Types.new_object( RplNumeric, 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
args = ( [[RplString], [RplString]] )
@stack << Types.new_object( RplNumeric, 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
args = ( [[RplNumeric], [RplNumeric], [RplString]] )
@stack << Types.new_object( RplString, "\"#{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
args = ( [[RplString, RplList]] )
args[0].value.reverse!
@stack << args[0]
end )
@dictionary.add_word( ['split'],
'String',
'( s c -- … ) split string s on character c',
proc do
args = ( [[RplString], [RplString]] )
args[1].value.split( args[0].value ).each do |elt|
@stack << Types.new_object( RplString, "\"#{elt}\"" )
end
end )
@dictionary.add_word( ['dolist'],
'Lists',
'( […] prg -- … ) run prg on each element of a list',
proc do
args = ( [[RplProgram], [RplList]] )
args[1].value.each do |elt|
@stack << elt
run( args[0].value )
end
run( "#{args[1].value.length} →list" )
end )
end
|