-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathAbort.fs
68 lines (58 loc) · 1.21 KB
/
Abort.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
######################################################
##
## Abort:
##
## A simple stack-smashing mechanism which can be
## used to create exception-like error handling
## functionality as well as other kinds of
## nonlinear control flow.
##
## John Earnest
##
######################################################
:array tries 20 0
:data try-ptr tries
:const /try 2
: try-rp try-ptr @ ; ( -- addr )
: try-dp try-ptr @ 1 + ; ( -- addr )
: try-src try-ptr @ 2 + ; ( -- addr )
: try+ try-ptr @ /try + try-ptr ! ; ( -- )
: try- try-ptr @ /try - try-ptr ! ; ( -- )
: try ( proc -- flag )
try+
DP @ 1 - try-dp !
RP @ try-rp !
exec
try-
true
;
: abort ( -- )
try-rp @ RP !
try-dp @ DP !
try-
false
;
######################################################
##
## Usage example:
##
######################################################
(
:include <Print.fs>
: doomed
5 6 abort "Shouldn't be here." typeln
;
: blessed
drop drop drop
;
: choose
if doomed "Shouldn't be here either." typeln else blessed then
"Made a choice." typeln
;
: main
666
{ 2 3 4 1 choose } try if "Succeeded." else "Failed." then typeln
{ 2 3 4 0 choose } try if "Awesome." else "Shoot." then typeln
. cr
;
)