i am writing an embedded lisp interpretor in ruby, and i noticed that
i ABSOLUTELY need to use a goto or another thing that reacts
like the goto. I found a link :
i am writing an embedded lisp interpretor in ruby, and i noticed that
i ABSOLUTELY need to use a goto or another thing that reacts
like the goto. I found a link :
Are you familiar with the “catch” method? It implements a common use
for goto (escaping several loops at once - catch even works through
method boundaries).
If you want longjmp-like behavious, try continuations. The Pickaxe is
the place to go for both of these topics.
Cheers,
Gavin
···
On Tuesday, January 13, 2004, 8:36:37 PM, smayemba wrote:
Hi all,
i am writing an embedded lisp interpretor in ruby, and i noticed that
i ABSOLUTELY need to use a goto or another thing that reacts
like the goto. I found a link :
I am working in an university project. I have to translate an embedded lisp
interpretor wrote in Java in another language. I chose Ruby. In the java
code,
i don’t success to translate that kind of code in Ruby :
public static void eval()
{
begin_eval: //label (1)
while( 1 )
{
//some code come here
evalcar: //label (2)
while( 1 )
{
switch( x )
{
case
...
default :
if foo
break begin_eval //goto (1)
continue evalcar //goto (2)
}
}
}
}
In ruby i don’t know how to translate a <> and <<continue
. It’s like some goto ??
Thanks
Sébastien
Université Paris 8, France
DESS Informatique des Systèmes Autonomes
i am writing an embedded lisp interpretor in ruby, and i noticed that
i ABSOLUTELY need to use a goto or another thing that reacts
like the goto. I found a link :
I am working in an university project. I have to translate an embedded
lisp
interpretor wrote in Java in another language. I chose Ruby. In the java
code,
i don’t success to translate that kind of code in Ruby :
public static void eval()
{
begin_eval: //label (1)
while( 1 )
{
//some code come here
evalcar: //label (2)
while( 1 )
{
switch( x )
{
case
...
default :
if foo
break begin_eval //goto (1)
continue evalcar //goto (2)
}
}
}
}
In ruby i don’t know how to translate a <> and <<continue
. It’s like some goto ??
You can use “next” and “break”:
def eval_lisp
loop do
# some code come here
loop do
case
# ...
else
break if foo
next
end
end
class Lisper
def eval
loop do
catch :begin_eval do
# some code here
loop do
# you may not need this one unless there’s other inner
# loops
catch :eval_car do
case x
when …
else
# continues looping from :begin_eval
throw :begin_eval if foo
throw :eval_car # could just be done with next
end
end
end
end
end
end
end
Breaking to a labelled loop isn’t directly possible in Ruby, but you
can wrap the body of a loop in a catch block, and then throw the
expected symbol to simulate it.
You can simplify the above code by doing:
def Kernel
alias_method :old_loop, :loop
def loop(label = nil, &block)
if label.nil?
old_loop &block
else
old_loop { catch label &block }
end
end
end
Your resulting code would then be:
class Lisper
def eval
loop (:begin_eval) do
# some code here
loop (:eval_car) do
# you may not need this one unless there’s other inner
# loops
case x
when …
else
# continues looping from :begin_eval
throw :begin_eval if foo
throw :eval_car # could just be done with next
end
end
end
end
end
-austin
···
On Tue, 13 Jan 2004 21:21:40 +0900, smayemba wrote:
I am working in an university project. I have to translate an
embedded lisp interpretor wrote in Java in another language. I
chose Ruby. In the java code, i don’t success to translate that
kind of code in Ruby :
public static void eval()
{
begin_eval: //label (1)
while( 1 )
{
//some code come here
evalcar: //label (2)
while( 1 )
{
switch( x )
{
case
…
default :
if foo
break begin_eval //goto (1)
continue evalcar //goto (2)
}
}
}
}
In ruby i don’t know how to translate a <> and
<> . It’s like some goto ??
nobu.nokada@softhome.net schrieb im Newsbeitrag
news:200401131327.i0DDRLnu007049@sharui.nakada.kanuma.tochigi.jp…
Hi,
if foo
break begin_eval //goto (1)
This should exit outer loop.
You can use “next” and “break”:
def eval_lisp
loop do
# some code come here
loop do
case
# ...
else
break if foo
So “return” instead of “break” should be here.
Oh, yes of course. Thanks! Alternatively one could use throw catch if
some work has to be done after the outer loop. That would then result in
this version:
def eval_lisp
catch :begin_eval do
loop do
# some code come here
loop do
case
# ...
else
throw :begin_eval if foo
next
end
end
end
end
work after loop
end
Regards
robert
···
At Tue, 13 Jan 2004 21:36:37 +0900, > Robert Klemme wrote: