Ejemplo n.º 1
0
        /// <summary>
        /// Evaluates a statement in the given stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="environment">The environment in which to evaluate the statement. Advanced feature.</param>
        /// <returns>Each evaluation.</returns>
        public IEnumerable <SymbolicExpression> Defer(Stream stream, REnvironment environment = null)
        {
            CheckEngineIsRunning();
            if (stream == null)
            {
                throw new ArgumentNullException();
            }
            if (!stream.CanRead)
            {
                throw new ArgumentException();
            }

            using (TextReader reader = new StreamReader(stream))
            {
                var    incompleteStatement = new StringBuilder();
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    foreach (var segment in Segment(line))
                    {
                        var result = Parse(segment, incompleteStatement, environment);
                        if (result != null)
                        {
                            yield return(result);
                        }
                    }
                }
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Assign a value to a name in a specific environment.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="expression">The symbol.</param>
 /// <param name="environment">The environment. If <c>null</c> is passed, <see cref="GlobalEnvironment"/> is used.</param>
 public void SetSymbol(string name, SymbolicExpression expression, REnvironment environment)
 {
     CheckEngineIsRunning();
     if (environment == null)
     {
         environment = GlobalEnvironment;
     }
     environment.SetSymbol(name, expression);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Gets a symbol defined in the global environment.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="environment">The environment. If <c>null</c> is passed, <see cref="GlobalEnvironment"/> is used.</param>
 /// <returns>The symbol.</returns>
 public SymbolicExpression GetSymbol(string name, REnvironment environment)
 {
     CheckEngineIsRunning();
     if (environment == null)
     {
         environment = GlobalEnvironment;
     }
     return(environment.GetSymbol(name));
 }
Ejemplo n.º 4
0
        /// <summary>
        /// Evaluates the expression in the specified environment.
        /// </summary>
        /// <param name="environment">The environment.</param>
        /// <returns>The evaluation result.</returns>
        public SymbolicExpression Evaluate(REnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }
            if (Engine != environment.Engine)
            {
                throw new ArgumentException(null, "environment");
            }

            return new SymbolicExpression(Engine, this.GetFunction<Rf_eval>()(handle, environment.DangerousGetHandle()));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Evaluates the expression in the specified environment.
        /// </summary>
        /// <param name="environment">The environment.</param>
        /// <returns>The evaluation result.</returns>
        public SymbolicExpression Evaluate(REnvironment environment)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }
            if (Engine != environment.Engine)
            {
                throw new ArgumentException(null, "environment");
            }

            return(new SymbolicExpression(Engine, this.GetFunction <Rf_eval>()(handle, environment.DangerousGetHandle())));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Evaluates the expression in the specified environment.
        /// </summary>
        /// <param name="environment">The environment.</param>
        /// <param name="result">The evaluation result, or <c>null</c> if the evaluation failed</param>
        /// <returns><c>True</c> if the evaluation succeeded.</returns>
        public bool TryEvaluate(REnvironment environment, out SymbolicExpression result)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }
            if (Engine != environment.Engine)
            {
                throw new ArgumentException(null, "environment");
            }

            bool errorOccurred;
            IntPtr pointer = this.GetFunction<R_tryEval>()(handle, environment.DangerousGetHandle(), out errorOccurred);
            result = errorOccurred ? null : new SymbolicExpression(Engine, pointer);
            return !errorOccurred;
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Creates a new environment.
 /// </summary>
 /// <param name="engine">The engine.</param>
 /// <param name="parent">The parent environment.</param>
 /// <returns>The newly created environment.</returns>
 public static REnvironment CreateEnvironment(this REngine engine, REnvironment parent)
 {
     if (engine == null)
     {
         throw new ArgumentNullException("engine");
     }
     if (parent == null)
     {
         throw new ArgumentNullException("parent");
     }
     if (!engine.IsRunning)
     {
         throw new ArgumentException();
     }
     return(new REnvironment(engine, parent));
 }
