gloo 7.2 → 7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/docs/application.md +19 -3
  3. data/docs/iterators.md +66 -1
  4. data/docs/language_objects.md +14 -7
  5. data/docs/language_scripting.md +17 -0
  6. data/docs/language_syntax.md +86 -3
  7. data/docs/objects.md +6 -6
  8. data/docs/operators.md +36 -2
  9. data/docs/plugins.md +26 -0
  10. data/docs/testing.md +31 -2
  11. data/docs/verbs.md +8 -0
  12. data/lib/VERSION +1 -1
  13. data/lib/VERSION_NOTES +15 -0
  14. data/lib/gloo/app/engine.rb +133 -17
  15. data/lib/gloo/app/prompt.rb +8 -0
  16. data/lib/gloo/convert/string_to_decimal.rb +3 -6
  17. data/lib/gloo/convert/string_to_integer.rb +3 -2
  18. data/lib/gloo/core/dictionary.rb +23 -12
  19. data/lib/gloo/core/obj.rb +77 -0
  20. data/lib/gloo/core/op.rb +99 -0
  21. data/lib/gloo/core/parser.rb +15 -0
  22. data/lib/gloo/core/tokens.rb +18 -0
  23. data/lib/gloo/core/verb.rb +130 -1
  24. data/lib/gloo/exec/dispatch.rb +7 -3
  25. data/lib/gloo/exec/runaway_recursion.rb +34 -0
  26. data/lib/gloo/exec/runner.rb +46 -0
  27. data/lib/gloo/exec/script.rb +16 -2
  28. data/lib/gloo/expr/expression.rb +77 -6
  29. data/lib/gloo/expr/l_decimal.rb +18 -2
  30. data/lib/gloo/expr/l_integer.rb +5 -2
  31. data/lib/gloo/expr/op_div.rb +4 -0
  32. data/lib/gloo/expr/op_eq.rb +4 -0
  33. data/lib/gloo/expr/op_gt.rb +4 -0
  34. data/lib/gloo/expr/op_gteq.rb +4 -0
  35. data/lib/gloo/expr/op_ineq.rb +4 -0
  36. data/lib/gloo/expr/op_lt.rb +4 -0
  37. data/lib/gloo/expr/op_lteq.rb +4 -0
  38. data/lib/gloo/expr/op_minus.rb +4 -0
  39. data/lib/gloo/expr/op_mult.rb +3 -7
  40. data/lib/gloo/expr/op_plus.rb +8 -1
  41. data/lib/gloo/objs/basic/alias.rb +6 -1
  42. data/lib/gloo/objs/basic/boolean.rb +15 -6
  43. data/lib/gloo/objs/basic/decimal.rb +25 -3
  44. data/lib/gloo/objs/basic/script.rb +3 -1
  45. data/lib/gloo/objs/basic/string_msgs.rb +50 -68
  46. data/lib/gloo/objs/ctrl/until.rb +219 -0
  47. data/lib/gloo/plugin/callback.rb +54 -10
  48. data/lib/gloo/plugin/ext_manager.rb +44 -0
  49. data/lib/gloo/plugin/lib_manager.rb +65 -1
  50. data/lib/gloo/shell/runner.rb +36 -9
  51. data/lib/gloo/verbs/break.rb +6 -4
  52. data/lib/gloo/verbs/check.rb +8 -2
  53. data/lib/gloo/verbs/create.rb +13 -4
  54. data/lib/gloo/verbs/if.rb +33 -10
  55. data/lib/gloo/verbs/invoke.rb +6 -1
  56. data/lib/gloo/verbs/list.rb +9 -2
  57. data/lib/gloo/verbs/load.rb +4 -1
  58. data/lib/gloo/verbs/move.rb +8 -2
  59. data/lib/gloo/verbs/put.rb +9 -2
  60. data/lib/gloo/verbs/reload.rb +2 -1
  61. data/lib/gloo/verbs/run.rb +8 -1
  62. data/lib/gloo/verbs/save.rb +4 -0
  63. data/lib/gloo/verbs/tell.rb +9 -2
  64. data/lib/gloo/verbs/unless.rb +34 -10
  65. data/lib/gloo/verbs/unload.rb +81 -16
  66. data/test.gloo/ctrl/until.test.gloo +100 -0
  67. data/test.gloo/lang/computed_paths.test.gloo +98 -0
  68. data/test.gloo/lang/literal.test.gloo +43 -0
  69. data/test.gloo/lang/ops.test.gloo +193 -0
  70. data/test.gloo/lang/runaway_recursion.test.gloo +44 -0
  71. data/test.gloo/objs/decimal.test.gloo +23 -0
  72. data/test.gloo/objs/string.test.gloo +30 -0
  73. data/test.gloo/verbs/create.test.gloo +6 -21
  74. data/test.gloo/verbs/if.test.gloo +41 -0
  75. data/test.gloo/verbs/load.test.gloo +4 -3
  76. data/test.gloo/verbs/unless.test.gloo +40 -0
  77. data/test.gloo/verbs/unload.test.gloo +61 -3
  78. metadata +6 -1
