2021-03-30 13:58:25 +02:00
|
|
|
Numbers without the # Sigil
|
2018-02-06 18:53:48 +01:00
|
|
|
|
2021-03-30 13:58:25 +02:00
|
|
|
RETRO requires a # sigil to identify numbers. But since new
|
|
|
|
sigils can be defined, it's possible to define sigils for
|
2018-02-06 18:53:48 +01:00
|
|
|
the number starting with 0-9.
|
|
|
|
|
|
|
|
This is actually easy (at least for positive numbers). Just
|
2021-03-30 13:58:25 +02:00
|
|
|
define a sigil handler that prepends the initial value back
|
|
|
|
on the string (since the string sans the sigil is passed to
|
2018-02-06 18:53:48 +01:00
|
|
|
the handler) and pass it to `s:to-number` and `class:data`.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
{{
|
2021-03-30 13:58:25 +02:00
|
|
|
:process-with-sigil (s-n)
|
2018-02-06 18:53:48 +01:00
|
|
|
s:prepend s:to-number class:data ;
|
|
|
|
---reveal---
|
2021-03-30 13:58:25 +02:00
|
|
|
:sigil:1 (s-n) '1 process-with-sigil ; immediate
|
|
|
|
:sigil:2 (s-n) '2 process-with-sigil ; immediate
|
|
|
|
:sigil:3 (s-n) '3 process-with-sigil ; immediate
|
|
|
|
:sigil:4 (s-n) '4 process-with-sigil ; immediate
|
|
|
|
:sigil:5 (s-n) '5 process-with-sigil ; immediate
|
|
|
|
:sigil:6 (s-n) '6 process-with-sigil ; immediate
|
|
|
|
:sigil:7 (s-n) '7 process-with-sigil ; immediate
|
|
|
|
:sigil:8 (s-n) '8 process-with-sigil ; immediate
|
|
|
|
:sigil:9 (s-n) '9 process-with-sigil ; immediate
|
2018-03-07 21:02:46 +01:00
|
|
|
~~~
|
|
|
|
|
|
|
|
`0` is a special case. Since RETRO has `0;` as a control
|
2021-03-30 13:58:25 +02:00
|
|
|
flow word, the `0` sigil would prevent using it. So the
|
2018-03-07 21:02:46 +01:00
|
|
|
handler for this checks to see if the part following the
|
2021-03-30 13:58:25 +02:00
|
|
|
sigil is a `;`. If so, it'll fall back to `0;`, otherwise
|
2018-03-07 21:02:46 +01:00
|
|
|
it treats the token as a number.
|
|
|
|
|
|
|
|
~~~
|
2021-03-30 13:58:25 +02:00
|
|
|
:sigil:0 (s-n)
|
2018-03-07 21:02:46 +01:00
|
|
|
dup '; s:eq?
|
|
|
|
[ drop &0; call ]
|
2021-03-30 13:58:25 +02:00
|
|
|
[ '0 process-with-sigil ] choose ; immediate
|
2018-02-06 18:53:48 +01:00
|
|
|
}}
|
|
|
|
~~~
|
|
|
|
|
2021-03-30 13:58:25 +02:00
|
|
|
For single digits, define each digit as a word. (Sigils
|
2018-03-06 17:40:19 +01:00
|
|
|
are not processed for tokens with nothing other than the
|
2021-03-30 13:58:25 +02:00
|
|
|
sigil character, so this takes care of the issue).
|
2018-03-06 17:40:19 +01:00
|
|
|
|
|
|
|
~~~
|
2018-03-07 03:55:10 +01:00
|
|
|
:0 (-n) #0 ;
|
2018-03-06 17:40:19 +01:00
|
|
|
:1 (-n) #1 ;
|
|
|
|
:2 (-n) #2 ;
|
|
|
|
:3 (-n) #3 ;
|
|
|
|
:4 (-n) #4 ;
|
|
|
|
:5 (-n) #5 ;
|
|
|
|
:6 (-n) #6 ;
|
|
|
|
:7 (-n) #7 ;
|
|
|
|
:8 (-n) #8 ;
|
|
|
|
:9 (-n) #9 ;
|
|
|
|
~~~
|
|
|
|
|
2018-02-06 18:53:48 +01:00
|
|
|
Handling negative numbers is a bigger headache though. By
|
|
|
|
convention, RETRO uses - to imply "not" (as in `-eq?`). So
|
2021-03-30 13:58:25 +02:00
|
|
|
to handle the `-` sigil for numbers, it needs to fall back
|
2018-02-06 18:53:48 +01:00
|
|
|
to a dictionary search if the token isn't actually a number.
|
|
|
|
|
|
|
|
~~~
|
|
|
|
{{
|
|
|
|
:numeric? (s-sf)
|
|
|
|
dup fetch c:digit? ;
|
|
|
|
:in-dictionary? (s-df)
|
|
|
|
'- s:prepend d:lookup dup n:zero? ;
|
|
|
|
:report-error (d-)
|
|
|
|
drop err:notfound ;
|
|
|
|
:call-with-class (d-)
|
|
|
|
[ d:xt fetch ] [ d:class fetch ] bi call ;
|
|
|
|
---reveal---
|
2021-03-30 13:58:25 +02:00
|
|
|
:sigil:- (s-)
|
2018-02-06 18:53:48 +01:00
|
|
|
numeric?
|
|
|
|
[ s:to-number n:negate class:data ]
|
|
|
|
[ in-dictionary? &report-error &call-with-class choose ] choose ; immediate
|
|
|
|
}}
|
|
|
|
~~~
|
|
|
|
|