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
@@ -9,6 +9,9 @@ module Gloo
9
9
  class Expression
10
10
 
11
11
  DIVIDE_BY_ZERO_ERR = 'Division by zero.'.freeze
12
+ EMPTY_GROUP_ERR = 'Empty parentheses.'.freeze
13
+ OPEN_PAREN = '('.freeze
14
+ CLOSE_PAREN = ')'.freeze
12
15
 
13
16
  # ---------------------------------------------------------------------
14
17
  # Constructor
@@ -50,11 +53,39 @@ module Gloo
50
53
  end
51
54
  missing_value( @op ) if @op
52
55
 
53
- return @left.value if @left.is_a? Gloo::Core::Literal
54
- return @left.value if @left.is_a? Gloo::Expr::Call
55
- return resolve_ref @left if @left.is_a? Gloo::Core::Pn
56
+ return evaluate_sym( @left )
57
+ end
58
+
59
+ #
60
+ # Split grouping parentheses off the tokens, so each is a token
61
+ # of its own: '(3' and '4)' become '(' '3' and '4' ')'. Quoted
62
+ # text and inline calls (invoke( ... )) are left whole.
63
+ #
64
+ def self.split_parens( tokens )
65
+ return tokens.flat_map do |t|
66
+ next [ t ] unless t.is_a?( String )
67
+ next [ t ] if Gloo::Core::Tokens::QUOTE_CHARS.include?( t[ 0 ] )
68
+ next [ t ] if Gloo::Expr::Call.call?( t )
69
+
70
+ split_paren_token( t )
71
+ end
72
+ end
56
73
 
57
- return @left
74
+ #
75
+ # Split the leading '(' and trailing ')' off one token.
76
+ #
77
+ def self.split_paren_token( token )
78
+ opens = []
79
+ while token.length > 1 && token.start_with?( OPEN_PAREN )
80
+ opens << OPEN_PAREN
81
+ token = token[ 1..-1 ]
82
+ end
83
+ closes = []
84
+ while token.length > 1 && token.end_with?( CLOSE_PAREN )
85
+ closes << CLOSE_PAREN
86
+ token = token[ 0..-2 ]
87
+ end
88
+ return opens + [ token ] + closes
58
89
  end
59
90
 
60
91
  # ---------------------------------------------------------------------
@@ -81,6 +112,7 @@ module Gloo
81
112
  # result is no value. A missing value (nil) was already reported.
82
113
  #
83
114
  def apply_op( op, left, right )
115
+ op.engine = @engine
84
116
  result = op.perform( left, right )
85
117
  if op.is_a?( Gloo::Expr::OpDiv ) && result.is_a?( Float ) &&
86
118
  ( result.nan? || result.infinite? )
@@ -110,6 +142,8 @@ module Gloo
110
142
  return sym.value if sym.is_a? Gloo::Core::Literal
111
143
  return sym.value if sym.is_a? Gloo::Expr::Call
112
144
  return resolve_ref sym if sym.is_a? Gloo::Core::Pn
145
+ # A group: worked out when it's reached, left to right.
146
+ return sym.evaluate if sym.is_a? Gloo::Expr::Expression
113
147
 
114
148
  return sym
115
149
  end
@@ -134,9 +168,46 @@ module Gloo
134
168
  # Identify each token in the list.
135
169
  #
136
170
  def identify_tokens
