]> gitweb.factorcode.org Git - factor.git/blob - core/math/parser/parser.factor
Cleanup some lint warnings.
[factor.git] / core / math / parser / parser.factor
1 ! (c)2009 Joe Groff bsd license
2 USING: accessors byte-arrays combinators kernel kernel.private
3 math namespaces sequences sequences.private splitting strings
4 make ;
5 IN: math.parser
6
7 : digit> ( ch -- n )
8     {
9         { [ dup CHAR: 9 <= ] [ CHAR: 0 -      dup  0 < [ drop 255 ] when ] }
10         { [ dup CHAR: a <  ] [ CHAR: A 10 - - dup 10 < [ drop 255 ] when ] }
11                              [ CHAR: a 10 - - dup 10 < [ drop 255 ] when ]
12     } cond ; inline
13
14 <PRIVATE
15
16 TUPLE: number-parse 
17     { str read-only }
18     { length fixnum read-only }
19     { radix fixnum read-only } ;
20
21 : <number-parse> ( str radix -- i number-parse n )
22     [ 0 ] 2dip
23     [ dup length ] dip
24     number-parse boa
25     0 ; inline
26
27 : (next-digit) ( i number-parse n digit-quot end-quot -- n/f )
28     [ 2over length>> < ] 2dip
29     [ [ 2over str>> nth-unsafe >fixnum [ 1 + >fixnum ] 3dip ] prepose ] dip if ; inline
30
31 : require-next-digit ( i number-parse n quot -- n/f )
32     [ 3drop f ] (next-digit) ; inline
33
34 : next-digit ( i number-parse n quot -- n/f )
35     [ 2nip ] (next-digit) ; inline
36
37 : add-digit ( i number-parse n digit quot -- n/f )
38     [ [ dup radix>> ] [ * ] [ + ] tri* ] dip next-digit ; inline
39
40 : digit-in-radix ( number-parse n char -- number-parse n digit ? )
41     digit> pick radix>> over > ; inline
42
43 : ?make-ratio ( num denom/f -- ratio/f )
44     [ / ] [ drop f ] if* ; inline
45
46 TUPLE: float-parse
47     { radix read-only }
48     { point read-only }
49     { exponent read-only } ;
50
51 : inc-point ( float-parse -- float-parse' )
52     [ radix>> ] [ point>> 1 + ] [ exponent>> ] tri float-parse boa ; inline
53
54 : store-exponent ( float-parse n expt -- float-parse' n )
55     swap [ [ drop radix>> ] [ drop point>> ] [ nip ] 2tri float-parse boa ] dip ; inline
56
57 : ?store-exponent ( float-parse n expt/f -- float-parse' n/f )
58     [ store-exponent ] [ drop f ] if* ; inline
59
60 : ((pow)) ( base x -- base^x )
61     iota 1 rot [ nip * ] curry reduce ; inline
62
63 : (pow) ( base x -- base^x )
64     dup 0 >= [ ((pow)) ] [ [ recip ] [ neg ] bi* ((pow)) ] if ; inline
65
66 : add-mantissa-digit ( float-parse i number-parse n digit quot -- float-parse' n/f )
67     [ [ inc-point ] 4dip ] dip add-digit ; inline
68
69 : make-float-dec-exponent ( float-parse n/f -- float/f )
70     [ [ radix>> ] [ point>> ] [ exponent>> ] tri - (pow) ] [ swap /f ] bi* ; inline
71
72 : make-float-bin-exponent ( float-parse n/f -- float/f )
73     [ drop [ radix>> ] [ point>> ] bi (pow) ]
74     [ nip swap /f ]
75     [ drop 2.0 swap exponent>> (pow) * ] 2tri ; inline
76
77 : ?make-float ( float-parse n/f -- float/f )
78     {
79         { [ dup not ] [ 2drop f ] }
80         { [ over radix>> 10 = ] [ make-float-dec-exponent ] }
81         [ make-float-bin-exponent ]
82     } cond ; inline
83
84 : ?neg ( n/f -- -n/f )
85     [ neg ] [ f ] if* ; inline
86
87 : ?add-ratio ( m n/f -- m+n/f )
88     dup ratio? [ + ] [ 2drop f ] if ; inline
89
90 : @abort ( i number-parse n x -- f )
91     2drop 2drop f ; inline
92
93 : @split ( i number-parse n -- n i number-parse n' )
94     -rot 0 ; inline
95
96 : @split-exponent ( i number-parse n -- n i number-parse' n' )
97     -rot [ str>> ] [ length>> ] bi 10 number-parse boa 0 ; inline
98
99 : <float-parse> ( i number-parse n -- float-parse i number-parse n )
100      [ drop nip radix>> 0 0 float-parse boa ] 3keep ; inline
101
102 DEFER: @exponent-digit
103 DEFER: @mantissa-digit
104 DEFER: @denom-digit
105 DEFER: @num-digit
106 DEFER: @pos-digit
107 DEFER: @neg-digit
108
109 : @exponent-digit-or-punc ( float-parse i number-parse n char -- float-parse n/f )
110     {
111         { CHAR: , [ [ @exponent-digit ] require-next-digit ] }
112         [ @exponent-digit ]
113     } case ; inline
114
115 : @exponent-digit ( float-parse i number-parse n char -- float-parse n/f )
116     { float-parse fixnum number-parse integer fixnum } declare
117     digit-in-radix [ [ @exponent-digit-or-punc ] add-digit ] [ @abort ] if ;
118
119 : @exponent-first-char ( float-parse i number-parse n char -- float-parse n/f )
120     {
121         { CHAR: - [ [ @exponent-digit ] require-next-digit ?neg ] }
122         { CHAR: + [ [ @exponent-digit ] require-next-digit ] }
123         [ @exponent-digit ]
124     } case ; inline
125
126 : ->exponent ( float-parse i number-parse n -- float-parse' n/f )
127     @split-exponent [ @exponent-first-char ] require-next-digit ?store-exponent ; inline
128
129 : exponent-char? ( number-parse n char -- number-parse n char ? )
130     3dup nip swap radix>> {
131         { 10 [ [ CHAR: e CHAR: E ] dip [ = ] curry either? ] }
132         [ drop [ CHAR: p CHAR: P ] dip [ = ] curry either? ]
133     } case ; inline
134
135 : or-exponent ( i number-parse n char quot -- n/f )
136     [ exponent-char? [ drop <float-parse> ->exponent ?make-float ] ] dip if ; inline
137
138 : or-mantissa->exponent ( float-parse i number-parse n char quot -- float-parse n/f )
139     [ exponent-char? [ drop ->exponent ] ] dip if ; inline
140
141 : @mantissa-digit-or-punc ( float-parse i number-parse n char -- float-parse n/f )
142     {
143         { CHAR: , [ [ @mantissa-digit ] require-next-digit ] }
144         [ @mantissa-digit ]
145     } case ; inline
146
147 : @mantissa-digit ( float-parse i number-parse n char -- float-parse n/f )
148     { float-parse fixnum number-parse integer fixnum } declare
149     [
150         digit-in-radix
151         [ [ @mantissa-digit-or-punc ] add-mantissa-digit ]
152         [ @abort ] if
153     ] or-mantissa->exponent ;
154
155 : ->mantissa ( i number-parse n -- n/f )
156     <float-parse> [ @mantissa-digit ] next-digit ?make-float ; inline
157
158 : ->required-mantissa ( i number-parse n -- n/f )
159     <float-parse> [ @mantissa-digit ] require-next-digit ?make-float ; inline
160
161 : @denom-digit-or-punc ( i number-parse n char -- n/f )
162     {
163         { CHAR: , [ [ @denom-digit ] require-next-digit ] }
164         { CHAR: . [ ->mantissa ] }
165         [ [ @denom-digit ] or-exponent ]
166     } case ; inline
167
168 : @denom-digit ( i number-parse n char -- n/f )
169     { fixnum number-parse integer fixnum } declare
170     digit-in-radix [ [ @denom-digit-or-punc ] add-digit ] [ @abort ] if ;
171
172 : @denom-first-digit ( i number-parse n char -- n/f )
173     {
174         { CHAR: . [ ->mantissa ] }
175         [ @denom-digit ]
176     } case ; inline
177
178 : ->denominator ( i number-parse n -- n/f )
179     @split [ @denom-first-digit ] require-next-digit ?make-ratio ; inline
180
181 : @num-digit-or-punc ( i number-parse n char -- n/f )
182     {
183         { CHAR: , [ [ @num-digit ] require-next-digit ] }
184         { CHAR: / [ ->denominator ] }
185         [ @num-digit ]
186     } case ; inline
187
188 : @num-digit ( i number-parse n char -- n/f )
189     { fixnum number-parse integer fixnum } declare
190     digit-in-radix [ [ @num-digit-or-punc ] add-digit ] [ @abort ] if ;
191
192 : ->numerator ( i number-parse n -- n/f )
193     @split [ @num-digit ] require-next-digit ?add-ratio ; inline
194
195 : @pos-digit-or-punc ( i number-parse n char -- n/f )
196     {
197         { CHAR: , [ [ @pos-digit ] require-next-digit ] }
198         { CHAR: + [ ->numerator ] }
199         { CHAR: / [ ->denominator ] }
200         { CHAR: . [ ->mantissa ] }
201         [ [ @pos-digit ] or-exponent ]
202     } case ; inline
203
204 : @pos-digit ( i number-parse n char -- n/f )
205     { fixnum number-parse integer fixnum } declare
206     digit-in-radix [ [ @pos-digit-or-punc ] add-digit ] [ @abort ] if ;
207
208 : @pos-first-digit ( i number-parse n char -- n/f )
209     {
210         { CHAR: . [ ->required-mantissa ] }
211         [ @pos-digit ]
212     } case ; inline
213
214 : @neg-digit-or-punc ( i number-parse n char -- n/f )
215     {
216         { CHAR: , [ [ @neg-digit ] require-next-digit ] }
217         { CHAR: - [ ->numerator ] }
218         { CHAR: / [ ->denominator ] }
219         { CHAR: . [ ->mantissa ] }
220         [ [ @neg-digit ] or-exponent ]
221     } case ; inline
222
223 : @neg-digit ( i number-parse n char -- n/f )
224     { fixnum number-parse integer fixnum } declare
225     digit-in-radix [ [ @neg-digit-or-punc ] add-digit ] [ @abort ] if ;
226
227 : @neg-first-digit ( i number-parse n char -- n/f )
228     {
229         { CHAR: . [ ->required-mantissa ] }
230         [ @neg-digit ]
231     } case ; inline
232
233 : @first-char ( i number-parse n char -- n/f ) 
234     {
235         { CHAR: - [ [ @neg-first-digit ] require-next-digit ?neg ] }
236         { CHAR: + [ [ @pos-first-digit ] require-next-digit ] }
237         [ @pos-first-digit ]
238     } case ; inline
239
240 PRIVATE>
241
242 : base> ( str radix -- n/f )
243     <number-parse> [ @first-char ] require-next-digit ;
244
245 : string>number ( str -- n/f ) 10 base> ; inline
246
247 : bin> ( str -- n/f )  2 base> ; inline
248 : oct> ( str -- n/f )  8 base> ; inline
249 : dec> ( str -- n/f ) 10 base> ; inline
250 : hex> ( str -- n/f ) 16 base> ; inline
251
252 : string>digits ( str -- digits )
253     [ digit> ] B{ } map-as ; inline
254
255 <PRIVATE
256
257 : (digits>integer) ( valid? accum digit radix -- valid? accum )
258     2dup < [ swapd * + ] [ 2drop 2drop f 0 ] if ; inline
259
260 : each-digit ( seq radix quot -- n/f )
261     [ t 0 ] 3dip curry each swap [ drop f ] unless ; inline
262
263 PRIVATE>
264
265 : digits>integer ( seq radix -- n/f )
266     [ (digits>integer) ] each-digit ; inline
267
268 : >digit ( n -- ch )
269     dup 10 < [ CHAR: 0 + ] [ 10 - CHAR: a + ] if ; inline
270
271 <PRIVATE
272
273 : positive>base ( num radix -- str )
274     dup 1 <= [ "Invalid radix" throw ] when
275     [ dup 0 > ] swap [ /mod >digit ] curry "" produce-as nip
276     reverse! ; inline
277
278 PRIVATE>
279
280 GENERIC# >base 1 ( n radix -- str )
281
282 : number>string ( n -- str ) 10 >base ; inline
283 : >bin ( n -- str ) 2 >base ; inline
284 : >oct ( n -- str ) 8 >base ; inline
285 : >hex ( n -- str ) 16 >base ; inline
286
287 <PRIVATE
288
289 SYMBOL: radix
290 SYMBOL: negative?
291
292 : sign ( -- str ) negative? get "-" "+" ? ;
293
294 : with-radix ( radix quot -- )
295     radix swap with-variable ; inline
296
297 : (>base) ( n -- str ) radix get positive>base ;
298
299 PRIVATE>
300
301 M: integer >base
302     over 0 = [
303         2drop "0"
304     ] [
305         over 0 > [
306             positive>base
307         ] [
308             [ neg ] dip positive>base CHAR: - prefix
309         ] if
310     ] if ;
311
312 M: ratio >base
313     [
314         dup 0 < negative? set
315         abs 1 /mod
316         [ [ "" ] [ (>base) sign append ] if-zero ]
317         [
318             [ numerator (>base) ]
319             [ denominator (>base) ] bi
320             "/" glue
321         ] bi* append
322         negative? get [ CHAR: - prefix ] when
323     ] with-radix ;
324
325 : fix-float ( str -- newstr )
326     {
327         {
328             [ CHAR: e over member? ]
329             [ "e" split1 [ fix-float "e" ] dip 3append ]
330         } {
331             [ CHAR: . over member? ]
332             [ ]
333         }
334         [ ".0" append ]
335     } cond ;
336
337 <PRIVATE
338
339 : mantissa-expt-normalize ( mantissa expt -- mantissa' expt' )
340     dup zero?
341     [ over log2 52 swap - [ shift 52 2^ 1 - bitand ] [ 1022 + - ] bi-curry bi* ]
342     [ 1023 - ] if ;
343
344 : mantissa-expt ( float -- mantissa expt )
345     [ 52 2^ 1 - bitand ]
346     [ -0.0 double>bits bitnot bitand -52 shift ] bi
347     mantissa-expt-normalize ;
348
349 : float>hex-sign ( bits -- str )
350     -0.0 double>bits bitand zero? "" "-" ? ;
351
352 : float>hex-value ( mantissa -- str )
353     >hex 13 CHAR: 0 pad-head [ CHAR: 0 = ] trim-tail
354     [ "0" ] when-empty "1." prepend ;
355
356 : float>hex-expt ( mantissa -- str )
357     10 >base "p" prepend ;
358
359 : float>hex ( n -- str )
360     double>bits
361     [ float>hex-sign ] [
362         mantissa-expt [ float>hex-value ] [ float>hex-expt ] bi*
363     ] bi 3append ;
364
365 : format-float ( n format -- string )
366     0 suffix >byte-array (format-float)
367     dup [ 0 = ] find drop head >string
368     fix-float ;
369
370 : float>base ( n base -- str )
371     {
372         { 16 [ float>hex ] }
373         [ drop "%.16g" format-float ]
374     } case ; inline
375
376 PRIVATE>
377
378 : float>string ( n -- str )
379     10 float>base ; inline
380
381 M: float >base
382     {
383         { [ over fp-nan? ] [ 2drop "0/0." ] }
384         { [ over 1/0. =  ] [ 2drop "1/0." ] }
385         { [ over -1/0. = ] [ 2drop "-1/0." ] }
386         { [ over  0.0 fp-bitwise= ] [ 2drop  "0.0" ] }
387         { [ over -0.0 fp-bitwise= ] [ 2drop "-0.0" ] }
388         [ float>base ]
389     } cond ;
390
391 : # ( n -- ) number>string % ; inline