@@ -17,6 +17,13 @@ module Gloo
17
17
  #
18
18
  TEST_LIB_NAME = 'test'
19
19
 
20
+ # Ctrl-C: what's shown, and the exit status after it (the shell
21
+ # convention for a run stopped by SIGINT).
22
+ INTERRUPTED_MSG = 'Stopped by Ctrl-C'.freeze
23
+ QUIT_CUT_SHORT_MSG = 'Quit cut short by Ctrl-C'.freeze
24
+ QUIT_HINT = "Use 'quit' to stop running gloo.".freeze
25
+ INTERRUPTED_EXIT = 130
26
+
20
27
  attr_reader :settings, :log, :running_app, :run_error_count
21
28
  attr_reader :args, :mode, :running, :platform,
22
29
  :dictionary, :parser, :heap, :factory,
@@ -44,6 +51,8 @@ module Gloo
44
51
  @handling_exception = false
45
52
  @handling_error = false
46
53
  @run_error_count = 0
54
+ @interrupted = false
55
+ @interrupt_location = nil
47
56
  @log.debug 'engine intialized...'
48
57
  end
49
58
 
@@ -98,7 +107,21 @@ module Gloo
98
107
  @persist_man = Gloo::Persist::PersistMan.new( self )
99
108
  end
100
109
 
101
- #
110
+ #
111
+ # Reset the session as if in a new process: what reset_state does
112
+ # (stop the app, a new empty heap), and then unload every library
113
+ # and extension, except the pinned ones gloo loaded itself. The
114
+ # test runner uses it between test files. The heap is emptied
115
+ # first, so no object of a library's types is left to stop it.
116
+ # (Plain unload and restart use reset_state, keeping libraries.)
117
+ #
118
+ def reset_session
119
+ reset_state
120
+ @lib_manager.unload_unpinned
121
+ @ext_manager.unload_all
122
+ end
123
+
124
+ #
102
125
  # Restart the engine with the same settings
103
126
  # and the same mode (with files, etc).
104
127
  #
@@ -142,8 +165,11 @@ module Gloo
142
165
  # script can tell a bad run from a good one.
143
166
  #
144
167
  def run_files
145
- load_files
168
+ # Ctrl-C stops the run; at a terminal, stay at the gloo prompt
169
+ # to look at what ran.
170
+ self.loop if !run_interruptible { load_files } && interactive?
146
171
  quit
172
+ exit( INTERRUPTED_EXIT ) if interrupted?
147
173
  exit( 1 ) if run_failed?
148
174
  end
149
175
 
@@ -161,9 +187,11 @@ module Gloo
161
187
  #
162
188
  def run_test
163
189
  @log.debug 'Running in Test mode…'
164
- passed = run_tests
190
+ passed = false
191
+ run_interruptible { passed = run_tests }
165
192
  @log.debug 'Tests complete.'
166
193
  quit
194
+ exit( INTERRUPTED_EXIT ) if interrupted?
167
195
  exit( 1 ) unless passed
168
196
  end
169
197
 