137
- @tokens.each do |o|
138
- @symbols << identify_token( o )
171
+ tokens = Expression.split_parens( @tokens )
172
+ i = 0
173
+ while i < tokens.length
174
+ token = tokens[ i ]
175
+ if token == OPEN_PAREN
176
+ close = matching_close( tokens, i )
177
+ @symbols << group( tokens[ i + 1...close ] )
178
+ i = close + 1
179
+ else
180
+ # A stray ')' was reported as a syntax error with the
181
+ # command; the best guess is to leave it out.
182
+ @symbols << identify_token( token ) unless token == CLOSE_PAREN
183
+ i += 1
184
+ end
185
+ end
186
+ end
187
+
188
+ #
189
+ # The index of the ')' that closes the '(' at open_index. An
190
+ # unclosed group (reported as a syntax error with the command)
191
+ # runs to the end, as the best guess.
192
+ #
193
+ def matching_close( tokens, open_index )
194
+ depth = 0
195
+ ( open_index...tokens.length ).each do |j|
196
+ depth += 1 if tokens[ j ] == OPEN_PAREN
197
+ depth -= 1 if tokens[ j ] == CLOSE_PAREN
198
+ return j if depth.zero?
139
199
  end
200
+ return tokens.length
201
+ end
202
+
203
+ #
204
+ # A group in the expression: its own expression, worked out when
205
+ # it is reached. An empty group is a syntax error, and no value.
206
+ #
207
+ def group( tokens )
208
+ @engine.syntax_err EMPTY_GROUP_ERR if tokens.empty?
209
+
210
+ return Expression.new( @engine, tokens )
140
211
  end
141
212
 
142
213
  #
@@ -8,14 +8,30 @@ module Gloo
8
8
  module Expr
9
9
  class LDecimal < Gloo::Core::Literal
10
10
 
11
+ # A decimal as written: an optional sign; digits with an optional
12
+ # fraction, or just a fraction (.5); an optional exponent (1.0e3,
13
+ # 2e-5). Not 5. (a path start, like 'list.'), and not 1_000 or
14
+ # 0x1A. Plain digits match the pattern too, but they're an integer
15
+ # (see decimal?).
16
+ PATTERN = /\A[-+]?(\d+(\.\d+)?|\.\d+)([eE][-+]?\d+)?\z/
17
+
11
18
  #
12
- # Is the given token a decimal?
19
+ # Is the given token a decimal: a number with a fraction or an
20
+ # exponent? A plain integer isn't (see LInteger).
13
21
  #
14
22
  def self.decimal?( token )
15
23
  return true if token.is_a? Numeric
16
24
 
17
25
  s = token.strip
18
- return s.to_f.to_s == s
26
+ return PATTERN.match?( s ) && !LInteger.integer?( s )
27
+ end
28
+
29
+ #
30
+ # Is the given text a number a decimal can hold: a decimal or an
31
+ # integer, as written in a literal?
32
+ #
33
+ def self.number?( text )
34
+ return decimal?( text ) || LInteger.integer?( text )
19
35
  end
20
36
 
21
37
  #
@@ -8,14 +8,17 @@ module Gloo
8
8
  module Expr
9
9
  class LInteger < Gloo::Core::Literal
10
10
 
11
+ # An integer as written: an optional sign, then digits. Leading
12
+ # zeros are fine (007 is 7, never octal).
13
+ PATTERN = /\A[-+]?\d+\z/
14
+
11
15
  #
12
16
  # Is the given token an integer?
13
17
  #
14
18
  def self.integer?( token )
15
19
  return true if token.is_a? Integer
16
20
 
17
- s = token.strip
18
- return s.to_i.to_s == s
21
+ return PATTERN.match?( token.strip )
19
22
  end
20
23
 
21
24
  #
@@ -14,6 +14,10 @@ module Gloo
14
14
  # Perform the operation and return the result.
15
15
  #
16
16
  def perform( left, right )
17
+ # Numbers, and text used as a number: promote, then work them out.
18
+ nums = math_numbers( left, right )
19
+ return nums[ 0 ] / nums[ 1 ] if nums
20
+
17
21
  return left / right.to_i if left.is_a? Integer
18
22
 
19
23
  return left / right.to_f if left.is_a? Numeric
@@ -18,6 +18,10 @@ module Gloo
18
18
  cmp = DtTools.compare_dt( left, right )
19
19
  return cmp == 0 unless cmp == :not_dt
20
20
 
