summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorBrice Figureau <brice-puppet@daysofwonder.com>2008-09-26 22:59:27 +0200
committerBrice Figureau <brice@daysofwonder.com>2008-09-30 17:08:52 +0200
commit9cdecfecce84b9e0a88b5ea86b3136a1025ac9d9 (patch)
tree1131c7e2c37bf02f0f6c5087ad3d71eb52f6dd2f /lib
parent8372dc4ca80d95e62c407708a48e51ac09ad2f55 (diff)
downloadpuppet-9cdecfecce84b9e0a88b5ea86b3136a1025ac9d9.tar.gz
puppet-9cdecfecce84b9e0a88b5ea86b3136a1025ac9d9.tar.xz
puppet-9cdecfecce84b9e0a88b5ea86b3136a1025ac9d9.zip
Add comparison operators (< > == != <= >=) to AST
Diffstat (limited to 'lib')
-rw-r--r--lib/puppet/parser/ast.rb1
-rw-r--r--lib/puppet/parser/ast/comparison_operator.rb37
2 files changed, 38 insertions, 0 deletions
diff --git a/lib/puppet/parser/ast.rb b/lib/puppet/parser/ast.rb
index 66744525e..da82a30c7 100644
--- a/lib/puppet/parser/ast.rb
+++ b/lib/puppet/parser/ast.rb
@@ -81,6 +81,7 @@ require 'puppet/parser/ast/caseopt'
require 'puppet/parser/ast/casestatement'
require 'puppet/parser/ast/collection'
require 'puppet/parser/ast/collexpr'
+require 'puppet/parser/ast/comparison_operator'
require 'puppet/parser/ast/definition'
require 'puppet/parser/ast/else'
require 'puppet/parser/ast/function'
diff --git a/lib/puppet/parser/ast/comparison_operator.rb b/lib/puppet/parser/ast/comparison_operator.rb
new file mode 100644
index 000000000..63aa36c7f
--- /dev/null
+++ b/lib/puppet/parser/ast/comparison_operator.rb
@@ -0,0 +1,37 @@
+require 'puppet'
+require 'puppet/parser/ast/branch'
+
+class Puppet::Parser::AST
+ class ComparisonOperator < 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 operands, should return a boolean value
+ lval = @lval.safeevaluate(scope)
+ rval = @rval.safeevaluate(scope)
+
+ # return result
+ unless @operator == '!='
+ lval.send(@operator,rval)
+ else
+ lval != rval
+ end
+ end
+
+ def initialize(hash)
+ super
+
+ unless %w{== != < > <= >=}.include?(@operator)
+ raise ArgumentError, "Invalid comparison operator %s" % @operator
+ end
+ end
+ end
+end