Re: SuperBASIC function for type validation
Posted: Fri Jul 10, 2020 5:30 pm
I cant remember whether or not WHEN ERRor is part of SuperBASIC. Q-Liberator has its own variant, as does Turbo.
Personally I prefer my own VALID% toolkit function (naturally):
ok% = VALID%(expected_type%, <variable or expression>)
It uses the standard Qdos calls to try to fetch the variable/expression on the
stack as the type you specify (int, long, float), and returns its success or not
in ok%. The advantage of this method is that its interpretation will be 100%
consistent with the S*BASIC interpreter's idea of a variable's validity.
For example x% = '1e700' is a valid integer! Namely 1. However, x = '1e700' is
not a valid float! It may not be easy to cater for such anomalies in a home-grown
validy-checking program. At least not without WHEN ERRor. Much better to let
S*BASIC do the hard work!
PRINT VALID%(3, '1e700') will return ok
PRINT VALID%(2, '1e700') will return not ok (2 being the code for float)
VALID%, including it source code (so you could, for example, add it to your own
bespoke toolkit) can be found at Knoware.no
Personally I prefer my own VALID% toolkit function (naturally):
ok% = VALID%(expected_type%, <variable or expression>)
It uses the standard Qdos calls to try to fetch the variable/expression on the
stack as the type you specify (int, long, float), and returns its success or not
in ok%. The advantage of this method is that its interpretation will be 100%
consistent with the S*BASIC interpreter's idea of a variable's validity.
For example x% = '1e700' is a valid integer! Namely 1. However, x = '1e700' is
not a valid float! It may not be easy to cater for such anomalies in a home-grown
validy-checking program. At least not without WHEN ERRor. Much better to let
S*BASIC do the hard work!
PRINT VALID%(3, '1e700') will return ok
PRINT VALID%(2, '1e700') will return not ok (2 being the code for float)
VALID%, including it source code (so you could, for example, add it to your own
bespoke toolkit) can be found at Knoware.no