21
+ # Numbers and text: compared by the shared rule.
22
+ cmp = compare( left, right )
23
+ return cmp.zero? if cmp
24
+
21
25
  return left == right.to_s if left.is_a? String
22
26
 
23
27
  return left == right.to_i if left.is_a? Integer
@@ -17,6 +17,10 @@ module Gloo
17
17
  cmp = DtTools.compare_dt( left, right )
18
18
  return cmp.is_a?( Integer ) && cmp.positive? unless cmp == :not_dt
19
19
 
20
+ # Numbers and text: compared by the shared rule.
21
+ cmp = compare( left, right )
22
+ return cmp.positive? if cmp
23
+
20
24
  return left > right.to_i if left.is_a? Integer
21
25
 
22
26
  return left > right.to_f if left.is_a? Numeric
@@ -17,6 +17,10 @@ module Gloo
17
17
  cmp = DtTools.compare_dt( left, right )
18
18
  return cmp.is_a?( Integer ) && !cmp.negative? unless cmp == :not_dt
19
19
 
20
+ # Numbers and text: compared by the shared rule.
21
+ cmp = compare( left, right )
22
+ return !cmp.negative? if cmp
23
+
20
24
  return left >= right.to_i if left.is_a? Integer
21
25
 
22
26
  return left >= right.to_f if left.is_a? Numeric
@@ -17,6 +17,10 @@ module Gloo
17
17
  cmp = DtTools.compare_dt( left, right )
18
18
  return cmp.is_a?( Integer ) && cmp != 0 unless cmp == :not_dt
19
19
 
20
+ # Numbers and text: compared by the shared rule.
21
+ cmp = compare( left, right )
22
+ return !cmp.zero? if cmp
23
+
20
24
  return left != right.to_s if left.is_a? String
21
25
 
22
26
  return left != right.to_i if left.is_a? Integer
@@ -17,6 +17,10 @@ module Gloo
17
17
  cmp = DtTools.compare_dt( left, right )
18
18
  return cmp.is_a?( Integer ) && cmp.negative? unless cmp == :not_dt
19
19
 
20
+ # Numbers and text: compared by the shared rule.
21
+ cmp = compare( left, right )
22
+ return cmp.negative? if cmp
23
+
20
24
  return left < right.to_i if left.is_a? Integer
21
25
 
22
26
  return left < right.to_f if left.is_a? Numeric
@@ -17,6 +17,10 @@ module Gloo
17
17
  cmp = DtTools.compare_dt( left, right )
18
18
  return cmp.is_a?( Integer ) && !cmp.positive? unless cmp == :not_dt
19
19
 
20
+ # Numbers and text: compared by the shared rule.
21
+ cmp = compare( left, right )
22
+ return !cmp.positive? if cmp
23
+
20
24
  return left <= right.to_i if left.is_a? Integer
21
25
 
22
26
  return left <= right.to_f if left.is_a? Numeric
@@ -14,6 +14,10 @@ module Gloo
14
14
  # Perform the operation and return the result.
15
15
  #
16
16
  def perform( left, right )
17
+ # Numbers, and text used as a number: promote, then work them out.
18
+ nums = math_numbers( left, right )
19
+ return nums[ 0 ] - nums[ 1 ] if nums
20
+
17
21
  return left - right.to_i if left.is_a? Integer
18
22
 
19
23
  return left - right.to_f if left.is_a? Numeric
@@ -14,13 +14,9 @@ module Gloo
14
14
  # Perform the operation and return the result.
15
15
  #
16
16
  def perform( left, right )
17
- if ( left.is_a? Integer ) && ( right.is_a? Integer )
18
- return left * right
19
- end
20
-
21
- if (left.is_a? Numeric) && (right.is_a? Numeric)
22
- return left * right
23
- end
17
+ # Numbers, and text used as a number: promote, then work them out.
18
+ nums = math_numbers( left, right )
19
+ return nums[ 0 ] * nums[ 1 ] if nums
24
20
 
25
21
  return left * right.to_i if left.is_a? Integer
26
22
 
