Class: Forthic::GlobalModule

Inherits:
ForthicModule show all
Defined in:
lib/forthic/global_module.rb

Instance Attribute Summary collapse

Attributes inherited from ForthicModule

#exportable, #forthic_code, #module_prefixes, #modules, #name, #required_modules, #variables, #words

Instance Method Summary collapse

Methods inherited from ForthicModule

#add_exportable, #add_exportable_word, #add_memo_words, #add_module_word, #add_variable, #add_word, #dup, #exportable_words, #find_dictionary_word, #find_module, #find_variable, #import_module, #initialize_modules, #register_module, #require_module

Constructor Details

#initialize(interp) ⇒ GlobalModule

Returns a new instance of GlobalModule.



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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/forthic/global_module.rb', line 18

def initialize(interp)
  super("<GLOBAL>", interp)
  @module_id = "<GLOBAL>-#{rand(1_000_000)}"

  # Set default flags
  interp.set_flags(@module_id, {
    with_key: nil,
    push_error: nil,
    comparator: nil,
    push_rest: nil,
    depth: nil,
    interps: 1
  })

  @literal_handlers = [
    method(:to_bool),
    method(:to_float),
    method(:to_literal_date),
    method(:to_time),
    method(:to_int)
  ]

  # --------------------------------------------------
  # Base words
  add_module_word("VARIABLES", method(:word_VARIABLES))
  add_module_word("!", method(:word_bang))
  add_module_word("@", method(:word_at))
  add_module_word("!@", method(:word_bang_at))
  add_module_word("INTERPRET", method(:word_INTERPRET))
  add_module_word("EXPORT", method(:word_EXPORT))
  add_module_word("USE-MODULES", method(:word_USE_MODULES))
  add_module_word("REC", method(:word_REC))
  add_module_word("REC@", method(:word_REC_at))
  add_module_word("<REC!", method(:word_l_REC_bang))

  # --------------------------------------------------
  # Array/Record words
  add_module_word("APPEND", method(:word_APPEND))
  add_module_word("REVERSE", method(:word_REVERSE))
  add_module_word("UNIQUE", method(:word_UNIQUE))
  add_module_word("<DEL", method(:word_L_DEL))
  add_module_word("RELABEL", method(:word_RELABEL))
  add_module_word("BY-FIELD", method(:word_BY_FIELD))
  add_module_word("GROUP-BY-FIELD", method(:word_GROUP_BY_FIELD))
  add_module_word("GROUP-BY", method(:word_GROUP_BY))
  add_module_word("GROUPS-OF", method(:word_GROUPS_OF))
  add_module_word("INDEX", method(:word_INDEX))
  add_module_word("MAP", method(:word_MAP))
  add_module_word("FOREACH", method(:word_FOREACH))
  add_module_word("INVERT-KEYS", method(:word_INVERT_KEYS))
  add_module_word("ZIP", method(:word_ZIP))
  add_module_word("ZIP-WITH", method(:word_ZIP_WITH))
  add_module_word("KEYS", method(:word_KEYS))
  add_module_word("VALUES", method(:word_VALUES))
  add_module_word("LENGTH", method(:word_LENGTH))
  add_module_word("RANGE", method(:word_RANGE))
  add_module_word("SLICE", method(:word_SLICE))
  add_module_word("DIFFERENCE", method(:word_DIFFERENCE))
  add_module_word("INTERSECTION", method(:word_INTERSECTION))
  add_module_word("UNION", method(:word_UNION))
  add_module_word("SELECT", method(:word_SELECT))
  add_module_word("TAKE", method(:word_TAKE))
  add_module_word("DROP", method(:word_DROP))
  add_module_word("ROTATE", method(:word_ROTATE))
  add_module_word("ARRAY?", method(:word_ARRAY_q))
  add_module_word("SHUFFLE", method(:word_SHUFFLE))
  add_module_word("SORT", method(:word_SORT))
  add_module_word("FIELD-KEY-FUNC", method(:word_FIELD_KEY_FUNC))
  add_module_word("NTH", method(:word_NTH))
  add_module_word("LAST", method(:word_LAST))
  add_module_word("UNPACK", method(:word_UNPACK))
  add_module_word("FLATTEN", method(:word_FLATTEN))
  add_module_word("KEY-OF", method(:word_KEY_OF))
  add_module_word("REDUCE", method(:word_REDUCE))

  # --------------------------------------------------
  # Stack words
  add_module_word("POP", method(:word_POP))
  add_module_word("DUP", method(:word_DUP))
  add_module_word("SWAP", method(:word_SWAP))

  # --------------------------------------------------
  # String words
  add_module_word(">STR", method(:word_to_STR))
  add_module_word("CONCAT", method(:word_CONCAT))
  add_module_word("SPLIT", method(:word_SPLIT))
  add_module_word("JOIN", method(:word_JOIN))
  add_module_word("/N", method(:word_slash_N))
  add_module_word("/R", method(:word_slash_R))
  add_module_word("/T", method(:word_slash_T))
  add_module_word("LOWERCASE", method(:word_LOWERCASE))
  add_module_word("UPPERCASE", method(:word_UPPERCASE))
  add_module_word("ASCII", method(:word_ASCII))
  add_module_word("STRIP", method(:word_STRIP))
  add_module_word("REPLACE", method(:word_REPLACE))
  add_module_word("RE-MATCH", method(:word_RE_MATCH))
  add_module_word("RE-MATCH-GROUP", method(:word_RE_MATCH_GROUP))
  add_module_word("RE-MATCH-ALL", method(:word_RE_MATCH_ALL))

  # --------------------------------------------------
  # Misc words
  add_module_word("NULL", method(:word_NULL))
  add_module_word("DEFAULT", method(:word_DEFAULT))
  add_module_word("*DEFAULT", method(:word_star_DEFAULT))
  add_module_word("<REPEAT", method(:word_l_REPEAT))
  add_module_word(">FIXED", method(:word_to_FIXED))
  add_module_word(">JSON", method(:word_to_JSON))
  add_module_word("JSON>", method(:word_JSON_to))
  add_module_word(".s", method(:word_dot_s))
  add_module_word(".S", method(:word_dot_S))

  # --------------------------------------------------
  # Date/time words
  add_module_word("AM", method(:word_AM))
  add_module_word("PM", method(:word_PM))
  add_module_word("NOW", method(:word_NOW))
  add_module_word(">TIME", method(:word_to_TIME))
  add_module_word(">DATE", method(:word_to_DATE))
  add_module_word("TODAY", method(:word_TODAY))
  add_module_word("MONDAY", method(:word_MONDAY))
  add_module_word("TUESDAY", method(:word_TUESDAY))
  add_module_word("WEDNESDAY", method(:word_WEDNESDAY))
  add_module_word("THURSDAY", method(:word_THURSDAY))
  add_module_word("FRIDAY", method(:word_FRIDAY))
  add_module_word("SATURDAY", method(:word_SATURDAY))
  add_module_word("SUNDAY", method(:word_SUNDAY))
  add_module_word("ADD-DAYS", method(:word_ADD_DAYS))
  add_module_word("SUBTRACT-DATES", method(:word_SUBTRACT_DATES))
  add_module_word("DATE>STR", method(:word_DATE_to_STR))
  add_module_word("TIME>STR", method(:word_TIME_to_STR))
  add_module_word("DATE-TIME>DATETIME", method(:word_DATE_TIME_to_DATETIME))
  add_module_word("DATETIME>TIMESTAMP", method(:word_DATETIME_to_TIMESTAMP))
  add_module_word("TIMESTAMP>DATETIME", method(:word_TIMESTAMP_to_DATETIME))
  add_module_word("STR>DATETIME", method(:word_STR_to_DATETIME))
  add_module_word("STR>TIMESTAMP", method(:word_STR_to_TIMESTAMP))

  # --------------------------------------------------
  # Math words
  add_module_word("+", method(:word_plus))
  add_module_word("-", method(:word_minus))
  add_module_word("*", method(:word_times))
  add_module_word("/", method(:word_divide_by))
  add_module_word("ADD", method(:word_plus))
  add_module_word("SUBTRACT", method(:word_minus))
  add_module_word("MULTIPLY", method(:word_times))
  add_module_word("DIVIDE", method(:word_divide_by))
  add_module_word("MOD", method(:word_MOD))
  add_module_word("MEAN", method(:word_MEAN))
  add_module_word("MAX", method(:word_MAX))
  add_module_word("MIN", method(:word_MIN))
  add_module_word("ROUND", method(:word_ROUND))
  add_module_word("==", method(:word_equal_equal))
  add_module_word("!=", method(:word_not_equal))
  add_module_word(">", method(:word_greater_than))
  add_module_word(">=", method(:word_greater_than_or_equal))
  add_module_word("<", method(:word_less_than))
  add_module_word("<=", method(:word_less_than_or_equal))
  add_module_word("OR", method(:word_OR))
  add_module_word("AND", method(:word_AND))
  add_module_word("NOT", method(:word_NOT))
  add_module_word("IN", method(:word_IN))
  add_module_word("ANY", method(:word_ANY))
  add_module_word("ALL", method(:word_ALL))
  add_module_word(">BOOL", method(:word_to_BOOL))
  add_module_word(">INT", method(:word_to_INT))
  add_module_word(">FLOAT", method(:word_to_FLOAT))
  add_module_word("RANGE-INDEX", method(:word_RANGE_INDEX))
  add_module_word("INFINITY", method(:word_INFINITY))

  # --------------------------------------------------
  # Flag words
  add_module_word("!PUSH-ERROR", method(:word_bang_PUSH_ERROR))
  add_module_word("!WITH-KEY", method(:word_bang_WITH_KEY))
  add_module_word("!COMPARATOR", method(:word_bang_COMPARATOR))
  add_module_word("!PUSH-REST", method(:word_bang_PUSH_REST))
  add_module_word("!DEPTH", method(:word_bang_DEPTH))
  add_module_word("!INTERPS", method(:word_bang_INTERPS))

  # --------------------------------------------------
  # Profiling words
  add_module_word("PROFILE-START", method(:word_PROFILE_START))
  add_module_word("PROFILE-TIMESTAMP", method(:word_PROFILE_TIMESTAMP))
  add_module_word("PROFILE-END", method(:word_PROFILE_END))
  add_module_word("PROFILE-DATA", method(:word_PROFILE_DATA))

  # --------------------------------------------------
  # Ruby-specific words
  add_module_word(">SYM", method(:word_to_SYM))
