@@ -50,7 +50,7 @@ grammar GenerativeSyntax
5050 end
5151
5252 rule set
53- set_scale:(prefix_scale)? '(' ':'? content:(set_items) repeat_two:(':')? repeat_times:(integer)? ')' p:(('~' / '?') [1-9]*)? t:(transform)? a:(array_method)? o:(operation)* eval_f:(ternary)? select_range:(set_range)?
53+ set_scale:(prefix_scale)? '(' ':'? content:(set_items) repeat_two:(':')? repeat_times:(integer)? ')' p:(('~' / '?') [1-9]*)? a:(array_method)? o:(operation)* t:(transform)? eval_f:(ternary)? select_range:(set_range)?
5454 {
5555 def value
5656 reps = repeat_times.value if repeat_times.methods.include?(:value)
@@ -127,7 +127,8 @@ grammar GenerativeSyntax
127127 o.elements.each do |op|
128128 if op.methods.include?(:value) then
129129 op_values = ZiffArray.new(op.value.flatten.compact) if op.value.is_a?(Array)
130- case op.operator
130+ op_operator = op.operator(set_id)
131+ case op_operator
131132 when '**', '*', '/', '+', '-', '^', '%', '|', '&', '<<', '>>'
132133 if op.value.kind_of?(Array)
133134 set = op_values.map do |z|
@@ -138,14 +139,14 @@ grammar GenerativeSyntax
138139 if z and c
139140 if z.is_a?(Hash) and c.is_a?(Hash)
140141 if z[:pc] and c[:pc]
141- c_dup[:pc] = z [:pc].method(op.operator ).(c [:pc])
142+ c_dup[:pc] = c [:pc].method(op_operator ).(z [:pc])
142143 elsif z[:hpcs] and c[:pc]
143- c_dup[:hpcs] = z_dup[:hpcs].map {|h| h = Marshal.load(Marshal.dump(h)) ; h[:pc] = h [:pc].method(op.operator ).(c [:pc]); h}
144+ c_dup[:hpcs] = z_dup[:hpcs].map {|h| h = Marshal.load(Marshal.dump(h)) ; h[:pc] = c [:pc].method(op_operator ).(h [:pc]); h}
144145 c_dup.delete(:pc)
145146 elsif c[:hpcs] and z[:pc]
146- c_dup[:hpcs] = c[:hpcs].map {|h| h = Marshal.load(Marshal.dump(h)) ; h[:pc] = h[:pc].method(op.operator ).(z[:pc]); h}
147+ c_dup[:hpcs] = c[:hpcs].map {|h| h = Marshal.load(Marshal.dump(h)) ; h[:pc] = h[:pc].method(op_operator ).(z[:pc]); h}
147148 elsif c[:hpcs] and z[:hpcs]
148- c_dup[:hpcs] = c_dup[:hpcs].map {|ch| z[:hpcs].map{|zh| zh = Marshal.load(Marshal.dump(zh)) ; zh[:pc] = zh [:pc].method(op.operator ).(ch [:pc]) ; zh }}.flatten
149+ c_dup[:hpcs] = c_dup[:hpcs].map {|ch| z[:hpcs].map{|zh| zh = Marshal.load(Marshal.dump(zh)) ; zh[:pc] = ch [:pc].method(op_operator ).(zh [:pc]) ; zh }}.flatten
149150 end
150151 end
151152 end
@@ -161,9 +162,9 @@ grammar GenerativeSyntax
161162 v = Marshal.load(Marshal.dump(v))
162163 if v.is_a?(Hash)
163164 if v[:pc]
164- v[:pc] = v[:pc].to_i.method(op.operator ).(operation_value)
165+ v[:pc] = v[:pc].to_i.method(op_operator ).(operation_value)
165166 elsif v[:hpcs]
166- v[:hpcs] = v[:hpcs].map {|h| h = Marshal.load(Marshal.dump(h)) ; h[:pc] = h[:pc].method(op.operator ).(operation_value); h }
167+ v[:hpcs] = v[:hpcs].map {|h| h = Marshal.load(Marshal.dump(h)) ; h[:pc] = h[:pc].method(op_operator ).(operation_value); h }
167168 end
168169 end
169170 v
@@ -177,19 +178,21 @@ grammar GenerativeSyntax
177178 set = set-op_values if op_values
178179 when '<*>'
179180 if op_values
180- set = set.ois.map do |v|
181- op_values.map do |z|
182- z = Marshal.load(Marshal.dump(z))
183- if z[:hpcs] and v.is_a?(Array)
184- z[:hpcs] = v.map {|ival| z[:hpcs].map {|h| ZiffHash[{pc: h[:pc]+ival}] }}.flatten
185- elsif z[:pc] and v.is_a?(Array)
186- z[:hpcs] = v.map {|ival| ZiffHash[{pc: z[:pc]+ival}] }
187- z.delete(:pc)
188- elsif z[:hpcs] and v.is_a?(Integer)
189- z[:hpcs] = z[:hpcs].map {|h| h[:pc] = h[:pc]+v ; h }
190- else
191- z[:pc] = z[:pc]+v
192- end
181+ set = op_values.ois.map do |v|
182+ set.map do |z|
183+ if z.is_a?(Hash)
184+ z = Marshal.load(Marshal.dump(z))
185+ if z[:hpcs] and v.is_a?(Array)
186+ z[:hpcs] = v.map {|ival| z[:hpcs].map {|h| ZiffHash[{pc: h[:pc]+ival}] }}.flatten
187+ elsif z[:pc] and v.is_a?(Array)
188+ z[:hpcs] = v.map {|ival| ZiffHash[{pc: z[:pc]+ival}] }
189+ z.delete(:pc)
190+ elsif z[:hpcs] and v.is_a?(Integer)
191+ z[:hpcs] = z[:hpcs].map {|h| h[:pc] = h[:pc]+v ; h }
192+ else
193+ z[:pc] = z[:pc]+v
194+ end
195+ end
193196 z
194197 end
195198 end
@@ -423,10 +426,18 @@ grammar GenerativeSyntax
423426 end
424427
425428 rule operation
426- op:('**' / '*' / '+' / '-' / '/' / '^' / '%' / '|' / '&' / '<<' / '>>' / '#' / '@' / '<@>' / '<&>' / '<->' / '<+>' / '<*>' / '<|>') val:(min_max / set / array)
427- {
428- def operator
429- op.text_value
429+ op:(cyclic_operators / '**' / '*' / '+' / '-' / '/' / '^' / '%' / '|' / '&' / '<<' / '>>' / '#' / '@' / '<@>' / '<&>' / '<->' / '<+>' / '<*>' / '<|>') val:(min_max / set / array)
430+ {
431+ def operator(set_id)
432+ if op.methods.include?(:value)
433+ reps = (Thread.current[:tshared][:set_repeat_index] || 0)
434+ operation_cycle_index = (Thread.current[:tshared][:operation_cycle_index] || 0) + 1
435+ Thread.current[:tshared][:operation_cycle_index] = operation_cycle_index
436+ val = op.value
437+ val = resolve_cycle(val,"OP"+(reps>0?("R"+set_id.to_s):operation_cycle_index.to_s),reps) if val.is_a?(Hash) and val[:cycle]
438+ else
439+ op.text_value
440+ end
430441 end
431442 def value
432443 if val.methods.include?(:value)
@@ -439,6 +450,18 @@ grammar GenerativeSyntax
439450 }
440451 end
441452
453+ rule cyclic_operators
454+ '<' ' '* first:(operator) following:(' ' i:operator)+ ' '* '>'
455+ {
456+ def value
457+ r = following.elements.map {|v| v.i.value }
458+ r.prepend(first.value)
459+ r = r.compact
460+ {:cycle=>r}
461+ end
462+ }
463+ end
464+
442465 rule parens
443466 '[' parens_subset ']'
444467 {
0 commit comments