blob: 6a4aaa0e550c2ff64d3912576a2574917f52d7e1 (
plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
#!/usr/bin/env ruby
exit 1 if ARGV.length != 1
source = ARGV.shift&.strip
lines = File.readlines(source)
class ProgramError < Exception ; end
class ParameterError < ProgramError ; end
class LabelError < ProgramError ; end
class Program
def initialize
@memory = []
@org = 0
@labeldb = {}
end
def parse(line)
case line[0]
when "@org"
@org = line[1].to_i
when "@dat"
line.shift
line.each do |d|
@memory[@org] = d.to_i
@org += 1
end
when "add"
raise ParameterError, "Expected positional argument" unless line[3] =~ /\A@/
@memory[@org] = Program.flag_digits(line[1..3]) + 1
@memory[@org + 1] = Program.desigil(line[1])
@memory[@org + 2] = Program.desigil(line[2])
@memory[@org + 3] = Program.desigil(line[3])
@org += 4
when "mul"
raise ParameterError, "Expected positional argument" unless line[3] =~ /\A@/
@memory[@org] = Program.flag_digits(line[1..3]) + 2
@memory[@org + 1] = Program.desigil(line[1])
@memory[@org + 2] = Program.desigil(line[2])
@memory[@org + 3] = Program.desigil(line[3])
@org += 4
when "inp"
raise ParameterError, "Expected positional argument" unless line[1] =~ /\A@/
@memory[@org] = 3
@memory[@org + 1] = Program.desigil(line[1])
@org += 2
when "out"
@memory[@org] = Program.flag_digits(line[1..1]) + 4
@memory[@org + 1] = Program.desigil(line[1])
@org += 2
when "jnz"
@memory[@org] = Program.flag_digits(line[1..2]) + 5
@memory[@org + 1] = Program.desigil(line[1])
@memory[@org + 2] = Program.desigil(line[2])
@org += 3
when "jez"
@memory[@org] = Program.flag_digits(line[1..2]) + 6
@memory[@org + 1] = Program.desigil(line[1])
@memory[@org + 2] = Program.desigil(line[2])
@org += 3
when "tlt"
raise ParameterError, "Expected positional argument" unless line[3] =~ /\A@/
@memory[@org] = Program.flag_digits(line[1..3]) + 7
@memory[@org + 1] = Program.desigil(line[1])
@memory[@org + 2] = Program.desigil(line[2])
@memory[@org + 3] = Program.desigil(line[3])
@org += 4
when "teq"
raise ParameterError, "Expected positional argument" unless line[3] =~ /\A@/
@memory[@org] = Program.flag_digits(line[1..3]) + 8
@memory[@org + 1] = Program.desigil(line[1])
@memory[@org + 2] = Program.desigil(line[2])
@memory[@org + 3] = Program.desigil(line[3])
@org += 4
when "arb"
@memory[@org] = Program.flag_digits(line[1..1]) + 9
@memory[@org + 1] = Program.desigil(line[1])
@org += 2
when "hlt"
@memory[@org] = 99
@org += 1
when /(.*):/
@labeldb[$1] = @org
end
end
def dump
reify_nils
reify_labels
@memory.join(",")
end
private
def reify_nils
@memory.map! do |x|
x.nil? ? 0 : x
end
end
def reify_labels
@memory.map! do |x|
if x.is_a?(String) then
if @labeldb.has_key?(x) then
@labeldb[x]
else
raise LabelError, "Label not found"
end
else
x
end
end
end
def self.flag_digits(args)
sig = 0
until args.empty? do
a = args.pop
case a
when /\A@/
when /\A&/
sig += 2
else
sig += 1
end
sig *= 10
end
sig * 10
end
def self.desigil(str)
case str
when /\A@/
str[1..-1].to_i
when /\A&/
str[1..-1].to_i
when /\A[+-0-9]/
str.to_i
else
str
end
end
end
p = Program.new
lines.each do |line|
line = line.split(";")[0].strip
next if line.nil? || line.empty?
line = line.split(/\s*[,\s]\s*/)
p.parse(line)
end
puts p.dump
|