Mgamerz

Untitled

May 8th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. MODULE the_tape
  2.   INTEGER, ALLOCATABLE :: tape(:), transitions(:, :)
  3.   INTEGER :: left_edge, right_edge, tape_head_position, tape_size
  4. END MODULE
  5.  
  6. MODULE states_and_transitions
  7.   INTEGER :: accept_state, current_state, current_transition, cardinality_alphabet
  8. END MODULE  
  9.  
  10.  
  11. program turing_machine_simulator
  12.  
  13.   USE the_tape
  14.   USE states_and_transitions
  15.  
  16.   INTEGER :: next_, write_, read_  
  17.   CHARACTER ::  move_
  18.  
  19.   CHARACTER(100) :: tape_prototype, arg
  20.  
  21.   INTEGER :: cardinality_Q
  22.  
  23.   external handle_sigint ! Must declare as external
  24.  
  25.   call get_command_argument(1, arg)
  26.  
  27.   OPEN(UNIT = 12, FILE=arg)
  28.   READ(12,*) cardinality_Q
  29.   READ(12,*) cardinality_alphabet
  30.   cardinality_alphabet = cardinality_alphabet + 1
  31.  
  32.   allocate(transitions(3, cardinality_alphabet * cardinality_Q))
  33.   do 1 i=1, cardinality_alphabet * cardinality_Q
  34.     READ(12,*) next_, write_, move_
  35.     transitions(1:3, i) = (/ next_ + 1, write_, merge(1, -1, move_=='R') /)
  36.   1 continue
  37.  
  38.   READ(12, *) tape_prototype
  39.   WRITE(*,*) tape_prototype
  40.  
  41.   CLOSE(12)
  42.  
  43.   allocate(tape(len(tape_prototype)))
  44.   do 2 i=1, len(tape_prototype)
  45.     tape(i) = IACHAR(tape_prototype(i:i)) - IACHAR('0')
  46.   2 continue
  47.  
  48.   call SIGNAL(2, handle_sigint)
  49.  
  50.   current_state = 1
  51.   accept_state = cardinality_Q + 1
  52.   tape_head_position = 1
  53.  
  54.   left_edge = 1
  55.   right_edge = len(trim(tape_prototype))
  56.   tape_size = right_edge
  57.  
  58.   call run
  59.  
  60.   call print_tape
  61.  
  62.   deallocate(tape)
  63.   deallocate(transitions)  
  64. end program
  65.  
  66.  
  67. SUBROUTINE run
  68.   USE the_tape
  69.   USE states_and_transitions
  70.  
  71.   INTEGER :: read_
  72.   read_ = 0
  73.   current_transition = 1
  74.  
  75.   do while(current_state .NE. accept_state)
  76.  
  77.     if (tape_head_position .LT. left_edge) then
  78.       left_edge = left_edge - 1
  79.     elseif (tape_head_position .GT. right_edge) then
  80.       right_edge = right_edge + 1
  81.     endif
  82.  
  83.     if (left_edge .LT. 1 .OR. right_edge .GT. tape_size) call grow_tape
  84.     read_ = tape(tape_head_position)
  85.    
  86.     current_transition = ((current_state - 1) * cardinality_alphabet) + 1 + read_
  87.     tape(tape_head_position) = transitions(2, current_transition)
  88.     tape_head_position = tape_head_position + transitions(3, current_transition)
  89.     current_state = transitions(1, current_transition)
  90.  
  91.   end do
  92. end SUBROUTINE
  93.  
  94.  
  95. SUBROUTINE print_tape
  96.   USE the_tape
  97.   do 10 i=left_edge, right_edge - 1 !Index by one, and this is inclusive
  98.     WRITE(*,*) tape(i)
  99.   10 continue
  100.   print *, ''
  101.   return
  102. end SUBROUTINE
  103.  
  104.  
  105. SUBROUTINE handle_sigint
  106.   USE the_tape
  107.   print *, 'Received sigint--bailing.'
  108.   call print_tape
  109.   deallocate(tape)
  110.   deallocate(transitions)  
  111.   stop
  112. end SUBROUTINE
  113.  
  114.  
  115. SUBROUTINE grow_tape
  116.   USE the_tape
  117.   INTEGER :: offset
  118.   INTEGER, ALLOCATABLE :: temp(:)
  119.  
  120.   print *, 'Growing every time', tape_size
  121.   offset = tape_size / 2
  122.  
  123.   allocate(temp(tape_size * 2))
  124.  
  125.   temp = 0 !sets everything to zero in the array
  126.   left_edge = left_edge + offset
  127.   right_edge = right_edge + offset
  128.   tape_head_position = tape_head_position + offset
  129.  
  130.   temp(left_edge:right_edge) = tape(1:tape_size)
  131.   tape = temp
  132.   tape_size = tape_size * 2
  133.   deallocate(temp)
  134.   return
  135. end SUBROUTINE
Advertisement
Add Comment
Please, Sign In to add comment