-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhitespace
executable file
·178 lines (161 loc) · 3.94 KB
/
whitespace
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
#!/usr/bin/ruby
# 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 ', :copy, :signed],
[' \n\t', :swap],
[' \n\n', :discard],
[' \t\n', :slide, :signed],
['\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
@tokens = []
@program = $<.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, $1.tr(" \t", "01").to_i(2)]
when :signed
value = $1[1..-1].tr(" \t", "01").to_i(2)
value *= -1 if ($1[0] == ?\t)
return [opcode, value]
else
return [opcode]
end
end
end
error("Unknown command: #{@program.inspect}")
end
end
class Executor
def initialize(tokens)
@tokens = tokens
end
def run
@pc = 0
@stack = []
@heap = {}
@callStack = []
loop do
opcode, arg = @tokens[@pc]
@pc += 1
case opcode
when :push
@stack.push arg
when :label
when :dup
@stack.push @stack[-1]
when :outnum
print @stack.pop
when :outchar
print @stack.pop.chr("UTF-8")
when :add
binaryOp("+")
when :sub
binaryOp("-")
when :mul
binaryOp("*")
when :div
binaryOp("/")
when :mod
binaryOp("%")
when :jz
jump(arg) if @stack.pop == 0
when :jn
jump(arg) if @stack.pop < 0
when :jump
jump(arg)
when :discard
@stack.pop
when :exit
exit
when :store
value = @stack.pop
address = @stack.pop
@heap[address] = value
when :call
@callStack.push(@pc)
jump(arg)
when :retrieve
@stack.push @heap[@stack.pop]
when :ret
@pc = @callStack.pop
when :readchar
@heap[@stack.pop] = $stdin.getc.each_codepoint.next
when :readnum
@heap[@stack.pop] = $stdin.gets.to_i
when :swap
@stack[-1], @stack[-2] = @stack[-2], @stack[-1]
when :copy
@stack.push @stack[-arg-1]
when :slide
@stack.slice!(-arg-1, arg)
else
error("Unknown opcode: #{opcode.inspect}")
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
end
Executor.new(Tokenizer.new.tokens).run if $0 == __FILE__