Diese Seite zeigt Implementierungen von HQ9+
Interpreter Implementierung von cypher:
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#!/usr/bin/ruby -w
# HQ9++ Interpreter written in Ruby, by cypher
class HelloWorld
def execute
puts "Hello World!"
end
end
class Quine
attr_reader :source
def initialize source
@source = source
end
def execute
puts source
end
end
class BeerSong
def execute
99.downto 1 do |num|
puts <<BEER
#{num} bottle#{num>1?'s':''} of beer on the wall
#{num} bottle#{num>1?'s':''} of beer
take one down, pass it around,
#{num-1!=0?num-1:"no more"} bottle#{num!=2?'s':''} of beer on the wall
BEER
puts "No more bottles of beer on the wall\nOh noes!"
end
end
end
$accumulator = 0
class IncreaseAccumulator
def execute
$accumulator += 1
end
end
class IncreaseAccumulatorAndCreateANewObject
def execute
$accumulator += 1
(@@objects ||= []) << Object.new
end
end
class HQNinePlusPlus
def initialize source
@source = source
@hello_worlds = []
@quines = []
@beer_songs = []
@increase_accumulators = []
@increase_accumulator_and_create_new_objects = []
end
def parse
accumulator_flag = false
@source.downcase.each_byte do |command|
case command
when ?h
@hello_worlds << HelloWorld.new
@hello_worlds.last.execute
when ?q
@quines << Quine.new( @source )
@quines.last.execute
when ?9
@beer_songs << BeerSong.new
@beer_songs.last.execute
when ?+
if accumulator_flag == true
@increase_accumulator_and_create_new_objects << IncreaseAccumulatorAndCreateANewObject.new
@increase_accumulator_and_create_new_objects.last.execute
else
@increase_accumulators << IncreaseAccumulator.new
@increase_accumulators.last.execute
accumulator_flag = true
next
end
else
raise "#{command} is not a valid HQ9++-Command!"
end
accumulator_flag = false
end
end
end
if __FILE__ == $0
source = IO.read(ARGV[0]).chomp
parser = HQNinePlusPlus.new( source.gsub( /[^hq9\+]/i, '' ) )
parser.parse
end |