end

Instance Attribute Details

#literal_handlersObject

Returns the value of attribute literal_handlers.



16
17
18
# File 'lib/forthic/global_module.rb', line 16

def literal_handlers
  @literal_handlers
end

#module_idObject

Returns the value of attribute module_id.



16
17
18
# File 'lib/forthic/global_module.rb', line 16

def module_id
  @module_id
end

Instance Method Details

#add_element_word(element_name) ⇒ Object



275
276
277
# File 'lib/forthic/global_module.rb', line 275

def add_element_word(element_name)
  add_module_word(element_name, make_element_word(element_name))
end

#find_literal_word(string) ⇒ Object



214
215
216
217
218
219
220
221
# File 'lib/forthic/global_module.rb', line 214

def find_literal_word(string)
  value = nil
  @literal_handlers.each do |handler|
    value = handler.call(string)
    return PushValueWord.new(string, value) unless value.nil?
  end
  nil
end

#find_word(name) ⇒ Object



208
209
210
211
212
# File 'lib/forthic/global_module.rb', line 208

def find_word(name)
  result = super
  result ||= find_literal_word(name)
  result
end

#make_element_word(element_name) ⇒ Object

Convenience function to create element word handlers



268
269
270
271
272
273
# File 'lib/forthic/global_module.rb', line 268

def make_element_word(element_name)
  proc do |interp|
    interp.stack_push(element_name)
    interp.run("Element")
  end
end

#to_bool(str_val) ⇒ Object

Literal handlers



224
225
226
227
228
# File 'lib/forthic/global_module.rb', line 224

def to_bool(str_val)
  return true if str_val == "TRUE"
  return false if str_val == "FALSE"
  nil
end

#to_float(str_val) ⇒ Object



236
237
238
239
240
241
242
243
# File 'lib/forthic/global_module.rb', line 236

def to_float(str_val)
  return nil unless str_val.include?(".")
  begin
    Float(str_val)
  rescue
    nil
  end
end

#to_int(str_val) ⇒ Object



230
231
232
233
234
# File 'lib/forthic/global_module.rb', line 230

def to_int(str_val)
  Integer(str_val)
rescue
  nil
end

#to_literal_date(str_val) ⇒ Object



245
246
247
248
249
250
251
252
253
# File 'lib/forthic/global_module.rb', line 245

def to_literal_date(str_val)
  match = str_val.match(/(\d{4})-(\d{2})-(\d{2})/)
  return nil unless match

  year = match[1].to_i
  month = match[2].to_i
  day = match[3].to_i
  Date.new(year, month, day)
end

#to_time(str_val) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
# File 'lib/forthic/global_module.rb', line 255

def to_time(str_val)
  match = str_val.match(/(\d{1,2}):(\d{2})/)
  return nil unless match

  hours = match[1].to_i
  minutes = match[2].to_i
  return nil if hours > 23 || minutes >= 60

  result = Time.now
  Time.new(result.year, result.month, result.day, hours, minutes, 0)
end

#word_ADD_DAYS(interp) ⇒ Object

( date num-days – date )



1613
1614
1615
1616
1617
1618
1619
# File 'lib/forthic/global_module.rb', line 1613

def word_ADD_DAYS(interp)
  num_days = interp.stack_pop
  date = interp.stack_pop

  result = date + num_days
  interp.stack_push(result)
end

#word_ALL(interp) ⇒ Object

( vals required_vals – bool )



1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
# File 'lib/forthic/global_module.rb', line 1879

def word_ALL(interp)
  required_vals = interp.stack_pop
  vals = interp.stack_pop

  vals ||= []
  required_vals ||= []

  result = required_vals.all? { |val| vals.include?(val) }
  interp.stack_push(result)
end

#word_AM(interp) ⇒ Object

( time – time )



1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
# File 'lib/forthic/global_module.rb', line 1513

def word_AM(interp)
  time = interp.stack_pop
  raise "AM expecting a time" unless time.is_a?(Time)

  result = time
  if time.hour >= 12
    result = Time.new(time.year, time.month, time.day, time.hour - 12, time.min, time.sec)
  end
  interp.stack_push(result)
end

#word_AND(interp) ⇒ Object

( l r – bool ) ( items – bool )



1833
1834
1835
1836
1837
1838
1839
# File 'lib/forthic/global_module.rb', line 1833

def word_AND(interp)
  r = interp.stack_pop

  items = r.is_a?(Array) ? r : [interp.stack_pop, r]
  result = items.all? { |item| item }
  interp.stack_push(result)
end

#word_ANY(interp) ⇒ Object

( vals required_vals – bool )



1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
# File 'lib/forthic/global_module.rb', line 1860

def word_ANY(interp)
  required_vals = interp.stack_pop
  vals = interp.stack_pop

  result = false
  required_vals.each do |rv|
    if vals.include?(rv)
      result = true
      break
    end
  end

  # If nothing is required, then all values are true
  result = true if required_vals.empty?

  interp.stack_push(result)
end

#word_APPEND(interp) ⇒ Object

( array item – array ) ( record key/val – record )



389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'lib/forthic/global_module.rb', line 389

def word_APPEND(interp)
  item = interp.stack_pop
  result = interp.stack_pop

  result ||= []

  if result.is_a?(Array)
    result.push(item)
  else
    result[item[0]] = item[1]
  end

  interp.stack_push(result)
end

#word_ARRAY_q(interp) ⇒ Object

( val – bool )



1037
1038
1039
1040
1041
# File 'lib/forthic/global_module.rb', line 1037

def word_ARRAY_q(interp)
  val = interp.stack_pop
  result = val.is_a?(Array)
  interp.stack_push(result)
end

#word_ASCII(interp) ⇒ Object

( string – string )



1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
# File 'lib/forthic/global_module.rb', line 1344

def word_ASCII(interp)
  string = interp.stack_pop

  if !string
    string = ""
  end

  result = ""
  string.each_char do |ch|
    result += ch if ch.ord < 256
  end
  interp.stack_push(result)
end

#word_at(interp) ⇒ Object

( variable – value )



305
306
307
308
# File 'lib/forthic/global_module.rb', line 305

def word_at(interp)
  variable = interp.stack_pop
  interp.stack_push(variable.value)
end

#word_bang(interp) ⇒ Object

( value variable – )



298
299
300
301
302
# File 'lib/forthic/global_module.rb', line 298

def word_bang(interp)
  variable = interp.stack_pop
  value = interp.stack_pop
  variable.value = value
end

#word_bang_at(interp) ⇒ Object

( value variable – value )



311
312
313
314
315
316
# File 'lib/forthic/global_module.rb', line 311

