/// <summary> /// Guarda un valor determinado, entero o booleano. /// </summary> /// <param name="where">Donde guardar el valor. La expresion puede ser una variable, o un arreglo.</param> /// <param name="what">La expresion que contiene el valor a guardar. Es evaluada, y el valor resultante guardado.</param> public void Assign(string where, string what) { what = what.ToLower(); where = where.ToLower(); if (where.Contains("[") && where.Contains("]")) // Es un array? { Tokenizer t = new Tokenizer(where); var enumerator = t.GetTokens().GetEnumerator(); enumerator.MoveNext(); if (enumerator.Current.Type != TokenType.Name) { throw new InvalidOperationException(string.Format("{0} is not either a valid variable or array expression.", where)); } else if (Variables.ContainsVariable(enumerator.Current.Value)) { throw new InvalidOperationException(string.Format("There is another variable with the name {0}.", enumerator.Current.Value)); } SaveToArray(where, Evaluate(what)); } else // Es una variable entonces... { Tokenizer t = new Tokenizer(where); var enumerator = t.GetTokens().GetEnumerator(); enumerator.MoveNext(); if (!enumerator.MoveNext()) { Variable v = new Variable(Evaluate(what)); Variables[where] = v; } else throw new InvalidOperationException(string.Format("{0} is not either a valid variable or array expression.", where)); } }
public void Add(string key, Variable v) { array[key] = v; }
public void Add(string variableName, object value) { Variable v = new Variable(value.ToString()); dic.Add(variableName, v); }
public void AssignToArray(string arrayName, string key, Variable v) { key = key.Trim(); if (arrays.ContainsKey(arrayName)) // El array existe? { if (v.Type == arrays[arrayName].Type) { if (arrays[arrayName].VariableIsDeclarated(key)) arrays[arrayName][key] = v; // Si el array esta asignado en ese indice, cambia su valor else arrays[arrayName].Add(key, v); // Sino, agrega el nuevo indice } else throw new InvalidOperationException(string.Format("Cannot assign a {0} to an array of {1}.", v.Type, arrays[arrayName].Type)); } else // Si el array no existe, crearlo { Array newArray = new Array(v.Type, arrayName); newArray[key] = new Variable(v.Value.ToString()); arrays[arrayName] = newArray; } }