gloo 7.1.3 → 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 (109) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +23 -0
  3. data/docs/application.md +65 -4
  4. data/docs/development.md +10 -0
  5. data/docs/iterators.md +66 -1
  6. data/docs/language_objects.md +34 -22
  7. data/docs/language_scripting.md +23 -2
  8. data/docs/language_syntax.md +86 -3
  9. data/docs/objects.md +6 -6
  10. data/docs/operators.md +36 -2
  11. data/docs/plugins.md +26 -0
  12. data/docs/testing.md +35 -9
  13. data/docs/verbs.md +31 -3
  14. data/lib/VERSION +1 -1
  15. data/lib/VERSION_NOTES +23 -0
  16. data/lib/dependencies.rb +4 -1
  17. data/lib/gloo/app/engine.rb +151 -23
  18. data/lib/gloo/app/prompt.rb +8 -0
  19. data/lib/gloo/convert/string_to_decimal.rb +3 -6
  20. data/lib/gloo/convert/string_to_integer.rb +3 -2
  21. data/lib/gloo/core/dictionary.rb +23 -12
  22. data/lib/gloo/core/obj.rb +77 -23
  23. data/lib/gloo/core/op.rb +99 -0
  24. data/lib/gloo/core/parser.rb +15 -0
  25. data/lib/gloo/core/tokens.rb +18 -0
  26. data/lib/gloo/core/verb.rb +130 -1
  27. data/lib/gloo/exec/dispatch.rb +7 -3
  28. data/lib/gloo/exec/runaway_recursion.rb +34 -0
  29. data/lib/gloo/exec/runner.rb +46 -0
  30. data/lib/gloo/exec/script.rb +16 -2
  31. data/lib/gloo/expr/expression.rb +77 -6
  32. data/lib/gloo/expr/l_decimal.rb +18 -2
  33. data/lib/gloo/expr/l_integer.rb +5 -2
  34. data/lib/gloo/expr/op_div.rb +4 -0
  35. data/lib/gloo/expr/op_eq.rb +4 -0
  36. data/lib/gloo/expr/op_gt.rb +4 -0
  37. data/lib/gloo/expr/op_gteq.rb +4 -0
  38. data/lib/gloo/expr/op_ineq.rb +4 -0
  39. data/lib/gloo/expr/op_lt.rb +4 -0
  40. data/lib/gloo/expr/op_lteq.rb +4 -0
  41. data/lib/gloo/expr/op_minus.rb +4 -0
  42. data/lib/gloo/expr/op_mult.rb +3 -7
  43. data/lib/gloo/expr/op_plus.rb +8 -1
  44. data/lib/gloo/objs/basic/alias.rb +6 -1
  45. data/lib/gloo/objs/basic/boolean.rb +15 -6
  46. data/lib/gloo/objs/basic/decimal.rb +25 -3
  47. data/lib/gloo/objs/basic/script.rb +3 -24
  48. data/lib/gloo/objs/basic/string_msgs.rb +50 -68
  49. data/lib/gloo/objs/ctrl/until.rb +219 -0
  50. data/lib/gloo/persist/declaration_ledger.rb +9 -9
  51. data/lib/gloo/persist/declaration_writer.rb +208 -0
  52. data/lib/gloo/persist/disc_mech.rb +70 -39
  53. data/lib/gloo/persist/file_loader.rb +125 -66
  54. data/lib/gloo/persist/file_saver.rb +130 -198
  55. data/lib/gloo/persist/file_storage.rb +15 -4
  56. data/lib/gloo/persist/file_style.rb +89 -0
  57. data/lib/gloo/persist/line_splitter.rb +16 -1
  58. data/lib/gloo/persist/persist_man.rb +112 -30
  59. data/lib/gloo/persist/save_index.rb +211 -0
  60. data/lib/gloo/persist/script_body_collector.rb +16 -21
  61. data/lib/gloo/persist/source/blank_node.rb +10 -3
  62. data/lib/gloo/persist/source/comment_node.rb +10 -2
  63. data/lib/gloo/persist/source/directive_node.rb +10 -2
  64. data/lib/gloo/persist/source/obj_node.rb +174 -9
  65. data/lib/gloo/persist/source/source_doc.rb +54 -0
  66. data/lib/gloo/persist/subtree_extractor.rb +9 -37
  67. data/lib/gloo/persist/trivia_buffer.rb +7 -7
  68. data/lib/gloo/plugin/callback.rb +54 -10
  69. data/lib/gloo/plugin/ext_manager.rb +44 -0
  70. data/lib/gloo/plugin/lib_manager.rb +65 -1
  71. data/lib/gloo/shell/runner.rb +36 -9
  72. data/lib/gloo/verbs/break.rb +6 -4
  73. data/lib/gloo/verbs/check.rb +8 -2
  74. data/lib/gloo/verbs/create.rb +13 -4
  75. data/lib/gloo/verbs/if.rb +33 -10
  76. data/lib/gloo/verbs/invoke.rb +6 -1
  77. data/lib/gloo/verbs/list.rb +9 -2
  78. data/lib/gloo/verbs/load.rb +31 -22
  79. data/lib/gloo/verbs/move.rb +50 -5
  80. data/lib/gloo/verbs/put.rb +9 -2
  81. data/lib/gloo/verbs/reload.rb +2 -1
  82. data/lib/gloo/verbs/run.rb +8 -1
  83. data/lib/gloo/verbs/save.rb +30 -4
  84. data/lib/gloo/verbs/tell.rb +9 -2
  85. data/lib/gloo/verbs/unless.rb +34 -10
  86. data/lib/gloo/verbs/unload.rb +81 -16
  87. data/test.gloo/app/exit_status.test.gloo +34 -0
  88. data/test.gloo/app/fixtures/exit_after_error.gloo +9 -0
  89. data/test.gloo/ctrl/until.test.gloo +100 -0
  90. data/test.gloo/lang/computed_paths.test.gloo +98 -0
  91. data/test.gloo/lang/declaration.test.gloo +36 -0
  92. data/test.gloo/lang/fixtures/mixed_indent.gloo +5 -0
  93. data/test.gloo/lang/literal.test.gloo +43 -0
  94. data/test.gloo/lang/naming.test.gloo +44 -6
  95. data/test.gloo/lang/ops.test.gloo +193 -0
  96. data/test.gloo/lang/runaway_recursion.test.gloo +44 -0
  97. data/test.gloo/objs/decimal.test.gloo +23 -0
  98. data/test.gloo/objs/string.test.gloo +30 -0
  99. data/test.gloo/verbs/create.test.gloo +6 -21
  100. data/test.gloo/verbs/fixtures/load_beside.gloo +6 -0
  101. data/test.gloo/verbs/fixtures/lossless_text.gloo +7 -0
  102. data/test.gloo/verbs/fixtures/lossless_text_saved.gloo +9 -0
  103. data/test.gloo/verbs/if.test.gloo +41 -0
  104. data/test.gloo/verbs/load.test.gloo +22 -3
  105. data/test.gloo/verbs/move.test.gloo +39 -0
  106. data/test.gloo/verbs/save.test.gloo +239 -0
  107. data/test.gloo/verbs/unless.test.gloo +40 -0
  108. data/test.gloo/verbs/unload.test.gloo +61 -3
  109. metadata +15 -1
@@ -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
 
@@ -127,29 +129,6 @@ module Gloo
127
129
  return false
128
130
  end
129
131
 
130
- #
131
- # Serialize this object's value for saving to a file.
132
- # Returns the text that follows "name [type]" on the declaration
133
- # line -- including the leading ' :' and, for a multi-line string
134
- # value, the additional indented lines and the closing END marker.
135
- # indent is the indentation level of the declaration line itself.
136
- #
137
- def serialize_value( indent )
138
- return " : #{value_display}" unless value_string? && value.include?( "\n" )
139
-
140
- return serialize_block_value( indent )
141
- end
142
-
143
- #
144
- # Serialize a multi-line string value as a BEGIN/END block.
145
- #
146
- def serialize_block_value( indent )
147
- t = "\t" * indent
148
- inner = "\t" * ( indent + 1 )
149
- body = value.split( "\n" ).map { |line| "#{inner}#{line}" }.join( "\n" )
150
- return " : BEGIN\n#{body}\n#{t}END"
151
- end
152
-
153
132
  #
154
133
  # Is the value a String?
155
134
  #
@@ -385,6 +364,81 @@ module Gloo
385
364
  return false
386
365
  end
387
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
+
388
442
  #
389
443
  # Dispatch the message to the object.
390
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
  # ---------------------------------------------------------------------
