class CTioga2::Commands::Variables

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

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

Attributes

variables[RW]

A hash “variable name” => String or InterpreterString

Public Class Methods

new() click to toggle source

Creates a new empty Variables object

# File lib/ctioga2/commands/variables.rb, line 52
def initialize
  @variables = {}
end

Public Instance Methods

define_variable(name, value, interpreter = nil, override = true) click to toggle source

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.

# 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) click to toggle source

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.

# 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