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); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); script.MoveBackIf(Constants.START_GROUP); if (args.Count != m_args.Length) { throw new ArgumentException("Function [" + m_name + "] arguments mismatch: " + m_args.Length + " declared, " + args.Count + " supplied"); } // 1. Add passed arguments as local variables to the Parser. RegisterArguments(args); // 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; } tempScript.ParentScript = script; tempScript.InTryBlock = script.InTryBlock; if (script.Debugger != null) { result = script.Debugger.StepInFunctionIfNeeded(tempScript); } while (tempScript.Pointer < m_body.Length - 1 && (result == null || !result.IsReturn)) { result = tempScript.ExecuteTo(); tempScript.GoToNextStatement(); } ParserFunction.PopLocalVariables(); if (result == null) { result = Variable.EmptyInstance; } else { result.IsReturn = false; } return(result); }
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); }
public Variable Run(List <Variable> args) { RegisterArguments(args); List <string> argsStr = new List <string>(); List <double> argsNum = new List <double>(); List <List <string> > argsArrStr = new List <List <string> >(); List <List <double> > argsArrNum = new List <List <double> >(); List <Dictionary <string, string> > argsMapStr = new List <Dictionary <string, string> >(); List <Dictionary <string, double> > argsMapNum = new List <Dictionary <string, double> >(); List <Variable> argsVar = new List <Variable>(); for (int i = 0; i < m_args.Length; i++) { Variable typeVar = m_argsMap[m_args[i]]; if (typeVar.Type == Variable.VarType.STRING) { argsStr.Add(args[i].AsString()); } else if (typeVar.Type == Variable.VarType.NUMBER) { argsNum.Add(args[i].AsDouble()); } else if (typeVar.Type == Variable.VarType.ARRAY_STR) { List <string> subArrayStr = new List <string>(); var tuple = args[i].Tuple; for (int j = 0; j < tuple.Count; j++) { subArrayStr.Add(tuple[j].AsString()); } argsArrStr.Add(subArrayStr); } else if (typeVar.Type == Variable.VarType.ARRAY_NUM) { List <double> subArrayNum = new List <double>(); var tuple = args[i].Tuple; for (int j = 0; j < tuple.Count; j++) { subArrayNum.Add(tuple[j].AsDouble()); } argsArrNum.Add(subArrayNum); } else if (typeVar.Type == Variable.VarType.MAP_STR) { Dictionary <string, string> subMapStr = new Dictionary <string, string>(); var tuple = args[i].Tuple; var keys = args[i].GetKeys(); for (int j = 0; j < tuple.Count; j++) { subMapStr.Add(keys[j], tuple[j].AsString()); } argsMapStr.Add(subMapStr); } else if (typeVar.Type == Variable.VarType.MAP_NUM) { Dictionary <string, double> subMapNum = new Dictionary <string, double>(); var tuple = args[i].Tuple; var keys = args[i].GetKeys(); for (int j = 0; j < tuple.Count; j++) { subMapNum.Add(keys[j], tuple[j].AsDouble()); } argsMapNum.Add(subMapNum); } else if (typeVar.Type == Variable.VarType.VARIABLE) { argsVar.Add(args[i]); } } Variable result = Precompiler.AsyncMode ? m_precompiler.RunAsync(argsStr, argsNum, argsArrStr, argsArrNum, argsMapStr, argsMapNum, argsVar, false) : m_precompiler.Run(argsStr, argsNum, argsArrStr, argsArrNum, argsMapStr, argsMapNum, argsVar, false); ParserFunction.PopLocalVariables(); return(result); }