-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathVector.fs
65 lines (54 loc) · 1.34 KB
/
Vector.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
######################################################
##
## Vector:
##
## A series of utility words for dealing with
## vectored execution. Vectoring is an extremely
## lightweight mechanism for redefining the behavior
## of a word at runtime.
##
## John Earnest
##
######################################################
: vectored? ('word -- flag)
dup 1 + @ swap 2 + xor -if false else true then
;
: revector ('new-word 'word --)
1 + !
;
: devector ('word --)
dup 2 + swap 1 + !
;
: default ('word -- 'word)
2 + exec
;
(
# use examples:
:include "Print.fs"
# To create a vectored word, use ':vector'.
# Vectored definitions can satisfy prototypes like
# normal colon definitions.
:vector a 5 . ;
: b 8 . ;
: main
# Initially, a vectored word will carry out
# the code specified in its definition as usual:
a # 5 expected
' a vectored? . # 0 expected
# By 'revectoring' we redirect all calls to a word
# to a new word's body. Note that vectoring can
# be chained arbitrarily.
' b ' a revector
a # 8 expected
' a vectored? . # -1 expected
# 'default' allows us to access the original
# implementation of a vectored word, disregarding
# revectoring:
' a default # 5 expected
# 'devector' rather obviously restores the
# original behavior.
' a devector
a # 5 expected
cr
;
)