@@ -17,7 +17,14 @@ module Gloo
17
17
  # Perform the operation and return the result.
18
18
  #
19
19
  def perform( left, right )
20
- return left + right.to_s if left.is_a? String
20
+ # Text on either side: join, as text.
21
+ if left.is_a?( String ) || right.is_a?( String )
22
+ return left.to_s + right.to_s
23
+ end
24
+
25
+ # Two numbers: promote, then work them out as numbers.
26
+ nums = promote( left, right )
27
+ return nums[ 0 ] + nums[ 1 ] if nums
21
28
 
22
29
  return left + right.to_i if left.is_a? Integer
23
30
 
@@ -95,7 +95,12 @@ module Gloo
95
95
  :messages => [
96
96
  'resolve — Check to see if the object referenced exists. Sets it to true or false.'
97
97
  ],
98
- :notes => 'The alias also reflects (forwards) the messages of the object it points to.',
98
+ :notes => 'The alias also reflects (forwards) the messages of the object it points to. ' \
99
+ 'For a path built and used on one line, a computed path needs no alias: ' \
100
+ "create ( 'list.' n ) as string. Putting an alias in parentheses adds a step: " \
101
+ '( ln ) follows the alias and uses its target\'s value as the path, while ' \
102
+ '( ln* ) uses the path the alias holds, the same as ln. ' \
103
+ 'See doc language_syntax, Computed Arguments.',
99
104
  :examples => <<~EXAMPLES.strip
100
105
  #
101
106
  # Alias object.
@@ -13,6 +13,7 @@ module Gloo
13
13
  TRUE = 'true'.freeze
14
14
  FALSE = 'false'.freeze
15
15
  BOOLEAN_STRINGS = [ TRUE, FALSE, 't', 'f' ].freeze
16
+ BAD_VALUE_WARN = "'%s' is not true or false; using false.".freeze
16
17
 
17
18
  #
18
19
  # The name of the object type.
@@ -32,10 +33,17 @@ module Gloo
32
33
  # Set the value with any necessary type conversions.
33
34
  #
34
35
  def set_value( new_value )
35
- self.value = Gloo::Objs::Boolean.coerse_to_bool( new_value )
36
- return unless Gloo::Objs::Boolean.bad_value?( new_value )
36
+ self.value = Gloo::Objs::Boolean.to_bool( @engine, new_value )
37
+ end
37
38
 
38
- @engine.warn "'#{new_value}' is not true or false; using #{self.value}."
39
+ #
40
+ # Convert a value to true or false, warning about text that is
41
+ # neither. The one rule for what counts as true: for a [bool]'s
42
+ # value and for every condition (if, unless, until).
43
+ #
44
+ def self.to_bool( engine, value )
45
+ engine.warn format( BAD_VALUE_WARN, value ) if bad_value?( value )
46
+ return coerse_to_bool( value )
39
47
  end
40
48
 
41
49
  #
@@ -63,8 +71,9 @@ module Gloo
63
71
  return false if new_value.strip.downcase == FALSE
64
72
  return true if new_value.strip.downcase == 't'
65
73
  return false if new_value.strip.downcase == 'f'
66
- elsif new_value.class.name == 'Integer'
67
- return new_value.zero? ? false : true
74
+ elsif new_value.is_a?( ::Numeric )
75
+ # Any number but zero is true, decimals included.
76
+ return !new_value.zero?
68
77
  end
69
78
 
70
79
  return new_value == true
@@ -147,7 +156,7 @@ module Gloo
147
156
  'true — Set the boolean to true.',
148
157
  'false — Set the boolean to false.'
149
158
  ],
150
- :notes => 'Recognized values are true, false, t and f (in any case), and integers (0 is false). Any other string is false, with a warning: "\'maybe\' is not true or false; using false." A blank value is false.',
159
+ :notes => 'Recognized values are true, false, t and f (in any case), and numbers (0 is false; any other number, decimals included, is true). Any other string is false, with a warning: "\'maybe\' is not true or false; using false." A blank value is false. Conditions (if, unless, until) use the same rule; see doc language_syntax, What Counts as True.',
151
160
  :examples => <<~EXAMPLES.strip