@@ -13,6 +13,13 @@ module Gloo
13
13
 
14
14
  attr_reader :tokens, :params
15
15
 
16
+ BLANK_PATH_ERR = "Object path '( %s )' is blank.".freeze
17
+ COMPUTED_PATH_DOC = '( {expression} ) — Instead of a literal ' \
18
+ 'path, an expression in parentheses: it is worked out and its ' \
19
+ 'value is used as the path, as if it had been typed there. The ' \
20
+ "fixed part is quoted text: ( 'list.' n ). See doc " \
21
+ 'language_syntax, Computed Arguments.'.freeze
22
+
16
23
  #
17
24
  # Set up the verb.
18
25
  #
@@ -92,7 +99,7 @@ module Gloo
92
99
  # couldn't be worked out (already reported), true otherwise.
93
100
  #
94
101
  def take_evaluated_arg
95
- return true unless @params
102
+ return take_leading_group_arg unless @params
96
103
 
97
104
  value, ok = evaluate_expr( @params.tokens )
98
105
  return false unless ok
@@ -102,6 +109,128 @@ module Gloo
102
109
  return true
103
110
  end
104
111
 
112
+ #
113
+ # The whole argument is a group right after the verb, as in
114
+ # load ( x ): the parser leaves that in the tokens (it groups,
115
+ # see Parser#split_params), so it's worked out here and its value
116
+ # put in its place. Returns false if the expression couldn't be
117
+ # worked out (already reported), true otherwise.
118
+ #
119
+ def take_leading_group_arg
120
+ return true unless @tokens.respond_to?( :token_count )
121
+
122
+ last = path_group_end( 1 )
123
+ return true unless last && last == @tokens.token_count - 1
124
+
125
+ value, ok = evaluate_expr( Gloo::Core::Tokens.new( path_group_text( 1, last ) ).tokens )
126
+ return false unless ok
127
+
128
+ @tokens = @tokens.with_tokens_replaced( 1, last, value.to_s )
129
+ return true
130
+ end
131
+
132
+ #
133
+ # For a verb whose argument at the given position is an object
134
+ # path: a ( expr ) group there is worked out, and its value put
135
+ # back as one token, so the path can be computed, as in
136
+ # put 1 into ( 'counts.' key ). The verb then reads its arguments
137
+ # as usual. The position is a token index (1 for the word after
138
+ # the verb), or a keyword, meaning the token after it ('into').
139
+ #
140
+ # The group is either mid-line, in the tokens (tell ( t ) to run),
141
+ # or, when the path is the last argument, already split off by
142
+ # the parser into params (put 1 into ( x )). A trailing group
143
+ # after some other argument (tell t to run ( 1 )) is left alone.
144
+ # Returns false if the expression couldn't be worked out (already
145
+ # reported), true otherwise.
146
+ #
147
+ def take_path_arg( position )
148
+ return true unless @tokens.respond_to?( :token_count )
149
+
150
+ i = path_arg_index( position )
151
+ return true unless i
152
+
153
+ return take_trailing_path_arg if i == @tokens.token_count
154
+
155
+ last = path_group_end( i )
156
+ return true unless last
157
+
158
+ text = path_group_text( i, last )
159
+ value, ok = evaluate_expr( Gloo::Core::Tokens.new( text ).tokens )
160
+ return false unless ok && path_value_given?( value, text )
161
+
162
+ @tokens = @tokens.with_tokens_replaced( i, last, value.to_s )
163
+ return true
164
+ end
165
+
166
+ #
167
+ # The token index of the path argument at the given position:
168
+ # an index as given, or the one after a keyword. Nil if the
169
+ # keyword isn't there.
170
+ #
171
+ def path_arg_index( position )
172
+ return position if position.is_a?( Integer )
173
+
174
+ i = @tokens.index_of( position )
175
+ return i ? i + 1 : nil
176
+ end
177
+
178
+ #
179
+ # The path is the last argument and has no token: if the parser
180
+ # split a trailing group off into params, that's the path. Work
181
+ # it out and add it as the last token.
182
+ #
183
+ def take_trailing_path_arg
184
+ return true unless @params.respond_to?( :tokens )
185
+
186
+ value, ok = evaluate_expr( @params.tokens )
187
+ return false unless ok && path_value_given?( value, @params.tokens.join( ' ' ) )
188
+
189
+ @tokens = @tokens.with_token( value.to_s )
190
+ @params = nil
191
+ return true
192
+ end
193
+
194
+ #
195
+ # A worked-out path must have some text; blank is reported (the
196
+ # group's text, as written, names it) and the verb doesn't run.
197
+ #
198
+ def path_value_given?( value, text )
199
+ return true unless value.to_s.strip.empty?
200
+
201
+ @engine.err format( BLANK_PATH_ERR, text.strip )
202
+ return false
203
+ end
204
+
205
+ #
206
+ # If the token at index i opens a group, the index of the token
207
+ # that closes it; nil if it doesn't open one or never closes. A
208
+ # group can be written ( p ), (p) or ('a.' b), so parentheses are
209
+ # counted in each token; a quoted token is text and isn't counted.
210
+ #
211
+ def path_group_end( i )
212
+ return nil unless @tokens.at( i )&.start_with?( '(' )
213
+
214
+ depth = 0
215
+ ( i...@tokens.token_count ).each do |j|
216
+ token = @tokens.at( j )
217
+ next if Gloo::Core::Tokens::QUOTE_CHARS.include?( token[ 0 ] )
218
+
219
+ depth += token.count( '(' ) - token.count( ')' )
220
+ return j if depth <= 0
221
+ end
222
+ return nil
223
+ end
224
+
225
+ #
226
+ # The text inside the group from token first to token last, with
227
+ # its outer parentheses taken off.
228
+ #
229
+ def path_group_text( first, last )
230
+ text = ( first..last ).map { |j| @tokens.at( j ) }.join( ' ' )
231
+ return text.strip[ 1..-2 ]
232
+ end
233
+
105
234
  #
