Exact constant representation in compiler Const node needed?

B

Bengt Richter

I think for some purposes it might be handy to have and exact value representation
in the compiler parse tree, without having to use strings as carrier. I.e.,
ED('0.1000000000000000055511151231257827021181583404541015625')

Obviously the constant is a floating point number, but should one have to write one's
own parser based on tokenizer output in order to use the numeric literal differently,
e.g. as representing an _exact_ value? Is the conversion to floating point premature?

From a little utility that uses the tokenizer, one sees NUMBER identified, but the value
still in string form. Perhaps the Const instance could have the string value as well, in case
someone wants to re-interpret it exactly?

[18:43] C:\pywk\Decimal>echo .1 |python C:\pywk\tok\prtokens.py -
---- - ----
<NUMBER '.1'> <NEWLINE '\n'>
<ENDMARKER ''>

Even the sign on signed numbers is separate in this low level stuff, and the comma operators
are just tokenized not parsed. Hate to have to go back to that stream to get the info:

[18:46] C:\pywk\Decimal>echo 1, -1, 1., .1, 'sss' |python C:\pywk\tok\prtokens.py -
---- - ----
<NUMBER '1'> <OP ','> <OP '-'> <NUMBER '1'> <OP ','> <NUMBER '1.'> <OP ','> <NUMBER '.1'> <OP ',
'> <STRING "'sss'"> <NEWLINE '\n'>
<ENDMARKER ''>

vs
[Const(1), UnarySub(Const(1)), Const(1.0), Const(0.10000000000000001), Const('sss')]


Is there an easy way I can write a walker/visitor that can access the source text of nodes
so I can rewrite using exact math if I want to? (Not that this is an important immediate goal,
it just occurred to me after writing the little exactdec.py module ;-)

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,464
Latest member
Bobbylenly

Latest Threads

Top