Die Programmiersprache Ruby

Blog| Forum| Wiki  

Diese Seite zeigt Implementierungen von Ook!

Interpreter Implementierung von closure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Ook 
@@i = 0;@@m = Array.new(30000){0};@@mp = 0 
@@symbols = {'.?' => [:inc_ptr,lambda{|i,mem| i + 1}], 
 '?.' => [:dec_ptr,lambda{|i,mem| i - 1}], 
'..' => [:inc_val,lambda{|i,mem| mem[i] +=1; i }], 
'!!' => [:dec_val,lambda{|i,mem| mem[i] -=1; i }], 
'!.' => [:out,lambda{|i,mem| $><< mem[i].chr; 
        $>.flush; i }], 
'.!' => [:in,lambda{|i,mem| mem[i] = $<.getc; i}], 
'!?' => [:jz,lambda{|i,mem| mem[i] == 0 }], 
'?!' => [:jnz,lambda{|i,mem| mem[i] != 0}] 
}.freeze  
def self.[](file) 
c=IO.read(file).chomp.scan(/Ook([\.\?!]) +Ook([\.\?!])/). 
compact.collect{|i|@@symbols[i.join][0]} 
until @@i >= c.size 
if c[@@i] == :jz 
if @@symbols.values.assoc(c[@@i])[1].call(@@mp,@@m) 
@@i+=1;s=1 
begin(s += 1 and next) if c[@@i] == :jz; 
(s -= 1 and next) if c[@@i] == :jnz 
@@i += 1 end until @@i > c.size || s == 0 
end; @@i +=1 and next elsif c[@@i] == :jnz 
if @@symbols.values.assoc(c[@@i])[1].call(@@mp,@@m) 
@@i -= 1;s = 1 
begin(s -= 1 and next) if c[@@i] == :jz 
(s += 1 and next) if c[@@i] == :jnz 
@@i -= 1 end until @@i < 0 || s == 0 
end; @@i+=1 and next else 
@@mp = @@symbols.values.assoc(c[@@i])[1].call(@@mp,@@m) 
@@i += 1 end end end end


Konverter Implementierung von closure:

1
2
3
4
5
6
7
8
9
10
s = {'.?' => 'i+=1;', 
        '?.' => 'i-=1;', 
        '..' => 'm[i]+=1;', 
        '!!' => 'm[i]-=1;', 
        '!.' => '$><<m[i].chr;', 
        '.!' => 'm[i]=$<.getc;', 
        '!?' => 'while(m[i]!=0);', 
        '?!' => 'end;' 
     } 
eval 'm=[0]*30000;i=0;' << IO.read($*[0]).scan(/Ook([\.\?!]) *Ook([\.\?!])/).collect{|i| s[i.join]}.join