protected override Variable Evaluate(ParsingScript script) { Variable arg1 = script.ExecuteTo(Constants.NEXT_ARG); script.Forward(); // eat separation Variable arg2 = script.ExecuteTo(Constants.END_ARG); arg1.Value = Math.Pow(arg1.Value, arg2.Value); return(arg1); }
protected override Variable Evaluate(ParsingScript script) { Variable arg = script.ExecuteTo(Constants.END_ARG); arg.Value = Math.Abs(arg.Value); return(arg); }
private Variable ProcessBlock(ParsingScript script) { int blockStart = script.Pointer; Variable result = null; while (script.StillValid()) { int endGroupRead = script.GoToNextStatement(); if (endGroupRead > 0) { return(result != null ? result : new Variable()); } if (!script.StillValid()) { throw new ArgumentException("Couldn't process block [" + script.Substr(blockStart,Constants.MAX_CHARS_TO_SHOW) + "]"); } result = script.ExecuteTo(); if (result.IsReturn || result.Type == Variable.VarType.BREAK || result.Type == Variable.VarType.CONTINUE) { return(result); } } return(result); }
protected override Variable Evaluate(ParsingScript script) { Variable result = script.ExecuteTo(Constants.END_ARG); result.Value = Math.Exp(result.Value); return(result); }
private Variable ProcessBlock(ParsingScript script) { int blockStart = script.Pointer; Variable result = null; if (script.Debugger != null) { bool done = false; result = script.Debugger.DebugBlockIfNeeded(script, done, (newDone) => { done = newDone; }).Result; if (done) { return(result); } } while (script.StillValid()) { int endGroupRead = script.GoToNextStatement(); if (endGroupRead > 0 || !script.StillValid()) { return(result != null ? result : new Variable()); } result = script.ExecuteTo(); if (result.IsReturn || result.Type == Variable.VarType.BREAK || result.Type == Variable.VarType.CONTINUE) { return(result); } } return(result); }
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 Variable Process(string script, string filename = "", bool mainFile = false) { Dictionary <int, int> char2Line; string data = Utils.ConvertToScript(script, out char2Line, filename); if (string.IsNullOrWhiteSpace(data)) { return(null); } ParsingScript toParse = new ParsingScript(data, 0, char2Line); toParse.OriginalScript = script; toParse.Filename = filename; if (mainFile) { toParse.MainFilename = toParse.Filename; } Variable result = null; while (toParse.Pointer < data.Length) { result = toParse.ExecuteTo(); toParse.GoToNextStatement(); } return(result); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, m_name, true); string filename = args[0].AsString(); string[] lines = Utils.GetFileLines(filename); string includeFile = string.Join(Environment.NewLine, lines); Dictionary <int, int> char2Line; string includeScript = Utils.ConvertToScript(includeFile, out char2Line); ParsingScript tempScript = new ParsingScript(includeScript, 0, char2Line); tempScript.Filename = filename; tempScript.OriginalScript = string.Join(Constants.END_LINE.ToString(), lines); tempScript.ParentScript = script; tempScript.InTryBlock = script.InTryBlock; Variable result = null; if (script.Debugger != null) { result = script.Debugger.StepInIncludeIfNeeded(tempScript); } while (tempScript.Pointer < includeScript.Length) { result = tempScript.ExecuteTo(); tempScript.GoToNextStatement(); } return(result == null ? Variable.EmptyInstance : result); }
protected override Variable Evaluate(ParsingScript script) { List <Variable> args = script.GetFunctionArgs(); Utils.CheckArgs(args.Count, 1, m_name, true); string filename = args[0].AsString(); string[] lines = Utils.GetFileLines(filename); string includeFile = string.Join(Environment.NewLine, lines); Dictionary <int, int> char2Line; string includeScript = Utils.ConvertToScript(includeFile, out char2Line); ParsingScript tempScript = new ParsingScript(includeScript, 0, char2Line); tempScript.Filename = filename; tempScript.OriginalScript = string.Join(Constants.END_LINE.ToString(), lines); while (tempScript.Pointer < includeScript.Length) { tempScript.ExecuteTo(); tempScript.GoToNextStatement(); } return(Variable.EmptyInstance); }
public static Variable GetAction(string funcName, string senderName, string eventArg, string eventArg2 = null) { if (senderName == "") { senderName = "\"\""; } if (eventArg == "") { eventArg = "\"\""; } string body = string.Format("{0}({1},{2}", funcName, senderName, eventArg); if (eventArg2 != null) { if (eventArg2 == "") { eventArg2 = "\"\""; } body += "," + eventArg2; } body += ");"; ParsingScript tempScript = new ParsingScript(body); Variable result = tempScript.ExecuteTo(); 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); }
internal Variable ProcessIf(ParsingScript script) { int startIfCondition = script.Pointer; Variable result = script.ExecuteTo(Constants.END_ARG); bool isTrue = Convert.ToBoolean(result.Value); if (isTrue) { result = ProcessBlock(script); if (result.IsReturn || result.Type == Variable.VarType.BREAK || result.Type == Variable.VarType.CONTINUE) { // We are here from the middle of the if-block. Skip it. script.Pointer = startIfCondition; SkipBlock(script); } SkipRestBlocks(script); return(result); //return Variable.EmptyInstance; } // We are in Else. Skip everything in the If statement. SkipBlock(script); ParsingScript nextData = new ParsingScript(script); nextData.ParentScript = script; string nextToken = Utils.GetNextToken(nextData); if (Constants.ELSE_IF_LIST.Contains(nextToken)) { script.Pointer = nextData.Pointer + 1; result = ProcessIf(script); } else if (Constants.ELSE_LIST.Contains(nextToken)) { script.Pointer = nextData.Pointer + 1; result = ProcessBlock(script); } if (result.IsReturn) { return(result); } return(Variable.EmptyInstance); }
public static List <Variable> GetArrayIndices(ParsingScript script, string varName, int end, Action <string, int> updateVals) { List <Variable> indices = new List <Variable>(); int argStart = varName.IndexOf(Constants.START_ARRAY); if (argStart < 0) { return(indices); } int firstIndexStart = argStart; while (argStart < varName.Length && varName[argStart] == Constants.START_ARRAY) { int argEnd = varName.IndexOf(Constants.END_ARRAY, argStart + 1); if (argEnd == -1 || argEnd <= argStart + 1) { break; } ParsingScript tempScript = script.GetTempScript(varName, argStart); /*ParsingScript tempScript = new ParsingScript(varName, argStart); * tempScript.ParentScript = script; * tempScript.Char2Line = script.Char2Line; * tempScript.Filename = script.Filename; * tempScript.OriginalScript = script.OriginalScript; * tempScript.InTryBlock = script.InTryBlock;*/ tempScript.MoveForwardIf(Constants.START_ARG, Constants.START_ARRAY); Variable index = tempScript.ExecuteTo(Constants.END_ARRAY); indices.Add(index); argStart = argEnd + 1; } if (indices.Count > 0) { varName = varName.Substring(0, firstIndexStart); end = argStart - 1; } updateVals(varName, end); return(indices); }
internal Variable ProcessWhile(ParsingScript script) { int startWhileCondition = script.Pointer; // A check against an infinite loop. int cycles = 0; bool stillValid = true; while (stillValid) { script.Pointer = startWhileCondition; //int startSkipOnBreakChar = from; Variable condResult = script.ExecuteTo(Constants.END_ARG); stillValid = Convert.ToBoolean(condResult.Value); if (!stillValid) { break; } // Check for an infinite loop if we are comparing same values: if (MAX_LOOPS > 0 && ++cycles >= MAX_LOOPS) { throw new ArgumentException("Looks like an infinite loop after " + cycles + " cycles."); } Variable result = ProcessBlock(script); if (result.IsReturn || result.Type == Variable.VarType.BREAK) { script.Pointer = startWhileCondition; break; } } // The while condition is not true anymore: must skip the whole while // block before continuing with next statements. SkipBlock(script); return(Variable.EmptyInstance); }
private Variable ProcessBlock(ParsingScript script) { int blockStart = script.Pointer; Variable result = null; if (script.Debugger != null) { bool done = false; result = script.Debugger.DebugBlockIfNeeded(script, ref done); if (done) { return(result); } } while (script.StillValid()) { int endGroupRead = script.GoToNextStatement(); if (endGroupRead > 0 || !script.StillValid()) { return(result != null ? result : new Variable()); } /*if (!script.StillValid()) * { * throw new ArgumentException("Couldn't process block [" + * script.Substr(blockStart, Constants.MAX_CHARS_TO_SHOW) + "]"); * }*/ result = script.ExecuteTo(); if (result.IsReturn || result.Type == Variable.VarType.BREAK || result.Type == Variable.VarType.CONTINUE) { return(result); } } return(result); }
public static List <Variable> GetArrayIndices(ref string varName, ref int end) { List <Variable> indices = new List <Variable> (); int argStart = varName.IndexOf(Constants.START_ARRAY); if (argStart < 0) { return(indices); } int firstIndexStart = argStart; while (argStart < varName.Length && varName [argStart] == Constants.START_ARRAY) { int argEnd = varName.IndexOf(Constants.END_ARRAY, argStart + 1); if (argEnd == -1 || argEnd <= argStart + 1) { break; } ParsingScript tempScript = new ParsingScript(varName, argStart); tempScript.MoveForwardIf(Constants.START_ARG, Constants.START_ARRAY); Variable index = tempScript.ExecuteTo(Constants.END_ARRAY); indices.Add(index); argStart = argEnd + 1; } if (indices.Count > 0) { varName = varName.Substring(0, firstIndexStart); end = argStart - 1; } return(indices); }
protected override Variable Evaluate(ParsingScript script) { return(script.ExecuteTo(Constants.END_ARG)); }