protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 2, m_name); Variable arrayVar = Utils.GetSafeVariable(args, 0); int col = Utils.GetSafeInt(args, 1); int fromCol = Utils.GetSafeInt(args, 2, 0); var tuple = arrayVar.Tuple; List <Variable> result = new List <Variable>(tuple.Count); for (int i = fromCol; i < tuple.Count; i++) { Variable current = tuple[i]; if (current.Tuple == null || current.Tuple.Count <= col) { throw new ArgumentException(m_name + ": Index [" + col + "] doesn't exist in column " + i + "/" + (tuple.Count - 1)); } result.Add(current.Tuple[col]); } return(new Variable(result)); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 3, m_name); string varName = Utils.GetSafeString(args, 0); Variable toAdd = Utils.GetSafeVariable(args, 1); string hash = Utils.GetSafeString(args, 2); var function = ParserFunction.GetFunction(varName); Variable mapVar = function != null?function.GetValue(script) : new Variable(Variable.VarType.ARRAY); mapVar.AddVariableToHash(hash, toAdd); for (int i = 3; i < args.Count; i++) { string hash2 = Utils.GetSafeString(args, 3); if (!string.IsNullOrWhiteSpace(hash2) && !hash2.Equals(hash, StringComparison.OrdinalIgnoreCase)) { mapVar.AddVariableToHash(hash2, toAdd); } } ParserFunction.AddGlobalOrLocalVariable(varName, new GetVarFunction(mapVar)); return(Variable.EmptyInstance); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 1, m_name); int limit = args[0].AsInt(); Utils.CheckPosInt(args[0]); int numberRandoms = Utils.GetSafeInt(args, 1, 1); if (numberRandoms <= 1) { return(new Variable(m_random.Next(0, limit))); } List <int> available = Enumerable.Range(0, limit).ToList(); List <Variable> result = new List <Variable>(); for (int i = 0; i < numberRandoms && available.Count > 0; i++) { int nextRandom = m_random.Next(0, available.Count); result.Add(new Variable(available[nextRandom])); available.RemoveAt(nextRandom); } return(new Variable(result)); }
protected override Variable Evaluate(ParsingScript script) { bool isList; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); string output = string.Empty; for (int i = 0; i < args.Count; i++) { output += args[i].AsString(); } output += (m_newLine ? Environment.NewLine : string.Empty); if (m_changeColor) { Utils.PrintColor(output, m_fgcolor); } else { Interpreter.Instance.AppendOutput(output); } return(Variable.EmptyInstance); }
protected override Variable Evaluate(ParsingScript script) { bool isList; List <Variable> functionArgs = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); //script.MoveForwardIf(Constants.END_ARG); script.MoveBackIf(Constants.START_GROUP); if (functionArgs.Count != m_args.Length) { throw new ArgumentException("Function [" + m_name + "] arguments mismatch: " + m_args.Length + " declared, " + functionArgs.Count + " supplied"); } // 1. Add passed arguments as local variables to the Parser. StackLevel stackLevel = new StackLevel(m_name); for (int i = 0; i < m_args.Length; i++) { stackLevel.Variables[m_args[i]] = new GetVarFunction(functionArgs[i]); } ParserFunction.AddLocalVariables(stackLevel); // 2. Execute the body of the function. Variable result = null; ParsingScript tempScript = new ParsingScript(m_body); tempScript.ScriptOffset = m_parentOffset; if (m_parentScript != null) { tempScript.Char2Line = m_parentScript.Char2Line; tempScript.Filename = m_parentScript.Filename; tempScript.OriginalScript = m_parentScript.OriginalScript; } while (tempScript.Pointer < m_body.Length - 1 && (result == null || !result.IsReturn)) { result = tempScript.ExecuteTo(); tempScript.GoToNextStatement(); } ParserFunction.PopLocalVariables(); //script.MoveForwardIf(Constants.END_ARG); //script.MoveForwardIf(Constants.END_STATEMENT); if (result == null) { result = Variable.EmptyInstance; } else { result.IsReturn = false; } return(result); }
public List <Variable> GetFunctionArgs(char start = Constants.START_ARG, char end = Constants.END_ARG) { bool isList; List <Variable> args = Utils.GetArgs(this, start, end, (outList) => { isList = outList; }); return(args); }
protected override Variable Evaluate(ParsingScript script) { string rest = script.Rest; bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 1, m_name); string source = Utils.GetSafeString(args, 0); string argument = Utils.GetSafeString(args, 1); string parameter = Utils.GetSafeString(args, 2, "case"); int startFrom = Utils.GetSafeInt(args, 3, 0); int length = Utils.GetSafeInt(args, 4, source.Length); StringComparison comp = StringComparison.Ordinal; if (parameter.Equals("nocase") || parameter.Equals("no_case")) { comp = StringComparison.OrdinalIgnoreCase; } switch (m_mode) { case Mode.CONTAINS: return(new Variable(source.IndexOf(argument, comp) >= 0)); case Mode.STARTS_WITH: return(new Variable(source.StartsWith(argument, comp))); case Mode.ENDS_WITH: return(new Variable(source.EndsWith(argument, comp))); case Mode.INDEX_OF: return(new Variable(source.IndexOf(argument, startFrom, comp))); case Mode.EQUALS: return(new Variable(source.Equals(argument, comp))); case Mode.REPLACE: return(new Variable(source.Replace(argument, parameter))); case Mode.UPPER: return(new Variable(source.ToUpper())); case Mode.LOWER: return(new Variable(source.ToLower())); case Mode.TRIM: return(new Variable(source.Trim())); case Mode.SUBSTRING: return(new Variable(source.Substring(startFrom, length))); } return(new Variable(-1)); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 1, m_name); int numberDigits = Utils.GetSafeInt(args, 1, 0); args[0].Value = Math.Round(args[0].Value, numberDigits); return(args[0]); }
protected override Variable Evaluate(ParsingScript script) { // Data buffer for incoming data. byte[] bytes = new byte[1024]; bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 3, Constants.CONNECTSRV); Utils.CheckPosInt(args[1]); string hostname = args[0].String; int port = (int)args[1].Value; string msgToSend = args[2].String; if (string.IsNullOrWhiteSpace(hostname) || hostname.Equals("localhost")) { hostname = Dns.GetHostName(); } try { IPHostEntry ipHostInfo = Dns.GetHostEntry(hostname); IPAddress ipAddress = ipHostInfo.AddressList[0]; IPEndPoint remoteEP = new IPEndPoint(ipAddress, port); // Create a TCP/IP socket. Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); sender.Connect(remoteEP); Interpreter.Instance.AppendOutput("Connected to [" + sender.RemoteEndPoint.ToString() + "]", true); byte[] msg = Encoding.UTF8.GetBytes(msgToSend); sender.Send(msg); // Receive the response from the remote device. int bytesRec = sender.Receive(bytes); string received = Encoding.UTF8.GetString(bytes, 0, bytesRec); Interpreter.Instance.AppendOutput("Received [" + received + "]", true); sender.Shutdown(SocketShutdown.Both); sender.Close(); } catch (Exception exc) { throw new ArgumentException("Couldn't connect to server: (" + exc.Message + ")"); } return(Variable.EmptyInstance); }
public static List <string> GetFunctionArgs(ParsingScript script) { bool isList; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); List <string> result = new List <string> (); for (int i = 0; i < args.Count; i++) { result.Add(args [i].AsString()); } return(result); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); string strFormat = Utils.GetSafeString(args, 0, "hh:mm:ss.fff"); Utils.CheckNotEmpty(strFormat, m_name); string when = DateTime.Now.ToString(strFormat); return(new Variable(when)); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); if (m_mode == Mode.START) { m_stopwatch.Restart(); return(Variable.EmptyInstance); } string strFormat = Utils.GetSafeString(args, 0, "secs"); string elapsedStr = ""; double elapsed = -1.0; if (strFormat == "hh::mm:ss.fff") { elapsedStr = string.Format("{0:D2}:{1:D2}:{2:D2}.{3:D3}", m_stopwatch.Elapsed.Hours, m_stopwatch.Elapsed.Minutes, m_stopwatch.Elapsed.Seconds, m_stopwatch.Elapsed.Milliseconds); } else if (strFormat == "mm:ss.fff") { elapsedStr = string.Format("{0:D2}:{1:D2}.{2:D3}", m_stopwatch.Elapsed.Minutes, m_stopwatch.Elapsed.Seconds, m_stopwatch.Elapsed.Milliseconds); } else if (strFormat == "ss.fff") { elapsedStr = string.Format("{0:D2}.{1:D3}", m_stopwatch.Elapsed.Seconds, m_stopwatch.Elapsed.Milliseconds); } else if (strFormat == "secs") { elapsed = Math.Round(m_stopwatch.Elapsed.TotalSeconds); } else if (strFormat == "ms") { elapsed = Math.Round(m_stopwatch.Elapsed.TotalMilliseconds); } if (m_mode == Mode.STOP) { m_stopwatch.Stop(); } return(elapsed >= 0 ? new Variable(elapsed) : new Variable(elapsedStr)); }
protected override Variable Evaluate(ParsingScript script) { bool isList; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); //if (args.Count > 0 && args[0] == "TIMESTAMP") for (int i = 0; i < args.Count; i++) { Console.Write(args[i].AsString()); } Console.WriteLine(); return(Variable.EmptyInstance); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 2, m_name); Variable all = Utils.GetSafeVariable(args, 0); string varName = Utils.GetSafeString(args, 1); int index = Utils.GetSafeInt(args, 2); var function = ParserFunction.GetFunction(varName); Variable mapVar = new Variable(Variable.VarType.ARRAY); if (all.Tuple == null) { return(Variable.EmptyInstance); } string currentValue = ""; int currentCount = 0; int globalCount = 0; for (int i = 0; i < all.Tuple.Count; i++) { Variable current = all.Tuple[i]; if (current.Tuple == null || current.Tuple.Count < index) { break; } string newValue = current.Tuple[index].AsString(); if (currentValue != newValue) { currentValue = newValue; currentCount = 0; } mapVar.Tuple.Add(new Variable(currentCount)); currentCount++; globalCount++; } ParserFunction.AddGlobalOrLocalVariable(varName, new GetVarFunction(mapVar)); return(mapVar); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 3, m_name); string varName = Utils.GetSafeString(args, 0); Variable lines = Utils.GetSafeVariable(args, 1); int fromLine = Utils.GetSafeInt(args, 2); string hash2 = Utils.GetSafeString(args, 3); string sepStr = Utils.GetSafeString(args, 4, "\t"); if (sepStr == "\\t") { sepStr = "\t"; } char[] sep = sepStr.ToCharArray(); var function = ParserFunction.GetFunction(varName); Variable mapVar = function != null?function.GetValue(script) : new Variable(Variable.VarType.ARRAY); for (int counter = fromLine; counter < lines.Tuple.Count; counter++) { Variable lineVar = lines.Tuple[counter]; Variable toAdd = new Variable(counter - fromLine); string line = lineVar.AsString(); var tokens = line.Split(sep); string hash = tokens[0]; mapVar.AddVariableToHash(hash, toAdd); if (!string.IsNullOrWhiteSpace(hash2) && !hash2.Equals(hash, StringComparison.OrdinalIgnoreCase)) { mapVar.AddVariableToHash(hash2, toAdd); } } ParserFunction.AddGlobalOrLocalVariable(varName, new GetVarFunction(mapVar)); return(Variable.EmptyInstance); }
protected override Variable Evaluate(string data, ref int from) { bool isList; List <Variable> args = Utils.GetArgs(data, ref from, Constants.START_ARG, Constants.END_ARG, out isList); string output = string.Empty; for (int i = 0; i < args.Count; i++) { output += args[i].AsString(); } print_interpreter.AppendOutput(output, _newLine); return(Variable.EmptyInstance); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 3, m_name); string varName = Utils.GetSafeString(args, 0); Variable lines = Utils.GetSafeVariable(args, 1); int fromLine = Utils.GetSafeInt(args, 2); string sepStr = Utils.GetSafeString(args, 3, "\t"); if (sepStr == "\\t") { sepStr = "\t"; } char[] sep = sepStr.ToCharArray(); var function = ParserFunction.GetFunction(varName); Variable allTokensVar = new Variable(Variable.VarType.ARRAY); for (int counter = fromLine; counter < lines.Tuple.Count; counter++) { Variable lineVar = lines.Tuple[counter]; Variable toAdd = new Variable(counter - fromLine); string line = lineVar.AsString(); var tokens = line.Split(sep); Variable tokensVar = new Variable(Variable.VarType.ARRAY); foreach (string token in tokens) { tokensVar.Tuple.Add(new Variable(token)); } allTokensVar.Tuple.Add(tokensVar); } ParserFunction.AddGlobalOrLocalVariable(varName, new GetVarFunction(allTokensVar)); return(Variable.EmptyInstance); }
protected override Variable Evaluate(ParsingScript script) { bool isList = false; List <Variable> args = Utils.GetArgs(script, Constants.START_ARG, Constants.END_ARG, out isList); Utils.CheckArgs(args.Count, 1, m_name); string data = Utils.GetSafeString(args, 0); string sep = Utils.GetSafeString(args, 1, "\t"); if (sep == "\\t") { sep = "\t"; } var sepArray = sep.ToCharArray(); string[] tokens = data.Split(sepArray); var option = Utils.GetSafeString(args, 2); List <Variable> results = new List <Variable>(); for (int i = 0; i < tokens.Length; i++) { string token = tokens[i]; if (i > 0 && string.IsNullOrWhiteSpace(token) && option.StartsWith("prev", StringComparison.OrdinalIgnoreCase)) { token = tokens[i - 1]; } results.Add(new Variable(token)); } return(new Variable(results)); }
protected override Variable Evaluate(string data, ref int from) { bool isList; List <Variable> functionArgs = Utils.GetArgs(data, ref from, Constants.START_ARG, Constants.END_ARG, out isList); Utils.MoveBackIf(data, ref from, Constants.START_GROUP); if (functionArgs.Count != _args.Length) { throw new ArgumentException("Функция [" + _name + "] аргументи несъответствие: " + _args.Length + " обявен, " + functionArgs.Count + " доставен"); } // 1. Добавете предаваните аргументи като локални променливи към анализатора. StackLevel stackLevel = new StackLevel(_name); for (int i = 0; i < _args.Length; i++) { stackLevel.Variables[_args[i]] = new GetVarFunction(functionArgs[i]); } ParserFunction.AddLocalVariables(stackLevel); // 2. Изпълнете тялото на функцията. int temp = 0; Variable result = null; while (temp < _body.Length - 1) { result = Parser.LoadAndCalculate(_body, ref temp, Constants.END_PARSE_ARRAY); Utils.GoToNextStatement(_body, ref temp); } ParserFunction.PopLocalVariables(); return(result); }