106
235
  # Work out an expression from the given tokens. Returns the value
107
236
  # and whether it worked: false if the expression reported a
@@ -48,9 +48,13 @@ module Gloo
48
48
  #
49
49
  def self.action( engine, action )
50
50
  engine.exec_env.push_action action
51
- engine.log.debug "Sending message #{action.msg} to #{action.to.name}"
52
- action.dispatch
53
- engine.exec_env.pop_action
51
+ begin
52
+ engine.log.debug "Sending message #{action.msg} to #{action.to.name}"
53
+ action.dispatch
54
+ ensure
55
+ # Also when something unwinds the run (runaway recursion).
56
+ engine.exec_env.pop_action
57
+ end
54
58
  end
55
59
 
56
60
  end
@@ -0,0 +1,34 @@
1
+ # Author:: Eric Crane (mailto:eric.crane@mac.com)
2
+ # Copyright:: Copyright (c) 2026 Eric Crane. All rights reserved.
3
+ #
4
+ # Scripts ran inside each other until Ruby ran out of stack: a script
5
+ # that runs itself (directly, or through others) with no end.
6
+ #
7
+ # It isn't a StandardError, on purpose: nothing on the way up may
8
+ # swallow it. It unwinds the run to the line that started the
9
+ # recursion, which reports it once (see Runner).
10
+ #
11
+
12
+ module Gloo
13
+ module Exec
14
+ class RunawayRecursion < Exception
15
+
16
+ MSG = 'Could not run %s: scripts ran %d deep, one inside ' \
17
+ 'another (runaway recursion?).'.freeze
18
+
19
+ attr_reader :location, :start_depth
20
+
21
+ #
22
+ # Note where it happened: the deepest script, how deep it was, and
23
+ # the depth (count of running scripts) of the line that started
24
+ # the recursion.
25
+ #
26
+ def initialize( script_pn, depth, location, start_depth = 0 )
27
+ super( format( MSG, script_pn, depth ) )
28
+ @location = location
29
+ @start_depth = start_depth
30
+ end
31
+
32
+ end
33
+ end
34
+ end
@@ -27,12 +27,58 @@ module Gloo
27
27
  # from a missing gem) isn't a StandardError, and would