def word_bang_at(interp)
  variable = interp.stack_pop
  value = interp.stack_pop
  variable.value = value
  interp.stack_push(variable.value)
end

#word_bang_COMPARATOR(interp) ⇒ Object

( comparator – )



2026
2027
2028
2029
# File 'lib/forthic/global_module.rb', line 2026

def word_bang_COMPARATOR(interp)
  comparator = interp.stack_pop
  interp.modify_flags(module_id, {comparator: comparator})
end

#word_bang_DEPTH(interp) ⇒ Object

( depth – )

NOTE: ‘depth` of 0 is the same not having set depth



2039
2040
2041
2042
# File 'lib/forthic/global_module.rb', line 2039

def word_bang_DEPTH(interp)
  depth = interp.stack_pop
  interp.modify_flags(module_id, {depth: depth})
end

#word_bang_INTERPS(interp) ⇒ Object

( num_inteprs – )



2045
2046
2047
2048
# File 'lib/forthic/global_module.rb', line 2045

def word_bang_INTERPS(interp)
  num_interps = interp.stack_pop
  interp.modify_flags(module_id, {interps: num_interps})
end

#word_bang_PUSH_ERROR(interp) ⇒ Object

( – )



2016
2017
2018
# File 'lib/forthic/global_module.rb', line 2016

def word_bang_PUSH_ERROR(interp)
  interp.modify_flags(module_id, {push_error: true})
end

#word_bang_PUSH_REST(interp) ⇒ Object

( – )



2032
2033
2034
# File 'lib/forthic/global_module.rb', line 2032

def word_bang_PUSH_REST(interp)
  interp.modify_flags(module_id, {push_rest: true})
end

#word_bang_WITH_KEY(interp) ⇒ Object

( – )



2021
2022
2023
# File 'lib/forthic/global_module.rb', line 2021

def word_bang_WITH_KEY(interp)
  interp.modify_flags(module_id, {with_key: true})
end

#word_BY_FIELD(interp) ⇒ Object

( array field – field_to_item ) ( record field – field_to_item )



484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/forthic/global_module.rb', line 484

def word_BY_FIELD(interp)
  field = interp.stack_pop
  container = interp.stack_pop

  container ||= []

  values = if container.is_a?(Array)
    container
  else
    container.values
  end

  result = {}
  values.each do |v|
    result[v[field]] = v if v
  end

  interp.stack_push(result)
end

#word_CONCAT(interp) ⇒ Object

( str1 str2 – str ) ( array_of_str – str )



1281
1282
1283
1284
1285
1286
# File 'lib/forthic/global_module.rb', line 1281

def word_CONCAT(interp)
  str2 = interp.stack_pop
  array = str2.is_a?(Array) ? str2 : [interp.stack_pop, str2]
  result = array.join("")
  interp.stack_push(result)
end

#word_DATE_TIME_to_DATETIME(interp) ⇒ Object

( date time – datetime )



1978
1979
1980
1981
1982
1983
1984
# File 'lib/forthic/global_module.rb', line 1978

def word_DATE_TIME_to_DATETIME(interp)
  time = interp.stack_pop
  date = interp.stack_pop
  dt_string = "#{date.year}-#{date.month}-#{date.day} #{time.hour}:#{time.min}"
  result = Time.parse(dt_string)
  interp.stack_push(result)
end

#word_DATE_to_STR(interp) ⇒ Object

( date – str )



1964
1965
1966
1967
1968
# File 'lib/forthic/global_module.rb', line 1964

def word_DATE_to_STR(interp)
  date = interp.stack_pop
  result = date_to_string(date)
  interp.stack_push(result)
end

#word_DATETIME_to_TIMESTAMP(interp) ⇒ Object

( datetime – timestamp )



1987
1988
1989
1990
1991
# File 'lib/forthic/global_module.rb', line 1987

def word_DATETIME_to_TIMESTAMP(interp)
  datetime = interp.stack_pop
  result = datetime.to_i
  interp.stack_push(result)
end

#word_DEFAULT(interp) ⇒ Object

( value default – value )



1423
1424
1425
1426
1427
1428
# File 'lib/forthic/global_module.rb', line 1423

def word_DEFAULT(interp)
  default_value = interp.stack_pop
  value = interp.stack_pop
  result = (value.nil? || value == "") ? default_value : value
  interp.stack_push(result)
end

#word_DIFFERENCE(interp) ⇒ Object

( larray rarray – array ) ( lrecord rrecord – record )



863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
# File 'lib/forthic/global_module.rb', line 863

def word_DIFFERENCE(interp)
  rcontainer = interp.stack_pop
  lcontainer = interp.stack_pop

  lcontainer ||= []
  rcontainer ||= []

  result = if rcontainer.is_a?(Array)
    lcontainer - rcontainer
  else
    diff = lcontainer.keys - rcontainer.keys
    diff.each_with_object({}) { |k, res| res[k] = lcontainer[k] }
  end

  interp.stack_push(result)
end

#word_divide_by(interp) ⇒ Object

( a b – a/b )



1681
1682
1683
1684
1685
1686
# File 'lib/forthic/global_module.rb', line 1681

def word_divide_by(interp)
  b = interp.stack_pop
  a = interp.stack_pop
  result = a / b
  interp.stack_push(result)
end

#word_dot_S(interp) ⇒ Object

( – )



1502
1503
1504
1505
1506
1507
1508
1509
1510
# File 'lib/forthic/global_module.rb', line 1502

def word_dot_S(interp)
  # Print full stack in reverse with index, pretty printing stack value
  puts "===> Stack <==="
  # Get reversed stack
  reversed_stack = interp.stack.reverse
  reversed_stack.each_with_index do |item, i|
    puts "#{i}: #{item.inspect}"
  end
end

#word_dot_s(interp) ⇒ Object

( – )



1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
# File 'lib/forthic/global_module.rb', line 1490

def word_dot_s(interp)
  stack = interp.stack
  puts "===> Stack <==="
  if stack.length > 0
    p stack[stack.length - 1]
  else
    puts "<STACK EMPTY>"
  end
  interp.halt
end

#word_DROP(interp) ⇒ Object

( array n – array ) ( record n – record )



994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
# File 'lib/forthic/global_module.rb', line 994

def word_DROP(interp)
  n = interp.stack_pop
  container = interp.stack_pop

  if !container
    interp.stack_push(container)
    return
  end

  result = nil
  if container.is_a?(Array)
    result = container[n..]
  else
    keys = container.keys.sort
    rest_keys = keys[n..]
    result = rest_keys.each_with_object({}) { |k, res| res[k] = container[k] }
  end

  interp.stack_push(result)
end

#word_DUP(interp) ⇒ Object

( a – a a )



1259
1260
1261
1262
1263
# File 'lib/forthic/global_module.rb', line 1259

def word_DUP(interp)
  a = interp.stack_pop
  interp.stack_push(a)
  interp.stack_push(a)
end

#word_equal_equal(interp) ⇒ Object

( a b – a == b ) ( items – all_equal )



1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
# File 'lib/forthic/global_module.rb', line 1751

def word_equal_equal(interp)
  items = interp.stack_pop

  if items.is_a?(Array)
    all_equal = items.all? { |item| item == items[0] }
    interp.stack_push(all_equal)
  else
    b = items
    a = interp.stack_pop
    interp.stack_push(a == b)
  end
end

#word_EXPORT(interp) ⇒ Object

( names – )



326
327
328
329
# File 'lib/forthic/global_module.rb', line 326

def word_EXPORT(interp)
  names = interp.stack_pop
  interp.cur_module.add_exportable(names)
end

#word_FIELD_KEY_FUNC(interp) ⇒ Object

( field – key_func )



1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'lib/forthic/global_module.rb', line 1089

def word_FIELD_KEY_FUNC(interp)
  field = interp.stack_pop

  result = lambda do |record|
    record[field]
  end

  interp.stack_push(result)
end

#word_FLATTEN(interp) ⇒ Object

( array – array ) ( record – record )



1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
# File 'lib/forthic/global_module.rb', line 1180

def word_FLATTEN(interp)
  nested = interp.stack_pop
  flags = interp.get_flags(module_id)

  nested ||= []
  depth = flags[:depth]

  result = if nested.is_a?(Array)
    flatten_array(nested, depth)
  else
    flatten_record(nested, depth, {}, [])
  end

  interp.stack_push(result)
end

#word_FOREACH(interp) ⇒ Object

( items forthic – )



626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
# File 'lib/forthic/global_module.rb', line 626

def word_FOREACH(interp)
  forthic = interp.stack_pop
  string_location = interp.get_string_location

  items = interp.stack_pop
  flags = interp.get_flags(module_id)

  if !items
    interp.stack_push(items)
    return
  end

  errors = []
  if items.is_a?(Array)
    items.each_with_index do |item, i|
      if flags[:with_key]
        interp.stack_push(i)
      end
      interp.stack_push(item)
      if flags[:push_error]
        errors.push(execute_returning_error(interp, forthic, string_location))
      else
        interp.run(forthic, string_location)
      end
    end
  else
    items.each do |k, item|
      if flags[:with_key]
        interp.stack_push(k)
      end
      interp.stack_push(item)
      if flags[:push_error]
        errors.push(execute_returning_error(interp, forthic, string_location))
      else
        interp.run(forthic, string_location)
      end
    end
  end

  if flags[:push_error]
    interp.stack_push(errors)
  end
end

#word_FRIDAY(interp) ⇒ Object

( – date )



1595
1596
1597
1598
# File 'lib/forthic/global_module.rb', line 1595

def word_FRIDAY(interp)
  result = get_day_this_week(4)
  interp.stack_push(result)
end

#word_greater_than(interp) ⇒ Object

( l r – bool )



1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
# File 'lib/forthic/global_module.rb', line 1780

def word_greater_than(interp)
  r = interp.stack_pop
  l = interp.stack_pop

  if l.nil? || r.nil?
    interp.stack_push(nil)
    return
  end

  result = l > r
  interp.stack_push(result)
end

#word_greater_than_or_equal(interp) ⇒ Object

( l r – bool )



1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
# File 'lib/forthic/global_module.rb', line 1794

def word_greater_than_or_equal(interp)
  r = interp.stack_pop
  l = interp.stack_pop

  if l.nil? || r.nil?
    interp.stack_push(nil)
    return
  end

  result = l >= r
  interp.stack_push(result)
end

#word_GROUP_BY(interp) ⇒ Object

( array forthic – group_to_items ) ( record forthic – group_to_items )



537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# File 'lib/forthic/global_module.rb', line 537

def word_GROUP_BY(interp)
  forthic = interp.stack_pop
  string_location = interp.get_string_location

  container = interp.stack_pop

  flags = interp.get_flags(module_id)

  container ||= []

  keys, values = if container.is_a?(Array)
    [Array.new(container.size) { |i| i }, container]
  else
    [container.keys, container.values]
  end

  result = {}
  values.each_with_index do |value, i|
    key = keys[i]
    interp.stack_push(key) if flags[:with_key]
    interp.stack_push(value)
    interp.run(forthic, string_location)
    group = interp.stack_pop
    result[group] ||= []
    result[group].push(value)
  end

  interp.stack_push(result)
end

#word_GROUP_BY_FIELD(interp) ⇒ Object

( array field – field_to_items ) ( record field – field_to_items )



506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# File 'lib/forthic/global_module.rb', line 506

def word_GROUP_BY_FIELD(interp)
  field = interp.stack_pop
  container = interp.stack_pop

  container ||= []

  values = if container.is_a?(Array)
    container
  else
    container.values
  end

  result = {}
  values.each do |v|
    field_val = v[field]
    if field_val.is_a?(Array)
      field_val.each do |fv|
        result[fv] ||= []
        result[fv].push(v)
      end
    else
      result[field_val] ||= []
      result[field_val].push(v)
    end
  end

  interp.stack_push(result)
end

#word_GROUPS_OF(interp) ⇒ Object

( array n – arrays ) ( record n – records )



569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
# File 'lib/forthic/global_module.rb', line 569

def word_GROUPS_OF(interp)
  size = interp.stack_pop
  container = interp.stack_pop
  raise "GROUPS-OF requires group size > 0" if size <= 0

  container ||= []
  result = if container.is_a?(Array)
    group_items(container, size)
  else
    keys = container.keys
    key_groups = group_items(keys, size)
    key_groups.map { |ks| extract_rec(container, ks) }
  end

  interp.stack_push(result)
end

#word_IN(interp) ⇒ Object

( item items – bool )



1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
# File 'lib/forthic/global_module.rb', line 1848

def word_IN(interp)
  items = interp.stack_pop
  item = interp.stack_pop

  if !items
    items = []
  end
  result = items.include?(item)
  interp.stack_push(result)
end

#word_INDEX(interp) ⇒ Object

( array forthic – record )



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
# File 'lib/forthic/global_module.rb', line 587

def word_INDEX(interp)
  forthic = interp.stack_pop
  string_location = interp.get_string_location
  items = interp.stack_pop

  if !items
    interp.stack_push(items)
    return
  end

  result = {}
  items.each do |item|
    interp.stack_push(item)
    interp.run(forthic, string_location)
    keys = interp.stack_pop
    keys.each do |k|
      lowercased_key = k.downcase
      if result[lowercased_key]
        result[lowercased_key].push(item)
      else
        result[lowercased_key] = [item]
      end
    end
  end
  interp.stack_push(result)
end

#word_INFINITY(interp) ⇒ Object

( – Infinity )



1945
1946
1947
# File 'lib/forthic/global_module.rb', line 1945

def word_INFINITY(interp)
  interp.stack_push(Float::INFINITY)
end

#word_INTERPRET(interp) ⇒ Object

( string – )



319
320
321
322
323
# File 'lib/forthic/global_module.rb', line 319

def word_INTERPRET(interp)
  string = interp.stack_pop
  string_location = interp.get_string_location
  interp.run(string, string_location) if string
end

#word_INTERSECTION(interp) ⇒ Object

( larray rarray – array ) ( lrecord rrecord – record )



882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
# File 'lib/forthic/global_module.rb', line 882

def word_INTERSECTION(interp)
  rcontainer = interp.stack_pop
  lcontainer = interp.stack_pop

  lcontainer ||= []
  rcontainer ||= []

  result = if rcontainer.is_a?(Array)
    lcontainer & rcontainer
  else
    lkeys = lcontainer.keys
    rkeys = rcontainer.keys

    intersect = lkeys & rkeys
    intersect.each_with_object({}) { |k, res| res[k] = lcontainer[k] }
  end

  interp.stack_push(result)
end

#word_INVERT_KEYS(interp) ⇒ Object

( record – record )



671
672
673
674
675
676
677
678
679
680
681
# File 'lib/forthic/global_module.rb', line 671

def word_INVERT_KEYS(interp)
  record = interp.stack_pop
  result = {}
  record.each do |first_key, sub_record|
    sub_record.each do |second_key, value|
      result[second_key] ||= {}
      result[second_key][first_key] = value
    end
  end
  interp.stack_push(result)
end

#word_JOIN(interp) ⇒ Object

( strings sep – string )



1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
# File 'lib/forthic/global_module.rb', line 1302

def word_JOIN(interp)
  sep = interp.stack_pop
  strings = interp.stack_pop

  if !strings
    strings = []
  end

  result = strings.join(sep)
  interp.stack_push(result)
end

#word_JSON_to(interp) ⇒ Object

( json – object )



1483
1484
1485
1486
1487
# File 'lib/forthic/global_module.rb', line 1483

def word_JSON_to(interp)
  json = interp.stack_pop
  result = JSON.parse(json)
  interp.stack_push(result)
end

#word_KEY_OF(interp) ⇒ Object

( array item – index ) ( record item – key )



1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
# File 'lib/forthic/global_module.rb', line 1198

def word_KEY_OF(interp)
  item = interp.stack_pop
  container = interp.stack_pop

  container ||= []

  result = nil
  if container.is_a?(Array)
    index = container.index(item)
    # If index is nil or < 0, return nil
    result = (index.nil? || index < 0) ? nil : index
  else
    keys = container.keys
    keys.each do |k|
      v = container[k]
      if v == item
        result = k
        break
      end
    end
  end

  interp.stack_push(result)
end

#word_KEYS(interp) ⇒ Object

( array – array ) ( record – array )



734
735
736
737
738
739
740
741
742
743
744
745
746
# File 'lib/forthic/global_module.rb', line 734

def word_KEYS(interp)
  container = interp.stack_pop

  container ||= []

  result = if container.is_a?(Array)
    container.each_index.to_a
  else
    container.keys
  end

  interp.stack_push(result)
end

#word_L_DEL(interp) ⇒ Object

( array index – array ) ( record key – record )



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'lib/forthic/global_module.rb', line 436

def word_L_DEL(interp)
  key = interp.stack_pop
  container = interp.stack_pop

  if container.nil?
    interp.stack_push(container)
    return
  end

  if container.is_a?(Array)
    container.delete_at(key)
  else
    container.delete(key)
  end

  interp.stack_push(container)
end

#word_l_REC_bang(interp) ⇒ Object

( rec value field – rec )



370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/forthic/global_module.rb', line 370

def word_l_REC_bang(interp)
  field = interp.stack_pop
  value = interp.stack_pop
  rec = interp.stack_pop || {}

  fields = field.is_a?(Array) ? field : [field]

  cur_rec = rec
  fields[0...-1].each do |f|
    cur_rec[f] ||= {}
    cur_rec = cur_rec[f]
  end

  cur_rec[fields[-1]] = value
  interp.stack_push(rec)
end

#word_l_REPEAT(interp) ⇒ Object

( item forthic num-times – ? )



1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
# File 'lib/forthic/global_module.rb', line 1443

def word_l_REPEAT(interp)
  num_times = interp.stack_pop
  forthic = interp.stack_pop
  string_location = interp.get_string_location

  num_times.times do
    item = interp.stack_pop
    interp.stack_push(item)

    interp.run(forthic, string_location)
    res = interp.stack_pop

    interp.stack_push(item)
    interp.stack_push(res)
  end
end

#word_LAST(interp) ⇒ Object

( array – item ) ( record – value )



1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
# File 'lib/forthic/global_module.rb', line 1132

def word_LAST(interp)
  container = interp.stack_pop

  if !container
    interp.stack_push(nil)
    return
  end

  result = nil
  if container.is_a?(Array)
    result = if container.length == 0
      nil
    else
      container[container.length - 1]
    end
  else
    keys = container.keys.sort
    result = if keys.length == 0
      nil
    else
      container[keys[keys.length - 1]]
    end
  end

  interp.stack_push(result)
end

#word_LENGTH(interp) ⇒ Object

( array – length ) ( record – length )



766
767
768
769
770
771
772
773
774
775
776
# File 'lib/forthic/global_module.rb', line 766

def word_LENGTH(interp)
  container = interp.stack_pop

  if container.nil?
    interp.stack_push(container)
    return
  end

  result = container.length
  interp.stack_push(result)
end

#word_less_than(interp) ⇒ Object

( l r – bool )



1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
# File 'lib/forthic/global_module.rb', line 1808

def word_less_than(interp)
  r = interp.stack_pop
  l = interp.stack_pop

  if l.nil? || r.nil?
    interp.stack_push(nil)
    return
  end

  result = l < r
  interp.stack_push(result)
end

#word_less_than_or_equal(interp) ⇒ Object

( l r – bool )



1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
# File 'lib/forthic/global_module.rb', line 1950

def word_less_than_or_equal(interp)
  r = interp.stack_pop
  l = interp.stack_pop

  if l.nil? || r.nil?
    interp.stack_push(nil)
    return
  end

  result = l <= r
  interp.stack_push(result)
end

#word_LOWERCASE(interp) ⇒ Object

( A – a )



1330
1331
1332
1333
1334
# File 'lib/forthic/global_module.rb', line 1330

def word_LOWERCASE(interp)
  string = interp.stack_pop
  result = string ? string.downcase : ""
  interp.stack_push(result)
end

#word_MAP(interp) ⇒ Object

( items forthic – [ ? ] )



615
616
617
618
619
620
621
622
623
# File 'lib/forthic/global_module.rb', line 615

def word_MAP(interp)
  forthic = interp.stack_pop
  string_location = interp.get_string_location
  items = interp.stack_pop
  flags = interp.get_flags(module_id)

  map_word = MapWord.new(items, forthic, string_location, flags)
  map_word.execute(interp)
end

#word_MAX(interp) ⇒ Object

( num1 num2 – num ) ( numbers – num )



1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
# File 'lib/forthic/global_module.rb', line 1712

def word_MAX(interp)
  num2 = interp.stack_pop

  numbers = []
  if num2.is_a?(Array)
    numbers = num2
  else
    num1 = interp.stack_pop
    numbers = [num1, num2]
  end

  interp.stack_push(numbers.max)
end

#word_MEAN(interp) ⇒ Object

( numbers – mean ) ( records – mean_record )



1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
# File 'lib/forthic/global_module.rb', line 1698

def word_MEAN(interp)
  values = interp.stack_pop

  if !values || values.empty?
    interp.stack_push(0)
    return
  end

  result = compute_mean(values)
  interp.stack_push(result)
end

#word_MIN(interp) ⇒ Object

( num1 num2 – num ) ( numbers – num )



1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
# File 'lib/forthic/global_module.rb', line 1728

def word_MIN(interp)
  num2 = interp.stack_pop

  numbers = []
  if num2.is_a?(Array)
    numbers = num2
  else
    num1 = interp.stack_pop
    numbers = [num1, num2]
  end

  interp.stack_push(numbers.min)
end

#word_minus(interp) ⇒ Object

( a b – a - b )



1648
1649
1650
1651
1652
# File 'lib/forthic/global_module.rb', line 1648

def word_minus(interp)
  b = interp.stack_pop
  a = interp.stack_pop
  interp.stack_push(a - b)
end

#word_MOD(interp) ⇒ Object

( value mod – remainder )



1689
1690
1691
1692
1693
1694
# File 'lib/forthic/global_module.rb', line 1689

def word_MOD(interp)
  mod = interp.stack_pop
  value = interp.stack_pop
  result = value % mod
  interp.stack_push(result)
end

#word_MONDAY(interp) ⇒ Object

( – date )



1571
1572
1573
1574
# File 'lib/forthic/global_module.rb', line 1571

def word_MONDAY(interp)
  result = get_day_this_week(0)
  interp.stack_push(result)
end

#word_NOT(interp) ⇒ Object

( bool – bool )



1842
1843
1844
1845
# File 'lib/forthic/global_module.rb', line 1842

def word_NOT(interp)
  value = interp.stack_pop
  interp.stack_push(!value)
end

#word_not_equal(interp) ⇒ Object

( a b – a != b ) ( items – all_not_equal )



1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
# File 'lib/forthic/global_module.rb', line 1766

def word_not_equal(interp)
  items = interp.stack_pop

  if items.is_a?(Array)
    all_not_equal = items.all? { |item| item != items[0] }
    interp.stack_push(all_not_equal)
  else
    b = items
    a = interp.stack_pop
    interp.stack_push(a != b)
  end
end

#word_NOW(interp) ⇒ Object

( – time )



1537
1538
1539
1540
# File 'lib/forthic/global_module.rb', line 1537

def word_NOW(interp)
  result = Time.now
  interp.stack_push(result)
end

#word_NTH(interp) ⇒ Object

( array n – item ) ( record n – value )



1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/forthic/global_module.rb', line 1101

def word_NTH(interp)
  n = interp.stack_pop
  container = interp.stack_pop

  if n.nil? || !container
    interp.stack_push(nil)
    return
  end

  result = nil
  if container.is_a?(Array)
    if n < 0 || n >= container.length
      interp.stack_push(nil)
      return
    end
    result = container[n]
  else
    if n < 0 || n >= container.keys.length
      interp.stack_push(nil)
      return
    end
    keys = container.keys.sort
    key = keys[n]
    result = container[key]
  end

  interp.stack_push(result)
end

#word_NULL(interp) ⇒ Object

( – nil )



1418
1419
1420
# File 'lib/forthic/global_module.rb', line 1418

def word_NULL(interp)
  interp.stack_push(nil)
end

#word_OR(interp) ⇒ Object

( l r – bool ) ( items – bool )



1823
1824
1825
1826
1827
1828
1829
# File 'lib/forthic/global_module.rb', line 1823

def word_OR(interp)
  r = interp.stack_pop

  items = r.is_a?(Array) ? r : [interp.stack_pop, r]
  result = items.any? { |item| item }
  interp.stack_push(result)
end

#word_plus(interp) ⇒ Object

( a b – a+b ) ( items – sum )



1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
# File 'lib/forthic/global_module.rb', line 1631

def word_plus(interp)
  items = interp.stack_pop

  if items.is_a?(Array)
    sum = 0
    items.each do |item|
      sum += item
    end
    interp.stack_push(sum)
  else
    b = items
    a = interp.stack_pop
    interp.stack_push(a + b)
  end
end

#word_PM(interp) ⇒ Object

( time – time )



1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
# File 'lib/forthic/global_module.rb', line 1525

def word_PM(interp)
  time = interp.stack_pop
  raise "PM expecting a time" unless time.is_a?(Time)

  result = time
  if time.hour < 12
    result = Time.new(time.year, time.month, time.day, time.hour + 12, time.min, time.sec)
  end
  interp.stack_push(result)
end

#word_POP(interp) ⇒ Object

( a – )



1254
1255
1256
# File 'lib/forthic/global_module.rb', line 1254

def word_POP(interp)
  interp.stack_pop
end

#word_PROFILE_DATA(interp) ⇒ Object

( – )



2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
# File 'lib/forthic/global_module.rb', line 2067

def word_PROFILE_DATA(interp)
  histogram = interp.word_histogram
  timestamps = interp.profile_timestamps

  result = {
    word_counts: [],
    timestamps: []
  }

  histogram.each do |val|
    rec = {word: val[:word], count: val[:count]}
    result[:word_counts].push(rec)
  end

  prev_time = 0.0
  timestamps.each do |t|
    rec = {
      label: t[:label],
      time_ms: t[:time_ms],
      delta: t[:time_ms] - prev_time
    }
    prev_time = t[:time_ms]
    result[:timestamps].push(rec)
  end

  interp.stack_push(result)
end

#word_PROFILE_END(interp) ⇒ Object

( – )



2056
2057
2058
# File 'lib/forthic/global_module.rb', line 2056

def word_PROFILE_END(interp)
  interp.stop_profiling
end

#word_PROFILE_START(interp) ⇒ Object

( – )



2051
2052
2053
# File 'lib/forthic/global_module.rb', line 2051

def word_PROFILE_START(interp)
  interp.start_profiling
end

#word_PROFILE_TIMESTAMP(interp) ⇒ Object

( label – )



2061
2062
2063
2064
# File 'lib/forthic/global_module.rb', line 2061

def word_PROFILE_TIMESTAMP(interp)
  label = interp.stack_pop
  interp.add_timestamp(label)
end

#word_RANGE(interp) ⇒ Object

( array start end – array ) ( record start end – record )



780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'lib/forthic/global_module.rb', line 780

def word_RANGE(interp)
  fend = interp.stack_pop
  fend_string_location = interp.get_string_location

  fstart = interp.stack_pop
  fstart_string_location = interp.get_string_location

  array = interp.stack_pop

  array ||= []

  start_found = false
  end_found = false

  start_index = nil
  end_index = nil

  array.each_with_index do |item, index|
    unless start_found
      interp.stack_push(item)
      interp.run(fstart, fstart_string_location)
      start_found = interp.stack_pop
      start_index = index if start_found
    end

    if start_found && !end_found
      interp.stack_push(item)
      interp.run(fend, fend_string_location)
      end_found = interp.stack_pop
      end_index = index if end_found
      break if end_found
    end
  end

  interp.stack_push([start_index, end_index])
end

#word_RANGE_INDEX(interp) ⇒ Object

( val start_ranges – index )



1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
# File 'lib/forthic/global_module.rb', line 1916

def word_RANGE_INDEX(interp)
  start_ranges = interp.stack_pop
  val = interp.stack_pop

  # Cap off the value ranges with infinity
  start_ranges.push(Float::INFINITY)

  if val.nil? || start_ranges.nil?
    interp.stack_push(nil)
    return
  end

  if val < start_ranges[0]
    interp.stack_push(nil)
    return
  end

  result = nil
  (0...start_ranges.length - 1).each do |i|
    if val >= start_ranges[i] && val < start_ranges[i + 1]
      result = i
      break
    end
  end

  interp.stack_push(result)
end

#word_RE_MATCH(interp) ⇒ Object

( string pattern – match )



1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
# File 'lib/forthic/global_module.rb', line 1380

def word_RE_MATCH(interp)
  pattern = interp.stack_pop
  string = interp.stack_pop

  re_pattern = Regexp.new(pattern)
  result = false
  if string
    result = string.match(re_pattern)
  end
  interp.stack_push(result)
end

#word_RE_MATCH_ALL(interp) ⇒ Object

( string pattern – matches )



1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
# File 'lib/forthic/global_module.rb', line 1404

def word_RE_MATCH_ALL(interp)
  pattern = interp.stack_pop
  string = interp.stack_pop

  re_pattern = Regexp.new(pattern)
  matches = []
  if string
    matches = string.scan(re_pattern)
  end
  result = matches.map { |m| m[0] }
  interp.stack_push(result)
end

#word_RE_MATCH_GROUP(interp) ⇒ Object

( match num – string )



1393
1394
1395
1396
1397
1398
1399
1400
1401
# File 'lib/forthic/global_module.rb', line 1393

def word_RE_MATCH_GROUP(interp)
  num = interp.stack_pop
  match = interp.stack_pop
  result = nil
  if match
    result = match[num]
  end
  interp.stack_push(result)
end

#word_REC(interp) ⇒ Object

( key_vals – rec )



343
344
345
346
347
348
349
350
351
# File 'lib/forthic/global_module.rb', line 343

def word_REC(interp)
  key_vals = interp.stack_pop || []
  result = {}
  key_vals.each do |pair|
    key, val = pair
    result[key] = val
  end
  interp.stack_push(result)
end

#word_REC_at(interp) ⇒ Object

( rec field – value ) ( rec fields – value )



355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'lib/forthic/global_module.rb', line 355

def word_REC_at(interp)
  field = interp.stack_pop
  rec = interp.stack_pop

  if rec.nil?
    interp.stack_push(nil)
    return
  end

  fields = field.is_a?(Array) ? field : [field]
  result = drill_for_value(rec, fields)
  interp.stack_push(result)
end

#word_REDUCE(interp) ⇒ Object

( array init forthic – value ) ( record init forthic – value )



1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
# File 'lib/forthic/global_module.rb', line 1225

def word_REDUCE(interp)
  forthic = interp.stack_pop
  string_location = interp.get_string_location

  initial = interp.stack_pop
  container = interp.stack_pop

  container ||= []

  interp.stack_push(initial)

  if container.is_a?(Array)
    container.each do |item|
      interp.stack_push(item)
      interp.run(forthic, string_location)
    end
  else
    container.each do |k, v|
      interp.stack_push(v)
      interp.run(forthic, string_location)
    end
  end

  result = interp.stack_pop

  interp.stack_push(result)
end

#word_RELABEL(interp) ⇒ Object

( array old_keys new_keys – array ) ( record old_keys new_keys – record )



456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
# File 'lib/forthic/global_module.rb', line 456

def word_RELABEL(interp)
  new_keys = interp.stack_pop
  old_keys = interp.stack_pop
  container = interp.stack_pop

  if container.nil?
    interp.stack_push(container)
    return
  end

  raise "RELABEL: old_keys and new_keys must be same length" if old_keys.length != new_keys.length

  new_to_old = {}
  old_keys.each_with_index do |old_key, i|
    new_to_old[new_keys[i]] = old_key
  end

  result = if container.is_a?(Array)
    new_to_old.keys.sort.map { |k| container[new_to_old[k]] }
  else
    new_to_old.each_with_object({}) { |(new_key, old_key), res| res[new_key] = container[old_key] }
  end

  interp.stack_push(result)
end

#word_REPLACE(interp) ⇒ Object

( string text replace – string )



1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
# File 'lib/forthic/global_module.rb', line 1366

def word_REPLACE(interp)
  replace = interp.stack_pop
  text = interp.stack_pop
  string = interp.stack_pop

  result = string
  if string
    pattern = Regexp.new(text)
    result = string.gsub(pattern, replace)
  end
  interp.stack_push(result)
end

#word_REVERSE(interp) ⇒ Object

( array – array ) ( record – record )



406
407
408
409
410
411
412
413
414
415
416
417
# File 'lib/forthic/global_module.rb', line 406

def word_REVERSE(interp)
  result = interp.stack_pop

  if result.nil?
    interp.stack_push(result)
    return
  end

  result = result.reverse if result.is_a?(Array)

  interp.stack_push(result)
end

#word_ROTATE(interp) ⇒ Object

( array – array ) ( record – record )



1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# File 'lib/forthic/global_module.rb', line 1017

def word_ROTATE(interp)
  container = interp.stack_pop

  result = container
  if !container
    result = container
  elsif container.is_a?(Array)
    result = container
    if container.length > 0
      val = result.pop
      result.unshift(val)
    end
  else
    result = container
  end

  interp.stack_push(result)
end

#word_ROUND(interp) ⇒ Object

( a – a )



1743
1744
1745
1746
1747
# File 'lib/forthic/global_module.rb', line 1743

def word_ROUND(interp)
  a = interp.stack_pop
  result = a.round
  interp.stack_push(result)
end

#word_SATURDAY(interp) ⇒ Object

( – date )



1601
1602
1603
1604
# File 'lib/forthic/global_module.rb', line 1601

def word_SATURDAY(interp)
  result = get_day_this_week(5)
  interp.stack_push(result)
end

#word_SELECT(interp) ⇒ Object

( larray forthic – array ) ( lrecord forthic – record )



930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
# File 'lib/forthic/global_module.rb', line 930

def word_SELECT(interp)
  forthic = interp.stack_pop
  string_location = interp.get_string_location

  container = interp.stack_pop
  flags = interp.get_flags(module_id)

  if !container
    interp.stack_push(container)
    return
  end

  result = nil
  if container.is_a?(Array)
    result = []
    container.each_with_index do |item, i|
      interp.stack_push(i) if flags[:with_key]
      interp.stack_push(item)
      interp.run(forthic, string_location)
      should_select = interp.stack_pop
      result.push(item) if should_select
    end
  else
    result = {}
    container.each do |k, v|
      interp.stack_push(k) if flags[:with_key]
      interp.stack_push(v)
      interp.run(forthic, string_location)
      should_select = interp.stack_pop
      result[k] = v if should_select
    end
  end
  interp.stack_push(result)
end

#word_SHUFFLE(interp) ⇒ Object

( array – array ) ( record – record )



1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
# File 'lib/forthic/global_module.rb', line 1045

def word_SHUFFLE(interp)
  container = interp.stack_pop

  if !container
    interp.stack_push(container)
    return
  end
  result = if container.is_a?(Array)
    container.shuffle
  else
    container
  end

  interp.stack_push(result)
end

#word_slash_N(interp) ⇒ Object

( – char )



1315
1316
1317
# File 'lib/forthic/global_module.rb', line 1315

def word_slash_N(interp)
  interp.stack_push("\n")
end

#word_slash_R(interp) ⇒ Object

( – char )



1320
1321
1322
# File 'lib/forthic/global_module.rb', line 1320

def word_slash_R(interp)
  interp.stack_push("\r")
end

#word_slash_T(interp) ⇒ Object

( – char )



1325
1326
1327
# File 'lib/forthic/global_module.rb', line 1325

def word_slash_T(interp)
  interp.stack_push("\t")
end

#word_SLICE(interp) ⇒ Object

( array start end – array ) ( record start end – record )



819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
# File 'lib/forthic/global_module.rb', line 819

def word_SLICE(interp)
  fend = interp.stack_pop.to_i
  fstart = interp.stack_pop.to_i
  container = interp.stack_pop

  container ||= []

  length = container.length

  start = normalize_index(fstart, length)
  fend = normalize_index(fend, length)

  step = 1
  step = -1 if start > fend

  indexes = [start]
  indexes = [] if start < 0 || start >= length

  while start != fend
    start += step
    if start < 0 || start >= length
      indexes.push(nil)
    else
      indexes.push(start)
    end
  end

  result = if container.is_a?(Array)
    indexes.map { |i| i.nil? ? nil : container[i] }
  else
    keys = container.keys.sort
    indexes.each_with_object({}) do |i, res|
      next if i.nil?

      k = keys[i]
      res[k] = container[k]
    end
  end

  interp.stack_push(result)
end

#word_SORT(interp) ⇒ Object

( array – array ) ( record – record )



1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
# File 'lib/forthic/global_module.rb', line 1063

def word_SORT(interp)
  flag_string_position = interp.get_string_location
  container = interp.stack_pop

  flags = interp.get_flags(module_id)
  comparator = flags[:comparator]

  container ||= []
  unless container.is_a?(Array)
    interp.stack_push(container)
    return
  end

  # Figure out what to do
  result = if comparator.is_a?(String)
    sort_with_forthic(interp, comparator, flag_string_position, container)
  elsif comparator.nil?
    sort_without_comparator(container)
  else
    sort_with_key_func(container, comparator)
  end

  interp.stack_push(result)
end

#word_SPLIT(interp) ⇒ Object

( string sep – items )



1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
# File 'lib/forthic/global_module.rb', line 1289

def word_SPLIT(interp)
  sep = interp.stack_pop
  string = interp.stack_pop

  if !string
    string = ""
  end

  result = string.split(sep)
  interp.stack_push(result)
end

#word_star_DEFAULT(interp) ⇒ Object

( value default_forthic – value )



1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
# File 'lib/forthic/global_module.rb', line 1431

def word_star_DEFAULT(interp)
  default_forthic = interp.stack_pop
  value = interp.stack_pop

  if value.nil? || value == ""
    interp.run(default_forthic)
    value = interp.stack_pop
  end
  interp.stack_push(value)
end

#word_STR_to_DATETIME(interp) ⇒ Object

( str – datetime )



2001
2002
2003
2004
2005
# File 'lib/forthic/global_module.rb', line 2001

def word_STR_to_DATETIME(interp)
  s = interp.stack_pop
  result = Time.parse(s)
  interp.stack_push(result)
end

#word_STR_to_TIMESTAMP(interp) ⇒ Object

( str – timestamp )



2008
2009
2010
2011
2012
2013
# File 'lib/forthic/global_module.rb', line 2008

def word_STR_to_TIMESTAMP(interp)
  s = interp.stack_pop
  datetime = Time.parse(s)
  result = datetime.to_i
  interp.stack_push(result)
end

#word_STRIP(interp) ⇒ Object

( string – string )



1359
1360
1361
1362
1363
# File 'lib/forthic/global_module.rb', line 1359

def word_STRIP(interp)
  string = interp.stack_pop
  result = string ? string.strip : ""
  interp.stack_push(result)
end

#word_SUBTRACT_DATES(interp) ⇒ Object

( l_date r_date – num_days )



1622
1623
1624
1625
1626
1627
# File 'lib/forthic/global_module.rb', line 1622

def word_SUBTRACT_DATES(interp)
  r_date = interp.stack_pop
  l_date = interp.stack_pop
  result = (l_date - r_date).to_i
  interp.stack_push(result)
end

#word_SUNDAY(interp) ⇒ Object

( – date )



1607
1608
1609
1610
# File 'lib/forthic/global_module.rb', line 1607

def word_SUNDAY(interp)
  result = get_day_this_week(6)
  interp.stack_push(result)
end

#word_SWAP(interp) ⇒ Object

( a b – b a )



1266
1267
1268
1269
1270
1271
# File 'lib/forthic/global_module.rb', line 1266

def word_SWAP(interp)
  b = interp.stack_pop
  a = interp.stack_pop
  interp.stack_push(b)
  interp.stack_push(a)
end

#word_TAKE(interp) ⇒ Object

( array n – taken rest ) ( record n – taken rest )



967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
# File 'lib/forthic/global_module.rb', line 967

def word_TAKE(interp)
  n = interp.stack_pop
  container = interp.stack_pop
  flags = interp.get_flags(module_id)

  container ||= []

  taken = nil
  rest = nil

  if container.is_a?(Array)
    taken = container[0...n]
    rest = container[n..]
  else
    keys = container.keys.sort
    taken_keys = keys[0...n]
    rest_keys = keys[n..]
    taken = taken_keys.each_with_object({}) { |k, res| res[k] = container[k] }
    rest = rest_keys.each_with_object({}) { |k, res| res[k] = container[k] }
  end

  interp.stack_push(taken)
  interp.stack_push(rest) if flags[:push_rest]
end

#word_THURSDAY(interp) ⇒ Object

( – date )



1589
1590
1591
1592
# File 'lib/forthic/global_module.rb', line 1589

def word_THURSDAY(interp)
  result = get_day_this_week(3)
  interp.stack_push(result)
end

#word_TIME_to_STR(interp) ⇒ Object

( time – str )



1971
1972
1973
1974
1975
# File 'lib/forthic/global_module.rb', line 1971

def word_TIME_to_STR(interp)
  time = interp.stack_pop
  result = time.strftime("%H:%M")
  interp.stack_push(result)
end

#word_times(interp) ⇒ Object

( a b – a*b )



1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
# File 'lib/forthic/global_module.rb', line 1655

def word_times(interp)
  b = interp.stack_pop
  result = 1
  numbers = []

  if b.is_a?(Array)
    numbers = b
  else
    a = interp.stack_pop
    numbers = [a, b]
  end

  nil_found = numbers.any?(&:nil?)
  if nil_found
    interp.stack_push(nil)
    return
  end

  numbers.each do |num|
    result *= num
  end

  interp.stack_push(result)
end

#word_TIMESTAMP_to_DATETIME(interp) ⇒ Object

( timestamp – datetime )



1994
1995
1996
1997
1998
# File 'lib/forthic/global_module.rb', line 1994

def word_TIMESTAMP_to_DATETIME(interp)
  timestamp = interp.stack_pop
  result = Time.at(timestamp)
  interp.stack_push(result)
end

#word_to_BOOL(interp) ⇒ Object

( item – bool )



1891
1892
1893
1894
1895
1896
1897
1898
1899
# File 'lib/forthic/global_module.rb', line 1891

def word_to_BOOL(interp)
  item = interp.stack_pop
  result = if item.nil? || item == "" || item == 0
    false
  else
    !!item
  end
  interp.stack_push(result)
end

#word_to_DATE(interp) ⇒ Object

( obj – date )



1554
1555
1556
1557
1558
1559
1560
1561
1562
# File 'lib/forthic/global_module.rb', line 1554

def word_to_DATE(interp)
  obj = interp.stack_pop
  result = if obj.is_a?(Date)
    obj
  else
    Date.parse(obj.to_s)
  end
  interp.stack_push(result)
end

#word_to_FIXED(interp) ⇒ Object

( value num_places – str )



1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
# File 'lib/forthic/global_module.rb', line 1461

def word_to_FIXED(interp)
  num_places = interp.stack_pop
  value = interp.stack_pop
  result = if value.nil?
    ""
  elsif !value.is_a?(Numeric)
    value
  else
    # Round value to num_places
    value.round(num_places)
  end
  interp.stack_push(result.to_s)
end

#word_to_FLOAT(interp) ⇒ Object

( item – float )



1909
1910
1911
1912
1913
# File 'lib/forthic/global_module.rb', line 1909

def word_to_FLOAT(interp)
  str = interp.stack_pop
  result = str.to_f
  interp.stack_push(result)
end

#word_to_INT(interp) ⇒ Object

( item – int )



1902
1903
1904
1905
1906
# File 'lib/forthic/global_module.rb', line 1902

def word_to_INT(interp)
  str = interp.stack_pop
  result = str.to_i
  interp.stack_push(result)
end

#word_to_JSON(interp) ⇒ Object

( object – json )



1476
1477
1478
1479
1480
# File 'lib/forthic/global_module.rb', line 1476

def word_to_JSON(interp)
  object = interp.stack_pop
  result = object.to_json
  interp.stack_push(result)
end

#word_to_STR(interp) ⇒ Object

( item – str )



1274
1275
1276
1277
# File 'lib/forthic/global_module.rb', line 1274

def word_to_STR(interp)
  item = interp.stack_pop
  interp.stack_push(item.to_s)
end

#word_to_SYM(interp) ⇒ Object

( string – symbol )



2096
2097
2098
2099
2100
# File 'lib/forthic/global_module.rb', line 2096

def word_to_SYM(interp)
  string = interp.stack_pop
  result = string.to_sym
  interp.stack_push(result)
end

#word_to_TIME(interp) ⇒ Object

( obj – time )



1543
1544
1545
1546
1547
1548
1549
1550
1551
# File 'lib/forthic/global_module.rb', line 1543

def word_to_TIME(interp)
  obj = interp.stack_pop
  result = if obj.is_a?(Time)
    obj
  else
    Time.parse(obj.to_s)
  end
  interp.stack_push(result)
end

#word_TODAY(interp) ⇒ Object

( – date )



1565
1566
1567
1568
# File 'lib/forthic/global_module.rb', line 1565

def word_TODAY(interp)
  result = Date.today
  interp.stack_push(result)
end

#word_TUESDAY(interp) ⇒ Object

( – date )



1577
1578
1579
1580
# File 'lib/forthic/global_module.rb', line 1577

def word_TUESDAY(interp)
  result = get_day_this_week(1)
  interp.stack_push(result)
end

#word_UNION(interp) ⇒ Object

( larray rarray – array ) ( lrecord rrecord – record )



904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
# File 'lib/forthic/global_module.rb', line 904

def word_UNION(interp)
  rcontainer = interp.stack_pop
  lcontainer = interp.stack_pop

  lcontainer ||= []
  rcontainer ||= []

  result = if rcontainer.is_a?(Array)
    lcontainer | rcontainer
  else
    lkeys = lcontainer.keys
    rkeys = rcontainer.keys

    keys = lkeys | rkeys
    keys.each_with_object({}) do |k, res|
      val = lcontainer[k]
      val = rcontainer[k] if val.nil?
      res[k] = val
    end
  end

  interp.stack_push(result)
end

#word_UNIQUE(interp) ⇒ Object

( array – array )



420
421
422
423
424
425
426
427
428
429
430
431
432
# File 'lib/forthic/global_module.rb', line 420

def word_UNIQUE(interp)
  container = interp.stack_pop

  if container.nil?
    interp.stack_push(container)
    return
  end

  result = container
  result = container.uniq if container.is_a?(Array)

  interp.stack_push(result)
end

#word_UNPACK(interp) ⇒ Object

( array – a1 a2 .. an ) ( record – v1 v2 .. vn )



1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
# File 'lib/forthic/global_module.rb', line 1161

def word_UNPACK(interp)
  container = interp.stack_pop

  container ||= []

  if container.is_a?(Array)
    container.each do |item|
      interp.stack_push(item)
    end
  else
    keys = container.keys.sort
    keys.each do |k|
      interp.stack_push(container[k])
    end
  end
end

#word_UPPERCASE(interp) ⇒ Object

( a – A )



1337
1338
1339
1340
1341
# File 'lib/forthic/global_module.rb', line 1337

def word_UPPERCASE(interp)
  string = interp.stack_pop
  result = string ? string.upcase : ""
  interp.stack_push(result)
end

#word_USE_MODULES(interp) ⇒ Object

( names – )



332
333
334
335
336
337
338
339
340
# File 'lib/forthic/global_module.rb', line 332

def word_USE_MODULES(interp)
  names = interp.stack_pop

  names.each do |name|
    module_name, prefix = name.is_a?(Array) ? name : [name, name]
    mod = interp.find_module(module_name)
    interp.get_app_module.import_module(prefix, mod, interp)
  end
end

#word_VALUES(interp) ⇒ Object

( array – array ) ( record – array )



750
751
752
753
754
755
756
757
758
759
760
761
762
# File 'lib/forthic/global_module.rb', line 750

def word_VALUES(interp)
  container = interp.stack_pop

  container ||= []

  result = if container.is_a?(Array)
    container
  else
    container.values
  end

  interp.stack_push(result)
end

#word_VARIABLES(interp) ⇒ Object

( varnames – )



282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/forthic/global_module.rb', line 282

def word_VARIABLES(interp)
  varnames = interp.stack_pop
  module_ = interp.cur_module
  varnames.each do |v|
    if /__.*/.match?(v)
      raise ForthicError.new(
        "global_module-696",
        "word_VARIABLES: variable names cannot begin with '__': '#{v}'",
        "This is a reserved variable naming convention"
      )
    end
    module_.add_variable(v)
  end
end

#word_WEDNESDAY(interp) ⇒ Object

( – date )



1583
1584
1585
1586
# File 'lib/forthic/global_module.rb', line 1583

def word_WEDNESDAY(interp)
  result = get_day_this_week(2)
  interp.stack_push(result)
end

#word_ZIP(interp) ⇒ Object

( array1 array2 – array ) ( record1 record2 – record )



685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
# File 'lib/forthic/global_module.rb', line 685

def word_ZIP(interp)
  container2 = interp.stack_pop
  container1 = interp.stack_pop

  container1 ||= []
  container2 ||= []

  result = if container2.is_a?(Array)
    container1.map.with_index { |v, i| [v, container2[i]] }
  else
    container1.each_with_object({}) { |(k, v), res| res[k] = [v, container2[k]] }
  end

  interp.stack_push(result)
end

#word_ZIP_WITH(interp) ⇒ Object

( array1 array2 forthic – array ) ( record1 record2 forthic – record )



703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
# File 'lib/forthic/global_module.rb', line 703

def word_ZIP_WITH(interp)
  forthic = interp.stack_pop
  string_location = interp.get_string_location

  container2 = interp.stack_pop
  container1 = interp.stack_pop

  container1 ||= []
  container2 ||= []

  result = if container2.is_a?(Array)
    container1.map.with_index do |v, i|
      interp.stack_push(v)
      interp.stack_push(container2[i])
      interp.run(forthic, string_location)
      interp.stack_pop
    end
  else
    container1.each_with_object({}) do |(k, v), res|
      interp.stack_push(v)
      interp.stack_push(container2[k])
      interp.run(forthic, string_location)
      res[k] = interp.stack_pop
    end
  end

  interp.stack_push(result)
end