649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
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
|
# File 'lib/eggshell/bundles/basics-old.rb', line 649
def process(buffer, macname, args, lines, depth)
p0 = args.is_a?(Array) ? args[0] : nil
lines ? lines.delete('') : ''
macname = macname.to_sym
st = @state[depth]
if !@state[depth]
st = {:type => macname}
@state[depth] = st
@state[depth+1] = nil
end
if macname == :for || macname == :loop
p0 = p0 || {}
st[:var] = p0['var']
st[:start] = p0['start']
st[:stop] = p0['stop']
st[:step] = p0['step'] || 1
st[:iter] = p0['items'] || 'items'
st[:item] = p0['item'] || 'item'
st[:counter] = p0['counter'] || 'counter'
st[:raw] = p0['raw']
st[:collect] = p0['collect']
st[:agg_block] = p0['aggregate_block']
mbuff = []
looper = nil
loop_is_map = false
if macname == :for
st[:item] = st[:var]
looper = Range.new(st[:start], st[:stop]).step(st[:step]).to_a
elsif macname == :loop
begin
looper = st[:iter].is_a?(Array) && st[:iter][0].is_a?(Symbol) ? @proc.expr_eval(st[:iter]) : st[:iter]
looper = nil if !looper.respond_to?(:each)
loop_is_map = looper.is_a?(Hash)
rescue
end
end
collector = []
if looper
counter = 0
looper.each do |i1, i2|
val = nil
if loop_is_map
@proc.vars[st[:counter]] = i1
val = i2
else
val = i1
@proc.vars[st[:counter]] = counter
end
@proc.vars[st[:item]] = val.is_a?(Array) && val[0].is_a?(Symbol) ? @proc.expr_eval(val) : val
if st[:collect]
lines.each do |_line|
if _line.is_a?(String)
collector << @proc.expand_expr(_line)
elsif _line.is_a?(Eggshell::Block)
_line.process(collector)
end
end
else
process_lines(lines, buffer, depth + 1, st[:raw])
end
break if st[:break]
counter += 1
end
end
if collector.length > 0
if st[:collect] == 'aggregate'
if st[:agg_block]
collector.unshift(st[:agg_block])
end
process_lines(collector, buffer, depth + 1, false)
else
buffer << collector.join("\n")
end
end
@state[depth] = nil
elsif macname == :while
raw = args[1]
while @proc.expr_eval(p0)
process_lines(lines, buffer, depth + 1, raw)
break if st[:break]
end
elsif macname == :if || macname == :elsif || macname == :else
cond = p0
st[:if] = true if macname == :if
if st[:cond_count] == nil || macname == :if
st[:cond_count] = 0
st[:cond_eval] = false
st[:cond_met] = false
end
last_action = st[:last_action]
st[:last_action] = macname.to_sym
if !st[:if] || (macname != :else && !cond)
return
end
if macname != :else
if !st[:cond_eval]
cond_struct = Eggshell::ExpressionEvaluator.struct(cond)
st[:cond_eval] = @proc.expr_eval(cond_struct)[0]
end
else
st[:cond_eval] = true
end
if st[:cond_eval] && !st[:cond_met]
st[:cond_met] = true
process_lines(lines, buffer, depth + 1)
end
elsif macname == :break
lvl = p0 || 1
i = depth - 1
while i >= 0
st = @state[i]
i -= 1
next if !st
if st[:type] == :for || st[:type] == :while || st[:type] == :loop
lvl -= 1
st[:break] = true
break if lvl <= 0
end
end
elsif macname == :next
lvl = p0 || 1
i = depth - 1
while i >= 0
st = @state[i]
i -= 1
next if !st
if st[:type] == :for || st[:type] == :while || st[:type] == :loop
lvl -= 1
st[:next] = true
break if lvl <= 0
end
end
end
end
|