forked from mame/quine-relay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grass.rb
204 lines (171 loc) · 4.74 KB
/
grass.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 -Ku
#
# grass.rb - Grass interpreter
# http://www.blue.sky.or.jp/grass/
#
# Copyright (C) 2006, 2007 UENO Katsuhiro. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# History:
#
# 2007-10-02
# - Follow the latest changes of the definition of Grass.
# 2007-09-20
# - First version.
#
require 'pp'
class Grass
RELEASE_DATE = '2007-10-02'
class Error < StandardError ; end
class RuntimeError < Error ; end
class IllegalState < Error ; end
Machine = Struct.new(:code, :env, :dump)
class Value
def char_code
raise RuntimeError, "not a char"
end
def app(m, arg)
raise RuntimeError, "app undefined"
end
end
class Insn
end
class App < Insn
def initialize(m, n)
@m, @n = m, n
end
def eval(m)
f, v = m.env[-@m], m.env[-@n]
raise "out of bound" unless f and v
f.app m, v
end
def pretty_print(q)
q.text 'App'
q.group(4, '(', ')') { q.seplist([@m,@n]) { |i| q.text "#{i}" } }
end
alias inspect pretty_print_inspect
end
class Abs < Insn
def initialize(body)
@body = body
end
def eval(m)
m.env.push Fn.new(@body, m.env.dup)
end
def pretty_print(q)
q.text 'Abs'
q.group(4, '(', ')') { @body.pretty_print(q) }
end
alias inspect pretty_print_inspect
end
class Fn < Value
def initialize(code, env)
@code, @env = code, env
end
def app(m, arg)
m.dump.push [m.code, m.env]
m.code, m.env = @code.dup, @env.dup
m.env.push arg
end
end
ChurchTrue = Fn.new([Abs.new([App.new(3,2)])], [Fn.new([],[])])
ChurchFalse = Fn.new([Abs.new([])], [])
class CharFn < Value
def initialize(char_code)
@char_code = char_code
end
attr_reader :char_code
def app(m, arg)
ret = @char_code == arg.char_code ? ChurchTrue : ChurchFalse
m.env.push ret
end
end
class Succ < Value
def app(m, arg)
m.env.push CharFn.new((arg.char_code + 1) & 255)
end
end
class Out < Value
def app(m, arg)
$stdout.print arg.char_code.chr
$stdout.flush
m.env.push arg
end
end
class In < Value
def app(m, arg)
ch = $stdin.getc
ret = ch ? CharFn.new(ch) : arg
m.env.push ret
end
end
private
def eval(m)
while true
insn = m.code.shift
if insn then
insn.eval m
else
break if m.dump.empty?
ret = m.env.last
raise IllegalState, "no return value" unless ret
m.code, m.env = m.dump.pop
m.env.push ret
end
end
raise IllegalState, "illegal final machine state" unless m.env.size == 1
m.env.first
end
InitialEnv = [In.new, CharFn.new(?w.ord), Succ.new, Out.new]
InitialDump = [[[], []], [[App.new(1, 1)], []]]
def start(code)
eval Machine.new(code, InitialEnv, InitialDump)
end
def parse(src)
code = []
src = src.sub(/\A[^ww]*/, '').gsub(/[^wwWWvv]/, '')
src.split(/[vv]+/).each { |s|
a = s.scan(/[ww]+|[WW]+/).map { |i| i.split(//).size }
arity = 0
arity = a.shift if /\A[ww]/ =~ s
raise "parse error at app" unless a.size % 2 == 0
body = []
0.step(a.size - 1, 2) { |i| body.push App.new(a[i], a[i+1]) }
insn = (0...arity).inject(body) { |body,| [Abs.new(body)] }
code.concat insn
}
code
end
public
def run(src)
start parse(src)
end
end
if $0 == __FILE__ then
$stderr.puts "Grass #{Grass::RELEASE_DATE}" if $VERBOSE
Grass.new.run $<.read
end
# Local Variables:
# coding: utf-8
# End: