Class: CTioga2::Commands::Variables

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/commands/variables.rb

Overview

A holder for variables. Variables in ctioga2 are very similar to the ones found in make(1). They are only pieces of text that are expanded using the

$(variable)

syntax, just like in make.

There are two kind of variables

  • immediate, defined by

    variable := value
    

    These ones are evaluated for once when they are defined. They are stored in the form of a String

  • recursively expanded variables. They are mostly like immediate variables, excepted that the values of the replacement texts for variables used within are expanded at the moment the variable is expanded, and not at the moment of its definition as before. They are defined by

    variable = value
    

    They are stored in the form on an InterpreterString

todo The variables system should automatically transform recursive variables into immediate ones when there is no variables replacement text.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeVariables

Creates a new empty Variables object



52
53
54
# File 'lib/ctioga2/commands/variables.rb', line 52

def initialize
  @variables = {}
end

Instance Attribute Details

#variablesObject

A hash “variable name” => String or InterpreterString



49
50
51
# File 'lib/ctioga2/commands/variables.rb', line 49

def variables
  @variables
end

Instance Method Details

#define_variable(name, value, interpreter = nil, override = true) ⇒ Object

Sets a the variable name to value (being an InterpreterString or a String). In the former case (InterpreterString), if interpreter is given, the value is expanded at the time of the definition, (immediate variable), whereas if it stays nil, the variable is defined as a recursively defined variable.



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ctioga2/commands/variables.rb', line 62

def define_variable(name, value, interpreter = nil, override = true)
  if (!override) && @variables.key?(name)
    # Not redefining an already defined variable.
    return
  end
  if value.respond_to? :expand_to_string
    if interpreter
      value = value.expand_to_string(interpreter)
    end
  end
  @variables[name] = value
end

#expand_variable(name, interpreter) ⇒ Object

Fully expands a variable. Returns a String. name is the name of the variable, and interpreter the context in which the expansion will take place.

Note it is assumed here that the variables live in the interpreter.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ctioga2/commands/variables.rb', line 81

def expand_variable(name, interpreter)
  if @variables.key? name
    var = @variables[name]
    if var.respond_to? :expand_to_string
      begin
        return var.expand_to_string(interpreter)
      rescue SystemStackError
        raise RecursiveExpansion, "The stack smashed while expanding variable #{name}. This probably means it is a recursive variable referring to itself. Use := in the definition to avoid that"
      end
    else
      return var
    end
  else
    return ""
  end
end