Skip to content

Commit a15c2b4

Browse files
committed
Adding continuation-prompt-available?
1 parent 9fc9171 commit a15c2b4

File tree

5 files changed

+55
-1
lines changed

5 files changed

+55
-1
lines changed

lib/sagittarius/continuations.scm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
call/delim-cc
3838
call-with-delimited-current-continuation
3939

40-
continuation?
40+
continuation? continuation-prompt-available?
4141

4242
default-continuation-prompt-tag
4343
make-continuation-prompt-tag continuation-prompt-tag?

src/lib_sagittarius.stub

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1856,6 +1856,9 @@
18561856
(define-c-proc %call/delim-cc (proc::<procedure> tag) :no-export
18571857
Sg_VMCallDelimitedCC)
18581858
(define-c-proc continuation? (o) ::<boolean> :no-side-effect Sg_ContinuationP)
1859+
(define-c-proc continuation-prompt-available? (tag :optional (cont #f))
1860+
::<boolean> :no-side-effect
1861+
Sg_ContinuationPromptAvailableP)
18591862

18601863
(define (make-continuation-prompt-tag :optional (name (gensym))) (list name))
18611864
(define default-continuation-prompt-tag

src/sagittarius/private/vm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,7 @@ SG_EXTERN SgObject Sg_VMCallComp(SgObject proc, SgObject tag);
417417
SG_EXTERN SgObject Sg_VMCallDelimitedCC(SgObject proc, SgObject tag);
418418
SG_EXTERN SgObject Sg_VMAbortCC(SgObject tag, SgObject args);
419419
SG_EXTERN int Sg_ContinuationP(SgObject o);
420+
SG_EXTERN int Sg_ContinuationPromptAvailableP(SgObject tag, SgObject cont);
420421
SG_EXTERN SgVM* Sg_VM(); /* get vm */
421422
SG_EXTERN int Sg_SetCurrentVM(SgVM *vm);
422423
SG_EXTERN int Sg_AttachVM(SgVM *vm);

src/vm.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1859,6 +1859,31 @@ int Sg_ContinuationP(SgObject o)
18591859
return SG_SUBRP(o) && SG_EQ(SG_PROCEDURE_NAME(o), sym_continuation);
18601860
}
18611861

1862+
int Sg_ContinuationPromptAvailableP(SgObject tag, SgObject k)
1863+
{
1864+
SgContFrame *cont = NULL;
1865+
SgPrompt *boundary = NULL;
1866+
SgVM *vm = theVM;
1867+
if (SG_FALSEP(k)) {
1868+
cont = vm->cont;
1869+
} else if (Sg_ContinuationP(k)) {
1870+
SgContinuation *c = (SgContinuation *)SG_CAR(SG_SUBR_DATA(k));
1871+
boundary = (SgPrompt *)SG_CDR(SG_SUBR_DATA(k));
1872+
cont = c->cont;
1873+
} else {
1874+
Sg_Error(UC("continuation or #f is required but got %S"), k);
1875+
}
1876+
while (!bottom_cont_frame_p(vm, cont)) {
1877+
if (PROMPT_FRAME_MARK_P(cont)) {
1878+
if (((SgPrompt *)cont->pc)->tag == tag) return TRUE;
1879+
if ((SgPrompt *)cont->pc == boundary) return FALSE;
1880+
1881+
}
1882+
cont = cont->prev;
1883+
}
1884+
return FALSE;
1885+
}
1886+
18621887
SgObject Sg_VMCallCC(SgObject proc)
18631888
{
18641889
SgContinuation *cont;

test/tests/sagittarius/continuations.scm

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,31 @@
3838
(lambda (x) 'outer x))))
3939
(test-equal 'invoked (check)))
4040

41+
(let ((p1 (make-continuation-prompt-tag 'p1))
42+
(p2 (make-continuation-prompt-tag 'p2)))
43+
;; using delim-cc
44+
(let ((k (call-with-continuation-prompt
45+
(lambda ()
46+
(test-assert "p1 (#t)" (continuation-prompt-available? p1))
47+
(test-assert "p2 (#f)" (not (continuation-prompt-available? p2)))
48+
(call-with-continuation-prompt
49+
(lambda ()
50+
(call/delim-cc values p2))
51+
p2))
52+
p1)))
53+
(test-assert "delim-cc p1 (#f)" (not (continuation-prompt-available? p1 k)))
54+
(test-assert "delim-cc p2 (#t)" (continuation-prompt-available? p2 k)))
55+
56+
;; using full continuation
57+
(let ((k (call-with-continuation-prompt
58+
(lambda ()
59+
(call-with-continuation-prompt
60+
(lambda () (call/cc values))
61+
p2))
62+
p1)))
63+
(test-assert "call/cc p1 (#t)" (continuation-prompt-available? p1 k))
64+
(test-assert "call/cc p2 (#t)" (continuation-prompt-available? p2 k))))
65+
4166
(define-syntax test
4267
(lambda (x)
4368
(syntax-case x ()

0 commit comments

Comments
 (0)