summaryrefslogtreecommitdiffstats
path: root/lib/puppet/parser/ast/function.rb
diff options
context:
space:
mode:
authorluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-04-26 05:39:58 +0000
committerluke <luke@980ebf18-57e1-0310-9a29-db15c13687c0>2006-04-26 05:39:58 +0000
commit46ce36b175962ce89e06af4863d2c9dc50f2a02f (patch)
treeaeff2dacfd81caddce54666e2d155e07b6ad09e8 /lib/puppet/parser/ast/function.rb
parentccc4d95dd28164f6f10763a758db85db0d48984c (diff)
downloadpuppet-46ce36b175962ce89e06af4863d2c9dc50f2a02f.tar.gz
puppet-46ce36b175962ce89e06af4863d2c9dc50f2a02f.tar.xz
puppet-46ce36b175962ce89e06af4863d2c9dc50f2a02f.zip
Creating a simplistic, generic function framework in the parser, so it is now very easy to add new functions. There is a pretty crappy, hardwired distinction between functions that return values and those that do not, but I do not see a good way around it right now. Functions are also currently responsible for handling their own arity, although I have plans for fixing that.
git-svn-id: https://reductivelabs.com/svn/puppet/trunk@1134 980ebf18-57e1-0310-9a29-db15c13687c0
Diffstat (limited to 'lib/puppet/parser/ast/function.rb')
-rw-r--r--lib/puppet/parser/ast/function.rb51
1 files changed, 51 insertions, 0 deletions
diff --git a/lib/puppet/parser/ast/function.rb b/lib/puppet/parser/ast/function.rb
new file mode 100644
index 000000000..80cc5f09e
--- /dev/null
+++ b/lib/puppet/parser/ast/function.rb
@@ -0,0 +1,51 @@
+class Puppet::Parser::AST
+ # The code associated with a class. This is different from components
+ # in that each class is a singleton -- only one will exist for a given
+ # node.
+ class Function < AST::Branch
+ attr_accessor :name, :arguments
+
+ def evaluate(hash)
+ # We don't need to evaluate the name, because it's plaintext
+
+ # Just evaluate the arguments
+ scope = hash[:scope]
+
+ args = @arguments.safeevaluate(:scope => scope)
+
+ return scope.send("function_" + @name, args)
+ end
+
+ def initialize(hash)
+ @ftype = hash[:ftype] || :rvalue
+ hash.delete(:ftype) if hash.include? :ftype
+
+ super(hash)
+
+ # Make sure it's a defined function
+ unless @fname = Puppet::Parser::Functions.function(@name)
+ raise Puppet::ParseError, "Unknown function %s" % @name
+ end
+
+ # Now check that it's been used correctly
+ case @ftype
+ when :rvalue:
+ unless Puppet::Parser::Functions.rvalue?(@name)
+ raise Puppet::ParseError, "Function '%s' does not return a value" %
+ @name
+ end
+ when :statement:
+ if Puppet::Parser::Functions.rvalue?(@name)
+ raise Puppet::ParseError, "Function '%s' must be the value of a statement" %
+ @name
+ end
+ else
+ raise Puppet::DevError, "Invalid function type %s" % @ftype.inspect
+ end
+
+ # Lastly, check the arity
+ end
+ end
+end
+
+# $Id$