~ubuntu-branches/ubuntu/utopic/ctioga2/utopic-proposed

« back to all changes in this revision

Viewing changes to lib/ctioga2/commands/function.rb

  • Committer: Package Import Robot
  • Author(s): Vincent Fourmond
  • Date: 2014-03-17 22:29:58 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20140317222958-mo23o3mhmblq1yyc
Tags: 0.10-1
New upstream release (yeah, I should package intermediate versions)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# function.rb: makefile-like functions
 
2
# copyright (c) 2014 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
require 'ctioga2/utils'
 
15
require 'ctioga2/commands/strings'
 
16
 
 
17
module CTioga2
 
18
 
 
19
  module Commands
 
20
 
 
21
    # A Function is a makefile-like "macro" or "function" that takes
 
22
    # one or more arguments (no argumentless functions for now).
 
23
    #
 
24
    # This class provides both the definition and handling of a
 
25
    # function and the global registry of functions.
 
26
    class Function
 
27
 
 
28
      # The underlying proc object. The first argument to the code is
 
29
      # *always* the plotmaker object.
 
30
      attr_accessor :code
 
31
 
 
32
      # The name of the function. Probably better lowercase ?
 
33
      attr_accessor :name
 
34
 
 
35
      # A short description
 
36
      attr_accessor :short_description
 
37
 
 
38
      # Long description, ie a help text like the rest
 
39
      attr_accessor :description
 
40
 
 
41
      # Registers a function.
 
42
      #
 
43
      # @todo Have self-documenting capacities !
 
44
      def initialize(name, short_desc, &blk)
 
45
        @code = blk
 
46
        @name = name
 
47
        @short_description = short_desc
 
48
        
 
49
        Function.register(self)
 
50
      end
 
51
 
 
52
      def describe(txt)
 
53
        @description = txt
 
54
      end
 
55
 
 
56
      # Expands the function, and returns the corresponding string.
 
57
      def expand(string, interpreter)
 
58
        if @code.arity == 2
 
59
          args = [string.expand_to_string(interpreter)]
 
60
        else
 
61
          args = string.expand_and_split(/\s+/, interpreter)
 
62
        end
 
63
        if (@code.arity > 0) and (args.size != (@code.arity - 1))
 
64
          raise "Function #{@name} expects #{@code.arity} arguments, but was given #{args.size}"
 
65
        end
 
66
        return @code.call(interpreter.plotmaker_target, *args).to_s
 
67
      end
 
68
 
 
69
      # Registers the given function definition
 
70
      def self.register(func)
 
71
        @functions ||= {}
 
72
        @functions[func.name] = func
 
73
      end
 
74
 
 
75
      # Returns the named function definition, or nil if there isn't
 
76
      # such.
 
77
      def self.named_function(name)
 
78
        return @functions[name]
 
79
      end
 
80
 
 
81
      # Returns the functions hash
 
82
      def self.functions
 
83
        return @functions
 
84
      end
 
85
      
 
86
    end
 
87
  end
 
88
 
 
89
end
 
90