/// <summary> /// Parses one single Expression-Block /// </summary> /// <param name="expression">the target expression to execute</param> /// <param name="implicitContext">implicit context that is used for the interpreter-session that is used for the codeblock</param> /// <param name="scopeInitializer">an initializer that can be used to provide special methods and values to the initial scope</param> /// <returns>the evaluation result</returns> public static object ParseBlock(string expression, object implicitContext, InitializeScopeVariables scopeInitializer = null) { ScriptVisitor visitor; using (var session = InterpreterBuffer.GetReplInstance(implicitContext, scopeInitializer, out visitor)) { return(ParseBlock(expression, session)); } }
/// <summary> /// Parses one single Expression /// </summary> /// <param name="expression">the target expression to execute</param> /// <param name="variables">the initial variables that are provided to the expression</param> /// <param name="scopeInitializer">an initializer that can be used to provide special methods and values to the initial scope</param> /// <returns>the evaluation result</returns> public static object Parse(string expression, IDictionary <string, object> variables, InitializeScopeVariables scopeInitializer = null) { ScriptVisitor visitor; using (var session = InterpreterBuffer.GetReplInstance(variables, scopeInitializer, out visitor)) { return(Parse(expression, session)); } }
/// <summary> /// Parses a Block inside an open ReplSession /// </summary> /// <param name="expression">the Expression-Block that must be executed</param> /// <param name="replSession">the current repl-session</param> /// <returns>the result of the Execution-block</returns> public static object ParseBlock(string expression, IDisposable replSession) { if (replSession is InterpreterBuffer.RunnerItem rii) { ITVScriptingBaseVisitor <ScriptValue> visitor = InterpreterBuffer.GetInterpreter(rii); ITVScriptingParser.ProgramContext executor = GetProgramTree(expression); ScriptValue retVal = visitor.VisitProgram(executor); return(ScriptValueHelper.GetScriptValueResult <object>(retVal, false)); } return(ParseBlock(expression, replSession, null)); }
/// <summary> /// Runs a script inside a specific Scripting context /// </summary> /// <param name="scriptingContext">the scripting context in which a script is running</param> /// <returns>the result of the script</returns> public TOutput Execute(IDisposable scriptingContext) { CheckDate(); bool ok = false; while (!ok) { lock (runCounterLock) { ok = executionWait.WaitOne(100); if (ok) { currentRuns++; } } if (!ok) { executionWait.WaitOne(); } } try { if (!runnable) { throw new ScriptException(string.Format("Script is not runnable! Suspect Line: {0}{2}Complete Error-List:{2} {1}", suspectLine, errors, Environment.NewLine)); } var visitor = InterpreterBuffer.GetInterpreter(scriptingContext); ScriptValue retVal = visitor.VisitProgram(program); return(ScriptValueHelper.GetScriptValueResult <TOutput>(retVal, false)); } finally { lock (runCounterLock) { currentRuns--; Monitor.Pulse(runCounterLock); } } }
/// <summary> /// Excecutes a Script /// </summary> /// <param name="variables">the initial variables for the script</param> /// <param name="prepareVariables">a callback that will be used in order to initialize lazy-evaluation variable values</param> /// <returns>the return value that was generated by the script</returns> public TOutput Execute(IDictionary <string, object> variables, InitializeScopeVariables prepareVariables) { ScriptVisitor visitor; InitializeScopeVariables dff = prepareVariables ?? (a => { DefaultCallbacks.PrepareDefaultCallbacks(a.Scope, a.ReplSession); }); InitializeScopeVariables newInitializer = args => { PreparePowerCalls(args.Scope, prepareVariables, args.Visitor); dff(args); }; using (var session = InterpreterBuffer.GetReplInstance(variables, newInitializer, out visitor)) { // todo: reactivate return(Execute(session)); } }
/// <summary> /// Begins a repl - session /// </summary> /// <param name="baseValues">the base values that are used for the current session</param> /// <param name="scopePreparer">a callback that is used to prepare the repl session variables</param> /// <returns>a value that can be used to end this repl - session</returns> public static IDisposable BeginRepl(IDictionary <string, object> baseValues, InitializeScopeVariables scopePreparer) { ScriptVisitor visitor; return(InterpreterBuffer.GetReplInstance(baseValues, scopePreparer, out visitor)); }