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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
~ Output
~ ~~~~~~
~
~ It is convenient to be able to output text. This depends on the
~ OS-specific stuff in linux.e, so it's in its own file.
~
~ The most interesting word we define here is ".", pronounced "dot"; the
~ math stuff is needed to implement it.
~
~ Unlike Jonesforth, we do not have a global "base" variable, and this word
~ does not change its behavior depending on that value. It's always base ten.
~
~ TODO surely we can find some way to get high-level flow-control
~ Strings are comparatively easy.
~
~ (string pointer --)
: emitstring dup stringlen swap sys-write ;
: space 32 value@ emitstring drop ;
: newline 10 value@ emitstring drop ;
~ (base, exponent -- base to the power of exponent)
: pow
1 swap
~ If the count of remaining powers is NOT equal to zero, the comparison will
~ return 0, which will cause the zbranch to skip the rest of the line.
dup 0 = 0branch [ 5 8 * , ] drop swap drop exit
~ (base, result so far, count of remaining powers)
1 - 3unroll
~ (updated count of remaining powers, base, result so far)
swap dup 4 unroll
~ (base, updated count of remaining powers, result so far, base)
* swap
~ (base, updated result so far, updated count of remaining powers)
branch [ -22 8 * , ] ;
~ (base, value -- floor of logarithm of value in base base)
: logfloor
~ Start with a product equal to the base, and a count of 0.
swap dup 3unroll 0
~ This is the start of the loop body.
~ (base, value, product so far, count of powers included so far)
3unroll 2dup
~ (base, count so far, value, product so far)
>unsigned 0branch [ 6 8 * , ]
~ If we get here, we're done.
~ (base, count so far, value, product so far)
drop drop swap drop exit
~ If we're here, we need to do another loop.
~ (base, count so far, value, product so far)
4 roll dup 5 unroll * 3roll 1 +
~ (base, value, updated product so far, updated count so far)
~ If the product is less than the base, we overflowed. In that case, the
~ product-so-far is the maximum, so just return it.
4 roll dup 5 unroll 3roll dup 4 unroll
< 0branch [ 8 8 * , ]
4 unroll drop drop drop exit
~ Nothing else weird going on, so loop.
branch [ -45 8 * , ] ;
~ (base, value -- ceiling of logarithm of value in base base)
: logceil
~ Start with a product of 1 and a count of 0.
1 0
~ This is the start of the loop body.
~ (base, value, product so far, count of powers included so far)
3unroll 2dup
~ (base, count so far, value, product so far)
>=unsigned 0branch [ 6 8 * , ]
~ If we get here, we're done.
~ (base, count so far, value, product so far)
drop drop swap drop exit
~ If we're here, we need to do another loop.
~ (base, count so far, value, product so far)
4 roll dup 5 unroll * 3roll 1 +
~ (base, value, updated product so far, updated count so far)
~ If the product is less than the base, we overflowed. In that case, the
~ product-so-far is the maximum, so just return it.
4 roll dup 5 unroll 3roll dup 4 unroll
< 0branch [ 8 8 * , ]
4 unroll drop drop drop exit
~ Nothing else weird going on, so loop.
branch [ -45 8 * , ] ;
~ This is an extremely inefficient implementation, but on the plus side,
~ doing that avoids having to think about any sort of memory management or
~ recursion, and lets us stick entirely with the trivial control-flow
~ constructs we already have.
~
~ (integer to print, base to print in, width to zero-pad to --)
: .base-unsigned
~ (input, base, width)
~ Compute how many digits we need to display. Because we use logfloor, the
~ logic of always printing at least one digit is already handled for us.
3unroll swap 2dup logfloor 1 +
~ (width, base, input, number of digits if no padding)
4 roll 2dup > 0branch [ 5 8 * , ]
~ (base, input, number of digits if no padding, width)
~ If we're here, we should use the padded width
swap drop branch [ 2 8 * , ]
~ (base, input, number of digits if no padding, width)
~ If we're here, we should use the unpadded width
drop
~ (base, input, number of digits remaining)
~ This is the start of the loop.
2dup 1 -
~ (base, input, number of digits remaining, input, intermediate value)
5 roll dup 6 unroll swap
~ (base, input, number of digits remaining, input, base, intermediate value)
pow /% swap drop
~ (base, input, number of digits remaining,
~ input divided by base^x appropriately)
4 roll dup 5 unroll
~ (base, input, number of digits remaining,
~ input divided by base^x appropriately, base)
/% drop
~ (base, input, number of digits remaining, current digit)
~ We construct a one-character string on the stack, then use a pointer to
~ it. It will always contain its own null-termination without us having to
~ do anything special to that end.
dup 10 > 0branch [ 5 8 * , ]
0x30 branch [ 6 8 * , ] ~ ASCII "0"
10 - 0x61 ~ ASCII "a"
+
value@ emitstring
~ We deallocate the string by dropping it.
~
~ Saying it like that makes it sound obvious; contemplate it until it feels
~ surprising.
drop
1 -
dup 0branch [ 3 8 * , ] branch [ -51 8 * , ]
drop drop drop ;
~ (integer to print, base to print in --)
: .base
swap
~ (base, input)
~ Deal with negative numbers.
dup 0 > 0branch [ 7 8 * , ]
-1 *
s" -" emitstring
swap 0 .base-unsigned ;
~ Although this could notionally be called emitinteger for symmetry, it's
~ well-known under the name dot and as a single period character, and that
~ name participates in conventions for names of other things. So, we go with
~ it.
~
~ (integer to print --)
: . 10 .base ;
: .hex 16 0 .base-unsigned ;
: .hex8 16 2 .base-unsigned ;
: .hex16 16 4 .base-unsigned ;
: .hex32 16 8 .base-unsigned ;
: .hex64 16 16 .base-unsigned ;
~ (integer to print, width --)
: .hexn 16 swap .base-unsigned ;
~ Debugging tools
~ ~~~~~~~~~~~~~~~
~ TODO this is a horrible, horrible hack
: s0 0x1000010008 ;
~ TODO replace these with the implementations that use proper flow-control
: stack
s0 @ 8 -
dup value@ 8 + !=
0branch [ 19 8 * , ]
dup s0 @ 8 - != 0branch [ 2 8 * , ] space dup @ . 8 -
branch [ -25 8 * , ]
drop newline ;
: stackhex
s0 @ 8 -
dup value@ 8 + !=
0branch [ 19 8 * , ]
dup s0 @ 8 - != 0branch [ 2 8 * , ] space dup @ .hex64 8 -
branch [ -25 8 * , ]
drop newline ;
|