Class: Rules::VM

Inherits:
Object
  • Object
show all
Defined in:
lib/rules.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



592
593
594
# File 'lib/rules.rb', line 592

def data
  @data
end

#data_stackObject (readonly)

Returns the value of attribute data_stack.



593
594
595
# File 'lib/rules.rb', line 593

def data_stack
  @data_stack
end

#ipObject (readonly)

Returns the value of attribute ip.



596
597
598
# File 'lib/rules.rb', line 596

def ip
  @ip
end

#limit_stackObject (readonly)

Returns the value of attribute limit_stack.



594
595
596
# File 'lib/rules.rb', line 594

def limit_stack
  @limit_stack
end

#progObject (readonly)

Returns the value of attribute prog.



595
596
597
# File 'lib/rules.rb', line 595

def prog
  @prog
end

Instance Method Details

#haltObject



629
630
631
# File 'lib/rules.rb', line 629

def halt
  @thread.terminate unless @thread.nil?
end

#runObject



633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
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
# File 'lib/rules.rb', line 633

def run
  Thread.abort_on_exception = true
  @thread = Thread.new do
    @ip = 0 
    while @ip < @prog.length do
      @cls.send(@mth, @debug[@ip]) unless @cls.nil?
      case @prog[@ip] 
        when :push
          @ip += 1; @data_stack.push @prog[@ip]
        when :call
          @call_stack.push @ip + 2
          @ip = @prog[@ip + 1]
          next
        when :ret
          @ip = @call_stack.pop
          next
        when :dup
          @data_stack.push @data_stack.last
        when :swap
          a = @data_stack.pop
          b = @data_stack.pop
          @data_stack.push a
          @data_stack.push b
        when :plus
          a = @data_stack.pop
          b = @data_stack.pop
          @data_stack.push b + a
        when :minus
          a = @data_stack.pop
          b = @data_stack.pop
          @data_stack.push b - a
        when :div
          a = @data_stack.pop
          b = @data_stack.pop
          @data_stack.push b / a
        when :mul
          a = @data_stack.pop
          b = @data_stack.pop
          @data_stack.push b * a
        when :great
          a = @data_stack.pop
          b = @data_stack.pop
          @data_stack.push b > a
        when :low
          a = @data_stack.pop
          b = @data_stack.pop
          @data_stack.push b < a
        when :ge
          a = @data_stack.pop
          b = @data_stack.pop
          @data_stack.push b >= a
        when :gl
          a = @data_stack.pop
          b = @data_stack.pop
          @data_stack.push b <= a
        when :equ
          a = @data_stack.pop
          b = @data_stack.pop
          @data_stack.push b == a
        when :dpush
          @ip += 1
          @data_stack.push @data[@prog[@ip]]
        when :dpop
          @ip += 1
          @data[@prog[@ip]] = @data_stack.pop 
        when :je
          a = @data_stack.pop
          a = (a > 0) if a.kind_of? Fixnum or a.kind_of? Float
          if a 
            @ip = @prog[@ip + 1] 
          else
            @ip += 2
          end
          next
        when :jne
          a = @data_stack.pop
          a = (a > 0) if a.kind_of? Fixnum or a.kind_of? Float
          unless a  
            @ip = @prog[@ip + 1]
          else
            @ip += 2
          end
          next
        when :jmp
          @ip = @prog[@ip + 1]
          next
        when :stop
          break
        when :ruby
          a = @data_stack.pop
          begin
            send(a)
          rescue Exception => e
            puts e
          end    
        when :lim
          @limit_stack.push @data_stack.length
        when :lcp
          a = @data_stack.length
          b = @limit_stack.pop
          result = a <= b
          @limit_stack.push b unless result
          @data_stack.push result
      end
      @ip += 1
    end
  end
end

#set_debug_callback(classp, mth) ⇒ Object



742
743
744
745
# File 'lib/rules.rb', line 742

def set_debug_callback(classp, mth)
  @cls = classp
  @mth = mth
end