Advertisement
T3RRYT3RR0R

Esolang BatchStax v1.4

Dec 17th, 2021 (edited)
1,226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 16.55 KB | None | 0 0
  1. @Echo off
  2. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  3. REM Esolang: BatchStax by T3RRY ; loosely based off isdo. See syntax for valid operations.
  4. REM This script is an interpreter for BatchStax, and includes limited debugging tools and error checking.
  5. :::
  6. ::: Version 1.4
  7. REM VERSION Change 21/12/2021: Added LIMITED support for Nested loops - See updated Syntax.
  8. REM                            Added
  9. REM                            Added option of calling interpreter with a filepath containing source code.
  10. REM                             instead of calling with Command line arguments. The interpreter will pause
  11. REM                             and wait for a keypress after each line has executed.
  12. REM                             ** Note:
  13. REM                              Each line is treated as a new script. stack values and rVal do not persist.
  14. REM VERSION Change 20/12/2021: Added simple looping / repetition - See updated Syntax.
  15. REM VERSION Change 19/12/2021: Now accepts Input argument to predefine stack value / stack array values and
  16. REM                            assign initial stack. - See updated Syntax
  17. REM                            - Removed Virtual terminal Color command 'E', replaced with
  18. REM                              'B' [background] and added 'F' [foreground]
  19. REM VERSION Change 18/12/2021: New Virtual terminal features:
  20. REM                            - Cursor left right up down
  21. REM                            - Background color change
  22. REM Creative Commomns License 4.0
  23. REM https://creativecommons.org/licenses/by-nc-sa/4.0/
  24. ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
  25.  
  26.  Set "CL=%*"
  27.  Set "_=%*"
  28.  
  29.  set "_" 2>&1 | %systemroot%\system32\findstr.exe /li "\/? -? help syntax defined" > nul && (
  30.   Mode 1000
  31.   Call :Syntax
  32.   Goto:Eof
  33.  )
  34.  
  35.  If exist "%~1" (
  36.   For /f "usebackq Delims=" %%G in ("%~1")Do (
  37.    Setlocal
  38.    Call "%~f0" %%G
  39.    If Errorlevel 1 (
  40.     Echo Error in Sourcefile "%~1"
  41.     Pause
  42.     Exit /b
  43.    )
  44.    Endlocal
  45.   )
  46.   Exit /b
  47.  )
  48.  
  49. Mode 1000 & Cls (Title )
  50.  
  51. Rem add a value to the below devmode variable definition to track operations during runtime, or use switch /D
  52.  
  53.  Set "devmode="
  54.  
  55. Rem Define backspace to permit output of whitespace without newline.
  56.  For /F %%# in ('"Prompt;$H;&For %%_ in (1) Do Rem"') Do Set "\b=%%#"
  57.  
  58. :# Windows Version control for VT sequences. Assigns flag true if system is windows 10 build GTR 10586
  59. :# https://en.wikipedia.org/wiki/ANSI_escape_code#DOS,_OS/2,_and_Windows
  60. :# Version 1511 build number = 10.0.10586
  61.  Set "Win10="
  62.  For /f "tokens=3 delims=." %%v in ('Ver')Do if %%v GTR 10586 Set "Win10=True"
  63.  
  64. :# If Win10 true ; Test if virtual terminal codes enabled ; enable if false
  65. :# removes win10 flag definition if version does not support Virtual Terminal sequences
  66. :# Reg values: https://devblogs.microsoft.com/commandline/understanding-windows-console-host-settings/
  67.  If defined Win10 (
  68.   Reg Query HKCU\Console | %SystemRoot%\System32\findstr.exe /LIC:"VirtualTerminalLevel    REG_DWORD    0x1" > nul || (
  69.     Reg Add HKCU\Console /f /v VirtualTerminalLevel /t REG_DWORD /d 1
  70.   ) > Nul && (
  71.     Echo(CMD restart required to enable Virtual terminal sequences.
  72.     Pause
  73.     EXIT
  74.   ) || Set "Win10="
  75.  )
  76.  
  77.  If not defined Win10 (
  78.   Echo( Some features of this interpreter are not avialable on your system.
  79.   Pause
  80.   Cls
  81.  )
  82.  If defined win10 For /f %%e in ('Echo Prompt $E^|cmd')Do Set "\E=%%e"
  83.  
  84. Setlocal EnableDelayedExpansion
  85.  Set "Spacing="
  86.  For /l %%i in (1 1 1000)Do Set "Spacing=!Spacing! "
  87.  
  88.  Set ^"_=!_:"=!"
  89.  
  90.  Set "_" | %SystemRoot%\System32\Findstr.exe /lic:"\/D" > nul && (
  91.   Set "_=!_: /D =!"
  92.   Set "_=!_:/D =!"
  93.   Set "_=!_: /D=!"
  94.   Set "_=!_:/D=!"
  95.   Set devmode=true
  96.  )
  97.  
  98. Rem catch use of /D switch without command list
  99.  If not defined _ Goto:Syntax
  100.  
  101. Set "Array?="
  102. Rem test {Array;Index} usage; assign Stack array and Index if valid syntax and trailing command list.
  103. Rem flag Syntax Errors.
  104.  If "!_:~0,1!"=="{" (
  105.   Set Array?=%CL:}=}&::%
  106.   If not "!_:;=!"=="!_!" (
  107.    If not "!_:}=!"=="!_!" (
  108.     For /f "tokens=1,2,3 delims={;}" %%G in ("!_!") Do (
  109.      Set "Stack=0"
  110.      For %%i in (%%G)Do (
  111.       Set /A "stack+=1,stack[!stack!]=%%i"
  112.      )
  113.      If not %%H GTR !Stack! (
  114.       If not %%H LSS 0 (
  115.        Set /A "Stack=%%H" 2> nul
  116.       )Else (
  117.        Echo Stack outside array boundary OR missing array / index in {arraylist;index} & Endlocal & Pause & Exit /b
  118.       )
  119.      ) Else (
  120.       Echo(Stack outside array boundary OR missing array / index in {arraylist;index} & Endlocal & Pause & Exit /b
  121.      )
  122.      If "%%I"=="" (
  123.       Echo(Command list Required & Endlocal & Pause & Exit /b
  124.      )
  125.      Set "_=%%I"
  126.     )
  127.     Set /A v=Stack[!stack!]
  128.    )Else (
  129.     Echo( Closing '}' missing in arg string.
  130.     Endlocal & Pause & Exit /B 1
  131.    )
  132.   )Else (
  133.    Echo( {array;index} sepererator ';' missing in arg string.
  134.    Endlocal & Pause & Exit /B 1
  135.   )
  136.  )
  137.  
  138. set "_=!_: =!"
  139. Set "ErrCounter=0"
  140. :BuildReps
  141.  
  142. Set /A ErrCounter += 1
  143.  
  144.  Set "Search=" & Set "Replace="
  145.  If not "!_:(=!"=="!_!" If not "!_:)=!"=="!_!" (
  146.   For /f "tokens=1,2,3 delims=(:)" %%G in ("!_!")Do Set /A "1/%%H" 2> nul && (
  147.    Set "Search=(%%G:%%H)"
  148.    For /L %%i in (1 1 %%H)Do (
  149.     Set "Replace=!Replace!%%G"
  150.     If not "!Replace:~3750,1!"=="" (
  151.      Echo Warning^^! Command line exceeds supported length.
  152.      Endlocal & Pause & Exit /B 1  
  153.     )
  154.    )
  155.   ) || Set /A "1/%%I" 2> nul && (
  156.    Set "Search=(%%H:%%I)"
  157.    For /L %%i in (1 1 %%I)Do (
  158.     Set "Replace=!Replace!%%H"
  159.     If not "!Replace:~3750,1!"=="" (
  160.      Echo Warning^^! Command line exceeds supported length.
  161.      Endlocal & Pause & Exit /B 1  
  162.     )
  163.    )
  164.   )
  165.  )
  166.  
  167.  If Defined Search If Defined Replace Set "_=!_:%Search%=%Replace%!"
  168.  
  169.  If not "!_:(=!"=="!_!" If not "!_:)=!"=="!_!" (
  170.   If !ErrCounter! GTR 512 (
  171.    Echo(Error -  Malformed use of (Commands:RepeatCount^) syntax in arg string:
  172.    Echo( "!_!"
  173.    Endlocal & Pause & Exit /B 1
  174.   )
  175.   Goto :BuildReps
  176.  )
  177.  
  178.  Set "_=!_: =!"
  179.  Set "_=!_:$=#A!"
  180.  
  181.  For %%G in (A B D F H I J N O P Q R S T W Z "+" "-" "@" "#" "[" "]" 1 2 4 6 7 8 ".")Do Set "_=!_:%%~G= %%~G!"
  182.  
  183.  Set "args=!_!"
  184.  
  185.  If not defined Stack set Stack=0
  186.  Set "C=Set/A"#"||Set v=0&"
  187.  If not defined v Set v=0
  188.  set r="v=( M=(v), x=M/(11*1024)+40, x=(M/x+x)>>1, x=(M/x+x)>>1, x=(M/x+x)>>1, x=(M/x+x)>>1, x=(M/x+x)>>1, x+=(M-x*x)>>31 )"
  189.  Set i=v+=1&Set d=v-=1&Set s=v*=v&Set z=v=0&Set h=v/=2&Set t=v*=2&Set .=v*=10
  190.  Set p="v=(v*3142)/1000"
  191.  >"%~f0:Dev.log" (
  192.   Echo(Command Line: !CL!
  193.   Echo(Parsed  CL  : !Array?!!args: =!
  194.  )
  195.  
  196.  Set "tops=0"
  197.  2>nul (
  198.   For %%i in (!args!)Do (
  199.    For %%v in (!Stack!)Do If "!stack[%%v]!"=="" Set /A stack[!stack!]=v+0
  200.    Set /A tops+=1
  201.    If /i "%%~i"=="A" (
  202.     Set /A "1/(v/32)","1/(126/v)" 2> nul && (
  203.      Cmd /C Exit !v!
  204.      <nul Set /p ^"=.%\b%!=ExitCodeASCII!"
  205.     )
  206.    )Else If /i "%%~i"=="N" (
  207.     Echo(
  208.    )Else If /i "%%~i"=="W" (
  209.     For %%v in (!v!)Do <nul set /p "=.%\b%!Spacing:~-%%v!"
  210.    )Else If /i "%%~i"=="2" (
  211.     If defined win10 <nul set /p=%\E%[B
  212.    )Else If /i "%%~i"=="4" (
  213.     If defined win10 <nul set /p=%\E%[D
  214.    )Else If /i "%%~i"=="6" (
  215.     If defined win10 <nul set /p=%\E%[C
  216.    )Else If /i "%%~i"=="8" (
  217.     If defined win10 <nul set /p=%\E%[A
  218.    )Else If /i "%%~i"=="1" (
  219.     If defined win10 <nul set /p=%\E%8
  220.    )Else If /i "%%~i"=="7" (
  221.     If defined win10 <nul set /p=%\E%7
  222.    )Else If /i "%%~i"=="B" (
  223.     If defined win10 <nul set /p "=%\E%[48;5;!v!m"
  224.    )Else If /i "%%~i"=="F" (
  225.     If defined win10 <nul set /p "=%\E%[38;5;!v!m"
  226.    )Else If /i "%%~i"=="J" (
  227.     If NOT defined win10 CLS
  228.     If defined win10 <nul set /p "=%\E%[2J"
  229.    )Else If /i "%%~i"=="+" (
  230.     Set /A stack+=1
  231.    )Else If /i "%%~i"=="-" (
  232.      Set /A stack-=1
  233.    )Else If /i "%%~i"=="@" (
  234.     Set /a stack[!stack!]=v
  235.    )Else If /i "%%~i"=="#" (
  236.     Set /a v=stack[!stack!]
  237.    )Else If /i "%%~i"=="]" (
  238.     Set /a v+=stack[!stack!]+0
  239.    )Else If /i "%%~i"=="[" (
  240.     Set /a v-=stack[!stack!]+0
  241.    )Else If /i "%%~i"=="Q" (
  242.     For %%v in (!Stack!)Do If !v!==!Stack[%%v]! (Echo 1)Else Echo 0
  243.    )Else (
  244.     Set/A!%%~i! 2>nul||(<nul Set /p"=!v!")
  245.    )
  246.    If defined devmode (
  247.     Set "Stackarray="&Set "outv=| Output: ``"
  248.     For %%v in (!stack!)Do (
  249.      if "!stack[%%v]!"=="" Set /A stack[!stack!]=v+0
  250.      For /f "delims=" %%G in ('Set Stack[')Do For /f "Tokens=2,3 delims=[]=" %%H in ("%%G")Do Set "stackarray=!stackarray!,%%I"
  251.      If /I "%%~i"=="A" (
  252.       Cmd /c Exit !v!
  253.       Set "outv=^| Output: '!=ExitcodeASCII!'"
  254.      )Else If /I "%%~i"=="O" (
  255.       Set "outv=^| Output: '!v!'"
  256.      )Else If /I "%%~i"=="N" Set "outv=| Output: '\n'"
  257.      >>"%~f0:Dev.log" Echo({Cmd: %%~i ; !tops! }    ^| Val:!v!  ^| Stack %%v: !Stack[%%v]!  !Outv!  ^| Stacks: !stackarray:~1!
  258.     )
  259.    )
  260.   )
  261.  )
  262.  
  263.  Title Press any key to continue
  264.  Pause > nul
  265.  (Title )
  266.  
  267.  Echo(
  268.  
  269.  Endlocal & Set "DevMode=%DevMode%" & Set "rVal=%v%"
  270.  
  271.  If defined DevMode (
  272.   For /f "Delims=" %%G in ('more ^< "%~f0:Dev.log"')Do Echo(%%G
  273.  )
  274.  
  275. Exit /b 0
  276.  
  277. :Syntax
  278.   Echo =============================================================================================================
  279.   Echo                                                                                          BatchStax by T3RRY
  280.   Echo       Switch Optional Input      Commands:
  281.   Echo( %~n0 [/D]   [{Arraylist;Index}]  $ A B D F H I J N O P Q R S T Z @ # [ ] + - 1 2 4 6 7 8
  282.   Echo(
  283.   Echo  %~n0 [/D] [{Arraylist;Index}] [(commands:RepeatCount)][commands]
  284.   Echo(
  285.   Echo  * [argument] denotes an optional argument - note: If used {Array;int} must preceede command list.
  286.   Echo  * Arraylist denotes a comma seperated list of initial stack values, followed by a ';' seperated Index[integer]
  287.   Echo    to declare the Stack location in which to commence. See examples below.
  288.   Echo  * (Commands:RepeatCount) ; where RepeatCount is an Integer: Repeat command group enclosed within parentheses
  289.   Echo    Integer times.
  290.   Echo      ** NOTE:
  291.   Echo         Nesting loops are now possible, however:
  292.   Echo          * Commands in nested loops Should not begin with 2 4 6 8 + -
  293.   Echo          * Loops are identified when the 1st and 2nd or 2nd and 3rd token of the command string delimited
  294.   Echo            by '(' ':' and ')' match the pattern: .*:Integer
  295.   Echo            If at any step during the parsing of the command argument there occurs a loop not matching this
  296.   Echo            pattern, particularly where the secon or third token is not an integer, the nested will be flagged
  297.   Echo            will be flagged as invalid.
  298.   Echo            
  299.   Echo         Stack position and rVal are not implicity modified when a loop is enacted; Commands must
  300.   Echo         be used to Ensure stack location and rVal have the values necessary to achieve expected output.
  301.   Echo          - note example 2, and how the final 2 commands are used to prepare for the next loop iteration.
  302.   Echo(
  303.   Echo  '/D' : switch ; Enables log output
  304.   Echo(
  305.   Echo  Arithmetic commands:
  306.   Echo  'I' + 1           'D' - 1
  307.   Echo  'T' * 2           'H' / 2
  308.   Echo  'P' * Pi          '.' * 10
  309.   Echo  'S' Square        'R' sqRoot
  310.   Echo  'Z' rVal = 0
  311.   Echo  * '[' and ']' operations are described in Stack based commands below.
  312.   Echo(
  313.   Echo  Virtual Terminal commands:
  314.   Echo  'J' Clear the screen from the current cursor position
  315.   Echo      ** If Virtual terminal unsupported, Clears the entire screen
  316.   Echo  'B' Backgound color = 48;5;rVal
  317.   Echo  'F' Foregound color = 38;5;rVal
  318.   Echo  '7' Save cursor position
  319.   Echo  '1' Restore cursor from saved position
  320.   Echo            '8' Cursor Up
  321.   Echo  '4' Cursor Left   '6' Cursor Right
  322.   Echo            '2' Cursor Down
  323.   Echo(
  324.   Echo  Output commands:
  325.   Echo  'Q' Test if rVal is EQU current stack Value. Outputs 0 if false 1 if true
  326.   Echo  'O' Output rVal as integer
  327.   Echo  'A' Output ASCII character for decimal value of rVal if within range: 32-126
  328.   Echo      - See: https://www.asciitable.com/
  329.   Echo  '$' Abbreviation of '#A'
  330.   Echo  'N' Output Newline
  331.   Echo  'W' Output rVal Spaces at the current cursor position.
  332.   Echo(
  333.   Echo  Stack based commands:
  334.   Echo  A note regarding Stacks:
  335.   Echo      - 'Stacks' in this script are a group of associated variables
  336.   Echo         that may be used as memory to store or retrieve values. Changing stack index using '+'
  337.   Echo         or '-' changes the variable being used as the storage / retrieval 'location'.
  338.   Echo      - Stacks are assigned the current rVal on creation, or the array values when {ArrayList:Index}
  339.   Echo        is used to predefine stacks.
  340.   Echo  '+' Push ; increment stack counter
  341.   Echo  '-' Pop  ; decrement stack counter
  342.   Echo  '@' Save current rVal to current stack index.
  343.   Echo  '#' Assign current stack value to rVal [retrieve]
  344.   Echo  ']' Add value stored in current stack to rVal
  345.   Echo      * if last operation was '@' or '+' creating a new stack, this is equivalent to doubling rVal
  346.   Echo  '[' Subtract value stored in current stack from rVal
  347.   Echo      * if last operation was '@' or '+' creating a new stack, this is equivalent to setting rVal to 0
  348.   Echo(
  349.   Echo  Example 1 - Print: "Hello World!"
  350.   Echo=   %~n0 ip@p+pd+]]-[a+]--]da+]ddaa-]][a++[[[iia]]ia]ddda--]a[[a+[iarpiia
  351.   Echo(
  352.   Echo  Example 2 - Print: "Hello World!" Three times with trailing newline
  353.   Echo=   %~n0 "(ip@p+pd+]]-[a+]--]da+]ddaa-]][a++[[[iia]]ia]ddda--]a[[a+[iarpiian-z:3)"
  354.   Echo(
  355.   Echo  Example 3 - Print "Hello World!" diagonally 0,0 to 11,11
  356.   Echo=   %~n0 ip@p+pd+]]-[a+]--]d2a+]dd2a2a-]][2a++[[[ii2a]]i2a]ddd2a--]2a[[2a+[i2arpii2a
  357.   Echo(
  358.   Echo  Example 4 - Print "COCA COLA!"
  359.   Echo=   %~n0 {33,65,67,79,32,76;2} $+$-$-$+++$--$+$++$----$-$
  360.   Echo(
  361.   Echo  Example 5 - Print: "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
  362.   Echo=   %~n0 {62,32;0} iiia+]a([-ia+]a:25)
  363.   Echo(
  364.   Echo  Nested loops are unofficially supported [Not thoroughly tested]. Examples:
  365.   Echo  Example 6 - nested loops - Print "GFEGFEHIJKHIJKHIJK"
  366.   Echo=   %~n0 {72;0} ((da:3)#:2)((ai:4)#:3)
  367.   Echo(
  368.   Echo  Example 7 - nested loop - Draw a box, emit newline, repeat:
  369.   Echo=   %~n0 {61,124;0} ((a:5)+#(24a:4)-#4a(44a:5)+#(84a:3)(n:5)-#:2)
  370.   Echo(
  371.   Echo  Example 8 - nested loop - Print all ASCII characters in blue from 32-123 as a column 5 characters wide
  372.   Echo                            change color to pink; move cursor to top of screen and print in another
  373.   Echo                            column in reverse order. Reset forground color to white at end of commandline.
  374.   Echo=   %~n0 {32,255;0} f((ai:5)n:19)(n88:19)666666f((da:5)n666666:19)+#f
  375.   Echo(
  376.   Echo =============================================================================================================
  377.   Timeout /t 1 /NoBreak > nul
  378.   Pause
  379.   Cls
  380.   Choice /N /C:YN /M "View example outputs Y/N?"
  381.   If %Errorlevel% EQU 1 (
  382.    For /f "tokens=2 Delims==" %%G in ('%SystemRoot%\System32\Findstr.exe /blic:"  Echo=" "%~f0"')Do (
  383.     Setlocal
  384.     Call call %%G
  385.     Endlocal
  386.    )
  387.   )
  388.   Exit/b
  389.  
  390. REM Advanced Usage Example: An extension of example 8. Repeats the output 3 times, however, between output: saves
  391. REM cursor position, return to screen home, clear the screen. After all iterations complete: restore cursor position
  392. REM %~n0 {32,255;0} (Jf((ai:5)n:19)(n88:19)666666f((da:5)n666666:19)+#f7-(n88:19)#:3)1
  393.  
  394. REM An example regarding Virtual terminal commands for cursor positioning:
  395. REM [almost] functionally equivalent:
  396. REM %~n0 {92,1;0}(a+#nwi@-#:15)
  397. REM %~n0 {92;0}(a2:15)
  398. REM Version 1: Uses inbuilt Spacer string via 'W' command to output rVal spaces, alternating between stacks 0 and 1 -
  399. REM            stack 0 contains the decimal value of ASCII character '\' which is retrieved at the end of each loop,
  400. REM            while stack 1 contains a count for spacings to use, which is retrieved, incremented and saved each loop.
  401. REM            Limitation: command line is longer, limiting the number of repetitions that can be built into the output.
  402.  
  403. REM Version 2: Uses only a single stack containing decimal value of ASCII character '\'
  404. REM            alternates between outputing '\' character and moving cursor down.
  405. REM            Limitations: Requires Windows 10 ver 10586 or newer. Cannot move cursor beyond bounds of terminal. Output will
  406. REM            continue horizontally along the last line of the terminal if boundary is encountered.
  407.  
  408. REM Both command lines Output diagonal line:
  409. REM \
  410. REM  \
  411. REM   \
  412. REM    \
  413. REM     \
  414. REM      \
  415. REM       \
  416. REM        \
  417. REM         \
  418. REM          \
  419. REM           \
  420. REM            \
  421. REM             \
  422. REM              \
  423. REM               \
  424.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement