-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhitespace.rb
More file actions
235 lines (217 loc) · 5.09 KB
/
whitespace.rb
File metadata and controls
235 lines (217 loc) · 5.09 KB
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/ruby1.8
# whitepsace-ruby
# Copyright (C) 2003 by Wayne E. Conrad
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Opcodes = [
[' ', :push, :signed],
[' \n ', :dup],
[' \t ', :dupnum, :unsigned],
[' \n\t', :swap],
[' \n\n', :discard],
[' \t\n', :discardnum, :unsigned],
['\t ', :add],
['\t \t', :sub],
['\t \n', :mul],
['\t \t ', :div],
['\t \t\t', :mod],
['\t\t ', :store],
['\t\t\t', :retrieve],
['\n ', :label, :unsigned],
['\n \t', :call, :unsigned],
['\n \n', :jump, :unsigned],
['\n\t ', :jz, :unsigned],
['\n\t\t', :jn, :unsigned],
['\n\t\n', :ret],
['\n\n\n', :exit],
['\t\n ', :outchar],
['\t\n \t', :outnum],
['\t\n\t ', :readchar],
['\t\n\t\t', :readnum],
]
def error(message)
$stderr.puts "Error: #{message}"
exit 1
end
class Tokenizer
attr_reader :tokens
def initialize source = nil
@tokens = []
if source.nil? || !(source.is_a?(String))
source = ARGV[0]
end
begin
file = File.open(source)
rescue
file = $<
end
@program = file.read.tr("^ \t\n", "")
while @program != "" do
@tokens << tokenize
end
end
private
def tokenize
for ws, opcode, arg in Opcodes
if @program =~ /\A#{ws}#{arg ? '([ \t]*)\n' : '()'}(.*)\z/m
@program = $2
case arg
when :unsigned
return [opcode, eval("0b#{$1.tr(" \t", "01")}")]
when :signed
value = eval("0b#{$1[1..-1].tr(" \t", "01")}")
value *= -1 if ($1[0] == ?\t)
return [opcode, value]
else
return [opcode]
end
end
end
error("Unknown command: #{@program.tr(" \t\n", "STL")}")
end
end
class Executor
def initialize(tokens)
@tokens = tokens
# whether or not to output transform result to trans
@release = true
if !@release
@trans = open("trans", "w")
end
end
def run
@pc = 0 #Program pointer
@stack = []
@heap = {}
@callStack = []
loop do
opcode, arg = @tokens[@pc]
@pc += 1
case opcode
when :push
@stack.push arg
debug "push " + arg.to_s
when :label
debug "label " + arg.to_s
when :dup
@stack.push @stack[-1]
debug "dup"
when :dupnum
debug "dupnum " + arg.to_s
break if arg == 0
if @stack.size-arg < 0
error("Stack underflow in dup")
end
@stack.push @stack[-arg]
when :outnum
print @stack.pop
debug "outnum"
when :outchar
print @stack.pop.chr
debug "outchar"
when :add
binaryOp("+")
debug "add"
when :sub
binaryOp("-")
debug "sub"
when :mul
binaryOp("*")
debug "mul"
when :div
binaryOp("/")
debug "div"
when :mod
binaryOp("%")
debug "mod"
when :jz
debug "jz " + arg.to_s
jump(arg) if @stack.pop == 0
when :jn
debug "jn " + arg.to_s
jump(arg) if @stack.pop < 0
when :jump
debug "jump " + arg.to_s
jump(arg)
when :discard
@stack.pop
debug "discard"
when :discardnum
debug "discardnum " + arg.to_s
break if arg == 0
if @stack.size-arg <= 0
error("Stack underflow in discard")
end
@stack[@stack.size-arg-1..-2] = nil
@stack.delete_at(-2)
when :exit
debug "exit"
if !@release
@trans.close
end
exit
when :store
value = @stack.pop
address = @stack.pop
@heap[address] = value
debug "store"
when :call
debug "call " + arg.to_s
@callStack.push(@pc)
jump(arg)
when :retrieve
@stack.push @heap[@stack.pop]
debug "retrieve"
when :ret
@pc = @callStack.pop
debug "pop"
when :readchar
debug "readchar"
@heap[@stack.pop] = $stdin.getc
when :readnum
debug "readnum"
@heap[@stack.pop] = $stdin.gets.to_i
when :swap
@stack[-1], @stack[-2] = @stack[-2], @stack[-1]
debug "swap"
else
error("Unknown opcode: #{opcode.tr(" \t\n", "STL")}")
end
end
end
private
def binaryOp(op)
b = @stack.pop
a = @stack.pop
@stack.push eval("a #{op} b")
end
def jump(label)
@tokens.each_with_index do |token, i|
if token == [:label, label]
@pc = i
return
end
end
error("Unknown label: #{label}")
end
def debug(msg)
if !@release
@trans.puts msg
end
end
end
if __FILE__ == $0
Executor.new(Tokenizer.new.tokens).run
end