On 21/09/08 2:27, Luke Kanies wrote:
> On Sep 20, 2008, at 1:39 PM, Brice Figureau wrote:
> 
>> Hi,
>>
>> The more I think about that patch, the more I think I should expand it
>> to a more generic expression parser/evaluator by adding arithmetic
>> operators and such, so that we could write these stupid things:
>>
>> class superfeaturethatneedsplentyofram {
>>   $min_ram = 2048 * 1024 * 1024
>>   if ($ram / (1024*1024) < $min_ram) {
>>     notice("not enough ram on node ${fqdn}")
>>   }
>> }
>>
>> I already started coding this in fact and I'm wondering if that's
>> something that:
>>  * is needed (I never myself got the need)
>>  * would be welcome
>>
>> Anyway, that's an interesting exercise (mainly due to the lack or racc
>> documentation or examples). Right now I'm struggling with shift/reduce
>> conflicts I don't really know how to solve (anyone familiar with LALR
>> parsers lurking here, please I welcome any help :-)).
> 
> Note that Puppet's language doesn't have numbers -- they're all  
> treated as strings (only 'true' and 'false', when unquoted, are  
> anything but strings) -- so you'll have to do a touch of magic to make  
> math work.

Yes, I know, the code has some "to_i" to convert strings matching 
numbers to integer. But I'm not satisfied with this, as it doesn't 
handle float numbers.
I'm willing to do something along the line (pseudocode) of:


safeevaluate operand
if !string or !number
   raise error

if string matches /^\d+$/
   call to_i

else if string matches /^\d+.\d+$/
   call to_f

then do the computation...
And also, is it ok to raise ArgumentErrors during evaluate() when I 
encounter something that can't be safely converted in a number?

Can I move this part to the scope class, as there is a true? method 
doing the same kind of things?

Also how does ruby manage coercion (or implicit cast) when doing 
integer/float computation?
I couldn't find at first glance anything online discussing this (I guess 
I'll have to purchase a ruby reference book sometimes).

> Generally, I'm not opposed to this but I don't think it's all that  
> important, either.  I expect others will stauchly disagree with me,  
> though, and would love this functionality.

I understand, it's just that's the low hanging fruits for me while 
coding in Puppet: I know this part of the code quite well now, and it's 
only a minor modification above my previous if expression patch.
And I know some people would welcome arithmetic computations, I already 
saw some threads on the user list mentioning this.

> The lack of racc documentation is quite fun -- I can only ever find my  
> own threads and code when looking for it. I can't imagine I'm the only  
> person to ever use it, but I might be the only one to ever publish  
> anything using it.

It seems people are now using Coco/R where I could find more examples.
In fact Racc mimic bison/yacc, so the bison documentation and samples 
are enough (but I found that after I sent my previous e-mail).

> In terms of shift/reduce conflicts, they are the bane of LALR parser  
> generators.  I'm willing to do what I can to help, but they're pretty  
> unfun.  What's your code look like right now?

I left my Dragon book at the office, so from home that was not easy to 
remember what I learnt 15 years ago about parsers :-)
But I managed to rewrite the grammar to not be ambiguous, and there is 
no s/r conflicts anymore (for the moment), it even works fine for 
integers :-)

> BTW, one thing that might be useful is to create a stand-alone parser  
> that just does the arithmetic, and when you're happy with it, import  
> it into Puppet.  Then again, that might be 10x harder. :)

Yes, puppet defines most of the thing (ie AST tree, the lexer and so 
on). I don't want to rewrite all of this :-)

Thanks for your help,
-- 
Brice Figureau
Days of Wonder
http://www.daysofwonder.com

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to