28
28
  # otherwise stop gloo altogether.
29
29
  engine.handle_exception( ex )
30
+ rescue SystemStackError
31
+ runaway( engine, runaway_recursion( engine ) )
32
+ rescue Gloo::Exec::RunawayRecursion => ex
33
+ runaway( engine, ex )
34
+ rescue Interrupt
35
+ # Ctrl-C: note where it stopped, and let it unwind the run to
36
+ # whatever is waiting for it (see Engine#run_interruptible).
37
+ engine.note_interrupt_location
38
+ raise
30
39
  ensure
31
40
  engine.exec_env.verbs.pop
32
41
  end
33
42
  engine.heap.error.clear_if_no_errors
34
43
  end
35
44
 
45
+ #
46
+ # Ruby ran out of stack: scripts ran inside each other with no
47
+ # end. Note the deepest script, how deep, and where the recursion
48
+ # started.
49
+ #
50
+ def self.runaway_recursion( engine )
51
+ env = engine.exec_env
52
+ pn = env.running_script&.obj&.pn
53
+ return Gloo::Exec::RunawayRecursion.new(
54
+ pn, env.scripts.size, env.current_location, recursion_start( env ) )
55
+ end
56
+
57
+ #
58
+ # The depth of the line that started the recursion: the number of
59
+ # scripts running below the first script that runs again further
60
+ # up the stack. 0 (the outermost line) if none repeats.
61
+ #
62
+ def self.recursion_start( env )
63
+ objs = env.scripts.stack.map( &:obj )
64
+ counts = objs.tally
65
+ return objs.index { |o| counts[ o ] > 1 } || 0
66
+ end
67
+
68
+ #
69
+ # Runaway recursion unwinds the run to the line that started it,
70
+ # and is reported there, once; that line is abandoned like any
71
+ # failed line, and its script carries on. Every verb inside passes
72
+ # it on. The outermost verb reports it in any case.
73
+ #
74
+ def self.runaway( engine, ex )
75
+ env = engine.exec_env
76
+ raise ex unless env.scripts.size <= ex.start_depth || env.verbs.size == 1
77
+
78
+ engine.err ex.message, location: ex.location
79
+ engine.heap.it.set_to false
80
+ end
81
+
36
82
  #
37
83
  # Send 'run' message to the object.
38
84
  # Resolve the path_name and then send the run message.
@@ -27,7 +27,23 @@ module Gloo
27
27
  #
28
28
  def run
29
29
  @engine.exec_env.push_script self
30
+ begin
31
+ run_lines
32
+ rescue Interrupt
33
+ # Ctrl-C between verbs (eg. while a line is parsed): note this
34
+ # script as where it stopped, before it's popped.
35
+ @engine.note_interrupt_location
36
+ raise
37
+ ensure
38
+ # Also when something unwinds the run (runaway recursion).
39
+ @engine.exec_env.pop_script
40
+ end
41
+ end
30
42
 
43
+ #
44
+ # Run the script's lines, until the end or a break.
45
+ #
46
+ def run_lines
31
47
  if @obj.value.is_a? String
32
48
  @line = 1
33
49
  @engine.parser.run @obj.value
@@ -39,8 +55,6 @@ module Gloo
39
55
  @engine.parser.run line
40
56
  end
41
57
  end
42
-
43
- @engine.exec_env.pop_script
44
58
  end
45
59
 
46
60
  #