152
161
  b [can] :
153
162
  flag [boolean] : true
@@ -11,6 +11,12 @@ module Gloo
11
11
  KEYWORD = 'decimal'.freeze
12
12
  KEYWORD_SHORT = 'num'.freeze
13
13
 
14
+ # A fixed-point spec in a format string, eg. %.2f or %8.2f; the
15
+ # precision (digits after the point) is captured, if given.
16
+ FIXED_SPEC = /%[-+ 0#]*\d*(?:\.(\d+))?[fF]/
17
+ # sprintf's precision for %f when none is given.
18
+ DEFAULT_PRECISION = 6
19
+
14
20
  #
15
21
  # The name of the object type.
16
22
  #
@@ -83,7 +89,7 @@ module Gloo
83
89
  if @params&.token_count&.positive?
84
90
  expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
85
91
  fmt = expr.evaluate
86
- formatted = format( fmt, value )
92
+ formatted = format( fmt, rounded_for( fmt ) )
87
93
  else
88
94
  whole, frac = value.to_s.split( '.' )
89
95
  grouped = whole.reverse.scan( /.{1,3}/ ).join( ',' ).reverse
@@ -92,6 +98,21 @@ module Gloo
92
98
  @engine.heap.it.set_to formatted
93
99
  end
94
100
 
101
+ #
102
+ # The value to format: for a fixed-point spec (%.2f), rounded to
103
+ # its precision first, the way round does (halves away from zero),
104
+ # so format and round always agree. sprintf alone works on the
105
+ # binary value, and gives '%.2f' of 1.005 as 1.00. Other specs
106
+ # (%e, %d, ...) get the value as it is.
107
+ #
108
+ def rounded_for( fmt )
109
+ spec = FIXED_SPEC.match( fmt.to_s )
110
+ return value unless spec
111
+
112
+ precision = spec[ 1 ] ? spec[ 1 ].to_i : DEFAULT_PRECISION
113
+ return value.round( precision )
114
+ end
115
+
95
116
  # ---------------------------------------------------------------------
96
117
  # Object Documentation
97
118
  # ---------------------------------------------------------------------
@@ -106,8 +127,9 @@ module Gloo
106
127
  :description => 'A decimal (numeric) value.',
107
128
  :messages => [
108
129
  'round ({precision}) — Round to the nearest whole value. If ' \
109
- 'an optional parameter is included, round to the precision specified.',
110
- 'format ({fmt}) — With no parameter, adds comma separators to the whole part (e.g. 1000.5 -> 1,000.5). With a parameter, uses it as a sprintf-style format string (e.g. \'%.2f\'). It will have the formatted string.'
130
+ 'an optional parameter is included, round to the precision specified. ' \
131
+ 'A half rounds away from zero: 2.5 -> 3, -2.5 -> -3, and 2.675 to 2 places -> 2.68.',
132
+ 'format ({fmt}) — With no parameter, adds comma separators to the whole part (e.g. 1000.5 -> 1,000.5). With a parameter, uses it as a sprintf-style format string (e.g. \'%.2f\'); for a fixed-point spec like %.2f the value is rounded first, as round does, so the two always agree (1.005 -> 1.01). It will have the formatted string.'
111
133
  ],
112
134
  :notes => 'A value that isn\'t a number (eg. \'x\' or \'1,5\') is used as a best guess, with a warning: "\'1,5\' is not a decimal number; using 1.0." A blank value is 0.0.',
113
135
  :examples => <<~EXAMPLES.strip
@@ -62,29 +62,6 @@ module Gloo
62
62
  return true
63
63
  end
64
64
 
65
- #
66
- # Serialize this object's value for saving to a file.
67
- # A script's value is an Array of command lines (once add_line
68
- # has been called) or a single-line String. Render as indented
69
- # body lines below the declaration, gloo's convention for
70
- # scripts -- not a BEGIN/END block, which is reserved for a
71
- # literal multi-line string value.
72
- #
73
- def serialize_value( indent )
74
- lines = if value_is_array?
75
- value
76
- elsif value_string? && !value.strip.empty?
77
- [ value ]
78
- else
79
- []
80
- end
81
- return ' :' if lines.empty?
82
-
83
- t = "\t" * ( indent + 1 )
84
- body = lines.map { |line| "#{t}#{line}" }.join( "\n" )
85
- return " :\n#{body}"
86
- end
87
-
88
65
  #
89
66
  # Get the number of lines in this script.
90
67
  #
@@ -138,7 +115,9 @@ module Gloo
138
115
  :name => KEYWORD,
139
116
  :shortcut => KEYWORD_SHORT,
140
117
  :description => 'An executable script — a set of commands to ' \
141
- 'be run. A command line may end with an inline `# ...` ' \
118
+ 'be run. A single command can go on the same line, after the ' \
119
+ ':, as in done [script] : eval x > 100; more go on the lines ' \
120
+ 'below. A command line may end with an inline `# ...` ' \
142
121
  'comment; it is ignored when the line runs but kept when ' \
143
122
  'the file is saved.',
144
123
  :messages => [
@@ -41,7 +41,7 @@ module Gloo
41
41
  'starts_with? ({str}) — Check if the string starts with the given string. A parameter is required: the string to look for at the beginning of this string. It will have a boolean.',
42
42
  'ends_with? ({str}) — Check if the string ends with the given string. A parameter is required: the string to look for at the end of this string. It will have a boolean.',
43
43
  'substring? ({str}) — Check if the string includes the given sub-string. A parameter is required: the string to look for in this string. It will have a boolean.',
44
- 'index_of ({needle} {from}) — Find the 0-based position of the first occurrence of {needle} at or after position {from}. The {needle} parameter is required; {from} is optional and defaults to 0. Does not change the value of the string. It will have the position, or -1 if {needle} is not found; an out-of-range {from} (negative, or past the end of the string) is an error, and it will have false.',
44
+ 'index_of ({needle} {from}) — Find the 0-based position of the first occurrence of {needle} at or after position {from}. The {needle} parameter is required; {from} is optional and defaults to 0. Either can be a group: index_of ( \'o\' ( n + 1 ) ). Does not change the value of the string. It will have the position, or -1 if {needle} is not found; an out-of-range {from} (negative, or past the end of the string) is an error, and it will have false.',
45
45
  'format_for_html — Format this string for HTML output. Tabs, spaces and returns are converted to HTML elements. The value of the string is changed. It will have the formatted string.',
46
46
  'encode64 — Base64 encode the string. This message changes the value of the string. It will have the encoded string.',
47
47
  'decode64 — Decode the string from Base64. This message changes the value of the string. It will have the decoded string.',
@@ -52,12 +52,12 @@ module Gloo
52
52
  'gen_hex ({len}) — Set the value of the string to a newly generated, random hex string. The {len} parameter is optional; the length is 10 if not specified. This message changes the value of the string.',
53
53
  'gen_base64 ({len}) — Set the value of the string to a newly generated, random base64 string. The {len} parameter is optional; the length is 12 if not specified. This message changes the value of the string.',
54
54
  'trim — Strip whitespace from the beginning and end of the string. This message changes the value of the string. It will have the trimmed string.',
55
- 'sub ({from} {to}) — Substitute the first occurrence of {from} with {to}. Both parameters are required. This message changes the value of the string. It will have the result.',
56
- 'gsub ({from} {to}) — Substitute all occurrences of {from} with {to}. Both parameters are required. This message changes the value of the string. It will have the result.',
57
- 'split ({from} {to}) — Get the substring from index {from} up to (not including) index {to}. Indexes are 0-based; out-of-range indexes are clamped to the start or end of the string. Both parameters are required. Does not change the value of the string. It will have the substring.',
55
+ 'sub ({from} {to}) — Substitute the first occurrence of {from} with {to}. Both parameters are required. Either can be a group: sub ( ( \'a\' + \'b\' ) \'x\' ). This message changes the value of the string. It will have the result.',
56
+ 'gsub ({from} {to}) — Substitute all occurrences of {from} with {to}. Both parameters are required. Either can be a group: gsub ( \' \' ( \'_\' + \'_\' ) ). This message changes the value of the string. It will have the result.',
57
+ 'split ({from} {to}) — Get the substring from index {from} up to (not including) index {to}. Indexes are 0-based; out-of-range indexes are clamped to the start or end of the string. Both parameters are required. Either can be a group: split ( n ( n + 3 ) ). Does not change the value of the string. It will have the substring.',
58
58
  'splitl ({index}) — Get the substring to the left of index {index} (same as split (0, {index})). A parameter is required. Does not change the value of the string. It will have the substring.',
59
59
  'splitr ({index}) — Get the substring from index {index} to the end of the string (same as split ({index}, size)). A parameter is required. Does not change the value of the string. It will have the substring.',
60
- 'split_list ({delim} {dst.path}) — Split the string by {delim} and put the parts into children of the container at {dst.path} (or an alias that points to one), one part per child, in order. Existing children are matched by position and have their values set; extra parts get new (untyped) children, numbered from 1; extra existing children are left alone. Both parameters are required. Does not change the value of the string. It will be true (also when the string is empty), or false if a parameter or the target is bad.',
60
+ 'split_list ({delim} {dst.path}) — Split the string by {delim} and put the parts into children of the container at {dst.path} (or an alias that points to one), one part per child, in order. Existing children are matched by position and have their values set; extra parts get new (untyped) children, numbered from 1; extra existing children are left alone. Both parameters are required. {delim} can be a group; {dst.path} in parentheses is a computed path: split_list ( \',\' ( \'parts.\' n ) ). Does not change the value of the string. It will be true (also when the string is empty), or false if a parameter or the target is bad.',
61
61
  'word_wrap ({width}) — Word-wrap the string to {width} columns, breaking on whitespace (never mid-word; an overlong word is left on its own line rather than broken). The {width} parameter is optional; it defaults to the terminal\'s current width. This message changes the value of the string. It will have the wrapped string.',
62
62
  'page — Show the value in a pager (less), for viewing long content a screen at a time.'
63
63
  ]
@@ -119,22 +119,18 @@ module Gloo
119
119
  #
120
120
  def msg_sub
121
121
  return @engine.heap.it.set_to( '' ) unless value
122
- if @params&.token_count&.positive?
123
- expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.first ] )
124
- from = expr.evaluate
125
- expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.last ] )
126
- to = expr.evaluate
122
+ return false unless params_given?( 2 )
127
123
 
