-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotes
More file actions
152 lines (97 loc) · 2.76 KB
/
notes
File metadata and controls
152 lines (97 loc) · 2.76 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
\ and ( are ordinary Forth words and therefore have to be separated with white space from the following text.
+ - / * mod sqrt
dup
swap
drop
negate
over
nip
tuck
5 5 * 4 4 * + . 41 ok
: s2 dup * swap dup * + ;
4 5 s2 .
: squared ( n -- n^2 )
dup * ;
5 squared .
7 squared .
: cubed ( n -- n^3 )
dup squared * ;
-5 cubed .
: fourth-power ( n -- n^4 )
squared squared ;
3 fourth-power .
by convention the comment after the name of a definition describes the stack effect: The part in front of the `--' describes the state of the stack before the execution of the definition, i.e., the parameters that are passed into the colon definition; the part behind the `--' is the state of the stack after the execution of the definition, i.e., the results of the definition. The stack comment only shows the top stack items that the definition accesses and/or changes.
decompilation
============
see squared
see .
see +
types
===
the names of the operations are not overloaded; so similar operations on different types need different names; e.g., + adds integers, and you have to use f+ to add floating-point numbers. The following prefixes are often used for related operations on different types:
(none)
signed integer
u
unsigned integer
c
character
d
signed double-cell integer
ud, du
unsigned double-cell integer
2
two cells (not-necessarily double-cell numbers)
m, um
mixed single-cell and double-cell operations
f
floating-point (note that in stack comments `f' represents flags, and `r' represents FP numbers).
: swap { a b -- b a }
b a ;
1 2 swap .s 2drop
bool
===
1 1 = .
1 0= .
0 1 < .
0 0 < .
-1 1 u< . \ type error, u< interprets -1 as large unsigned number
-1 1 < .
1 2 and .
1 2 or .
1 3 xor .
1 invert .
You can convert a zero/non-zero flag into a canonical flag with 0<> (and complement it on the way with 0=).
1 0= .
1 0<> .
conditionals
=========
< compares the top two stack elements and produces a flag:
1 2 < .
2 1 < .
1 1 < .
: abs ( n1 -- +n2 )
dup 0 < if
negate
endif ;
5 abs .
-5 abs .
if takes a flag from the stack. If the flag is non-zero (true), the following code is performed, otherwise execution continues after the endif (or else).
: min ( n1 n2 -- n )
2dup < if
drop
else
nip
endif ;
2 3 min .
3 2 min .
: bart ." i will not do anything bad ever again" ;
bart
bart bart
gforth test.fs -e by
while (x > 3) { print(x); x--; }
\ Assuming x is on the top of the stack.
begin dup 3 > while dup . 1- repeat
\ Or if x is in memory.
begin x @ 3 > while x ? -1 x +! repeat
while (predicate) { expression(s) }
begin predicate while expressions repeat