Case Statements
BartC
bc at freeuk.com
Wed Mar 16 15:41:32 EDT 2016
On 16/03/2016 09:51, Mark Lawrence wrote:
> On 16/03/2016 09:35, Antoon Pardon wrote:
>> So I guess those who would like a case statement in Python can
>> only hope a core developer gets bitten by a nasty bug while using
>> one of those ways of simulating switches.
>>
>
> So that core developers can waste their time putting something into the
> language that we've done without for 25 years, yes, that strikes me as
> extremely worthwhile.
I've noticed that Python doesn't appear to have a way of putting
separators into numeric literals. (Or if it does, I've no idea how).
That means being able to write:
a = 1'000'000
b = 239_288
c = 0x7FFF`FFFF`FFFF`FFFF
depending on what separator is used. Despite waiting for it for 25
years, would that be worthwhile or not? (And if not, why not? And if it
is, perhaps other things can be too.)
> Of course the change is actually trivial,
The above really is trivial (to anyone already familiar with the
workings of the byte-code compiler).
> as
> BartC has all ready pointed out. The work involved is shown here
> https://mail.python.org/pipermail/python-dev/2006-June/065827.html
That article appears to try to do without using a new switch byte-code,
as the author doesn't see the point. My code to implement a 'switch'
byte-code (for integer expression and constant integer case-expressions)
is below.
Not shown is the support needed in the byte-code compiler (about 300
lines in my case as it's a bit fiddly, but it's not a run-time cost).
For a Python version, it might be an idea to make use of the convention
for constants (all-caps), then a streamlined switch could be on the cards.
global function k_switch:ref void =
int index,n,lower
n := getopnda
lower := getopndb
case sptr^.tag
when tint,ttype then
else
pcerror("switch not int")
esac
index:=(sptr++)^.value-lower
if u32(index)>=u32(n) then # out of range
return ref int((pcptr+n*2+4)^)
else
return ref int((pcptr+index*2+4)^)
fi
end
--
Bartc
More information about the Python-list
mailing list