@@ -174,7 +202,9 @@ module Gloo
174
202
  # counts as passed, as it always has.
175
203
  #
176
204
  def run_tests
177
- @lib_manager.load_lib TEST_LIB_NAME
205
+ # Pinned: the runner needs it for the whole run, so a test can't
206
+ # unload it, and a session reset between test files keeps it.
207
+ @lib_manager.load_lib TEST_LIB_NAME, pinned: true
178
208
  return TestRunner.new( self, @args.files ).run != false
179
209
  rescue StandardError, ScriptError => ex
180
210
  handle_exception ex
@@ -185,17 +215,21 @@ module Gloo
185
215
  # Run in interactive mode.
186
216
  #
187
217
  def run
188
- # Open default file(s)
189
- self.open_start_file
218
+ started = run_interruptible do
219
+ # Open default file(s)
220
+ self.open_start_file
190
221
 
191
- # Open any files specifed in args
192
- load_files
222
+ # Open any files specifed in args
223
+ load_files
224
+ end
193
225
 
194
226
  unless @mode == Mode::SCRIPT || @args.quiet? || @args.single_command?
195
- self.loop
227
+ # Stopped by Ctrl-C with no one at a terminal: don't wait for input.
228
+ self.loop if started || interactive?
196
229
  end
197
230
 
198
231
  quit
232
+ exit( INTERRUPTED_EXIT ) if interrupted?
199
233
  end
200
234
 
201
235
  #
@@ -239,8 +273,14 @@ module Gloo
239
273
  #
240
274
  def loop
241
275
  while @running
242
- @last_cmd = @platform.prompt.ask
243
- process_cmd
276
+ begin
277
+ @last_cmd = @platform.prompt.ask
278
+ rescue Interrupt
279
+ # Ctrl-C at the prompt: the line is dropped.
280
+ interrupted_at_prompt( @platform.prompt.line_typed )
281
+ next
282
+ end
283
+ run_interruptible { process_cmd }
244
284
  end
245
285
  end
246
286
 
@@ -273,18 +313,84 @@ module Gloo
273
313
  # Do any clean up and quit.
274
314
  #
275
315
  def quit
276
- if app_running?
277
- @log.debug 'stopping running app...'
278
- stop_running_app
316
+ begin
317
+ if app_running?
318
+ @log.debug 'stopping running app...'
319
+ stop_running_app
320
+ end
321
+
322
+ @log.debug 'triggering on_quit events...'
323
+ @event_manager.on_quit
324
+ rescue Interrupt
325
+ # Ctrl-C while quitting: stop quitting and leave now.
326
+ @log.warn interrupt_msg( QUIT_CUT_SHORT_MSG )
327
+ exit( INTERRUPTED_EXIT )
279
328
  end
280
329
 
281
- @log.debug 'triggering on_quit events...'
282
- @event_manager.on_quit
283
-
284
330
  @log.info 'Gloo engine is quitting...'
285
331
  end
286
332
 
287
333
 
334
+ # ---------------------------------------------------------------------
335
+ # Ctrl-C
336
+ # ---------------------------------------------------------------------
337
+
338
+ #
339
+ # Run the block; if Ctrl-C stops it, say so and carry on. Returns
340
+ # false if it was stopped.
341
+ #
342
+ def run_interruptible
343
+ yield
344
+ return true
345
+ rescue Interrupt
346
+ interrupted
347
+ return false
348
+ end
349
+
350
+ #
351
+ # Ctrl-C stopped something that was running. It isn't an error (no
352
+ # on_error, not a failed run), but it's never silent: a warning,
353
+ # with where it stopped. A later quit exits with 130.
354
+ #
355
+ def interrupted
356
+ @interrupted = true
357
+ @log.warn interrupt_msg( INTERRUPTED_MSG )
358
+ end
359
+
360
+ #
361
+ # Ctrl-C at a prompt with nothing running: the line is dropped. If
362
+ # nothing had been typed, say how to leave (the hint; nil for
363
+ # none). A core library's own prompt (a shell, a menu) passes its
364
+ # own hint.
365
+ #
366
+ def interrupted_at_prompt( line_typed, hint = QUIT_HINT )
367
+ @interrupt_location = nil
368
+ @log.show hint if hint && line_typed.to_s.empty?
369
+ end
370
+
371
+ #
372
+ # Note where Ctrl-C stopped the run, the first (deepest) time it's
373
+ # seen on its way up (see Runner).
374
+ #
375
+ def note_interrupt_location
376
+ @interrupt_location ||= @exec_env&.current_location
377
+ end
378
+
379
+ #
380
+ # Did Ctrl-C stop something during this run?
381
+ #
382
+ def interrupted?
383
+ return @interrupted
384
+ end
385
+
386
+ #
387
+ # Is someone at a terminal to type at a prompt?
388
+ #
389
+ def interactive?
390
+ return $stdin.tty?
391
+ end
392
+
393
+
288
394
  # ---------------------------------------------------------------------
