SHARE
TWEET

Untitled

a guest Jul 1st, 2011 581 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @ECHO off
  2. setlocal ENABLEDELAYEDEXPANSION ENABLEEXTENSIONS
  3. rem Глобальные переменные
  4. rem Набор выводимых символов. Символы, вывод которых вызывал ошибки исполнения cmd заменены символом "."
  5. set char=".#$...'()*+,-./0123456789.....?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]._`abcdefghijklmnopqrstuvwxyz{.}~_"
  6. rem Размер maxmem можно поставить 30000 ячеек памяти, но тогда инициализация идет очень долго.
  7. set maxmem=100
  8.  
  9. echo Brainfuck Interpreter
  10. echo.
  11.  
  12. IF "%1" NEQ "" GOTO :init
  13. echo Usage: %~nx0 file.bf
  14. goto :eof
  15.  
  16. :init
  17. rem Инициализируем память.
  18. set /a __maxindex=%maxmem% - 1
  19. for /l %%i in (0,1,%__maxindex%) do set mem_%%i=0
  20. set mp=0
  21. set sp=0
  22. set cp=0
  23.  
  24. rem читаем программу в память
  25. set work_file=%1
  26. set bf_prog=
  27. FOR /F "eol=c delims=*" %%I IN (%work_file%) DO SET bf_prog=!bf_prog!%% class="re2">I
  28. rem узнаем длину программы
  29. Echo.%bf_prog%>"%TEMP%\%~n0.tmp"
  30. For %%i In ("%TEMP%\%~n0.tmp") Do Set /A bf_len=%%~zi-2
  31. rem Выводим тест, что программа работает, а не зависла.
  32. echo Executing programm
  33. echo %bf_prog%
  34. echo.
  35.  
  36. rem рабочий цикл
  37. :work
  38. set cop=!bf_prog:~% class="re2">cp%,1!
  39. rem call :debug
  40.  
  41. if "%cop%" == "+" (
  42.   set tmp=!mem_% class="re2">mp%!
  43.   set /a tmp += 1
  44.   if !tmp!==256 set tmp=0
  45.   set mem_%mp%=!tmp!
  46. ) else if "%cop%" == "-" (
  47.   set tmp=!mem_% class="re2">mp%!
  48.   set /a tmp -= 1
  49.   if !tmp! LSS 0 set tmp=255
  50.   set mem_%mp%=!tmp!
  51. ) else if "%cop%" == ")" (
  52.   set /a mp +=1
  53.   if !mp! == % class="re2">maxmem% set mp=0
  54. ) else if "%cop%" == "(" (
  55.   set /a mp -=1
  56.   if !mp! == -1 set /a mp=%maxmem% - 1
  57. ) else if "%cop%" == "," (
  58.   call :comma
  59. ) else if "%cop%" == "." (
  60.   set tmp=!mem_% class="re2">mp%!
  61.   call :Echochr !tmp!
  62. ) else if "%cop%" == "[" (
  63.   set tmp=!mem_% class="re2">mp%!
  64.   if !tmp!==0 (
  65.     call :skip1
  66.   )
  67. ) else if "%cop%" == "]" (
  68.   set tmp=!mem_% class="re2">mp%!
  69.   if !tmp! NEQ 0 (
  70.     call :skip2
  71.   )
  72. )
  73.  
  74. set /a cp += 1
  75. if %cp% GEQ %bf_len% goto :exit
  76. goto :work
  77.  
  78. :skip1
  79. :w11
  80.     set /a cp += 1
  81.     if %cp% == %bf_len% (
  82.       call :err_print "] not found"
  83.       exit /b 0
  84.     )
  85.     set cop=!bf_prog:~% class="re2">cp%,1!
  86.     if "%cop%" == "[" (set /a sp +=1)
  87.     if "%cop%" == "]" if %sp% NEQ 0 (set /a sp -=1) else (goto :w12)
  88.     goto :w11
  89. :w12
  90. exit /b 0
  91.  
  92. :skip2
  93. :w21
  94.     set /a cp -= 1
  95.     if "%cp%" LSS "0" (
  96.       call :err_print "[ not found"
  97.       exit /b 0
  98.     )
  99.     set cop=!bf_prog:~% class="re2">cp%,1!
  100.     if "%cop%" == "]" (set /a sp +=1)
  101.     if "%cop%" == "[" if %sp% NEQ 0 (set /a sp -=1) else (goto :w22)
  102.     goto :w21
  103. :w22
  104. exit /b 0
  105.  
  106. :comma
  107. rem Не реализованно
  108. exit /b 0
  109.  
  110. rem ==========================================================================
  111. rem Процедура echochr
  112. rem Эмуляция функции chr()
  113. rem ==========================================================================
  114. :echochr
  115. if %1==10 echo.
  116. if %1==13 echo.
  117. if %1==32 <nul set /p strTemp="_"
  118. if %1 GTR 32 (
  119. set /a code=%1 - 32
  120. for /f %%t in ('cmd /c "echo %%char:~!code!,1%%"') do <nul set /p strTemp="%%t"
  121. )
  122. exit /b 0
  123.  
  124.  
  125. rem ==========================================================================
  126. rem Процедура debug
  127. rem Печать состояния переменных и памяти
  128. rem ==========================================================================
  129. :debug
  130. set tmp=!mem_% class="re2">mp%!
  131. echo cp=%cp%, mp=%mp%, cop=%cop%, mem[mp]=%tmp%
  132. exit /b 0
  133. rem ==========================================================================
  134.  
  135.  
  136. rem ==========================================================================
  137. rem Процедура EchoWithoutCrLf
  138. rem %1 : текст для вывода.
  139. rem ==========================================================================
  140. :EchoWithoutCrLf
  141. <nul set /p strTemp="%~1"
  142. exit /b 0
  143. rem ==========================================================================
  144.  
  145.  
  146. rem ==========================================================================
  147. rem Процедура err_print
  148. rem Печать кода ошибки. Создание дампов работы.
  149. rem ==========================================================================
  150. :err_print
  151. echo Error on %cp% position.
  152. echo %1
  153. echo.
  154. echo cp: %cp% >register.dmp
  155. echo mp: %mp% >>register.dmp
  156. echo sp: %sp% >>register.dmp
  157. set /a __maxindex=%maxmem% - 1
  158. if exist memory.dmp del memory.dmp
  159. for /l %%i in (0,1,%__maxindex%) do echo !mem_%% class="re2">i! >>memory.dmp
  160. exit /b 0
  161. rem ==========================================================================
  162.  
  163. :exit
  164. set /a __maxindex=%maxmem% - 1
  165. if exist memory.dmp del memory.dmp
  166. for /l %%i in (0,1,%__maxindex%) do echo !mem_%% class="re2">i! >>memory.dmp
  167. echo.
  168. ENDLOCAL
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top