On Sun, 05 Sep 2010 18:24:43 +0400, Philippe Sigaud
<philippe.sig...@gmail.com> wrote:
2010/9/5 Denis Koroskin <2kor...@gmail.com>
On Sun, 05 Sep 2010 16:59:31 +0400, Philippe Sigaud <
philippe.sig...@gmail.com> wrote:
But the real challenge in the SO question is the 'going back in time'
part,
which I have trouble to understand : how can you modify x and y
through a
multiplication and a comparison?
It can be done using setjmp/longjmp, see C implementation for an
example:
http://homepage.mac.com/sigfpe/Computing/continuations.html
How can we access setjmp/longjmp in D?
Anyway, I'm a bit nervous with using such a raw statement. An
oh-so-slightly
wrapped equivalent for D would be better.
Philippe
It is available after the following import:
import core.sys.posix.setjmp;
It is not defined on Windows, but I believe you can use the following
declaration with dmd (works for ddmd):
version (Windows) {
alias int[16] jmp_buf;
extern (C) extern
{
int setjmp(ref jmp_buf env);
void longjmp(ref jmp_buf env, int value);
}
}
Some documentation:
http://en.wikipedia.org/wiki/Setjmp.h
Use the following struct (or make it a class) if you prefer OOP style:
struct State
{
int save() {
return setjmp(buf);
}
void restore(int status) {
assert(status != 0);
longjmp(buf, status);
}
private jmp_buf buf;
}
import std.stdio;
void main()
{
State state;
int result = state.save();
if (result == 0) {
// first time here
writeln("state saved");
state.restore(1);
} else {
assert(result == 1);
writeln("state restored");
}
}
The code above should print:
state saved
state restored
(not tested)