reconsidered 0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.autotest +23 -0
- data/.gemtest +0 -0
- data/History.txt +4 -0
- data/LICENSE.txt +28 -0
- data/Manifest.txt +11 -0
- data/README.rdoc +176 -0
- data/README.txt +176 -0
- data/Rakefile +11 -0
- data/examples/guessing.rb +16 -0
- data/examples/simple.rb +7 -0
- data/lib/reconsidered.rb +55 -0
- data/test/test_reconsidered.rb +50 -0
- metadata +115 -0
data/.autotest
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'autotest/restart'
|
4
|
+
|
5
|
+
# Autotest.add_hook :initialize do |at|
|
6
|
+
# at.extra_files << "../some/external/dependency.rb"
|
7
|
+
#
|
8
|
+
# at.libs << ":../some/external"
|
9
|
+
#
|
10
|
+
# at.add_exception 'vendor'
|
11
|
+
#
|
12
|
+
# at.add_mapping(/dependency.rb/) do |f, _|
|
13
|
+
# at.files_matching(/test_.*rb$/)
|
14
|
+
# end
|
15
|
+
#
|
16
|
+
# %w(TestA TestB).each do |klass|
|
17
|
+
# at.extra_class_map[klass] = "test/test_misc.rb"
|
18
|
+
# end
|
19
|
+
# end
|
20
|
+
|
21
|
+
# Autotest.add_hook :run_command do |at|
|
22
|
+
# system "rake build"
|
23
|
+
# end
|
data/.gemtest
ADDED
File without changes
|
data/History.txt
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
== LICENSE:
|
2
|
+
|
3
|
+
(The BSD 2-clause License)
|
4
|
+
|
5
|
+
Copyright (c) 2011,2012 Jim Wise
|
6
|
+
All rights reserved.
|
7
|
+
|
8
|
+
Redistribution and use in source and binary forms, with or without
|
9
|
+
modification, are permitted provided that the following conditions
|
10
|
+
are met:
|
11
|
+
|
12
|
+
1. Redistributions of source code must retain the above copyright
|
13
|
+
notice, this list of conditions and the following disclaimer.
|
14
|
+
2. Redistributions in binary form must reproduce the above copyright
|
15
|
+
notice, this list of conditions and the following disclaimer in the
|
16
|
+
documentation and/or other materials provided with the distribution.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
19
|
+
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
20
|
+
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
21
|
+
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
|
22
|
+
BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
23
|
+
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
24
|
+
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
25
|
+
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
26
|
+
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
27
|
+
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28
|
+
POSSIBILITY OF SUCH DAMAGE.
|
data/Manifest.txt
ADDED
data/README.rdoc
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
= reconsidered
|
2
|
+
|
3
|
+
https://github.com/jimwise/reconsidered
|
4
|
+
|
5
|
+
Author:: Jim Wise (mailto:jwise@draga.com)
|
6
|
+
Copyright:: Copyright (c) 2012 Jim Wise
|
7
|
+
License:: 2-clause BSD-Style (see LICENSE.txt)
|
8
|
+
|
9
|
+
== DESCRIPTION:
|
10
|
+
|
11
|
+
Reconsidered adds a vital and sorely missed language feature to Ruby: the
|
12
|
+
well known and widely loved GO TO operation, as discussed in Donald Knuth's
|
13
|
+
seminal paper {<em>Structured Programming with <tt>go to</tt>
|
14
|
+
Statements</em>} (see REFERENCES, below).
|
15
|
+
|
16
|
+
However, this implementation is itself severely limited -- it only allows
|
17
|
+
execution to jump to a point in the program which has already been passed
|
18
|
+
during execution of the program; it is thus less general purpose a tool than
|
19
|
+
the true GOTO of languages such as Fortran.
|
20
|
+
|
21
|
+
== REQUIREMENTS:
|
22
|
+
|
23
|
+
<b>This code will not work in JRuby or MacRuby (no callcc). It is tested
|
24
|
+
(and should work fine) in Ruby 1.8.7 and 1.9.3.</b>
|
25
|
+
|
26
|
+
== INSTALL:
|
27
|
+
|
28
|
+
To install:
|
29
|
+
|
30
|
+
$ gem install reconsidered
|
31
|
+
|
32
|
+
== SYNOPSIS:
|
33
|
+
|
34
|
+
=== General usage
|
35
|
+
|
36
|
+
The syntax here mirrors that of the optional __goto__ extension included
|
37
|
+
(disabled by default) as a joke (see REFERENCES, below) in the Ruby 1.9 sources
|
38
|
+
|
39
|
+
You may label any point in your program with the Kernel#__label__ statement, as
|
40
|
+
in:
|
41
|
+
|
42
|
+
__label__ 10
|
43
|
+
puts "Hello, world!"
|
44
|
+
|
45
|
+
or
|
46
|
+
|
47
|
+
__label__ :guess
|
48
|
+
print "Can you guess it? "
|
49
|
+
|
50
|
+
This creates a <em>global label</em>, which you may return to at any point
|
51
|
+
in your program using the Kernel#__goto__ statement, as in:
|
52
|
+
|
53
|
+
__goto__ 10
|
54
|
+
|
55
|
+
or
|
56
|
+
|
57
|
+
guess = gets.to_i
|
58
|
+
if num != guess
|
59
|
+
puts "Wrong!"
|
60
|
+
__goto__ :guess
|
61
|
+
else
|
62
|
+
puts "Right!"
|
63
|
+
end
|
64
|
+
|
65
|
+
The __goto__ statement will immediately resume execution from the last point
|
66
|
+
at which __label__ was called with the same value.
|
67
|
+
|
68
|
+
Note that this implies that you may only use __goto__ to return to a __label__
|
69
|
+
statement which has already been executed, unlike the GOTO statement in,
|
70
|
+
e.g. C, which can be used to jump forward (but only within a given
|
71
|
+
function):
|
72
|
+
|
73
|
+
if (num == guess)
|
74
|
+
goto right;
|
75
|
+
else
|
76
|
+
goto guess;
|
77
|
+
|
78
|
+
right:
|
79
|
+
puts("Right!")
|
80
|
+
|
81
|
+
Working around this limitation is left as an exercise for the reader.
|
82
|
+
|
83
|
+
If you try to __goto__ a label which you have not defined (or not yet
|
84
|
+
defined), you're gonna have a hard time (and raise a
|
85
|
+
Reconsidered::NoSuchLabel exception).
|
86
|
+
|
87
|
+
|
88
|
+
=== Private Labels
|
89
|
+
|
90
|
+
In addition to the default global scope used with __label__ and __goto__,
|
91
|
+
you can allocate a private scope of labels using the Reconsidered::Labels
|
92
|
+
class. This class provides methods Labels#label and Lables#goto which can
|
93
|
+
be used as follows:
|
94
|
+
|
95
|
+
l = Reconsidered::Labels.new
|
96
|
+
acc = 0
|
97
|
+
l.label 30
|
98
|
+
acc = acc + 1
|
99
|
+
if acc < 10
|
100
|
+
l.goto 30
|
101
|
+
end
|
102
|
+
|
103
|
+
this not only provides a private set of objects (not overlapping with other
|
104
|
+
use of __label__ and __goto__), but has the added advantage that all memory
|
105
|
+
used to store labels within such an object is freed when the object is
|
106
|
+
garbage collected.
|
107
|
+
|
108
|
+
== DEVELOPERS:
|
109
|
+
|
110
|
+
After checking out the source, run:
|
111
|
+
|
112
|
+
$ rake newb
|
113
|
+
|
114
|
+
This task will install any missing dependencies, run the tests/specs,
|
115
|
+
and generate the RDoc.
|
116
|
+
|
117
|
+
== REFERENCES:
|
118
|
+
|
119
|
+
For competing views on GOTO as a language feature, see
|
120
|
+
|
121
|
+
* Knuth, Donald E. {<em>Structured Programming with <tt>go to</tt>
|
122
|
+
Statements</em>}[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.103.6084&rep=rep1&type=pdf],
|
123
|
+
in <em>Computing Surveys</em>, Vol 6, No 4, 1974
|
124
|
+
|
125
|
+
* Dijkstra, Edsger W., {<em><tt>go to</tt> Statement Considered
|
126
|
+
Harmful</em>}[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.132.875&rep=rep1&type=pdf]
|
127
|
+
in <em>Communications of the ACM</em>, Vol. 11, No. 3, 1968
|
128
|
+
|
129
|
+
For information on the implementation strategy used here (and the general
|
130
|
+
equivalence of the more powerful call-with-current-continuation operation to
|
131
|
+
<tt>go to</tt>), see
|
132
|
+
|
133
|
+
* Steele, Guy Lewis, Jr., {<em>Debunking the 'Expensive Procedure Call'
|
134
|
+
Myth, or, Procedure Call Implementations Considered Harmful, or, Lambda:
|
135
|
+
The Ultimate
|
136
|
+
<tt>GOTO</tt></em>}[http://dspace.mit.edu/handle/1721.1/5753], MIT
|
137
|
+
Artificial Intelligence Memo AIM-443, 1977
|
138
|
+
|
139
|
+
For information on Ruby's implementation of call-with-current-continuation
|
140
|
+
see
|
141
|
+
|
142
|
+
* {<em>Ruby Core Documentation:
|
143
|
+
Continuation</em>}[http://www.ruby-doc.org/core-1.9.3/Continuation.html],
|
144
|
+
Ruby Core Documentation, Version 1.9.3
|
145
|
+
|
146
|
+
For information on the (different, and less limited) implementation provided
|
147
|
+
in ruby 1.9 as a language feature, but disabled by default, see
|
148
|
+
|
149
|
+
* Shaugnessy, Pat {<em>The Joke Is On Us: How Ruby 1.9 Supports the <tt>Goto</tt>
|
150
|
+
Statement</em>}[http://patshaughnessy.net/2012/2/29/the-joke-is-on-us-how-ruby-1-9-supports-the-goto-statement],
|
151
|
+
<em>patshaugnessy.net</em> blog, February 29, 2012
|
152
|
+
|
153
|
+
== LICENSE:
|
154
|
+
|
155
|
+
(The MIT License)
|
156
|
+
|
157
|
+
Copyright (c) 2012 FIX
|
158
|
+
|
159
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
160
|
+
a copy of this software and associated documentation files (the
|
161
|
+
'Software'), to deal in the Software without restriction, including
|
162
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
163
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
164
|
+
permit persons to whom the Software is furnished to do so, subject to
|
165
|
+
the following conditions:
|
166
|
+
|
167
|
+
The above copyright notice and this permission notice shall be
|
168
|
+
included in all copies or substantial portions of the Software.
|
169
|
+
|
170
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
171
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
172
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
173
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
174
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
175
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
176
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.txt
ADDED
@@ -0,0 +1,176 @@
|
|
1
|
+
= reconsidered
|
2
|
+
|
3
|
+
https://github.com/jimwise/reconsidered
|
4
|
+
|
5
|
+
Author:: Jim Wise (mailto:jwise@draga.com)
|
6
|
+
Copyright:: Copyright (c) 2012 Jim Wise
|
7
|
+
License:: 2-clause BSD-Style (see LICENSE.txt)
|
8
|
+
|
9
|
+
== DESCRIPTION:
|
10
|
+
|
11
|
+
Reconsidered adds a vital and sorely missed language feature to Ruby: the
|
12
|
+
well known and widely loved GO TO operation, as discussed in Donald Knuth's
|
13
|
+
seminal paper {<em>Structured Programming with <tt>go to</tt>
|
14
|
+
Statements</em>} (see REFERENCES, below).
|
15
|
+
|
16
|
+
However, this implementation is itself severely limited -- it only allows
|
17
|
+
execution to jump to a point in the program which has already been passed
|
18
|
+
during execution of the program; it is thus less general purpose a tool than
|
19
|
+
the true GOTO of languages such as Fortran.
|
20
|
+
|
21
|
+
== REQUIREMENTS:
|
22
|
+
|
23
|
+
<b>This code will not work in JRuby or MacRuby (no callcc). It is tested
|
24
|
+
(and should work fine) in Ruby 1.8.7 and 1.9.3.</b>
|
25
|
+
|
26
|
+
== INSTALL:
|
27
|
+
|
28
|
+
To install:
|
29
|
+
|
30
|
+
$ gem install reconsidered
|
31
|
+
|
32
|
+
== SYNOPSIS:
|
33
|
+
|
34
|
+
=== General usage
|
35
|
+
|
36
|
+
The syntax here mirrors that of the optional __goto__ extension included
|
37
|
+
(disabled by default) as a joke (see REFERENCES, below) in the Ruby 1.9 sources
|
38
|
+
|
39
|
+
You may label any point in your program with the Kernel#__label__ statement, as
|
40
|
+
in:
|
41
|
+
|
42
|
+
__label__ 10
|
43
|
+
puts "Hello, world!"
|
44
|
+
|
45
|
+
or
|
46
|
+
|
47
|
+
__label__ :guess
|
48
|
+
print "Can you guess it? "
|
49
|
+
|
50
|
+
This creates a <em>global label</em>, which you may return to at any point
|
51
|
+
in your program using the Kernel#__goto__ statement, as in:
|
52
|
+
|
53
|
+
__goto__ 10
|
54
|
+
|
55
|
+
or
|
56
|
+
|
57
|
+
guess = gets.to_i
|
58
|
+
if num != guess
|
59
|
+
puts "Wrong!"
|
60
|
+
__goto__ :guess
|
61
|
+
else
|
62
|
+
puts "Right!"
|
63
|
+
end
|
64
|
+
|
65
|
+
The __goto__ statement will immediately resume execution from the last point
|
66
|
+
at which __label__ was called with the same value.
|
67
|
+
|
68
|
+
Note that this implies that you may only use __goto__ to return to a __label__
|
69
|
+
statement which has already been executed, unlike the GOTO statement in,
|
70
|
+
e.g. C, which can be used to jump forward (but only within a given
|
71
|
+
function):
|
72
|
+
|
73
|
+
if (num == guess)
|
74
|
+
goto right;
|
75
|
+
else
|
76
|
+
goto guess;
|
77
|
+
|
78
|
+
right:
|
79
|
+
puts("Right!")
|
80
|
+
|
81
|
+
Working around this limitation is left as an exercise for the reader.
|
82
|
+
|
83
|
+
If you try to __goto__ a label which you have not defined (or not yet
|
84
|
+
defined), you're gonna have a hard time (and raise a
|
85
|
+
Reconsidered::NoSuchLabel exception).
|
86
|
+
|
87
|
+
|
88
|
+
=== Private Labels
|
89
|
+
|
90
|
+
In addition to the default global scope used with __label__ and __goto__,
|
91
|
+
you can allocate a private scope of labels using the Reconsidered::Labels
|
92
|
+
class. This class provides methods Labels#label and Lables#goto which can
|
93
|
+
be used as follows:
|
94
|
+
|
95
|
+
l = Reconsidered::Labels.new
|
96
|
+
acc = 0
|
97
|
+
l.label 30
|
98
|
+
acc = acc + 1
|
99
|
+
if acc < 10
|
100
|
+
l.goto 30
|
101
|
+
end
|
102
|
+
|
103
|
+
this not only provides a private set of objects (not overlapping with other
|
104
|
+
use of __label__ and __goto__), but has the added advantage that all memory
|
105
|
+
used to store labels within such an object is freed when the object is
|
106
|
+
garbage collected.
|
107
|
+
|
108
|
+
== DEVELOPERS:
|
109
|
+
|
110
|
+
After checking out the source, run:
|
111
|
+
|
112
|
+
$ rake newb
|
113
|
+
|
114
|
+
This task will install any missing dependencies, run the tests/specs,
|
115
|
+
and generate the RDoc.
|
116
|
+
|
117
|
+
== REFERENCES:
|
118
|
+
|
119
|
+
For competing views on GOTO as a language feature, see
|
120
|
+
|
121
|
+
* Knuth, Donald E. {<em>Structured Programming with <tt>go to</tt>
|
122
|
+
Statements</em>}[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.103.6084&rep=rep1&type=pdf],
|
123
|
+
in <em>Computing Surveys</em>, Vol 6, No 4, 1974
|
124
|
+
|
125
|
+
* Dijkstra, Edsger W., {<em><tt>go to</tt> Statement Considered
|
126
|
+
Harmful</em>}[http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.132.875&rep=rep1&type=pdf]
|
127
|
+
in <em>Communications of the ACM</em>, Vol. 11, No. 3, 1968
|
128
|
+
|
129
|
+
For information on the implementation strategy used here (and the general
|
130
|
+
equivalence of the more powerful call-with-current-continuation operation to
|
131
|
+
<tt>go to</tt>), see
|
132
|
+
|
133
|
+
* Steele, Guy Lewis, Jr., {<em>Debunking the 'Expensive Procedure Call'
|
134
|
+
Myth, or, Procedure Call Implementations Considered Harmful, or, Lambda:
|
135
|
+
The Ultimate
|
136
|
+
<tt>GOTO</tt></em>}[http://dspace.mit.edu/handle/1721.1/5753], MIT
|
137
|
+
Artificial Intelligence Memo AIM-443, 1977
|
138
|
+
|
139
|
+
For information on Ruby's implementation of call-with-current-continuation
|
140
|
+
see
|
141
|
+
|
142
|
+
* {<em>Ruby Core Documentation:
|
143
|
+
Continuation</em>}[http://www.ruby-doc.org/core-1.9.3/Continuation.html],
|
144
|
+
Ruby Core Documentation, Version 1.9.3
|
145
|
+
|
146
|
+
For information on the (different, and less limited) implementation provided
|
147
|
+
in ruby 1.9 as a language feature, but disabled by default, see
|
148
|
+
|
149
|
+
* Shaugnessy, Pat {<em>The Joke Is On Us: How Ruby 1.9 Supports the <tt>Goto</tt>
|
150
|
+
Statement</em>}[http://patshaughnessy.net/2012/2/29/the-joke-is-on-us-how-ruby-1-9-supports-the-goto-statement],
|
151
|
+
<em>patshaugnessy.net</em> blog, February 29, 2012
|
152
|
+
|
153
|
+
== LICENSE:
|
154
|
+
|
155
|
+
(The MIT License)
|
156
|
+
|
157
|
+
Copyright (c) 2012 FIX
|
158
|
+
|
159
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
160
|
+
a copy of this software and associated documentation files (the
|
161
|
+
'Software'), to deal in the Software without restriction, including
|
162
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
163
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
164
|
+
permit persons to whom the Software is furnished to do so, subject to
|
165
|
+
the following conditions:
|
166
|
+
|
167
|
+
The above copyright notice and this permission notice shall be
|
168
|
+
included in all copies or substantial portions of the Software.
|
169
|
+
|
170
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
171
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
172
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
173
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
174
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
175
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
176
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
|
3
|
+
require 'reconsidered'
|
4
|
+
|
5
|
+
num = Random.rand 10
|
6
|
+
puts "I'm thinking of a number between 1 and 10."
|
7
|
+
|
8
|
+
__label__ :guess
|
9
|
+
print "Can you guess it? "
|
10
|
+
guess = gets.to_i
|
11
|
+
if num != guess
|
12
|
+
puts "Wrong!"
|
13
|
+
__goto__ :guess
|
14
|
+
else
|
15
|
+
puts "Right!"
|
16
|
+
end
|
data/examples/simple.rb
ADDED
data/lib/reconsidered.rb
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# This gem provides the classical "GO TO" statement for Ruby
|
2
|
+
#
|
3
|
+
# Author:: Jim Wise (mailto:jwise@draga.com)
|
4
|
+
# Copyright:: Copyright (c) 2012 Jim Wise
|
5
|
+
# License:: 2-clause BSD-Style (see LICENSE[link:files/LICENSE.html])
|
6
|
+
|
7
|
+
require 'continuation' unless RUBY_VERSION.start_with?("1.8")
|
8
|
+
|
9
|
+
module Reconsidered
|
10
|
+
VERSION = '0.9'
|
11
|
+
|
12
|
+
# Exception thrown if you __goto__ a non-existent label
|
13
|
+
class NoSuchLabel < StandardError
|
14
|
+
end
|
15
|
+
|
16
|
+
class Labels
|
17
|
+
# Allocate a new private Label set. Usually not needed -- use Kernel#__label__
|
18
|
+
# and Kernel#__goto__ isnstead.
|
19
|
+
#
|
20
|
+
# See "Private Labels" in the README for details
|
21
|
+
def initialize
|
22
|
+
@labels = {}
|
23
|
+
end
|
24
|
+
|
25
|
+
# Create a new label which, when used with #goto, will return to the current
|
26
|
+
# location in the program
|
27
|
+
def label sym
|
28
|
+
callcc do |cc|
|
29
|
+
@labels[sym] = cc
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Return to the location in the code labeled with sym
|
34
|
+
def goto sym
|
35
|
+
raise NoSuchLabel unless @labels[sym].instance_of? Continuation
|
36
|
+
@labels[sym].call
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
# :nodoc:
|
41
|
+
Default_Labels = Labels.new
|
42
|
+
end
|
43
|
+
|
44
|
+
module Kernel
|
45
|
+
# Create a new label which, when used with __goto__, will return to the current
|
46
|
+
# location in the program
|
47
|
+
def __label__ sym
|
48
|
+
Reconsidered::Default_Labels.label sym
|
49
|
+
end
|
50
|
+
|
51
|
+
# Return to the location in the code labeled with sym
|
52
|
+
def __goto__ sym
|
53
|
+
Reconsidered::Default_Labels.goto sym
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "test/unit"
|
2
|
+
require "reconsidered"
|
3
|
+
|
4
|
+
class TestReconsidered < Test::Unit::TestCase
|
5
|
+
def test_simple
|
6
|
+
acc = 0
|
7
|
+
__label__ 10
|
8
|
+
acc = acc + 1
|
9
|
+
if acc < 10
|
10
|
+
__goto__ 10
|
11
|
+
end
|
12
|
+
assert_equal 10, acc
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_nosuchlabel
|
16
|
+
assert_raise Reconsidered::NoSuchLabel do
|
17
|
+
__goto__ 42
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# this is actually testing call/cc, but fine
|
22
|
+
def test_after_exit
|
23
|
+
flag = false
|
24
|
+
@acc = 0
|
25
|
+
def bar
|
26
|
+
__label__ 20
|
27
|
+
@acc = @acc + 1
|
28
|
+
end
|
29
|
+
bar
|
30
|
+
unless flag
|
31
|
+
flag = true
|
32
|
+
__goto__ 20
|
33
|
+
end
|
34
|
+
assert_equal 2, @acc
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_private
|
38
|
+
l = Reconsidered::Labels.new
|
39
|
+
acc = 0
|
40
|
+
l.label 30
|
41
|
+
acc = acc + 1
|
42
|
+
if acc < 10
|
43
|
+
l.goto 30
|
44
|
+
end
|
45
|
+
assert_raise Reconsidered::NoSuchLabel do
|
46
|
+
__goto__ 30
|
47
|
+
end
|
48
|
+
assert_equal 10, acc
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: reconsidered
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.9'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Jim Wise
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rdoc
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.10'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.10'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: hoe
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '3.1'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '3.1'
|
46
|
+
description: ! 'Reconsidered adds a vital and sorely missed language feature to Ruby: the
|
47
|
+
|
48
|
+
well known and widely loved GO TO operation, as discussed in Donald Knuth''s
|
49
|
+
|
50
|
+
seminal paper {<em>Structured Programming with <tt>go to</tt>
|
51
|
+
|
52
|
+
Statements</em>} (see REFERENCES, below).
|
53
|
+
|
54
|
+
|
55
|
+
However, this implementation is itself severely limited -- it only allows
|
56
|
+
|
57
|
+
execution to jump to a point in the program which has already been passed
|
58
|
+
|
59
|
+
during execution of the program; it is thus less general purpose a tool than
|
60
|
+
|
61
|
+
the true GOTO of languages such as Fortran.'
|
62
|
+
email:
|
63
|
+
- jwise@draga.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files:
|
67
|
+
- History.txt
|
68
|
+
- LICENSE.txt
|
69
|
+
- Manifest.txt
|
70
|
+
- README.rdoc
|
71
|
+
- README.txt
|
72
|
+
files:
|
73
|
+
- .autotest
|
74
|
+
- History.txt
|
75
|
+
- LICENSE.txt
|
76
|
+
- Manifest.txt
|
77
|
+
- README.rdoc
|
78
|
+
- README.txt
|
79
|
+
- Rakefile
|
80
|
+
- examples/guessing.rb
|
81
|
+
- examples/simple.rb
|
82
|
+
- lib/reconsidered.rb
|
83
|
+
- test/test_reconsidered.rb
|
84
|
+
- .gemtest
|
85
|
+
homepage: https://github.com/jimwise/reconsidered
|
86
|
+
licenses: []
|
87
|
+
post_install_message:
|
88
|
+
rdoc_options:
|
89
|
+
- --main
|
90
|
+
- README.rdoc
|
91
|
+
require_paths:
|
92
|
+
- lib
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
none: false
|
101
|
+
requirements:
|
102
|
+
- - ! '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project: reconsidered
|
107
|
+
rubygems_version: 1.8.24
|
108
|
+
signing_key:
|
109
|
+
specification_version: 3
|
110
|
+
summary: ! 'Reconsidered adds a vital and sorely missed language feature to Ruby: the
|
111
|
+
well known and widely loved GO TO operation, as discussed in Donald Knuth''s seminal
|
112
|
+
paper {<em>Structured Programming with <tt>go to</tt> Statements</em>} (see REFERENCES,
|
113
|
+
below)'
|
114
|
+
test_files:
|
115
|
+
- test/test_reconsidered.rb
|