Exact constant representation in compiler Const node needed?

Bengt Richter bokr at oz.net
Sun Nov 9 03:48:45 CET 2003

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.,

 >>> from exactdec import ED
 >>> import compiler
 >>> compiler.parse('.1','eval')
 >>> compiler.parse('.1','eval').node.value
 >>> ED(compiler.parse('.1','eval').node.value,'all')

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'>

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'>


 >>> compiler.parse("1, -1, 1., .1, 'sss'",'eval').node.nodes
 [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 ;-)

Bengt Richter

More information about the Python-list mailing list