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
-
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.
Attributes
A hash “variable name” => String
or InterpreterString
Public Class Methods
Creates a new empty Variables
object
# File lib/ctioga2/commands/variables.rb, line 52 def initialize @variables = {} end
Public Instance Methods
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
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