forked from KeenS/whitelie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asm.rb
204 lines (180 loc) · 3.73 KB
/
asm.rb
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/ruby
$debug = false
def debug(str)
puts str if $debug
end
def out(str)
debug "out #{str}"
print str.gsub("s", " ").gsub("t", "\t").gsub("n", "\n")
end
def out_number(n)
# * number requires terminal \n
# * first character indicates sign where s is + and t is -
debug "outnumber #{n} as #{sprintf("%02x", n)}"
if n < 0
print sprintf("\t%08b\n", n.abs()).gsub("0", " ").gsub("1", "\t")
else
print sprintf(" %08b\n", n.abs()).gsub("0", " ").gsub("1", "\t")
end
end
def lex(input)
input.gsub(/#.*$/, "").split
end
def parse(tokens)
tokens.reverse!
while !tokens.empty?
case token = tokens.pop
when "Stack"
debug "stack"
out "s"
case token = tokens.pop
when "push"
debug "push"
out "s"
arg = parse_number(tokens.pop)
debug arg
out_number arg
when "dup"
debug "dup"
out "ns"
when "discard"
debug "discard"
out "nn"
when "copy"
debug "copy nth item"
out "ts"
arg = parse_number(tokens.pop)
debug arg
out_number arg
when "swap"
debug "swap"
out "nt"
when "slide"
debug "slide n items off"
out "tn"
arg = parse_number(tokens.pop)
debug arg
out_number arg
else
puts "unknown Stack inst #{token}"
end
when "Arith"
debug "arith"
out "ts"
case token = tokens.pop
when "add"
debug "add"
out "ss"
when "sub"
debug "sub"
out "st"
when "mul"
debug "mul"
out "sn"
when "div"
debug "div"
out "ts"
when "mod"
debug "mod"
out "tt"
else
puts "unknown Arith inst #{token}"
end
when "Heap"
debug "heap"
out "tt"
case token = tokens.pop
when "store"
debug "store"
out "s"
when "retrieve"
debug "retrieve"
out "t"
else
puts "unknown Heap inst #{token}"
end
when "Flow"
debug "flow"
out "n"
case token = tokens.pop
when "label"
debug "mark location"
out "ss"
arg = parse_number(tokens.pop)
debug arg
out_number arg
when "jump"
debug "jump to label"
out "sn"
arg = parse_number(tokens.pop)
debug arg
out_number arg
when "jz"
debug "jump if zero"
out "ts"
arg = parse_number(tokens.pop)
debug arg
out_number arg
when "jneg"
debug "jump if neg"
out "tt"
arg = parse_number(tokens.pop)
debug arg
out_number arg
when "call"
debug "call subroutine"
out "st"
arg = parse_number(tokens.pop)
debug arg
out_number arg
when "return"
debug "return"
out "tn"
when "end"
debug "exit"
out "nn"
else
puts "unknown Flow inst #{token}"
end
when "IO"
debug "io"
out "tn"
case token = tokens.pop
when "outchar"
debug "outchar"
out "ss"
when "readchar"
debug "readchar"
out "ts"
when "outnumber"
debug "outnumber"
out "st"
when "readnumber"
debug "readnumber"
out "tt"
else
puts "unknown IO inst #{token}"
end
else
puts "unknown Inst type #{token}"
end
end
end
def parse_number(arg)
if arg =~ /'(.)'/
$1.ord
elsif arg =~ /'\\(.)'/
case $1
when "n"
"\n".ord
when "t"
"\t".ord
end
elsif arg =~ /0x([0-9a-f]{1,})/
$1.to_i(16)
else
arg.to_i
end
end
input ||= open(ARGV[0],"r").read()
parse(lex(input))