289
395
  # Running app within gloo
290
396
  # ---------------------------------------------------------------------
@@ -462,6 +568,16 @@ module Gloo
462
568
 
463
569
  private
464
570
 
571
+ #
572
+ # The Ctrl-C message, with where it stopped if known; the location
573
+ # is used once.
574
+ #
575
+ def interrupt_msg( msg )
576
+ location = @interrupt_location
577
+ @interrupt_location = nil
578
+ return location ? "#{msg}: #{location}." : "#{msg}."
579
+ end
580
+
465
581
  #
466
582
  # Resolve the start_with setting to a loadable name/path.
467
583
  # A bare filename (no directory separator) is resolved against
@@ -44,6 +44,14 @@ module Gloo
44
44
  return response
45
45
  end
46
46
 
47
+ #
48
+ # What had been typed at the prompt when it was last left: after
49
+ # Ctrl-C, empty if nothing had been typed.
50
+ #
51
+ def line_typed
52
+ return Reline.line_buffer.to_s
53
+ end
54
+
47
55
  #
48
56
  # Prompt for multiline input.
49
57
  #
@@ -16,16 +16,13 @@ module Gloo
16
16
  end
17
17
 
18
18
  #
19
- # Is the value really a decimal number? A blank value is no
20
- # value (0.0).
19
+ # Is the value really a decimal number, written the way a decimal
20
+ # literal is (see LDecimal)? A blank value is no value (0.0).
21
21
  #
22
22
  def valid?( value, _result )
23
23
  return true if value.blank?
24
24
 
25
- Float( value.strip )
26
- return true
27
- rescue ArgumentError
28
- return false
25
+ return Gloo::Expr::LDecimal.number?( value )
29
26
  end
30
27
 
31
28
  end
@@ -18,10 +18,11 @@ module Gloo
18
18
  end
19
19
 
20
20
  #
21
- # Is the value really an integer? A blank value is no value (0).
21
+ # Is the value really an integer, written the way an integer
22
+ # literal is (see LInteger)? A blank value is no value (0).
22
23
  #
23
24
  def valid?( value, _result )
24
- return value.blank? || value.match?( /\A\s*[-+]?\d+\s*\z/ )
25
+ return value.blank? || Gloo::Expr::LInteger.integer?( value )
25
26
  end
26
27
 
27
28
  end
@@ -99,17 +99,22 @@ module Gloo
99
99
  end
100
100
 
101
101
  #
102
- # Get the list of verbs, sorted.
102
+ # Get the object types that are registered now, sorted. Built
103
+ # from the same table find_obj uses (one entry per type, though
104
+ # it's there under its name and its shortcut), so a type that
105
+ # was unregistered, eg. by unloading its library, isn't listed.
103
106
  #
104
107
  def get_obj_types
105
- return @obj_references.sort { |a, b| a.typename <=> b.typename }
108
+ return @objs.values.uniq.sort { |a, b| a.typename <=> b.typename }
106
109
  end
107
110
 
108
111
  #
109
- # Get the list of verbs, sorted.
112
+ # Get the verbs that are registered now, sorted. Built from the
113
+ # same table find_verb uses, one entry per verb, so a verb that
114
+ # was unregistered isn't listed.
110
115
  #