128
- result = value.sub(from, to)
129
- @engine.heap.it.set_to result
130
- set_value(result)
131
- return result
132
- else
133
- # Error
134
- @engine.syntax_err MISSING_PARAM_MSG
135
- @engine.heap.it.set_to false
136
- return false
137
- end
124
+ from, ok = param_value( 0 )
125
+ return false unless ok
126
+
127
+ to, ok = param_value( 1 )
128
+ return false unless ok
129
+
130
+ result = value.sub( from.to_s, to.to_s )
131
+ @engine.heap.it.set_to result
132
+ set_value( result )
133
+ return result
138
134
  end
139
135
 
140
136
  #
@@ -143,23 +139,18 @@ module Gloo
143
139
  #
144
140
  def msg_gsub
145
141
  return @engine.heap.it.set_to( '' ) unless value
142
+ return false unless params_given?( 2 )
146
143
 
147
- if @params&.token_count&.positive?
148
- expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.first ] )
149
- from = expr.evaluate
150
- expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.last ] )
151
- to = expr.evaluate
144
+ from, ok = param_value( 0 )
145
+ return false unless ok
152
146
 
153
- result = value.gsub(from, to)
154
- @engine.heap.it.set_to result
155
- set_value(result)
156
- return result
157
- else
158
- # Error
159
- @engine.syntax_err MISSING_PARAM_MSG
160
- @engine.heap.it.set_to false
161
- return false
162
- end
147
+ to, ok = param_value( 1 )
148
+ return false unless ok
149
+
150
+ result = value.gsub( from.to_s, to.to_s )
151
+ @engine.heap.it.set_to result
152
+ set_value( result )
153
+ return result
163
154
  end
