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
|
#!/usr/bin/env ruby
def gen_label(here, there)
return "label_#{here}_#{there.gsub(/-/, '_')}"
end
input = $stdin.readlines.map(&:chomp)
lines = []
labels = {}
input.each_with_index do |line, i|
istr = line.split
case istr[0]
when 'set'
lines << "#{istr[1]} = #{istr[2]};"
when 'sub'
lines << "#{istr[1]} -= #{istr[2]};"
when 'mul'
lines << "#{istr[1]} *= #{istr[2]};"
when 'jnz'
lab = gen_label(i, istr[2])
lines << "if (#{istr[1]}) goto #{lab};"
jmp = istr[2].to_i
labels[i+jmp] = [] if labels[i+jmp].nil?
labels[i+jmp] << "#{lab}:"
end
end
labels.keys.sort.reverse.each do |i|
labels[i].each do |lab|
lines.insert(i, lab)
end
end
puts <<EOT
#include <stdio.h>
void main() {
int
a = 1,
b = 0,
c = 0,
d = 0,
e = 0,
f = 0,
g = 0,
h = 0;
// BEGIN GENERATED CODE BLOCK
EOT
lines.each do |line|
puts "#{line[0] == "l" ? "" : " "} #{line}"
end
puts <<EOT
//END GENERATED CODE BLOCK
printf("%d\\n", h);
}
EOT
|