-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
API: unrecognized vs zero #4
Comments
A one argument in favour of making recognizers return a special (formally valid and non-zero) descriptor when recognizing is unsuccessful is that a further action can be performed without any checks. And it makes code shorter. But a strong counter argument is that if we change special non-zero value to zero, total lexical size of the overall code will be decreased. Actually, in any Forth system we only have up to three places that become longer on this change. But every place become shorter where In my set of integer number recognizers — 3 times. In As we can see, almost any non trivial recognizer becomes lexically shorter. And all other recognizers become literally shorter since So we already have 19 examples. And they cover with a wide margin those three places where the code becomes longer. The command to search instances of testing
|
Another argument against the special xt on fail (which is called Actually, it's a question about the choice between a special object and the common object (zero) for the case of fail. Many other words returns an object id (on success), or zero (on fail) — not a special object on fail. For example:
Why the recognizers should not follow this practice and return a special id on fail, instead of zero? Yes, the choice of special object on fail makes code smaller in some places of the use, but it makes code longer in more other places of the use! So, it just increases the overall lexical size. I checked the source codes in Gforth (as of 2023-09-17), which include both the implementation and usage of this API:
If we use
Thus, replacing of |
An excerpt from the updated above post:
One more argument: the phrase The name Actually, in these use cases we want to translate a lexeme. Thus, it is better to introduce a word like I would define this word as follows. : ?found ( xt.translator -- xt.translator | 0 -- never ) dup if exit then -13 throw ;
: translate-lexeme ( i*x sd.lexeme -- j*x ) perceive ?found execute ; Where : perceive ( sd.lexeme -- token translator | 0 ) perceptor execute ; |
Choice a value that is returned by a recognizer when a lexeme is not recognized.
By Recognizer v4 API, a recognizer should return a special system dependent value in the case of unsuccessful recognizing. The
rectype-null
word (orunrecognized
word in the rephrase 2020) returns this value.In v4 its choice is justified by the (rather unfair) implementations of POSTPONE and INTERPRET only. I.e., among a system implementation and a system using, only one part was considered: a system internal implementation.
But the use of recognizers is always simpler if a recognizer returns zero in the case of unsuccessful recognizing.
1. The use of simple recognizers is easier if they return 0 on unsuccess. In any case there is no need to check for particular descriptors (since only the single descriptor can be returned). So, a code just become simpler with 0 if unsuccessful. E.g.:
vs
2. Creating a new recognizer as a colon definition using other recognizers is simpler if they return 0 on unsuccess. Checking for the special unrecognized value is just a redundant boilerplate without any profit. E.g.:
vs
3. Using of recognizers with other common libraries is easier if they return 0 on unsuccess. Since distinction zero and nonzero values is a very common and convenient approach that used everywhere.
4. If a recognizer returns a special value on unsuccess, the specification has to forbid using of zero to avoid error prone code (see an example: "The code assumes that the numeric value of any rectype-data item is never zero"). It will limit the implementations that can and want to use zero as a special value for unsuccess. Therefore, we will have two special values instead of only one. And one of them is even forbidden to be used.
The text was updated successfully, but these errors were encountered: