/// <summary> /// Prints the variable to the console. If it's not a previously /// assigned variable, it will print the variable name itself as /// if it's an immediate value. /// </summary> /// <param name="console">Console to print to.</param> public override void Handle(MBConsole console) { // Check for valid identifier if (_ident == string.Empty) { console.Write("Parse error: Identifier expected."); return; } // Check if the variable exists already if (console.Vars.ContainsKey(_ident)) { var printFmt = console.Vars[_ident].ToString(); // Format string between quotes for printing if (console.Vars[_ident] is string) { printFmt = "\'" + printFmt + "\'"; } console.Write(printFmt); return; } double testDouble; bool testBool; // Checks immediate values. If not a valid string, double, or // bool returns parse error, otherwise prints the variable name // as if it was an immediate value. if (_tokenType == Token.String) { console.Write("'{0}'", _ident); } else if (double.TryParse(_ident, out testDouble) || bool.TryParse(_ident, out testBool)) { console.Write(_ident); } else { console.Write( "Parse error: Unknown variable: '{0}'", _ident ); } }
/// <summary> /// Handles setting the variable using the consoles Vars property. /// Checks if the identifier only starts with an alpha or underscore /// and handles any type checking or parse errors. /// </summary> /// <param name="console">Console to handle.</param> public override void Handle(MBConsole console) { // Get the type-specific value from object var typedVal = Convert.ChangeType(_value.Value, _value.Type); // Check if identifier exists if (_ident.Length <= 0) { console.Write("Parse error: Identifier expected."); return; } // Check if value is valid if (typedVal.GetType() == typeof(object)) { console.Write("Parse error: Value expected."); return; } // Check if identifier is valid if (!Regex.Match(_ident, "[a-zA-Z_][a-zA-Z0-9_]*").Success) { console.Write( "Parse error: Identifier must start with an" + " alpha or underscore character." ); return; } // Check if the console already has the variable and assign // the new value if so if (!console.Vars.ContainsKey(_ident)) { console.AddVar(_ident, typedVal); return; } // Otherwise create a new variable console.Vars[_ident] = typedVal; }
/// <summary> /// Checks for valid identifier and executes the given function /// from the console if correctly defined. /// </summary> /// <param name="console">Console to handle.</param> public override void Handle(MBConsole console) { // Check for valid identifier if (_ident.Length <= 0) { console.Write("Parse error: Identifier expected."); return; } // Check if function exists if (console.Funcs.ContainsKey(_ident)) { console.Funcs[_ident].Invoke(_args); } else { console.Write( "Parse error: Unknown function: '{0}'", _ident ); } }