164
155
 
165
156
  #
@@ -169,22 +160,17 @@ module Gloo
169
160
  #
170
161
  def msg_split
171
162
  return @engine.heap.it.set_to( '' ) unless value
163
+ return false unless params_given?( 2 )
172
164
 
173
- if @params&.token_count&.positive?
174
- expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.first ] )
175
- from = expr.evaluate.to_i
176
- expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.last ] )
177
- to = expr.evaluate.to_i
165
+ from, ok = param_value( 0 )
166
+ return false unless ok
178
167
 
179
- result = clamped_substring( from, to )
180
- @engine.heap.it.set_to result
181
- return result
182
- else
183
- # Error
184
- @engine.syntax_err MISSING_PARAM_MSG
185
- @engine.heap.it.set_to false
186
- return false
187
- end
168
+ to, ok = param_value( 1 )
169
+ return false unless ok
170
+
171
+ result = clamped_substring( from.to_i, to.to_i )
172
+ @engine.heap.it.set_to result
173
+ return result
188
174
  end
189
175
 
190
176
  #
@@ -245,17 +231,16 @@ module Gloo
245
231
  #
246
232
  def msg_split_list
247
233
  return @engine.heap.it.set_to( true ) unless value
234
+ return false unless params_given?( 2 )
248
235
 
249
- if @params&.token_count.to_i < 2
250
- @engine.syntax_err MISSING_PARAM_MSG
251
- @engine.heap.it.set_to false
252
- return false
253
- end
236
+ delim, ok = param_value( 0 )
237
+ return false unless ok
254
238
 
255
- expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.first ] )
256
- delim = expr.evaluate
239
+ # The target is a path; a group works out to a computed path.
240
+ path = param_path( 1 )
241
+ return false unless path
257
242
 
258
- target = split_list_target( @params.tokens.last )
243
+ target = split_list_target( path )
259
244
  return false unless target
260
245
 
261
246
  parts = value.split( delim )
@@ -298,20 +283,17 @@ module Gloo
298
283
  # into 'it'. Does not change the string's value.
299
284
  #
300
285
  def msg_index_of
301
- unless @params&.token_count&.positive?
302
- @engine.syntax_err MISSING_PARAM_MSG
303
- @engine.heap.it.set_to false
304
- return false
305
- end
286
+ return false unless params_given?( 1 )
306
287
 
307
- expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.first ] )
308
- needle = expr.evaluate.to_s
288
+ needle, ok = param_value( 0 )
289
+ return false unless ok
309
290
 
310
291
  from = 0
311
- if @params.token_count > 1
312
- expr = Gloo::Expr::Expression.new( @engine, [ @params.tokens.last ] )
313
- from = expr.evaluate.to_i
292
+ if param_groups.length > 1
293
+ from, ok = param_value( 1 )
294
+ return false unless ok
314
295
  end
296
+ from = from.to_i
315
297
 
316
298
  str = value.to_s
317
299
  if from.negative? || from > str.length
@@ -320,7 +302,7 @@ module Gloo
320
302
  return false
321
303
  end
322
304
 
323
- result = str.index( needle, from ) || -1
305
+ result = str.index( needle.to_s, from ) || -1
324
306
  @engine.heap.it.set_to result
325
307
  return result
326
308
  end