æ°ããã°ã©ãã³ã°è¨èªãã´ãªã¹ãã¼ã¹ããå ¬éãã¾ãã
éé·é¡ã®ä¸ã§ãç¹ã«ãªã©ã³ã¦ã¼ã¿ã³ã»ãã³ãã³ã¸ã¼ã»ãããã»ã´ãªã©ã®ç¥è½ã¯é«ãã人éã®5æå ç¨åº¦ã«ç¸å½ããããç¨åº¦ã¯æ½è±¡çãªæèãããªãããã¨ãåãã£ã¦ãã¾ããè¨èã«é¢ãã¦ãæ話ãæããã³ãã³ã¸ã¼ãã´ãªã©ã®è©±ã¯æåã§ãããã
ä¸ã§ãã´ãªã©ã«ã¤ãã¦ã¯å®éã«æ£®ã«ä½ãã´ãªã©ã¨äººéãè¨èã§ããåãããããã¨ããå¯è½ã§ã京é½å¤§å¦ã®山極寿一ææã¯10種é¡ã»ã©ã®ã´ãªã©èªã話ããã¨ãã§ããã¨ããã¾ããããèããã¨å½¼ãã®çºã®ããã°ã©ãã³ã°è¨èªããã£ã¦ãããããããã¾ãããããããããç¡ããã¨ã®æ¹ãããããæããã¾ãã
ããã§ç§ã¯ã´ãªã©ç¨ããã°ã©ãã³ã°è¨èªãã´ãªã¹ãã¼ã¹ããéçºãã¾ããã*1
- ã½ã¼ã¹ã³ã¼ããµã³ãã«
ã¦ããããã¦ããããã¦ãããã¦ããããã¦ããããã¦ãã¦ããããããã¦ãããããã¦ããã¦ãã¦ããã¼ã¤ã¦ããã¦ãããã¼ã¤ãã¦ããããã¦ãã¦ãããããã¦ããã¦ãããããã¦ãããã¦ãããããã¦ãããã¦ãããã¦ããã¦ãã¦ããããã¦ãããã¼ã¤ã¦ããããã¦ããã¼ã¤ã¦ããããã¦ããã¦ãããããã¦ãããããã¦ãã¦ãããã¦ããããã¦ãããã¦ããããããã¦ããããã¦ãããã¦ãããã¦ãããã¼ã¤ãã¦ãããããã¦ããããã¼ã¤ã¦ããã¦ãããã¦ãããããã¦ããã¦ãããã¦ãããããã¦ãããããã¦ããããã¦ãããããã¦ãããã¦ããããã¦ãããã¦ããããã¼ã¤ãã¦ããã¦ãã¼ã¤ã¦ããããã¦ãã¦ããããã¦ããããã¦ãããã¦ãããã¦ãããã¦ãããã¦ããããã¦ãããã¦ãããã¦ãããã¦ãããã¼ã¤ã¦ãããããã¦ãã¼ã¤ã¦ãããã¦ãããã¦ãããããã¦ããããã¦ããããã¦ãããããã¦ãããã¦ãããããã¦ããã¦ããããã¦ããã¦ããããã¼ã¤ãã¦ããããããã¦ãããã¼ã¤ãã¦ããã¦ãã¦ãããã¦ããããã¦ãã¦ããããããã¦ãã¦ãããã¦ãããã¦ãããããã¦ããããã¦ãããããã¦ããããã¼ã¤ãã¦ãããã¦ãããã¼ã¤ã¦ããã¦ããããã¦ãã¦ãããããã¦ãã¦ããã¦ããããã¦ããã¦ãããã¦ãããããã¦ãããããã¦ãããã¦ããããã¼ã¤ã¦ãããããã¦ããã¼ã¤ã¦ããããã¦ãããããã¦ããã¦ãããããã¦ããããã¦ãããã¦ããããããã¦ãããã¦ãã¦ãããã¦ããããããã¦ããããã¦ãã¼ã¤ã¦ãããã¦ãããã¼ã¤ãã¦ããã¦ããããã¦ããã¦ããããã¦ããããã¦ããããã¦ããã¦ããã¦ãããããã¦ããã¦ãããã¦ããã¦ãã¼ã¤ã¦ãããã¦ãã¼ã¤ãã¦ãã¦ããããã¦ããã¦ããã¦ãããã¦ããã¦ããããã¦ããã¦ãããã¦ããã¦ãããã¦ãã¦ããã¼ã¤ãã¦ããããããã¦ãã¼ã¤ã¦ãã¦ããã¦ãããã¦ããããã¦ãã¦ãããã¦ããããã¦ããã¦ãããããã¦ããããã¦ãããã¦ãããããã¦ããã¼ã¤ãã¦ãããããã¦ããããã¼ã¤ã¦ãããããã¦ããã¦ãããã¼ã¤ã¦ãã¼ã¤ãã¦ããããã¼ã¤
$ bin/gorispace samples/helloworld.gs Hello World
ã¦ãã¦ãã¦ããããã¦ããã¦ãããã¦ãããã¦ããããã¦ãããã¦ããã¦ããã¦ããããã¼ã¤ãã¦ããã¦ãã¼ã¤ã¦ããããã¦ãã¦ãããããã¦ãã¦ãã¦ãããããã¦ããããããã¦ããã¦ãããããã¦ããããã¦ããããã¦ããã¦ãã¼ã¤ãã¦ãããããã¦ããããã¼ã¤ãã¦ããããã¦ããã¦ããã¦ãããã¦ãããã¦ããããã¦ããããã¦ããããããã¦ãããã¦ããããããã¦ãããããã¦ããããã¦ãã¼ã¤ãã¦ãããããã¦ãã¼ã¤ã¦ãã¦ããã¦ãããã¦ããã¦ãã¦ãããããã¦ãããã¦ãããããã¦ãã¦ãããã¦ãã¦ãã¼ã¤ã¦ãããã¦ãã¼ã¤ã¦ããã¦ããããã¦ãããã¦ãããããã¦ããããã¦ãããããã¦ããããããã¦ããã¦ããããããã¦ããã¦ããããã¦ãããããã¦ãã¼ã¤ã¦ãããã¦ããã¼ã¤ã¦ãããã¦ããããã¦ããããã¦ããããã¦ããããã¦ããã¦ããããã¦ãããããã¦ãã¦ãããã¦ããããã¦ããã¦ãããã¼ã¤ãã¦ããããã¦ããããã¼ã¤ã¦ãããããã¦ãã¦ããã¦ãã¦ããã¦ãããããã¦ããããããã¦ãããã¦ãããããã¦ããã¦ããããã¦ãã¦ãããã¼ã¤ã¦ããããã¦ããã¼ã¤ã¦ãããã¦ãã¦ãããã¦ãããããã¦ããã¦ãããããã¦ããããããã¦ãããããã¦ãããã¦ããããã¦ãã¦ãããã¦ããã¼ã¤ãã¦ããããã¦ããã¼ã¤ã¦ãããããã¦ãããã¦ãã¦ãããã¦ããããã¦ããããã¦ããã¦ãããããã¦ãããã¦ããããããã¦ãããã¦ããã¼ã¤ã¦ãããããã¦ãã¼ã¤ã¦ããã¦ããã¦ããã¦ãããããã¦ãããããã¦ãããããã¦ãããããã¦ãããã¦ããããã¦ããããã¦ãã¦ãããã¼ã¤ã¦ãããã¦ãã¼ã¤ã¦ãããã¦ããããã¦ããã¦ããããã¦ããããã¦ãããããã¦ãããã¦ããã¼ã¤ã¦ããã¦ããããã¼ã¤ã¦ãããã¦ããã¦ãããã¦ãããããã¦ãããããã¦ãããã¦ãã¼ã¤ãã¦ãããããã¦ãããããã¦ãããã¦ãããããã¦ããã¼ã¤ã¦ããã¦ãã¼ã¤ã¦ãããã¦ãããããã¦ããããã¼ã¤ã¦ãããã¦ãããããã¦ããã¦ããã¦ãã¦ããããããã¦ãããã¦ããããã¦ãããã¦ãããã¼ã¤ã¦ããã¦ããã¼ã¤ã¦ãããããã¦ããããã¦ãã¼ã¤ãã¦ããã¦ãããã¦ãããã¦ãã¼ã¤ãã¦ããã¦ããã¼ã¤ã¦ãããã¦ãããããã¦ããããã¦ããã¦ãããã¦ãããã¼ã¤ã¦ããã¦ãã¼ã¤ã¦ãããããã¦ããããããã¦ããããã¦ããã¦ãããã¦ãã¦ããããã¦ãã¦ããããã¦ããããã¦ããã¦ããããã¦ããã¼ã¤ã¦ããããã¦ããããããã¦ããããããã¦ããããã¦ãããã¼ã¤ã¦ããããã¦ãããã¦ããã¼ã¤ã¦ãããã¦ãããã¦ããã¼ã¤ã¦ããã¦ããããã¦ããã¦ãã¦ãããã¦ãããããã¦ããã¦ãããã¦ããããã¦ãããã¼ã¤ã¦ãããã¦ãããã¼ã¤ã¦ããããã¦ããã¦ããã¦ãããããã¦ããã¦ããã¦ãããã¦ããããã¼ã¤ã¦ããããã¦ãããããã¦ãããããã¦ãããã¦ããããã¦ããã¦ãããã¦ããããã¼ã¤ã¦ãããã¦ããããã¦ããã¦ãããã¦ãã¦ãã¼ã¤ã¦ããããã¦ããããã¦ãããã¦ãããããã¦ããã¦ãããããã¦ããã¼ã¤ã¦ããããã¦ããã¼ã¤ã¦ããããã¦ããã¦ãããããã¦ããã¦ãããã¼ã¤ãã¦ããããããã¦ããããããã¦ããããããã¦ããããã¦ããã¼ã¤ãã¦ãã¼ã¤ã¦ãããããã¦ããã¼ã¤ãã¦ããããã¦ãã¼ã¤ãã¦ãããã¼ã¤ã¦ããããã¦ããã¦ããããã¦ãã¦ããããã¼ã¤ã¦ãã¼ã¤ã¦ãããã¼ã¤ã¦ãã¼ã¤
$ bin/gorispace samples/fibonacci.gs How many? 5 1 1 2 3 5 8 13
ã
ãã®ä»ã®ãµã³ãã«
http://github.com/technohippy/gorispace/tree/master/samples
- è¨èªè§£èª¬
ã´ãªã¹ãã¼ã¹ã¯ã¹ã¿ãã¯ãã¼ã¹ã®ããã°ã©ãã³ã°è¨èªã§ãIMP(Instruction Modification Parameter)ãã³ãã³ãããã©ã¡ã¼ã¿ã®3ã¤çµã§ä¸ã¤ã®å½ä»¤ã表ç¾ãã¾ãã
IMP | æå³ |
---|---|
ã¦ã | ã¹ã¿ãã¯æä½ |
ã¦ããã¦ã | æ´æ°æ¼ç® |
ã¦ããã¦ãã | ãã¼ãã¢ã¯ã»ã¹ |
ã¦ãã¼ã¤ | ããã¼å¶å¾¡ |
ã¦ããã¦ãã¼ã¤ | å ¥åºå |
ã¹ã¿ãã¯æä½ | IMP: ã¦ã | |
---|---|---|
ã³ãã³ã | ãã©ã¡ã¼ã¿ | æå³ |
ã¦ã | æ°å¤ | æ°å¤ãã¹ã¿ãã¯ã«ããã·ã¥ |
ã¦ãã¼ã¤ã¦ã | - | ã¹ã¿ãã¯ããããè¤è£½ |
ã¦ãã¼ã¤ã¦ãã | - | ã¹ã¿ãã¯ã®1çªç®ã¨2çªç®ã交æ |
ã¦ãã¼ã¤ã¦ãã¼ã¤ | - | ã¹ã¿ãã¯ããããç ´æ£ |
æ´æ°æ¼ç® | IMP: ã¦ããã¦ã | |
---|---|---|
ã³ãã³ã | ãã©ã¡ã¼ã¿ | æå³ |
ã¦ãã¦ã | - | ã¹ã¿ãã¯ã®ä¸ããäºã¤ã足ãç® |
ã¦ãã¦ãã | - | ã¹ã¿ãã¯ã®ä¸ããäºã¤ãå¼ãç® |
ã¦ãã¦ãã¼ã¤ | - | ã¹ã¿ãã¯ã®ä¸ããäºã¤ãæãç® |
ã¦ããã¦ã | - | ã¹ã¿ãã¯ã®ä¸ããäºã¤ãå²ãç® |
ã¦ããã¦ãã | - | ã¹ã¿ãã¯ã®ä¸ããäºã¤ã§å°ä½ |
ãã¼ãã¢ã¯ã»ã¹ | IMP: ã¦ããã¦ãã | |
---|---|---|
ã³ãã³ã | ãã©ã¡ã¼ã¿ | æå³ |
ã¦ã | - | å¤ãã¢ãã¬ã¹ã«æ ¼ç´ |
ã¦ãã | - | ã¢ãã¬ã¹ããå¤ãã¹ã¿ãã¯ã« |
ããã¼å¶å¾¡ | IMP: ã¦ãã¼ã¤ | |
---|---|---|
ã³ãã³ã | ãã©ã¡ã¼ã¿ | æå³ |
ã¦ãã¦ã | ã©ãã« | ã©ãã«å®ç¾© |
ã¦ãã¦ãã | ã©ãã« | ãµãã«ã¼ãã³å¼ã³åºã |
ã¦ãã¦ãã¼ã¤ | ã©ãã« | ç¡æ¡ä»¶ã¸ã£ã³ã |
ã¦ããã¦ã | ã©ãã« | ã¹ã¿ãã¯ããããã¼ããªãã¸ã£ã³ã |
ã¦ããã¦ãã | ã©ãã« | ã¹ã¿ãã¯ããããè² ãªãã¸ã£ã³ã |
ã¦ããã¦ãã¼ã¤ | - | ãµãã«ã¼ãã³çµäº |
ã¦ãã¼ã¤ã¦ãã¼ã¤ | - | ããã°ã©ã çµäº |
å ¥åºå | IMP: ã¦ããã¦ãã¼ã¤ | |
---|---|---|
ã³ãã³ã | ãã©ã¡ã¼ã¿ | æå³ |
ã¦ãã¦ã | - | ã¹ã¿ãã¯ãããã®æåãåºå |
ã¦ãã¦ãã | - | ã¹ã¿ãã¯ãããã®æ°å¤ãåºå |
ã¦ããã¦ã | - | æåãèªã¿è¾¼ã¿ã¢ãã¬ã¹ã«æ ¼ç´ |
ã¦ããã¦ãã | - | æ°å¤ãèªã¿è¾¼ã¿ã¢ãã¬ã¹ã«æ ¼ç´ |
æ°å¤ | æ°å¤ã¯äºé²æ°ã§è¡¨ããã¦ã ã 0ãã¦ãã ã 1ãã¦ãã¼ã¤ ãçµç«¯è¨å· |
---|---|
ã©ãã« | ã©ãã«ã¯ ã¦ã 㨠ã¦ãã ã®åã§è¡¨ç¾ãããã¦ãã¼ã¤ ãçµç«¯è¨å· |
ãªãããããã®é£ç¶ã¯ãããã¨ãã¦æ±ããããã¦ããããããããã¼ããã¤ã以å¤ã®æåã¯å
¨ã¦ç¡è¦ããã¾ãã
ã¨ãããã¨ã§ãããããåãã£ã¦ãã¨æããã©ãã´ãªã¹ãã¼ã¹ã¯è¦ããã«Whitespaceã®äºç¨®ã§ãï¼Brainf*ckãå ã«ãã¦ãããã£ããã ãã©ãããã ã¨ã¾ãã¾Ook!ã«ãªã£ã¦ãã¾ãã®ã§æ¢ãã¦Whitespaceãé¸æï¼ãWhitespaceã®åãã¼ã¯ã³ï¼ã¨ã®å¯¾å¿ã¯æ¬¡ã®ãããªæãã
Whitespace | Gorispace |
---|---|
[Space] | ã¦ã |
[Tab] | ã¦ãã |
[LF] | ã¦ãã¼ã¤ |
http://github.com/technohippy/gorispace/tree/master/src/gorispace.rb
$KCODE='u' require 'strscan' @ops = { /AA([AB]+)(C)/ => :stack_push, /ACA/ => :stack_copy, /ABA([AB]+)(C)/ => :stack_copynth, /ACB/ => :stack_swap, /ACC/ => :stack_discard, #/ABC([AB]+)(C)/ => :stack_slide, /BAAA/ => :arithmetic_add, /BAAB/ => :arithmetic_sub, /BAAC/ => :arithmetic_mul, /BABA/ => :arithmetic_div, /BABB/ => :arithmetic_mod, /BBA/ => :heap_store, /BBB/ => :heap_restore, /CAA([AB]+)C/ => :flow_mark, /CAB([AB]+)C/ => :flow_call, /CAC([AB]+)C/ => :flow_jump, /CBA([AB]+)C/ => :flow_zero, /CBB([AB]+)C/ => :flow_nega, /CBC/ => :flow_return, /CCC/ => :flow_exit, /BCAA/ => :io_outchar, /BCAB/ => :io_outnum, /BCBA/ => :io_readchar, /BCBB/ => :io_readnum } @instructions = [] @stack = [] @heap = {} @marks = {} @call_stack = [] def scan(code) scanner = StringScanner.new(code) until scanner.eos? converted = false @ops.keys.each do |key| if scanner.scan(key) command = @ops[key] param = scanner[1] if scanner[2] sign = param[0] == ?A ? 1 : -1 abs = 0 param[1..-1].each_byte do |c| abs <<= 1 abs |= 1 if c == ?B end param = sign * abs end @instructions << [command, param] @marks[param] = @instructions.size - 1 if command == :flow_mark converted = true break end end break unless converted end end def execute pc = 0 while pc < @instructions.size old_stack_size = @stack.size old_heap_size = @heap.size command, param = *@instructions[pc] case command when :stack_push @stack.push(param) when :stack_copy @stack.push(@stack.last) when :stack_copynth @stack.push(@stack[param]) when :stack_swap val1 = @stack.pop val2 = @stack.pop @stack.push(val1) @stack.push(val2) when :stack_discard @stack.pop #when :stack_slide when :arithmetic_add val1 = @stack.pop val2 = @stack.pop @stack.push(val2 + val1) when :arithmetic_sub val1 = @stack.pop val2 = @stack.pop @stack.push(val2 - val1) when :arithmetic_mul val1 = @stack.pop val2 = @stack.pop @stack.push(val2 * val1) when :arithmetic_div val1 = @stack.pop val2 = @stack.pop @stack.push(val2 / val1) when :arithmetic_mod val1 = @stack.pop val2 = @stack.pop @stack.push(val2 % val1) when :heap_store val = @stack.pop addr = @stack.pop @heap[addr] = val when :heap_restore addr = @stack.pop @stack.push(@heap[addr]) when :flow_mark # do nothing when :flow_call @call_stack.push(pc) pc = @marks[param] when :flow_jump pc = @marks[param] when :flow_zero pc = @marks[param] if @stack.pop == 0 when :flow_nega pc = @marks[param] if @stack.pop < 0 when :flow_return pc = @call_stack.pop when :flow_exit exit(0) when :io_outchar print(@stack.pop.chr) when :io_outnum print(@stack.pop) when :io_readchar @heap[@stack.pop] = $stdin.readline[0] when :io_readnum @heap[@stack.pop] = $stdin.readline.to_i end @stack.compact! pc += 1 end end def gorispace(code) code = code.gsub(/[^ã¦ããã¼ã¤]/, '').gsub(/ã+/, 'ã').gsub(/ã¦ãã¼ã¤/, 'C').gsub(/ã¦ãã/, 'B').gsub(/ã¦ã/, 'A').gsub(/[^ABC]/, '') scan(code) execute end if __FILE__ == $0 unless ARGV.empty? gorispace(ARGF.read) else puts('filename required') end end
http://d.hatena.ne.jp/technohippy/20090119#1232380298
*1:ãªãããªã©ã³ã¦ã¼ã¿ã³ç¨ããã°ã©ãã³ã°è¨èªã¨ãã¦ã¯ãOok!ã¨ããè¨èªããã§ã«åå¨ãã¾ã