å ãã¿ã¯Wandboxã«Vim scriptã追å ããããã¨ã§ããï¼ Wandboxãå®è¡ããã³ãã³ãã©ã¤ã³ããã¯ãã°ï¼ã·ã§ã«ä¸ã§Vim scriptãå®è¡ã§ãããã ã¨èããï¼ ãã ï¼ãªãã·ã§ã³ã®æ°ãå¤ãï¼ãã¡ãã¡æã¡è¾¼ãã®ã¯é¢åãªã®ã§ï¼ã·ã§ã«ã¹ã¯ãªããã¨ããããã¡ã¤ã«ãä½ã£ãï¼ vsã¨ãããã¼ãã³ã°ã¯ï¼æ£ç´å¾®å¦ãªæ°ããããï¼Javascriptã®ã¨ã³ã¸ã³ã®ã²ã¨ã¤ã§ããSpiderMonkeyã®ãã¤ããªåãçä¼¼ãçµæã ï¼ ããã«ï¼ååãçãæ¹ãæã¡ãããã¨ããå©ç¹ãããã¨æãï¼
ä½æããã«ããã£ã¦ï¼å°ã è¦å´ããç¹ãããï¼ Vimã®åºåãå ¨ã¦æ¨æºã¨ã©ã¼åºåã ã£ããï¼ã³ãã³ãããã³ããã¯ã¯ã¤ã«ãã«ã¼ããå±éããªãï¼ã¨ããç¹ã ï¼
#!/bin/sh vim=vim if [ $# -lt 1 ]; then echo 'Invalid arguments' 1>&2 echo '[USAGE]' echo ' vs SRC [ARGS...]' exit 1 fi if [ -f $1 ]; then $vim -X -N -u NONE -i NONE -e --cmd "source $1 | qall!" $@ 2>&1 else echo "File not found: $1" 1>&2 fi
@echo off setlocal ENABLEDELAYEDEXPANSION set vim=vim set src=%1 set argv=%1 shift :LOOPSTART if "%~1" == "" ( goto LOOPEND ) for %%i in (%1) do ( set argv=!argv! %%i ) shift goto LOOPSTART :LOOPEND if "%src%" == "" ( echo Invalid arguments 1>&2 echo [USAGE] echo vs SRC [ARGS...] ) else ( if exist %src% ( %vim% -X -N -u NONE -i NONE -e --cmd "source %src% | qall!" %argv% 2>&1 ) else ( echo File not found: %src% 1>&2 ) )
æåã®åé¡ã¯ï¼2>&1
ã¨ãã¦ï¼æ¨æºã¨ã©ã¼åºåãæ¨æºå
¥åã«æµããã¨ã§ç°¡åã«è§£æ±ºããï¼
å¾è
ã®åé¡ã¯ï¼é å¼µã£ã¦ã«ã¼ããåããã¨ã§ï¼ã¯ã¤ã«ãã«ã¼ããå±éããï¼
ããããã¡ã¤ã«ã®ææ³ãï¼ã«ã¼ãå
ã§ã®ä»£å
¥ã¯ã¯ã»ããã£ã¦ï¼ãªããªãè¦å´ãããã®ã ...ï¼
ï¼ããããã¡ã¤ã«ã«ã¤ãã¦ã¯å
¨ãèªä¿¡ãç¡ãã®ã§ï¼ããã³ãã©ããã¯æºè¼ã ã¨æãï¼
使ãæ¹
GitHubã®ãªãã¸ããªã«æ¸ãã¦ããéãï¼ãã¹ã®éã£ãã¨ããã«ï¼vs
ï¼ãããã¯vs.bat
ãç½®ãã¦ï¼å®è¡ãããVim scriptãæå®ããã ãã ï¼
ä¾ãã°ï¼ä»¥ä¸ã®ãããªVim scriptãç¨æããï¼
" src.vim echo 'argc:' argc() echo 'argv:' argv() echo 'Hello World!'
ããã¦ï¼ä»¥ä¸ã®ããã«å®è¡ããï¼
$ vs src.vim argc: 1 argv: ['src.vim'] Hello World! $ vs src.vim apple banana cake 4 åã®ãã¡ã¤ã«ãç·¨éãæ§ãã¦ãã¾ã argc: 4 argv: ['src.vim', 'apple', 'banana', 'cake'] Hello World!
ãã ï¼ããã¤ãã®åé¡ãããï¼
ã¾ãï¼ã³ãã³ãã©ã¤ã³å¼æ°ãã¨ããããã«ãã¦ã¿ããã®ã®ï¼Vimã«å¼æ¸¡ãã¦ããã®ã§ï¼ãã¡ã¤ã«åã¨ãã¦æ¸¡ã£ã¦ãããã¨ã«ãªãï¼
ãã®ããï¼ã©ããã¦ãã4 åã®ãã¡ã¤ã«ãç·¨éãæ§ãã¦ãã¾ããã®ãããªã¡ãã»ã¼ã¸ãåºãã¨ãããï¼ãã¡ã¤ã«åã¨ãã¦èªèã§ããªãæååï¼ä¾ãã°ï¼+
ãªã©ï¼ã渡ããã¨ãã§ããªãç¹ãåé¡ã ï¼
ã¾ãï¼åºåããã¡ã¤ã«ã«ãªãã¤ã¬ã¯ãããã¨ãã«ï¼ç«¯æ«å¶å¾¡ç¨ã¨æãããæååãåºåããã¦ãã¾ãã®ãåé¡ã ï¼
æå¾ã«ï¼Vim scriptã§å®è£
ããBrainfuckå¦çç³»ãæ«é²ãããï¼
ãã®è¨äºã®å®è£
ã«å°ãæãå ããã ãã®ãã®ã ï¼
é£ç¶ãã><+-
ãã²ã¨ã¤ã«ã¾ã¨ãããï¼[-]
ããã¼ã代å
¥å½ä»¤ãã«ç½®ãæããã¨ãã軽度ã®æé©åãè¡ãããã«ãã¦ããï¼
let s:Brainfuck = { \ 'pc': 0, \ 'dc': 0, \ 'bytecode': [], \ 'buf': repeat([0], 65536) \} function! s:Brainfuck.add(val) abort dict let self.buf[self.dc] += a:val endfunction function! s:Brainfuck.sub(val) abort dict let self.buf[self.dc] -= a:val endfunction function! s:Brainfuck.next(val) abort dict let self.dc += a:val endfunction function! s:Brainfuck.prev(val) abort dict let self.dc -= a:val endfunction function! s:Brainfuck.putchar(val) abort dict echon nr2char(self.buf[self.dc]) endfunction function! s:Brainfuck.getchar(val) abort dict let self.buf[self.dc] = getchar() endfunction function! s:Brainfuck.loop_start(val) abort dict if self.buf[self.dc] != 0 | return | endif let self.pc += 1 let c = 0 while c > 0 || self.bytecode[self.pc].func !=# 'loop_end' if self.bytecode[self.pc].func ==# 'loop_start' | let c += 1 elseif self.bytecode[self.pc].func ==# 'loop_end' | let c -= 1 | endif let self.pc += 1 endwhile endfunction function! s:Brainfuck.assign(val) abort dict let self.buf[self.dc] = a:val endfunction function! s:Brainfuck.loop_end(val) abort dict let self.pc -= 1 let c = 0 while c > 0 || self.bytecode[self.pc].func !=# 'loop_start' if self.bytecode[self.pc].func ==# 'loop_end' | let c += 1 elseif self.bytecode[self.pc].func ==# 'loop_start' | let c -= 1 | endif let self.pc -= 1 endwhile let self.pc -= 1 endfunction function! s:Brainfuck.compile(src) abort dict let inst_table = { \ '+': 'add', \ '-': 'sub', \ '>': 'next', \ '<': 'prev', \ '.': 'putchar', \ ',': 'getchar', \ '[': 'loop_start', \ ']': 'loop_end', \ '[-]': 'assign', \} let src = substitute(a:src, '[^+-><\[\]\.,]', '', 'g') let src = substitute(src, '\[-\]', '\=printf("{\"func\": \"%s\"# \"val\": 0}# ", inst_table[submatch(0)])', 'g') let src = substitute(src, '\%(+\+\|-\+\|>\+\|<\+\)', '\=printf("{\"func\": \"%s\"# \"val\": %d}# ", inst_table[submatch(0)[0]], len(submatch(0)))', 'g') let src = substitute(src, '\%(\[\|\]\|\.\|,\)', '\=printf("{\"func\": \"%s\"# \"val\": 0}# ", inst_table[submatch(0)])', 'g') let src = substitute(src, '#', ',', 'g') let self.bytecode = eval('[' . src . ']') endfunction function! s:Brainfuck.run() abort dict let b = self.bytecode let l = len(self.bytecode) while self.pc < l call self[b[self.pc].func](b[self.pc].val) let self.pc += 1 endwhile endfunction function! s:main() abort let start_time = reltime() call s:Brainfuck.compile( \ '+++++++++[>++++++++>+++++++++++>+++++<<<-]>.>++.+++++++..+++.>-.' \ . '------------.<++++++++.--------.+++.------.--------.>+.' \) call s:Brainfuck.run() echomsg '[Brainfuck] execute time:' reltimestr(reltime(start_time)) endfunction call s:main()
ãããå®è¡ããã¨ï¼ç¡äºHello, World!ã表示ãããï¼
$ vs brainfuck.vim Hello, world! [Brainfuck] execute time: 0.002001
ãã®ã·ã§ã«ã¹ã¯ãªããã¨ããããã¡ã¤ã«ã¯ï¼ãEmacsã¦ã¼ã¶ã ãã©ï¼Vim scriptãæ¸ãã¦å®è¡ããããã¨ãã人ã®å½¹ã«ç«ã¤ã¯ãã ï¼ï¼ï¼ ã¾ãï¼ç«¶æããã°ã©ãã³ã°ãã¡ãã£ã¨ããããã°ã©ãã³ã°ã®åé¡ï¼ä¾ãã°ï¼å°ãåã«è©±é¡ã«ãªã£ããã½ããã¦ã§ã¢ã¨ã³ã¸ãã¢ãªãã°1æé以å ã«è§£ããªããã°ãããªã5ã¤ã®åé¡ãï¼ãªã©ãVim scriptã§å®è£ ãï¼å®è¡ããã®ã楽ã«ãªãã ããï¼ï¼ï¼ï¼ï¼