111
116
  def get_verbs
112
- return @verb_references.sort { |a, b| a.keyword <=> b.keyword }
117
+ return @verbs.values.uniq.sort { |a, b| a.keyword <=> b.keyword }
113
118
  end
114
119
 
115
120
  #
@@ -164,22 +169,28 @@ module Gloo
164
169
  # Un-Register a verb.
165
170
  #
166
171
  def unregister_verb( verb_class )
167
- @verbs.delete( verb_class.keyword )
168
- @verbs.delete( verb_class.keyword_shortcut )
172
+ [ verb_class.keyword, verb_class.keyword_shortcut ].each do |key|
173
+ # Only this class's own entry: if another class won the
174
+ # keyword (a duplicate at registration), it stays.
175
+ next unless @verbs[ key ] == verb_class
169
176
 
170
- @keywords.delete( verb_class.keyword )
171
- @keywords.delete( verb_class.keyword_shortcut )
177
+ @verbs.delete( key )
178
+ @keywords.delete( key )
179
+ end
172
180
  end
173
181
 
174
182
  #
175
183
  # Un-Register an object.
176
184
  #
177
185
  def unregister_obj( obj_class )
178
- @objs.delete( obj_class.typename )
179
- @objs.delete( obj_class.short_typename )
186
+ [ obj_class.typename, obj_class.short_typename ].each do |key|
187
+ # Only this class's own entry: if another class won the
188
+ # name (a duplicate at registration), it stays.
189
+ next unless @objs[ key ] == obj_class
180
190
 
181
- @keywords.delete( obj_class.typename )
182
- @keywords.delete( obj_class.short_typename )
191
+ @objs.delete( key )
192
+ @keywords.delete( key )
193
+ end
183
194
  end
184
195
 
185
196
 
data/lib/gloo/core/obj.rb CHANGED
@@ -9,6 +9,8 @@ module Gloo
9
9
  module Core
10
10
  class Obj < Baseo
11
11
 
12
+ MISSING_PARAM_ERR = 'Missing parameter!'.freeze
13
+
12
14
  attr_accessor :value
13
15
  attr_reader :children, :parent
14
16
 
@@ -362,6 +364,81 @@ module Gloo
362
364
  return false
363
365
  end
364
366
 
367
+ #
368
+ # The message's parameters, one entry per parameter: each is the
369
+ # list of its tokens, either a single token or a whole ( ... )
370
+ # group, so a group can be one parameter: sub ( ( 'a' + 'b' ) 'x' ).
371
+ #
372
+ def param_groups
373
+ return [] unless @params&.token_count&.positive?
374
+
375
+ tokens = Gloo::Expr::Expression.split_parens( @params.tokens )
376
+ groups = []
377
+ i = 0
378
+ while i < tokens.length
379
+ last = param_group_end( tokens, i )
380
+ groups << tokens[ i..last ]
381
+ i = last + 1
382
+ end
383
+ return groups
384
+ end
385
+
386
+ #
387
+ # Are there at least the given number of parameters? If not, it's
388
+ # a syntax error, and it is false.
389
+ #
390
+ def params_given?( count )
391
+ return true if param_groups.length >= count
392
+
393
+ @engine.syntax_err MISSING_PARAM_ERR
394
+ @engine.heap.it.set_to false
395
+ return false
396
+ end
397
+
398
+ #
399
+ # Work out the parameter at the given index as a value. Returns
400
+ # the value and whether it worked: false if working it out
401
+ # reported a problem (it is then false).
402
+ #
403
+ def param_value( index )
404
+ errors_before = @engine.heap.error.error_count
405
+ value = Gloo::Expr::Expression.new( @engine, param_groups[ index ] || [] ).evaluate
406
+ return value, true if @engine.heap.error.error_count == errors_before
407
+
408
+ @engine.heap.it.set_to false
409
+ return value, false
410
+ end
411
+
412
+ #
413
+ # The parameter at the given index as an object path: the token as
414
+ # written, or for a ( ... ) group, the path it works out to (a
415
+ # computed path). Nil if a group couldn't be worked out (reported).
416
+ #
417
+ def param_path( index )
418
+ tokens = param_groups[ index ] || []
419
+ return tokens.first unless tokens.length > 1
420
+
421
+ value, ok = param_value( index )
422
+ return ok ? value.to_s : nil
423
+ end
424
+
425
+ #
426
+ # The index of the last token of the parameter starting at index
427
+ # i: the ')' that closes a group (or the end, if it never closes,
428
+ # already a syntax error), otherwise i itself.
429
+ #
430
+ def param_group_end( tokens, i )
431
+ return i unless tokens[ i ] == Gloo::Expr::Expression::OPEN_PAREN
432
+
433
+ depth = 0
434
+ ( i...tokens.length ).each do |j|
435
+ depth += 1 if tokens[ j ] == Gloo::Expr::Expression::OPEN_PAREN
436
+ depth -= 1 if tokens[ j ] == Gloo::Expr::Expression::CLOSE_PAREN
437
+ return j if depth.zero?
438
+ end
439
+ return tokens.length - 1
440
+ end
441
+
365
442
  #
