diff options
author | Brice Figureau <brice-puppet@daysofwonder.com> | 2008-09-26 22:56:17 +0200 |
---|---|---|
committer | Brice Figureau <brice@daysofwonder.com> | 2008-09-30 17:08:52 +0200 |
commit | 8372dc4ca80d95e62c407708a48e51ac09ad2f55 (patch) | |
tree | 843731a1bd6282bf007c4adc7a154560780dafbe /lib | |
parent | e6698c2b8624fe2c2bbeef594318e3e8d932d345 (diff) | |
download | puppet-8372dc4ca80d95e62c407708a48e51ac09ad2f55.tar.gz puppet-8372dc4ca80d95e62c407708a48e51ac09ad2f55.tar.xz puppet-8372dc4ca80d95e62c407708a48e51ac09ad2f55.zip |
Add boolean operators to AST
Diffstat (limited to 'lib')
-rw-r--r-- | lib/puppet/parser/ast.rb | 1 | ||||
-rw-r--r-- | lib/puppet/parser/ast/boolean_operator.rb | 48 |
2 files changed, 49 insertions, 0 deletions
diff --git a/lib/puppet/parser/ast.rb b/lib/puppet/parser/ast.rb index c9bd7c9e8..66744525e 100644 --- a/lib/puppet/parser/ast.rb +++ b/lib/puppet/parser/ast.rb @@ -76,6 +76,7 @@ end # And include all of the AST subclasses. require 'puppet/parser/ast/astarray' require 'puppet/parser/ast/branch' +require 'puppet/parser/ast/boolean_operator' require 'puppet/parser/ast/caseopt' require 'puppet/parser/ast/casestatement' require 'puppet/parser/ast/collection' diff --git a/lib/puppet/parser/ast/boolean_operator.rb b/lib/puppet/parser/ast/boolean_operator.rb new file mode 100644 index 000000000..c3b5c7d41 --- /dev/null +++ b/lib/puppet/parser/ast/boolean_operator.rb @@ -0,0 +1,48 @@ +require 'puppet' +require 'puppet/parser/ast/branch' + +class Puppet::Parser::AST + class BooleanOperator < AST::Branch + + attr_accessor :operator, :lval, :rval + + # Iterate across all of our children. + def each + [@lval,@rval,@operator].each { |child| yield child } + end + + # Returns a boolean which is the result of the boolean operation + # of lval and rval operands + def evaluate(scope) + # evaluate the first operand, should return a boolean value + lval = @lval.safeevaluate(scope) + + # return result + # lazy evaluate right operand + case @operator + when "and"; + if Puppet::Parser::Scope.true?(lval) + rval = @rval.safeevaluate(scope) + Puppet::Parser::Scope.true?(rval) + else # false and false == false + false + end + when "or"; + if Puppet::Parser::Scope.true?(lval) + true + else + rval = @rval.safeevaluate(scope) + Puppet::Parser::Scope.true?(rval) + end + end + end + + def initialize(hash) + super + + unless %w{and or}.include?(@operator) + raise ArgumentError, "Invalid boolean operator %s" % @operator + end + end + end +end |