all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to exit out of a function ? what is try-catch-throw in terms of Program Counter
@ 2007-10-20 20:45 gnuist006
  2007-10-20 20:57 ` gnuist006
                   ` (7 more replies)
  0 siblings, 8 replies; 33+ messages in thread
From: gnuist006 @ 2007-10-20 20:45 UTC (permalink / raw
  To: help-gnu-emacs

I have some code like this:

(if  (test)
    (exit)
    (do something))


or

(if (test)
   ( do something)
   (exit))


Various levels of nestings.

I have several questions, basic to sophisticated.

(1) What is the lisp equivalent idiom for (exit) as in bash or
in C.
(2) What is the best practice to handle this kind of problems?

(3) What is the intermediate practice to handle this kind of
problems.

NOTE: I am really afraid of try-catch-throw. I have never been
able to understand it since it does not exist in C and I cant
really visualize the construct in terms of C. That is what my
brain can process. If you understand it so well, you can show
me how one would really implement that kind of construct in
C and then by extension I can see that kind of program flow
in LISP. Whether its imperative programming or functional,
beneath there is program counter and assembly. C is close
to machine so much that it is almost assembly. So understanding try-c-
t in C is equivalent to understanding at
the level of machine language.

I therefore take the liberty to crosspost in C and C++ groups.

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-20 20:45 How to exit out of a function ? what is try-catch-throw in terms of Program Counter gnuist006
@ 2007-10-20 20:57 ` gnuist006
  2007-10-20 22:55 ` Alf P. Steinbach
                   ` (6 subsequent siblings)
  7 siblings, 0 replies; 33+ messages in thread
From: gnuist006 @ 2007-10-20 20:57 UTC (permalink / raw
  To: help-gnu-emacs

On Oct 20, 1:45 pm, gnuist...@gmail.com wrote:
> I have some code like this:
>
> (if  (test)
>     (exit)
>     (do something))
>
> or
>
> (if (test)
>    ( do something)
>    (exit))
>
> Various levels of nestings.
>
> I have several questions, basic to sophisticated.
>
> (1) What is the lisp equivalent idiom for (exit) as in bash or
> in C.
> (2) What is the best practice to handle this kind of problems?
>
> (3) What is the intermediate practice to handle this kind of
> problems.
>
> NOTE: I am really afraid of try-catch-throw. I have never been
> able to understand it since it does not exist in C and I cant
> really visualize the construct in terms of C. That is what my
> brain can process. If you understand it so well, you can show
> me how one would really implement that kind of construct in
> C and then by extension I can see that kind of program flow
> in LISP. Whether its imperative programming or functional,
> beneath there is program counter and assembly. C is close
> to machine so much that it is almost assembly. So understanding try-c-
> t in C is equivalent to understanding at
> the level of machine language.
>
> I therefore take the liberty to crosspost in C and C++ groups.

Is there any explanation of try-catch-throw in terms of
gotos than PC ? Is that explanation more simpler ?

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-20 20:45 How to exit out of a function ? what is try-catch-throw in terms of Program Counter gnuist006
  2007-10-20 20:57 ` gnuist006
@ 2007-10-20 22:55 ` Alf P. Steinbach
  2007-10-23 18:06   ` gnuist006
  2007-11-04 23:02   ` David Thompson
  2007-10-21  1:08 ` Jim Langston
                   ` (5 subsequent siblings)
  7 siblings, 2 replies; 33+ messages in thread
From: Alf P. Steinbach @ 2007-10-20 22:55 UTC (permalink / raw
  To: help-gnu-emacs

* gnuist006@gmail.com:
> I have some code like this:
> 
> (if  (test)
>     (exit)
>     (do something))
> 
> 
> or
> 
> (if (test)
>    ( do something)
>    (exit))
> 
> 
> Various levels of nestings.
> 
> I have several questions, basic to sophisticated.
> 
> (1) What is the lisp equivalent idiom for (exit) as in bash or
> in C.

C++ does not have a built-in 'exit' command.  There is a library 
function 'exit' which exits the process.  One must assume that's not 
what you mean, and that you're not asking C and C++ programmers to teach 
you Lisp.

Therefore, assuming you want to exit the function or the block.


> (2) What is the best practice to handle this kind of problems?

It's not a general class of problem.

Appropriate solutions depend on the problem at hand.

E.g., in C++,

   // (if (test) (exit) (do something))

   void foo()
   {
       if( !test )
       {
           doSomething();
       }
   }

   void bar()
   {
       if( test ) { return; }
       doSomething();
   }



> (3) What is the intermediate practice to handle this kind of
> problems.

?


> NOTE: I am really afraid of try-catch-throw. I have never been
> able to understand it since it does not exist in C and I cant
> really visualize the construct in terms of C. That is what my
> brain can process. If you understand it so well, you can show
> me how one would really implement that kind of construct in
> C and then by extension I can see that kind of program flow
> in LISP. Whether its imperative programming or functional,
> beneath there is program counter and assembly. C is close
> to machine so much that it is almost assembly. So understanding try-c-
> t in C is equivalent to understanding at
> the level of machine language.

The closest equivalent in C would be a 'longjmp'.  However, a C++ 
exception is more limited, in that it will only jump up the call chain, 
and it's more powerful, in that it will destroy local objects as it does 
so.  Also, if you use 'longjmp' in C++ you're practically doomed (unless 
you use it to jump between co-routines with their own stacks), because 
'longjmp' doesn't destroy local objects.



> I therefore take the liberty to crosspost in C and C++ groups.

Uh.

Cheers, & hth.,

- Alf

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-20 20:45 How to exit out of a function ? what is try-catch-throw in terms of Program Counter gnuist006
  2007-10-20 20:57 ` gnuist006
  2007-10-20 22:55 ` Alf P. Steinbach
@ 2007-10-21  1:08 ` Jim Langston
  2007-10-21  2:30 ` Keith Thompson
                   ` (4 subsequent siblings)
  7 siblings, 0 replies; 33+ messages in thread
From: Jim Langston @ 2007-10-21  1:08 UTC (permalink / raw
  To: help-gnu-emacs

<gnuist006@gmail.com> wrote in message 
news:1192913158.922454.108100@k35g2000prh.googlegroups.com...
>I have some code like this:
>
> (if  (test)
>    (exit)
>    (do something))
>
>
> or
>
> (if (test)
>   ( do something)
>   (exit))
>
>
> Various levels of nestings.
>
> I have several questions, basic to sophisticated.
>
> (1) What is the lisp equivalent idiom for (exit) as in bash or
> in C.

Your message post is asking "How to exit out of a function..." yet you are 
showing code that almost exits out of the program.  exit() would do that.

If you wish to exit out of the function then the correct keyword is return. 
Such as:

if (condition)
   return;
(do something)

if ( condition )
   (do something )
return;

return can return a value if the function returns a value, such as
return 1;
return foo;
return bar();
etc..

> (2) What is the best practice to handle this kind of problems?

It depends on coding style.  There is some coding style that states that any 
function will only have one return point.  In which case you would do:

if ( condition )
   ( do something )
return;

Personally, I chose to return early if it's what I would consider an error 
condition.

if ( condition )
   return NULL;
( do something )
return Value;

There really is no best way, it depends on coding standard and how easy the 
code is to write, read and maintain.

> (3) What is the intermediate practice to handle this kind of
> problems.

Again, it depends on the problems.  At different times in my career I have 
gone with returning whenever I've wanted to to returning only at the very 
end.
I find that huge if statments, however, make my code harder to write, read 
and maintain, so I will check for conditions early and return early if I 
can.  I try not to return in the middle of a function, but only at the top 
or bottom.  But it depends on the situation.

> NOTE: I am really afraid of try-catch-throw. I have never been
> able to understand it since it does not exist in C and I cant
> really visualize the construct in terms of C. That is what my
> brain can process. If you understand it so well, you can show
> me how one would really implement that kind of construct in
> C and then by extension I can see that kind of program flow
> in LISP. Whether its imperative programming or functional,
> beneath there is program counter and assembly. C is close
> to machine so much that it is almost assembly. So understanding try-c-
> t in C is equivalent to understanding at
> the level of machine language.
>
> I therefore take the liberty to crosspost in C and C++ groups.

As for try...catch blocks, I try to reserve those only for actual errors. 
One reason I have found I need to use them when a function is supposed to 
return a reference to an object, and that object doesn't exist.  I have to 
return something, and something is not good enough.  So I get around it by 
throwing out of the function.

(Untested code)

Player& FindPlayer( const std::string& Name, std::map<std::string, Player> )
{
   std::map<std::string, Player>::iterator it = Player.find( Name );
   if ( it == Player.end() )
   {
      // Player was not found in map.  Have to return something.  Lets just 
throw.
      throw std::exception("Player not found");
   }
   return (*it).second;
}

int main()
{
   // ...
   try
   {
       Player ThisPlayer& = FindPlayer( SomePlayer );
       // use ThisPlayer
    }
    castch ( std::exception e )
    {
       std::cout << "Player " << SomePlayer << " not found.\n";
    }
} 

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-20 20:45 How to exit out of a function ? what is try-catch-throw in terms of Program Counter gnuist006
                   ` (2 preceding siblings ...)
  2007-10-21  1:08 ` Jim Langston
@ 2007-10-21  2:30 ` Keith Thompson
  2007-10-21 10:44   ` Kenny McCormack
  2007-10-21  8:03 ` Malcolm McLean
                   ` (3 subsequent siblings)
  7 siblings, 1 reply; 33+ messages in thread
From: Keith Thompson @ 2007-10-21  2:30 UTC (permalink / raw
  To: help-gnu-emacs

gnuist006@gmail.com writes:
> I have some code like this:
>
> (if  (test)
>     (exit)
>     (do something))
>
>
> or
>
> (if (test)
>    ( do something)
>    (exit))

That's Lisp, yes?  Saying so would be good, since we naturally assume
that anything posted to comp.lang.c is C.

> Various levels of nestings.
>
> I have several questions, basic to sophisticated.
>
> (1) What is the lisp equivalent idiom for (exit) as in bash or
> in C.

You're expecting C programmers to know what (exit) does in Lisp?

[snip]

> I therefore take the liberty to crosspost in C and C++ groups.

That's rarely a good idea.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center             <*>  <http://users.sdsc.edu/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-20 20:45 How to exit out of a function ? what is try-catch-throw in terms of Program Counter gnuist006
                   ` (3 preceding siblings ...)
  2007-10-21  2:30 ` Keith Thompson
@ 2007-10-21  8:03 ` Malcolm McLean
  2007-10-21 16:07 ` abhy
                   ` (2 subsequent siblings)
  7 siblings, 0 replies; 33+ messages in thread
From: Malcolm McLean @ 2007-10-21  8:03 UTC (permalink / raw
  To: help-gnu-emacs


<gnuist006@gmail.com> wrote in message 
news:1192913158.922454.108100@k35g2000prh.googlegroups.com...
>I have some code like this:
>
> (if  (test)
>    (exit)
>    (do something))
>
>
> or
>
> (if (test)
>   ( do something)
>   (exit))
>
>
> Various levels of nestings.
>
> I have several questions, basic to sophisticated.
>
> (1) What is the lisp equivalent idiom for (exit) as in bash or
> in C.
> (2) What is the best practice to handle this kind of problems?
>
> (3) What is the intermediate practice to handle this kind of
> problems.
>
> NOTE: I am really afraid of try-catch-throw. I have never been
> able to understand it since it does not exist in C and I cant
> really visualize the construct in terms of C. That is what my
> brain can process. If you understand it so well, you can show
> me how one would really implement that kind of construct in
> C and then by extension I can see that kind of program flow
> in LISP. Whether its imperative programming or functional,
> beneath there is program counter and assembly. C is close
> to machine so much that it is almost assembly. So understanding try-c-
> t in C is equivalent to understanding at
> the level of machine language.
>
> I therefore take the liberty to crosspost in C and C++ groups.
>
C++ compilers that compile to C will produce C code something like this

C++

double safesqrt(double x)
{
   if(x < 0)
     throw "imaginary root";
  return sqrt(x);
}

double foo()
{
   if( pow( sqrt(-1), sqrt(-1)) != exp(-M_PI/2) )
     printf("Stupid computer can't do basic maths\n");
}

int main(void)
{
   try
  {
     foo();
  }
  catch(char * err)
  {
     printf("Sorry %s\n", err);
  }
}

C

void * safesqsrt(int *type, double *ret, double x)
{
   if(x < 0)
   {
     *type = CHARSTAR;
      return "imaginary root";
   }
    * ret = sqrt(x);
    return 0;
}

void *foo(int *type)
{
   double temp;
   void *throw;
   int throwtype;

   throw = safesqrt(&throwtype, &temp, -1.0);
   if(throw)
   {
      *type - throwtype;
      return throw;
   }
   /* etc */
}

int main(void)
{
   char *throw;
   int throwtype;
   char *err;

    throw = foo(&throwtype);
    if(throw)
      goto catch;
   return 0;
catch:
  switch(throwtype)
  {
     case CHARSTAR:
        err = throw;
        printf("Sorry %s\n", err);
        break;
  }

}

As you see it is totally impractical to try to do this in handwritten  C 
code for very long, though a compiler will happily chug through it. There 
are in fact more subtleties - local objects in foo() and safesqrt() have to 
be destroyed.

-- 
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-21  2:30 ` Keith Thompson
@ 2007-10-21 10:44   ` Kenny McCormack
  2007-10-21 10:55     ` santosh
  2007-10-21 12:09     ` Malcolm McLean
  0 siblings, 2 replies; 33+ messages in thread
From: Kenny McCormack @ 2007-10-21 10:44 UTC (permalink / raw
  To: help-gnu-emacs

In article <lnodetyzbc.fsf@nuthaus.mib.org>,
Keith Thompson  <kst-u@mib.org> wrote:
>gnuist006@gmail.com writes:
>> I have some code like this:
>>
>> (if  (test)
>>     (exit)
>>     (do something))
>>
>>
>> or
>>
>> (if (test)
>>    ( do something)
>>    (exit))
>
>That's Lisp, yes?  Saying so would be good, since we naturally assume
>that anything posted to comp.lang.c is C.

Actually, most of what is posted here is "not C".  Since, according to
many of the regulars, if it includes anything "off topic", it is "not C".

Since all code used in the real world uses extensions, there is no C
code in the real world.

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-21 10:44   ` Kenny McCormack
@ 2007-10-21 10:55     ` santosh
  2007-10-21 12:26       ` Richard Heathfield
  2007-10-21 22:35       ` Mark McIntyre
  2007-10-21 12:09     ` Malcolm McLean
  1 sibling, 2 replies; 33+ messages in thread
From: santosh @ 2007-10-21 10:55 UTC (permalink / raw
  To: help-gnu-emacs

Kenny McCormack wrote:

> In article <lnodetyzbc.fsf@nuthaus.mib.org>,
> Keith Thompson  <kst-u@mib.org> wrote:
>>gnuist006@gmail.com writes:
>>> I have some code like this:
>>>
>>> (if  (test)
>>>     (exit)
>>>     (do something))
>>>
>>>
>>> or
>>>
>>> (if (test)
>>>    ( do something)
>>>    (exit))
>>
>>That's Lisp, yes?  Saying so would be good, since we naturally assume
>>that anything posted to comp.lang.c is C.
> 
> Actually, most of what is posted here is "not C".  Since, according to
> many of the regulars, if it includes anything "off topic", it is "not
> C".
> 
> Since all code used in the real world uses extensions, there is no C
> code in the real world.

Perhaps you mean to say that there are no C _programs_ in the real
world. I'm sure there are pieces of fully Standard C code in many, if
not most programs. It's very likely though that the program, taken as a
whole, includes some non-Standard C.

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-21 10:44   ` Kenny McCormack
  2007-10-21 10:55     ` santosh
@ 2007-10-21 12:09     ` Malcolm McLean
  2007-10-21 15:23       ` Kenny McCormack
  1 sibling, 1 reply; 33+ messages in thread
From: Malcolm McLean @ 2007-10-21 12:09 UTC (permalink / raw
  To: help-gnu-emacs


"Kenny McCormack" <gazelle@xmission.xmission.com> wrote in message
>
> Since all code used in the real world uses extensions, there is no C
> code in the real world.
>
Sort of true. You'll find a non-trivial program on my website to build fuzzy 
logic trees. It is written in pure ANSI C89 except for one detail. The 
comma-separated value file loader uses nan to indicate missing values. 
Missing values are not allowed in the program, so it plays almost no part in 
the main flow control. But it loads, checks for nans, and rejects if they 
are present.

-- 
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-21 10:55     ` santosh
@ 2007-10-21 12:26       ` Richard Heathfield
  2007-10-21 22:35       ` Mark McIntyre
  1 sibling, 0 replies; 33+ messages in thread
From: Richard Heathfield @ 2007-10-21 12:26 UTC (permalink / raw
  To: help-gnu-emacs

[Followup corrected to clc]

santosh said:

> Kenny McCormack wrote:
> 
<snip>
 
>> Since all code used in the real world uses extensions, there is no C
>> code in the real world.
> 
> Perhaps you mean to say that there are no C _programs_ in the real
> world.

Even if that's what he meant, he's still wrong.

<snip>

-- 
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-21 12:09     ` Malcolm McLean
@ 2007-10-21 15:23       ` Kenny McCormack
  2007-10-21 15:54         ` santosh
  2007-10-21 17:39         ` Malcolm McLean
  0 siblings, 2 replies; 33+ messages in thread
From: Kenny McCormack @ 2007-10-21 15:23 UTC (permalink / raw
  To: help-gnu-emacs

In article <t46dnSTbU5T_3IbanZ2dnUVZ8sSrnZ2d@bt.com>,
Malcolm McLean <regniztar@btinternet.com> wrote:
>
>"Kenny McCormack" <gazelle@xmission.xmission.com> wrote in message
>>
>> Since all code used in the real world uses extensions, there is no C
>> code in the real world.
>>
>Sort of true. You'll find a non-trivial program on my website to build fuzzy 
>logic trees. It is written in pure ANSI C89 except for one detail. The 
>comma-separated value file loader uses nan to indicate missing values. 
>Missing values are not allowed in the program, so it plays almost no part in 
>the main flow control. But it loads, checks for nans, and rejects if they 
>are present.

Obviously, the statement that "_all_ code used in the real world..."
is false in the mathematical sense of the word "all", but it is true in
the normal sense of the word "all".

But here's the thing: I seriously doubt that, in the hosted world (at
any rate), anything that can be written in "ISO C" (or whatever term you
prefer) should be.  I.e., anything that is that pure (such as your fuzzy
logic program) could be written much more easily and readably in
something like AWK.

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-21 15:23       ` Kenny McCormack
@ 2007-10-21 15:54         ` santosh
  2007-10-21 17:39         ` Malcolm McLean
  1 sibling, 0 replies; 33+ messages in thread
From: santosh @ 2007-10-21 15:54 UTC (permalink / raw
  To: help-gnu-emacs

Kenny McCormack wrote:

<snip>

> But here's the thing: I seriously doubt that, in the hosted world (at
> any rate), anything that can be written in "ISO C" [ ... ]

Lookup libraries like libTomCrypt, libTomMath etc. Many applications
that do "number crunching", by and large, can be, and often are,
written in Standard C.

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-20 20:45 How to exit out of a function ? what is try-catch-throw in terms of Program Counter gnuist006
                   ` (4 preceding siblings ...)
  2007-10-21  8:03 ` Malcolm McLean
@ 2007-10-21 16:07 ` abhy
  2007-10-21 17:43   ` santosh
  2007-10-23  9:04 ` Joel Yliluoma
  2007-10-23 16:33 ` Stefan Monnier
  7 siblings, 1 reply; 33+ messages in thread
From: abhy @ 2007-10-21 16:07 UTC (permalink / raw
  To: help-gnu-emacs

On Oct 21, 1:45 am, gnuist...@gmail.com wrote:
> I have some code like this:
>
> (if  (test)
>     (exit)
>     (do something))
>
> or
>
> (if (test)
>    ( do something)
>    (exit))
>
> Various levels of nestings.
>
> I have several questions, basic to sophisticated.
>
> (1) What is the lisp equivalent idiom for (exit) as in bash or
> in C.
> (2) What is the best practice to handle this kind of problems?
>
> (3) What is the intermediate practice to handle this kind of
> problems.
>
> NOTE: I am really afraid of try-catch-throw. I have never been
> able to understand it since it does not exist in C and I cant
> really visualize the construct in terms of C. That is what my
> brain can process. If you understand it so well, you can show
> me how one would really implement that kind of construct in
> C and then by extension I can see that kind of program flow
> in LISP. Whether its imperative programming or functional,
> beneath there is program counter and assembly. C is close
> to machine so much that it is almost assembly. So understanding try-c-
> t in C is equivalent to understanding at
> the level of machine language.
>
> I therefore take the liberty to crosspost in C and C++ groups.

ok i guess we coudl write this one like as below.

if(test)
   do something

else
  exit


does this solve u r problem..?
If u want to exit from program exit is the keyword..if u want to break
from loops break is the key word.

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-21 15:23       ` Kenny McCormack
  2007-10-21 15:54         ` santosh
@ 2007-10-21 17:39         ` Malcolm McLean
  2007-10-21 21:53           ` Kenny McCormack
  1 sibling, 1 reply; 33+ messages in thread
From: Malcolm McLean @ 2007-10-21 17:39 UTC (permalink / raw
  To: help-gnu-emacs


"Kenny McCormack" <gazelle@xmission.xmission.com> wrote in message
>
> But here's the thing: I seriously doubt that, in the hosted world (at
> any rate), anything that can be written in "ISO C" (or whatever term you
> prefer) should be.  I.e., anything that is that pure (such as your fuzzy
> logic program) could be written much more easily and readably in
> something like AWK.
>
I don't know AWK. If you've got time, try doing it. Seriously. I am not in 
the business of selling C compilers, and if fuzzy logic trees are better 
implemented in another language I'd be glad to be aware of it.

-- 
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-21 16:07 ` abhy
@ 2007-10-21 17:43   ` santosh
  0 siblings, 0 replies; 33+ messages in thread
From: santosh @ 2007-10-21 17:43 UTC (permalink / raw
  To: help-gnu-emacs

abhy wrote:

<snip>

> If u want to exit from program exit is the keyword..

C has no keyword called exit. exit() is a Standard library function.

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-21 17:39         ` Malcolm McLean
@ 2007-10-21 21:53           ` Kenny McCormack
  0 siblings, 0 replies; 33+ messages in thread
From: Kenny McCormack @ 2007-10-21 21:53 UTC (permalink / raw
  To: help-gnu-emacs

In article <OaSdnQ1OjuhcE4banZ2dnUVZ8qOtnZ2d@bt.com>,
Malcolm McLean <regniztar@btinternet.com> wrote:
>
>"Kenny McCormack" <gazelle@xmission.xmission.com> wrote in message
>>
>> But here's the thing: I seriously doubt that, in the hosted world (at
>> any rate), anything that can be written in "ISO C" (or whatever term you
>> prefer) should be.  I.e., anything that is that pure (such as your fuzzy
>> logic program) could be written much more easily and readably in
>> something like AWK.
>>
>I don't know AWK. If you've got time, try doing it. Seriously. I am not in 
>the business of selling C compilers, and if fuzzy logic trees are better 
>implemented in another language I'd be glad to be aware of it.

I may just do that.  No promises, but I might get around to it at some
point.

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-21 10:55     ` santosh
  2007-10-21 12:26       ` Richard Heathfield
@ 2007-10-21 22:35       ` Mark McIntyre
  1 sibling, 0 replies; 33+ messages in thread
From: Mark McIntyre @ 2007-10-21 22:35 UTC (permalink / raw
  To: help-gnu-emacs

On Sun, 21 Oct 2007 16:25:28 +0530, in comp.lang.c , santosh
<santosh.k83@gmail.com> wrote:

>Kenny McCormack wrote:
>

>Perhaps you mean to say 

(no, he said exactly what he meant to say. He's a troll).

-- 

                                _____________________
                    /|  /|     |                     |
                    ||__||     |    Please do not    |
                   /   O O\__  |   feed the          |
                  /          \ |     Trolls          |
                 /      \     \|_____________________|
                /   _    \     \      ||
               /    |\____\     \     ||
              /     | | | |\____/     ||
             /       \|_|_|/   |     _||
            /  /  \            |____| ||
           /   |   |           |      --|
           |   |   |           |____  --|
    * _    |  |_|_|_|          |     \-/
 *-- _--\ _ \                  |      ||
   /  _     \\        |        /      `
 *  /   \_ /- |       |       |
   *      ___ c_c_c_C/ \C_c_c_c____________

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-20 20:45 How to exit out of a function ? what is try-catch-throw in terms of Program Counter gnuist006
                   ` (5 preceding siblings ...)
  2007-10-21 16:07 ` abhy
@ 2007-10-23  9:04 ` Joel Yliluoma
  2007-10-23 16:33 ` Stefan Monnier
  7 siblings, 0 replies; 33+ messages in thread
From: Joel Yliluoma @ 2007-10-23  9:04 UTC (permalink / raw
  To: help-gnu-emacs

On Sat, 20 Oct 2007 20:45:58 -0000, gnuist006@gmail.com wrote:
> NOTE: I am really afraid of try-catch-throw. I have never been
> able to understand it since it does not exist in C and I cant
> really visualize the construct in terms of C.

How try-catch-throw is actually implemented depends on the compiler,
but one can explain it like this.

Assume the following C++ code is written:

  #include <cstdio>

  // here's a sample object with a constructor and destructor
  // to demonstrate scope.
  class someobj
  {
  public:
      someobj() { std::puts("constructor"); }
      ~someobj() { std::puts("destructor"); }
  private:
      int x; // a dummy member variable
  };
  
  // A dummy type, it could be a typedef of int or whatever.
  // Just for the purpose of throwing an exception of this particular type.
  struct someexceptiontype
  {
  };

  void code_that_may_throw()
  {
      someobj obj; // instantiating someobj in this scope.
      
      if(false != true)
      {
          // some error situation happened, throw an exception.
          //  someexceptiontype() instantiates an object of
          //  "someexceptiontype" (without binding it into a variable),
          //  and throw throws it.
          throw someexceptiontype();
      }
      
      std::puts("wow, false is true");
  }
  
  void some_intermediate_function()
  {
      std::puts("1");
      code_that_may_throw();
      std::puts("2");
  }
  
  int main()
  {
      try
      {
          some_intermediate_function();
          std::puts("executed without hitch");
      }
      catch(int e)
      {
          std::puts("caught an int");
      }
      catch(someexceptiontype e)
      {
          std::puts("caught someexceptiontype");
      }
      std::puts("end of main()");
      return 0;
  }

The code above contains high-level concepts that approximately translate
to the following lower-level concepts in C. It could be implemented
differently, but the function is the same.

  #include <stdio.h>
  
  typedef struct someobj
  {
      int x;
  } someobj;
  
  void someobj__construct(someobj* this)
  {
      puts("constructor");
      if(__system_exception_ptr) goto __scope_end;
  __scope_end: ;
  }
  void someobj__destruct(someobj* this)
  {
      puts("destructor");
      if(__system_exception_ptr) goto __scope_end;
  __scope_end: ;
  }
  
  struct someexceptiontype
  {
  };
  
  /*** This global code is defined in some system library by the compiler */
  void* __system_exception_ptr  = (void*)0;
  int   __system_exception_type = 0;
  void __clear_exception()
  {
      __system_exception_type = 0;
      free(__system_exception_ptr);
      __system_exception_ptr = (void*)0;
  }
  /*** End of compiler library code */

  void code_that_may_throw(void)
  {
      someobj obj; // instantiating someobj in this scope.
      someobj__construct(&obj);
      if(__system_exception_ptr) goto __scope_end_before_obj;
      
      if(0 != 1)
      {
          someexceptiontype* e = (someexceptiontype*) malloc(sizeof(*e));
          __system_exception_ptr  = e;
          __system_exception_type = 2;
          /* ^ a compiler-specific tag that identifies the exception type */
          goto __scope_end;
      }
      
      puts("wow, false is true");
      if(__system_exception_ptr) goto __scope_end;
      
  __scope_end: ;
      someobj__destruct(&obj);
  __scope_end_before_obj: ;
  }
  
  void some_intermediate_function(void)
  {
      puts("1");
      if(__system_exception_ptr) goto __scope_end;
      
      code_that_may_throw();
      if(__system_exception_ptr) goto __scope_end;
      
      puts("2");
      if(__system_exception_ptr) goto __scope_end;
  __scope_end: ;
  }
  
  int main(void)
  {
      some_intermediate_function();
      if(__system_exception_ptr) goto try_catch;
      puts("executed without hitch");
      if(__system_exception_ptr) goto try_catch;
      goto past_catch;
  try_catch: ;
      switch(__system_exception_type)
      {
          case 1: /* example denoting int type */
          {
              __clear_exception();
              puts("caught an int");
	      if(__system_exception_ptr) goto __scope_end;
              break;
          }
          case 2: /* example denoting someexceptiontype */
          {
              __clear_exception();
              puts("caught someexceptiontype");
	      if(__system_exception_ptr) goto __scope_end;
              break;
          }
          default:
              goto __scope_end; /* still not caught */
      }
  past_catch: ;
      puts("end of main()");
      if(__system_exception_ptr) goto __scope_end;
  
  __scope_end: ;
      return 0;
  }

Of course, for efficiency reasons there is no "if" test after every
function return for exceptions (rather, execution may be transferred
to a dedicated stack and scope unfolder when an exception happens),
but this was the easiest way to explain what happens as regards for
scopes and execution paths.
Also, in the exception handler (catch {}), the exception object is
not supposed to be deallocated until the end of the handler, but
for simplicity I wrote the deallocation first.

Followups set to comp.lang.c++ .

-- 
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-20 20:45 How to exit out of a function ? what is try-catch-throw in terms of Program Counter gnuist006
                   ` (6 preceding siblings ...)
  2007-10-23  9:04 ` Joel Yliluoma
@ 2007-10-23 16:33 ` Stefan Monnier
  2007-10-23 16:44   ` gnuist006
                     ` (3 more replies)
  7 siblings, 4 replies; 33+ messages in thread
From: Stefan Monnier @ 2007-10-23 16:33 UTC (permalink / raw
  To: help-gnu-emacs

> NOTE: I am really afraid of try-catch-throw. I have never been
> able to understand it since it does not exist in C and I can't
> really visualize the construct in terms of C. That is what my

Actually, these constructs pretty much exist in C as well: `catch' is called
`setjmp', and `throw' is called `longjmp'.


        Stefan

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-23 16:33 ` Stefan Monnier
@ 2007-10-23 16:44   ` gnuist006
  2007-10-23 16:45   ` Victor Bazarov
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 33+ messages in thread
From: gnuist006 @ 2007-10-23 16:44 UTC (permalink / raw
  To: help-gnu-emacs

On Oct 23, 9:33 am, Stefan Monnier <monn...@iro.umontreal.ca> wrote:
> > NOTE: I am really afraid of try-catch-throw. I have never been
> > able to understand it since it does not exist in C and I can't
> > really visualize the construct in terms of C. That is what my
>
> Actually, these constructs pretty much exist in C as well: `catch' is called
> `setjmp', and `throw' is called `longjmp'.
>
>         Stefan

Is it in some obscure corner of K&R ANSI ? I was never taught this one
by my instructor. Can you explain its syntax and patterns of usage ?

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-23 16:33 ` Stefan Monnier
  2007-10-23 16:44   ` gnuist006
@ 2007-10-23 16:45   ` Victor Bazarov
  2007-10-24  1:06     ` Stefan Monnier
  2007-10-23 16:54   ` gnuist006
  2007-10-24  0:02   ` Joel Yliluoma
  3 siblings, 1 reply; 33+ messages in thread
From: Victor Bazarov @ 2007-10-23 16:45 UTC (permalink / raw
  To: help-gnu-emacs

Stefan Monnier wrote:
>> NOTE: I am really afraid of try-catch-throw. I have never been
>> able to understand it since it does not exist in C and I can't
>> really visualize the construct in terms of C. That is what my
>
> Actually, these constructs pretty much exist in C as well: `catch' is
> called `setjmp', and `throw' is called `longjmp'.

I believe a better way would be to imagine that 'try', not 'catch',
is called 'setjmp'.

V
-- 
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask 

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-23 16:33 ` Stefan Monnier
  2007-10-23 16:44   ` gnuist006
  2007-10-23 16:45   ` Victor Bazarov
@ 2007-10-23 16:54   ` gnuist006
  2007-10-23 17:14     ` Alf P. Steinbach
  2007-10-24  1:09     ` Stefan Monnier
  2007-10-24  0:02   ` Joel Yliluoma
  3 siblings, 2 replies; 33+ messages in thread
From: gnuist006 @ 2007-10-23 16:54 UTC (permalink / raw
  To: help-gnu-emacs

On Oct 23, 9:33 am, Stefan Monnier <monn...@iro.umontreal.ca> wrote:
> > NOTE: I am really afraid of try-catch-throw. I have never been
> > able to understand it since it does not exist in C and I can't
> > really visualize the construct in terms of C. That is what my
>
> Actually, these constructs pretty much exist in C as well: `catch' is called
> `setjmp', and `throw' is called `longjmp'.
>
>         Stefan

Stefan, let me thank you for what seems to me to be the correct
concept.
I searched this whole thread in google for setjmp and YOU are the only
one who mentioned it. I applaud you. Because, it does not seem that
there
is any other construct that can implement try-catch-throw. I still
have to
read up on it, but thats what my gut instinct says.

Anyone, care to show how this translates into assembly after we deal
thoroughly with this in the context of C ?

Everyone, please ignore the the mean spirits trying to derail a
serious
conceptual discussions and calling each other trolls or giving
obfuscated
explanations for ego purposes, and not LUCID explanation.

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-23 16:54   ` gnuist006
@ 2007-10-23 17:14     ` Alf P. Steinbach
  2007-10-24  1:09     ` Stefan Monnier
  1 sibling, 0 replies; 33+ messages in thread
From: Alf P. Steinbach @ 2007-10-23 17:14 UTC (permalink / raw
  To: help-gnu-emacs

* gnuist006@gmail.com:
> On Oct 23, 9:33 am, Stefan Monnier <monn...@iro.umontreal.ca> wrote:
>>> NOTE: I am really afraid of try-catch-throw. I have never been
>>> able to understand it since it does not exist in C and I can't
>>> really visualize the construct in terms of C. That is what my
>> Actually, these constructs pretty much exist in C as well: `catch' is called
>> `setjmp', and `throw' is called `longjmp'.
>>
>>         Stefan
> 
> Stefan, let me thank you for what seems to me to be the correct
> concept.
> I searched this whole thread in google for setjmp and YOU are the only
> one who mentioned it.

Uh, have you plonked me, then?

I think that my reply was the very first reply in the thread.

I suggest you read that article again, because it contains some 
important details not mentioned by Stefan et.al.


- Alf

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-20 22:55 ` Alf P. Steinbach
@ 2007-10-23 18:06   ` gnuist006
  2007-10-23 19:53     ` Alf P. Steinbach
  2007-11-04 23:02   ` David Thompson
  1 sibling, 1 reply; 33+ messages in thread
From: gnuist006 @ 2007-10-23 18:06 UTC (permalink / raw
  To: help-gnu-emacs

On Oct 20, 3:55 pm, "Alf P. Steinbach" <al...@start.no> wrote:
> * gnuist...@gmail.com:
>
>
>
> > I have some code like this:
>
> > (if  (test)
> >     (exit)
> >     (do something))
>
> > or
>
> > (if (test)
> >    ( do something)
> >    (exit))
>
> > Various levels of nestings.
>
> > I have several questions, basic to sophisticated.
>
> > (1) What is the lisp equivalent idiom for (exit) as in bash or
> > in C.
>
> C++ does not have a built-in 'exit' command.  There is a library
> function 'exit' which exits the process.  One must assume that's not
> what you mean, and that you're not asking C and C++ programmers to teach
> you Lisp.
>
> Therefore, assuming you want to exit the function or the block.
>
> > (2) What is the best practice to handle this kind of problems?
>
> It's not a general class of problem.
>
> Appropriate solutions depend on the problem at hand.
>
> E.g., in C++,
>
>    // (if (test) (exit) (do something))
>
>    void foo()
>    {
>        if( !test )
>        {
>            doSomething();
>        }
>    }
>
>    void bar()
>    {
>        if( test ) { return; }
>        doSomething();
>    }
>
> > (3) What is the intermediate practice to handle this kind of
> > problems.
>
> ?
>
> > NOTE: I am really afraid of try-catch-throw. I have never been
> > able to understand it since it does not exist in C and I cant
> > really visualize the construct in terms of C. That is what my
> > brain can process. If you understand it so well, you can show
> > me how one would really implement that kind of construct in
> > C and then by extension I can see that kind of program flow
> > in LISP. Whether its imperative programming or functional,
> > beneath there is program counter and assembly. C is close
> > to machine so much that it is almost assembly. So understanding try-c-
> > t in C is equivalent to understanding at
> > the level of machine language.
>
> The closest equivalent in C would be a 'longjmp'.  However, a C++
> exception is more limited, in that it will only jump up the call chain,
> and it's more powerful, in that it will destroy local objects as it does
> so.  Also, if you use 'longjmp' in C++ you're practically doomed (unless
> you use it to jump between co-routines with their own stacks), because
> 'longjmp' doesn't destroy local objects.

Sure you have good ideas.

I still would like an equivalent implementation explained. Ofcourse,
smart
companies and smart programmers were doing all this before C++ came
and even in LISP they have atleast two of try catch throw.

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-23 18:06   ` gnuist006
@ 2007-10-23 19:53     ` Alf P. Steinbach
  0 siblings, 0 replies; 33+ messages in thread
From: Alf P. Steinbach @ 2007-10-23 19:53 UTC (permalink / raw
  To: help-gnu-emacs

* gnuist006@gmail.com:
> On Oct 20, 3:55 pm, "Alf P. Steinbach" <al...@start.no> wrote:
>> The closest equivalent in C would be a 'longjmp'.  However, a C++
>> exception is more limited, in that it will only jump up the call chain,
>> and it's more powerful, in that it will destroy local objects as it does
>> so.  Also, if you use 'longjmp' in C++ you're practically doomed (unless
>> you use it to jump between co-routines with their own stacks), because
>> 'longjmp' doesn't destroy local objects.
> 
> Sure you have good ideas.

Note that some people could read that as an attempt at insulting.


> I still would like an equivalent implementation explained. Ofcourse,
> smart
> companies and smart programmers were doing all this before C++ came
> and even in LISP they have atleast two of try catch throw.

Ada introduced to the wider community much that was subsequently adopted 
in C++.  Interestingly, (much of) the STL was implemented in Ada before 
it was implemented in C++.  And perhaps also interestingly, Ada's high 
level thread primitives are seemingly now /not/ considered for C++.

Now as for equivalence, you don't really want C code, because that would 
have to emulate C++ objects!

But in C++ such longjm-based code is hairy compiler-dependent stuff, 
with formally Undefined Behavior.

Also, as an example of equivalent-except-for-efficiency, note that a 
call of a virtual function can be specified equivalently as a dynamic 
lookup in most derived class, base class, base class' base class and so 
on, a search up the base class chain, but is in actuality implemented as 
a table look-up (with all compilers).  Exceptions are implemented in 
more than just one main way.  However, analogously to the case with 
virtual functions, equivalent code that performs dynamic lookup, such as 
the code below, is extremely ineffecient compared to the Real Thing(TM).

Depending on the actual implementation of exceptions, there can be no 
overhead at all for normal case code.


<code>
#include    <vector>
#include    <csetjmp>
#include    <string>
#include    <iostream>
#include    <ostream>

#if         defined( _MSC_VER )
#   define  LNGJMP_DESTROYS_AUTOMAGICALLY

#elif       defined( __GNUC__ )
#   undef   LNGJMP_DESTROYS_AUTOMAGICALLY
#   // No automatic destruction, at least in MingW 3.4.4 version.

#else
#   error Your compiler is not supported by this program.
#endif

struct AbstractLngjmpCleanup
{
     virtual ~AbstractLngjmpCleanup() {}
     virtual void destroy() = 0;
};

template< typename T >
struct LngjmpCleanup: AbstractLngjmpCleanup
{
     T*  myTarget;
     LngjmpCleanup( T& target ): myTarget( &target ) {}
     virtual void destroy()
     {
         #ifndef LNGJMP_DESTROYS_AUTOMAGICALLY
             myTarget->T::~T();      // Call destructor on target.
         #endif
     }
};

struct LongjmpCleanups
{
     std::vector<AbstractLngjmpCleanup*> myDestroyers;

     ~LongjmpCleanups()
     {
         for( size_t i = 0;  i < myDestroyers.size();  ++i )
         {
             delete myDestroyers.at( i );
         }
     }

     template< typename T >
     void add( T& target )
     {
         myDestroyers.push_back( new LngjmpCleanup<T>( target ) );
     }

     void destroyAll()
     {
         for( size_t i = 0;  i < myDestroyers.size();  ++i )
         {
             myDestroyers.at( i )->destroy();
         }
     }
};

template< typename T >
void say( T const& v ) { std::cout << v << std::endl; }

struct Whatever
{
     std::string myId;
     Whatever( std::string id ): myId( id )
     { say( "Constructed " + myId + "." ); }

     ~Whatever()
     { say( "Destroyed " + myId + "." ); }
};


jmp_buf*    pReturnAddress = 0;

void bottom()
{
     LongjmpCleanups                 destroyers;
     LngjmpCleanup<LongjmpCleanups>  destroyersDestroyer( destroyers );

     Whatever    localObject( "bottom()'s local object" );

     destroyers.add( localObject );

     say( "Executing body of bottom()." );

     say( "Throwing simulated exception." );
     {
         destroyers.destroyAll();
         destroyersDestroyer.destroy();
         longjmp( *pReturnAddress, 1 );
     }
}

void middle()
{
     jmp_buf                         returnAddress;
     jmp_buf*                        pOldReturnAddress;
     LongjmpCleanups                 destroyers;
     LngjmpCleanup<LongjmpCleanups>  destroyersDestroyer( destroyers );

     Whatever    localObject( "middle()'s local object" );

     destroyers.add( localObject );
     pOldReturnAddress = pReturnAddress;
     if( setjmp( returnAddress ) == 0 )
     {
         pReturnAddress = &returnAddress;

         say( "Executing body of middle(), calling bottom()." );
         bottom();

         pReturnAddress = pOldReturnAddress;
     }
     else
     {
         destroyers.destroyAll();
         destroyersDestroyer.destroy();
         pReturnAddress = pOldReturnAddress;
         longjmp( *pReturnAddress, 1 );
     }
}

void top()
{
     jmp_buf                         returnAddress;
     jmp_buf*                        pOldReturnAddress;
     LongjmpCleanups                 destroyers;
     LngjmpCleanup<LongjmpCleanups>  destroyersDestroyer( destroyers );

     Whatever    localObject( "top()'s local object" );

     destroyers.add( localObject );
     pOldReturnAddress = pReturnAddress;
     if( setjmp( returnAddress ) == 0 )
     {
         pReturnAddress = &returnAddress;

         say( "Executing body of top(), calling middle()." );
         middle();

         pReturnAddress = pOldReturnAddress;
     }
     else
     {
         destroyers.destroyAll();
         destroyersDestroyer.destroy();
         pReturnAddress = pOldReturnAddress;
         longjmp( *pReturnAddress, 1 );
     }
}

int main()
{
     jmp_buf     returnAddress;

     pReturnAddress = &returnAddress;
     if( setjmp( returnAddress ) == 0 )
     {
         say( "Main business code, calling top()..." );
         top();
         return EXIT_SUCCESS;
     }
     else
     {
         say( "Caught simulated exception!" );
         return EXIT_FAILURE;
     }
}
</code>

<output>
Main business code, calling top()...
Constructed top()'s local object.
Executing body of top(), calling middle().
Constructed middle()'s local object.
Executing body of middle(), calling bottom().
Constructed bottom()'s local object.
Executing body of bottom().
Throwing simulated exception.
Destroyed bottom()'s local object.
Destroyed middle()'s local object.
Destroyed top()'s local object.
Caught simulated exception!
</output>

Now I leave it as an exercise to reimplement this program to use C++ 
exceptions instead of longjmp, and perhaps compare clarity (and 
efficiency, if that's interesting).

Cheers, & hth.,

- Alf


-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-23 16:33 ` Stefan Monnier
                     ` (2 preceding siblings ...)
  2007-10-23 16:54   ` gnuist006
@ 2007-10-24  0:02   ` Joel Yliluoma
  3 siblings, 0 replies; 33+ messages in thread
From: Joel Yliluoma @ 2007-10-24  0:02 UTC (permalink / raw
  To: help-gnu-emacs

On Tue, 23 Oct 2007 12:33:17 -0400, Stefan Monnier wrote:
>> NOTE: I am really afraid of try-catch-throw. I have never been
>> able to understand it since it does not exist in C and I can't
>> really visualize the construct in terms of C. That is what my
>
> Actually, these constructs pretty much exist in C as well:
> `catch' is called `setjmp', and `throw' is called `longjmp'.

If you ignore the thing about scope that I was being very careful
to illustrate properly, then yes.
And, the fact that try-catch blocks can be nested, recursed, etc,
and only catching the matching type of exception stops the unwinding.

-- 
Joel Yliluoma - http://bisqwit.iki.fi/
: comprehension = 1 / (2 ^ precision)

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-23 16:45   ` Victor Bazarov
@ 2007-10-24  1:06     ` Stefan Monnier
  0 siblings, 0 replies; 33+ messages in thread
From: Stefan Monnier @ 2007-10-24  1:06 UTC (permalink / raw
  To: help-gnu-emacs

>>> NOTE: I am really afraid of try-catch-throw. I have never been
>>> able to understand it since it does not exist in C and I can't
>>> really visualize the construct in terms of C. That is what my
>> 
>> Actually, these constructs pretty much exist in C as well: `catch' is
>> called `setjmp', and `throw' is called `longjmp'.

> I believe a better way would be to imagine that 'try', not 'catch',
> is called 'setjmp'.

Sorry, I'm reading this on gnu.emacs.help where Elisp only provides `catch'
and `throw' (no `try') and these map pretty closely to setjmp/longjmp.


        Stefan

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-23 16:54   ` gnuist006
  2007-10-23 17:14     ` Alf P. Steinbach
@ 2007-10-24  1:09     ` Stefan Monnier
  1 sibling, 0 replies; 33+ messages in thread
From: Stefan Monnier @ 2007-10-24  1:09 UTC (permalink / raw
  To: help-gnu-emacs

> Anyone, care to show how this translates into assembly after we deal
> thoroughly with this in the context of C ?

I believe that one way to look at setjmp/longjmp in C is that setjmp saves
a copy of the registers (most importantly PC and SP) and longjmp uses that
copy to jump back to the corresponding point in the program (and stack
activation).


        Stefan

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-10-20 22:55 ` Alf P. Steinbach
  2007-10-23 18:06   ` gnuist006
@ 2007-11-04 23:02   ` David Thompson
  2007-11-05  5:07     ` Alf P. Steinbach
  1 sibling, 1 reply; 33+ messages in thread
From: David Thompson @ 2007-11-04 23:02 UTC (permalink / raw
  To: help-gnu-emacs

On Sun, 21 Oct 2007 00:55:53 +0200, "Alf P. Steinbach"
<alfps@start.no> wrote:

> * gnuist006@gmail.com:

> > NOTE: I am really afraid of try-catch-throw. I have never been
> > able to understand it since it does not exist in C and I cant
> > really visualize the construct in terms of C. <snip>
> 
> The closest equivalent in C would be a 'longjmp'.  However, a C++ 
> exception is more limited, in that it will only jump up the call chain, 

C longjmp/setjmp also is only guaranteed to work up the stack; the
fact that _some_ implementations can work cross-stack and in
particular cross-thread is not standard nor portable.

> and it's more powerful, in that it will destroy local objects as it does 
> so.  Also, if you use 'longjmp' in C++ you're practically doomed (unless 
> you use it to jump between co-routines with their own stacks), because 
> 'longjmp' doesn't destroy local objects.
> 
Actually it's Undefined Behavior; a good quality C++ implementation
CAN coordinate longjmp, and also pthreads cancellation, with
exceptions to destruct locals cleanly -- but it's not required.

- formerly david.thompson1 || achar(64) || worldnet.att.net

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-11-04 23:02   ` David Thompson
@ 2007-11-05  5:07     ` Alf P. Steinbach
  2007-11-05  5:59       ` Alf P. Steinbach
  2007-12-02 23:20       ` David Thompson
  0 siblings, 2 replies; 33+ messages in thread
From: Alf P. Steinbach @ 2007-11-05  5:07 UTC (permalink / raw
  To: help-gnu-emacs

* David Thompson:
> On Sun, 21 Oct 2007 00:55:53 +0200, "Alf P. Steinbach"
> <alfps@start.no> wrote:
> 
>> * gnuist006@gmail.com:
> 
>>> NOTE: I am really afraid of try-catch-throw. I have never been
>>> able to understand it since it does not exist in C and I cant
>>> really visualize the construct in terms of C. <snip>
>> The closest equivalent in C would be a 'longjmp'.  However, a C++ 
>> exception is more limited, in that it will only jump up the call chain, 
> 
> C longjmp/setjmp also is only guaranteed to work up the stack; the
> fact that _some_ implementations can work cross-stack and in
> particular cross-thread is not standard nor portable.

So?

But also, what on Earth do you mean by a cross-thread longjmp?  I 
implemented coroutines in terms of longjmp at the time that was popular, 
so the concepts involved are not unfamiliar to me.  Yet I fail to 
envision what you could be talking about, especially as "fact".  I think 
perhaps you're talking about restoring the full context (registers etc) 
of a moment in a thread's execution?


>> and it's more powerful, in that it will destroy local objects as it does 
>> so.  Also, if you use 'longjmp' in C++ you're practically doomed (unless 
>> you use it to jump between co-routines with their own stacks), because 
>> 'longjmp' doesn't destroy local objects.
>>
> Actually it's Undefined Behavior; a good quality C++ implementation
> CAN coordinate longjmp, and also pthreads cancellation, with
> exceptions to destruct locals cleanly -- but it's not required.

I don't know about ptheads cancellation, but other than that you're 
right.  Visual C++ coordinates longjmp with C++ stack unwinding.  g++, 
on the other hand, does not.

Cheers, & hth.,

- Alf

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-11-05  5:07     ` Alf P. Steinbach
@ 2007-11-05  5:59       ` Alf P. Steinbach
  2007-12-02 23:20       ` David Thompson
  1 sibling, 0 replies; 33+ messages in thread
From: Alf P. Steinbach @ 2007-11-05  5:59 UTC (permalink / raw
  To: help-gnu-emacs

* Alf P. Steinbach:
> * David Thompson:
>> On Sun, 21 Oct 2007 00:55:53 +0200, "Alf P. Steinbach"
>> <alfps@start.no> wrote:
>>
>>> * gnuist006@gmail.com:
>>
>>>> NOTE: I am really afraid of try-catch-throw. I have never been
>>>> able to understand it since it does not exist in C and I cant
>>>> really visualize the construct in terms of C. <snip>
>>> The closest equivalent in C would be a 'longjmp'.  However, a C++ 
>>> exception is more limited, in that it will only jump up the call chain, 
>>
>> C longjmp/setjmp also is only guaranteed to work up the stack; the
>> fact that _some_ implementations can work cross-stack and in
>> particular cross-thread is not standard nor portable.
> 
> So?
> 
> But also, what on Earth do you mean by a cross-thread longjmp?  I 
> implemented coroutines in terms of longjmp at the time that was popular, 
> so the concepts involved are not unfamiliar to me.  Yet I fail to 
> envision what you could be talking about, especially as "fact".  I think 
> perhaps you're talking about restoring the full context (registers etc) 
> of a moment in a thread's execution?
> 
> 
>>> and it's more powerful, in that it will destroy local objects as it 
>>> does so.  Also, if you use 'longjmp' in C++ you're practically doomed 
>>> (unless you use it to jump between co-routines with their own 
>>> stacks), because 'longjmp' doesn't destroy local objects.
>>>
>> Actually it's Undefined Behavior; a good quality C++ implementation
>> CAN coordinate longjmp, and also pthreads cancellation, with
>> exceptions to destruct locals cleanly -- but it's not required.
> 
> I don't know about ptheads cancellation, but other than that you're 
> right.  Visual C++ coordinates longjmp with C++ stack unwinding.  g++, 
> on the other hand, does not.

Sorry, I didn't see the weasel-word "actually", which indicates a 
contradiction.

When I wrote that you're right, that just meant that you supplied some 
extra info that wasn't incorrect.

Cheers,

- Alf


-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-11-05  5:07     ` Alf P. Steinbach
  2007-11-05  5:59       ` Alf P. Steinbach
@ 2007-12-02 23:20       ` David Thompson
  2007-12-02 23:51         ` Alf P. Steinbach
  1 sibling, 1 reply; 33+ messages in thread
From: David Thompson @ 2007-12-02 23:20 UTC (permalink / raw
  To: help-gnu-emacs

On Mon, 05 Nov 2007 06:07:25 +0100, "Alf P. Steinbach"
<alfps@start.no> wrote:

> * David Thompson:
> > On Sun, 21 Oct 2007 00:55:53 +0200, "Alf P. Steinbach"
> > <alfps@start.no> wrote:

> >> The closest equivalent in C would be a 'longjmp'.  However, a C++ 
> >> exception is more limited, in that it will only jump up the call chain, 
> > 
> > C longjmp/setjmp also is only guaranteed to work up the stack; the
> > fact that _some_ implementations can work cross-stack and in
> > particular cross-thread is not standard nor portable.
> 
> So?
> 
> But also, what on Earth do you mean by a cross-thread longjmp?  I 
> implemented coroutines in terms of longjmp at the time that was popular, 
> so the concepts involved are not unfamiliar to me.  Yet I fail to 
> envision what you could be talking about, especially as "fact".  I think 

IME 'coroutine' has been used for several slightly different concepts,
but if you mean the one of separate threads of control passing CPU
ownership often along with data anytime they choose, also known more
specifically as cooperative/nonpreemptive threading/tasking, yes. I
think you are agreeing that it did actually work, because 'restoring'
PC and SP (or equivalents) was enough; but I am pointing out it wasn't
and isn't _required_ to work that way.

> perhaps you're talking about restoring the full context (registers etc) 
> of a moment in a thread's execution?
> 
IME a cooperative switch itself doesn't need to save and restore other
state, as the language mechanism(s) e.g. 'call yield' handle it. Or
for cache-y things it happens automatically, or mostly automatically.

- formerly david.thompson1 || achar(64) || worldnet.att.net

^ permalink raw reply	[flat|nested] 33+ messages in thread

* Re: How to exit out of a function ? what is try-catch-throw in terms of Program Counter
  2007-12-02 23:20       ` David Thompson
@ 2007-12-02 23:51         ` Alf P. Steinbach
  0 siblings, 0 replies; 33+ messages in thread
From: Alf P. Steinbach @ 2007-12-02 23:51 UTC (permalink / raw
  To: help-gnu-emacs

* David Thompson:
> On Mon, 05 Nov 2007 06:07:25 +0100, "Alf P. Steinbach"
> <alfps@start.no> wrote:
> 
>> * David Thompson:
>>> On Sun, 21 Oct 2007 00:55:53 +0200, "Alf P. Steinbach"
>>> <alfps@start.no> wrote:
> 
>>>> The closest equivalent in C would be a 'longjmp'.  However, a C++ 
>>>> exception is more limited, in that it will only jump up the call chain, 
>>> C longjmp/setjmp also is only guaranteed to work up the stack; the
>>> fact that _some_ implementations can work cross-stack and in
>>> particular cross-thread is not standard nor portable.
>> So?
>>
>> But also, what on Earth do you mean by a cross-thread longjmp?  I 
>> implemented coroutines in terms of longjmp at the time that was popular, 
>> so the concepts involved are not unfamiliar to me.  Yet I fail to 
>> envision what you could be talking about, especially as "fact".  I think 
> 
> IME 'coroutine' has been used for several slightly different concepts,
> but if you mean the one of separate threads of control passing CPU
> ownership often along with data anytime they choose, also known more
> specifically as cooperative/nonpreemptive threading/tasking, yes. I
> think you are agreeing that it did actually work, because 'restoring'
> PC and SP (or equivalents) was enough; but I am pointing out it wasn't
> and isn't _required_ to work that way.
> 
>> perhaps you're talking about restoring the full context (registers etc) 
>> of a moment in a thread's execution?
>>
> IME a cooperative switch itself doesn't need to save and restore other
> state, as the language mechanism(s) e.g. 'call yield' handle it. Or
> for cache-y things it happens automatically, or mostly automatically.

Sorry, I fail to see the point, whatever it is.

But regarding definition of 'coroutine', it really doesn't map to more 
than one concept.

Coroutines are treated in Knuth's TAOCPM, which locked in the 
terminology (although Knuth didn't always succeed in in establishing 
convention, e.g. he had to redraw his trees because he at first did them 
with the root down, while the rest of the CS community chose root up, 
and same for his misconception of "real time" as "reel time", he 
couldn't make that stick either :-) ); some languages, notably Modula-2, 
had built-in support for coroutines; you can find some dicussion of 
coroutines at <url: http://en.wikipedia.org/wiki/Coroutine>.

Cheers, & hth.,

- Alf

-- 
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

^ permalink raw reply	[flat|nested] 33+ messages in thread

end of thread, other threads:[~2007-12-02 23:51 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-20 20:45 How to exit out of a function ? what is try-catch-throw in terms of Program Counter gnuist006
2007-10-20 20:57 ` gnuist006
2007-10-20 22:55 ` Alf P. Steinbach
2007-10-23 18:06   ` gnuist006
2007-10-23 19:53     ` Alf P. Steinbach
2007-11-04 23:02   ` David Thompson
2007-11-05  5:07     ` Alf P. Steinbach
2007-11-05  5:59       ` Alf P. Steinbach
2007-12-02 23:20       ` David Thompson
2007-12-02 23:51         ` Alf P. Steinbach
2007-10-21  1:08 ` Jim Langston
2007-10-21  2:30 ` Keith Thompson
2007-10-21 10:44   ` Kenny McCormack
2007-10-21 10:55     ` santosh
2007-10-21 12:26       ` Richard Heathfield
2007-10-21 22:35       ` Mark McIntyre
2007-10-21 12:09     ` Malcolm McLean
2007-10-21 15:23       ` Kenny McCormack
2007-10-21 15:54         ` santosh
2007-10-21 17:39         ` Malcolm McLean
2007-10-21 21:53           ` Kenny McCormack
2007-10-21  8:03 ` Malcolm McLean
2007-10-21 16:07 ` abhy
2007-10-21 17:43   ` santosh
2007-10-23  9:04 ` Joel Yliluoma
2007-10-23 16:33 ` Stefan Monnier
2007-10-23 16:44   ` gnuist006
2007-10-23 16:45   ` Victor Bazarov
2007-10-24  1:06     ` Stefan Monnier
2007-10-23 16:54   ` gnuist006
2007-10-23 17:14     ` Alf P. Steinbach
2007-10-24  1:09     ` Stefan Monnier
2007-10-24  0:02   ` Joel Yliluoma

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.