366
443
  # Dispatch the message to the object.
367
444
  #
data/lib/gloo/core/op.rb CHANGED
@@ -9,6 +9,11 @@ module Gloo
9
9
  module Core
10
10
  class Op
11
11
 
12
+ NOT_A_NUMBER_WARN = "'%s' is not a number; using 0.".freeze
13
+
14
+ # The engine, for warnings; set by the expression before perform.
15
+ attr_accessor :engine
16
+
12
17
  #
13
18
  # Is the token an operator?
14
19
  #
@@ -57,6 +62,100 @@ module Gloo
57
62
  return Gloo::Expr::OpPlus.new
58
63
  end
59
64
 
65
+ #
66
+ # Promote two numbers to a common type, the one rule every
67
+ # operator uses when both values are numbers.
68
+ # An integer with an integer stays an integer; a decimal on
69
+ # either side makes both decimals.
70
+ # Returns [ left, right ], or nil unless both are numbers.
71
+ #
72
+ def promote( left, right )
73
+ return nil unless left.is_a?( Numeric ) && right.is_a?( Numeric )
74
+ return [ left, right ] if left.is_a?( Integer ) && right.is_a?( Integer )
75
+
76
+ return [ left.to_f, right.to_f ]
77
+ end
78
+
79
+ #
80
+ # Compare two strings in dictionary order, the rule the
81
+ # ordering operators (< > <= >=) use for text with text.
82
+ # Case is ignored first; only on a tie does a capital come
83
+ # first, so 'apple' < 'Banana' < 'banana'.
84
+ # Equal only when the strings are exactly the same, as with =.
85
+ # Returns -1, 0 or 1, or nil unless both are strings.
86
+ #
87
+ def compare_text( left, right )
88
+ return nil unless left.is_a?( String ) && right.is_a?( String )
89
+
90
+ cmp = left.downcase <=> right.downcase
91
+ return cmp unless cmp.zero?
92
+
93
+ # Same letters; character codes put the capital first.
94
+ return left <=> right
95
+ end
96
+
97
+ #
98
+ # Compare two values, the rule every comparison operator uses:
99
+ # number with number, numerically (after promotion); text with
100
+ # text, in dictionary order; number with text, numerically if the
101
+ # text is a number, otherwise as text.
102
+ # Returns -1, 0 or 1, or nil for any other kinds of value.
103
+ #
104
+ def compare( left, right )
105
+ if left.is_a?( Numeric ) && right.is_a?( String )
106
+ right = text_to_number( right ) || right
107
+ left = left.to_s if right.is_a?( String )
108
+ elsif left.is_a?( String ) && right.is_a?( Numeric )
109
+ left = text_to_number( left ) || left
110
+ right = right.to_s if left.is_a?( String )
111
+ end
112
+
113
+ nums = promote( left, right )
114
+ return nums[ 0 ] <=> nums[ 1 ] if nums
115
+
116
+ return compare_text( left, right )
117
+ end
118
+
119
+ #
120
+ # Get the two values as numbers for math (- * /), promoted.
121
+ # Text is used as a number; text that isn't one is 0, with a
122
+ # warning (blank text is 0 without one, as in an [int]).
123
+ # Returns [ left, right ], or nil unless both are numbers then.
124
+ #
125
+ def math_numbers( left, right )
126
+ return promote( text_as_number( left ), text_as_number( right ) )
127
+ end
128
+
129
+ #
130
+ # The number written in the text (as a literal would be:
131
+ # 5, -5, 2.50, .5), or nil if the text isn't a number.
132
+ #
133
+ def text_to_number( text )
134
+ s = text.strip
135
+ return s.to_i if Gloo::Expr::LInteger.integer?( s )
136
+ return s.to_f if Gloo::Expr::LDecimal.decimal?( s )
137
+
138
+ return nil
139
+ end
140
+
141
+ private
142
+
143
+ #
144
+ # Text as a number for math: the number it holds, or 0 (with a
145
+ # warning unless blank). Anything that isn't text is unchanged.
146
+ #
147
+ def text_as_number( value )
148
+ return value unless value.is_a?( String )
149
+
150
+ num = text_to_number( value )
151
+ return num if num
152
+ return 0 if value.strip.empty?
153
+
154
+ # Ops made outside an expression (eg. in a unit test) have no engine.
155
+ @engine&.warn format( NOT_A_NUMBER_WARN, value )
156
+ return 0
157
+ end
158
+
60
159
  end
61
160
  end
62
161
  end
@@ -79,6 +79,7 @@ module Gloo
79
79
  i = matching_open_paren_index( cmd )
80
80
  return cmd, nil unless i
81
81
  return cmd, nil if inline_call_opener?( cmd, i )
82
+ return cmd, nil unless follows_value?( cmd, i )
82
83
 
83
84
  pstr = cmd[ i + 1..-1 ]
84
85
  params = pstr.strip[ 0..-2 ] if pstr
@@ -108,6 +109,20 @@ module Gloo
108
109
  return nil
109
110
  end
110
111
 
112
+ #
113
+ # Is the trailing group at the given index the verb's extra part
114
+ # (message parameters, show's colour)? Only when it follows a
115
+ # value. Right after the verb, or after an operator, it's a group
116
+ # in the expression (eval ( 3 + 4 ), show 2 * ( 3 + 4 )), or the
117
+ # verb's computed argument (load ( x )), so it stays in the command.
118
+ #
119
+ def follows_value?( cmd, paren_index )
120
+ before = Gloo::Core::Tokens.new( cmd[ 0...paren_index ] )
121
+ return false if before.token_count < 2
122
+
123
+ return !Gloo::Core::Op.op?( before.last )
124
+ end
125
+
111
126
  #
112
127
  # Is the '(' at the given index immediately preceded (no
113
128
  # space) by an inline-call keyword, eg. invoke( or ~>( ?
@@ -187,6 +187,16 @@ module Gloo
187
187
  return copy
188
188
  end
189
189
 
190
+ #
191
+ # A copy of these tokens with the tokens from first to last
192
+ # (indexes, inclusive) replaced by one token.
193
+ #
194
+ def with_tokens_replaced( first, last, token )
195
+ copy = dup
196
+ copy.replace_tokens( first, last, token )
197
+ return copy
198
+ end
199
+
190
200
  #
191
201
  # Get the item after a given token.
192
202
  #
@@ -206,6 +216,14 @@ module Gloo
206
216
  @tokens += [ token ]
207
217
  end
208
218
 
219
+ #
220
+ # Replace the tokens from first to last with one token, in a new
221
+ # list (see with_tokens_replaced).
222
+ #
223
+ def replace_tokens( first, last, token )
224
+ @tokens = @tokens[ 0...first ] + [ token ] + @tokens[ last + 1..-1 ]
225
+ end
226
+
209
227
  # ---------------------------------------------------------------------
210
228
  # Private functions
211
229
  # ---------------------------------------------------------------------