static Variable ExtractValue(ParsingScript script) { if (script.TryCurrent() == '{') { return(ExtractObject(script)); } if (script.TryCurrent() == '[') { return(ExtractArray(script)); } var token = Utils.GetToken(script, SEP); return(new Variable(token)); }
async Task <string> ProcessRepl(string repl, string filename = "") { ReplMode = true; Dictionary <int, int> char2Line; string script = Utils.ConvertToScript(repl, out char2Line); ParsingScript tempScript = new ParsingScript(script, 0, char2Line); tempScript.OriginalScript = repl; tempScript.Debugger = this; if (!string.IsNullOrWhiteSpace(filename)) { tempScript.Filename = filename; } Variable result = null; bool excThrown = false; string stringRes = ""; try { while (tempScript.Pointer < script.Length) { result = await DebuggerUtils.Execute(tempScript); tempScript.GoToNextStatement(); while (tempScript.TryCurrent() == Constants.END_STATEMENT) { tempScript.Forward(); } } if (TrySendFile(result, tempScript, ref excThrown) || excThrown) { return(""); } stringRes = string.IsNullOrEmpty(Output) ? "" : Output + (Output.EndsWith("\n") ? "" : "\n"); stringRes += result == null ? "" : result.AsString(); stringRes += (stringRes.EndsWith("\n") ? "" : "\n"); } catch (Exception exc) { Console.WriteLine("ProcessRepl Exception: " + exc); return(""); // The exception was already thrown and sent back. } finally { ReplMode = false; } return(stringRes); }
static bool StillCollecting(string item, char[] to, ParsingScript script, ref string action) { char prev = script.TryPrevPrev(); char ch = script.TryPrev(); char next = script.TryCurrent(); if (to.Contains(ch) || ch == Constants.START_ARG || ch == Constants.START_GROUP || next == Constants.EMPTY) { return(false); } // Case of a negative number, or starting with the closing bracket: if (item.Length == 0 && ((ch == '-' && next != '-') || ch == Constants.END_ARRAY || ch == Constants.END_ARG)) { return(true); } // Case of a scientific notation 1.2e+5 or 1.2e-5 or 1e5: if (Char.ToUpper(prev) == 'E' && (ch == '-' || ch == '+' || Char.IsDigit(ch)) && item.Length > 1 && Char.IsDigit(item[item.Length - 2])) { return(true); } // Otherwise if it's an action (+, -, *, etc.) or a space // we're done collecting current token. if ((action = Utils.ValidAction(script.FromPrev())) != null || (item.Length > 0 && ch == Constants.SPACE)) { return(false); } if (ch == Constants.TERNARY_OPERATOR) { script.Backward(); return(false); } return(true); }
static bool UpdateResult(ParsingScript script, char[] to, List <Variable> listToMerge, string token, bool negSign, ref Variable current, ref int negated, ref string action) { if (current == null) { current = Variable.EmptyInstance; } current.ParsingToken = token; if (negSign) { current = new Variable(-1 * current.Value); } if (negated > 0 && current.Type == Variable.VarType.NUMBER) { // If there has been a NOT sign, this is a boolean. // Use XOR (true if exactly one of the arguments is true). bool neg = !((negated % 2 == 0) ^ Convert.ToBoolean(current.Value)); current = new Variable(Convert.ToDouble(neg)); negated = 0; } if (script.Current == '.') { bool inQuotes = false; int arrayIndexDepth = 0; script.Forward(); string property = ExtractNextToken(script, to, ref inQuotes, ref arrayIndexDepth, ref negated, out _, out action); Variable propValue = current.Type == Variable.VarType.ENUM ? current.GetEnumProperty(property, script) : current.GetProperty(property, script); current = propValue; } if (action == null) { action = UpdateAction(script, to); } else { script.MoveForwardIf(action[0]); } char next = script.TryCurrent(); // we've already moved forward bool done = listToMerge.Count == 0 && (next == Constants.END_STATEMENT || (action == Constants.NULL_ACTION && current.Type != Variable.VarType.NUMBER) || current.IsReturn); if (done) { if (action != null && action != Constants.END_ARG_STR && token != Constants.DEFAULT) { throw new ArgumentException("Action [" + action + "] without an argument."); } // If there is no numerical result, we are not in a math expression. listToMerge.Add(current); return(true); } Variable cell = current.Clone(); cell.Action = action; bool addIt = UpdateIfBool(script, cell, (Variable newCell) => { cell = newCell; }, listToMerge, (List <Variable> var) => { listToMerge = var; }); if (addIt) { listToMerge.Add(cell); } return(false); }