Ejemplo n.º 8
0
        /// <summary>
        /// Evaluates the expression in the specified environment.
        /// </summary>
        /// <param name="environment">The environment.</param>
        /// <param name="result">The evaluation result, or <c>null</c> if the evaluation failed</param>
        /// <returns><c>True</c> if the evaluation succeeded.</returns>
        public bool TryEvaluate(REnvironment environment, out SymbolicExpression result)
        {
            if (environment == null)
            {
                throw new ArgumentNullException("environment");
            }
            if (Engine != environment.Engine)
            {
                throw new ArgumentException(null, "environment");
            }

            bool   errorOccurred;
            IntPtr pointer = this.GetFunction <R_tryEval>()(handle, environment.DangerousGetHandle(), out errorOccurred);

            result = errorOccurred ? null : new SymbolicExpression(Engine, pointer);
            return(!errorOccurred);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Assign a value to a name in a specific environment.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="expression">The symbol.</param>
 /// <param name="environment">The environment. If <c>null</c> is passed, <see cref="GlobalEnvironment"/> is used.</param>
 public void SetSymbol(string name, SymbolicExpression expression, REnvironment environment)
 {
     CheckEngineIsRunning();
     if (environment == null)
     {
         environment = GlobalEnvironment;
     }
     environment.SetSymbol(name, expression);
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Gets a symbol defined in the global environment.
 /// </summary>
 /// <param name="name">The name.</param>
 /// <param name="environment">The environment. If <c>null</c> is passed, <see cref="GlobalEnvironment"/> is used.</param>
 /// <returns>The symbol.</returns>
 public SymbolicExpression GetSymbol(string name, REnvironment environment)
 {
     CheckEngineIsRunning();
     if (environment == null)
     {
         environment = GlobalEnvironment;
     }
     return environment.GetSymbol(name);
 }
Ejemplo n.º 11
0
 public SymbolicExpression LoadHistory(Language call, SymbolicExpression operation, Pairlist args, REnvironment environment)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Creates a new environment object.
 /// </summary>
 /// <param name="engine">The engine.</param>
 /// <param name="parent">The parent environment.</param>
 public REnvironment(REngine engine, REnvironment parent)
     : base(engine, engine.GetFunction <Rf_NewEnvironment>()(engine.NilValue.DangerousGetHandle(), engine.NilValue.DangerousGetHandle(), parent.handle))
 {
 }
Ejemplo n.º 13
0
        private SymbolicExpression Parse(string statement, StringBuilder incompleteStatement, REnvironment environment = null)
        {
            incompleteStatement.Append(statement);
            var    s = GetFunction <Rf_mkString>()(InternalString.NativeUtf8FromString(incompleteStatement.ToString()));
            string errorStatement;

            using (new ProtectedPointer(this, s))
            {
                ParseStatus status;
                var         vector = new ExpressionVector(this, GetFunction <R_ParseVector>()(s, -1, out status, NilValue.DangerousGetHandle()));

                switch (status)
                {
                case ParseStatus.OK:
                    incompleteStatement.Clear();
                    if (vector.Length == 0)
                    {
                        return(null);
                    }
                    using (new ProtectedPointer(vector))
                    {
                        SymbolicExpression result;
                        if (!vector.First().TryEvaluate((environment == null) ? GlobalEnvironment : environment, out result))
                        {
                            throw new EvaluationException(LastErrorMessage);
                        }

                        if (AutoPrint && !result.IsInvalid && GetVisible())
                        {
                            GetFunction <Rf_PrintValue>()(result.DangerousGetHandle());
                        }
                        return(result);
                    }

                case ParseStatus.Incomplete:
                    return(null);

                case ParseStatus.Error:
                    // TODO: use LastErrorMessage if below is just a subset
                    var parseErrorMsg = this.GetAnsiString("R_ParseErrorMsg");
                    errorStatement = incompleteStatement.ToString();
                    incompleteStatement.Clear();
                    throw new ParseException(status, errorStatement, parseErrorMsg);

                default:
                    errorStatement = incompleteStatement.ToString();
                    incompleteStatement.Clear();
                    throw new ParseException(status, errorStatement, "");
                }
            }
        }
Ejemplo n.º 14
0
 /// <summary>
 /// Evaluates a statement in the given stream.
 /// </summary>
 /// <param name="stream">The stream.</param>
 /// <param name="environment">The environment in which to evaluate the statement. Advanced feature.</param>
 /// <returns>Last evaluation.</returns>
 public SymbolicExpression Evaluate(Stream stream, REnvironment environment = null)
 {
     CheckEngineIsRunning();
     return(Defer(stream, environment).LastOrDefault());
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Evaluates a statement in the given string.
 /// </summary>
 /// <param name="statement">The statement.</param>
 /// <param name="environment">The environment in which to evaluate the statement. Advanced feature.</param>
 /// <returns>Last evaluation.</returns>
 public SymbolicExpression Evaluate(string statement, REnvironment environment = null)
 {
     CheckEngineIsRunning();
     return(Defer(EncodeNonAsciiCharacters(statement), environment).LastOrDefault());
 }
Ejemplo n.º 16
0
 public SymbolicExpression SaveHistory(Language call, SymbolicExpression operation, Pairlist args, REnvironment environment)
 {
     throw new NotImplementedException();
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Creates a new environment.
 /// </summary>
 /// <param name="engine">The engine.</param>
 /// <param name="parent">The parent environment.</param>
 /// <returns>The newly created environment.</returns>
 public static REnvironment CreateEnvironment(this REngine engine, REnvironment parent)
 {
     if (engine == null)
     {
         throw new ArgumentNullException("engine");
     }
     if (parent == null)
     {
         throw new ArgumentNullException("parent");
     }
     if (!engine.IsRunning)
     {
         throw new ArgumentException();
     }
     return new REnvironment(engine, parent);
 }
 public SymbolicExpression SaveHistory(Language call, SymbolicExpression operation, Pairlist args, REnvironment environment)
 {
     return environment.Engine.NilValue;
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Creates a new environment object.
 /// </summary>
 /// <param name="engine">The engine.</param>
 /// <param name="parent">The parent environment.</param>
 public REnvironment(REngine engine, REnvironment parent)
     : base(engine, engine.GetFunction<Rf_NewEnvironment>()(engine.NilValue.DangerousGetHandle(), engine.NilValue.DangerousGetHandle(), parent.handle))
 { }