CXXR (C++ R)
Evaluator.h
Go to the documentation of this file.
1 /*CXXR $Id: Evaluator.h 1348 2013-02-25 17:49:03Z arr $
2  *CXXR
3  *CXXR This file is part of CXXR, a project to refactor the R interpreter
4  *CXXR into C++. It may consist in whole or in part of program code and
5  *CXXR documentation taken from the R project itself, incorporated into
6  *CXXR CXXR (and possibly MODIFIED) under the terms of the GNU General Public
7  *CXXR Licence.
8  *CXXR
9  *CXXR CXXR is Copyright (C) 2008-13 Andrew R. Runnalls, subject to such other
10  *CXXR copyrights and copyright restrictions as may be stated below.
11  *CXXR
12  *CXXR CXXR is not part of the R project, and bugs and other issues should
13  *CXXR not be reported via r-bugs or other R project channels; instead refer
14  *CXXR to the CXXR website.
15  *CXXR */
16 
17 /*
18  * R : A Computer Language for Statistical Data Analysis
19  *
20  * This program is free software; you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation; either version 2.1 of the License, or
23  * (at your option) any later version.
24  *
25  * This program is distributed in the hope that it will be useful,
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28  * GNU Lesser General Public License for more details.
29  *
30  * You should have received a copy of the GNU General Public License
31  * along with this program; if not, a copy is available at
32  * http://www.r-project.org/Licenses/
33  */
34 
40 #ifndef EVALUATOR_H
41 #define EVALUATOR_H
42 
43 #include "R_ext/Boolean.h"
44 #include "CXXR/Environment.h"
45 #include "CXXR/PairList.h"
46 
47 // Move this to ByteCode.hpp in due course:
48 #define BYTECODE
49 
50 #ifdef __cplusplus
51 #include <utility>
52 
53 extern "C" {
54 #endif
55 
64  extern Rboolean R_Visible;
65 
72  extern int R_interrupts_pending;
73 
76  extern Rboolean R_interrupts_suspended;
77 
92  int R_isMissing(SEXP symbol, SEXP rho);
93 
94 #ifdef __cplusplus
95 } // extern "C"
96 
97 namespace CXXR {
98  class RObject;
99  class Expression;
100 
134  class Evaluator {
135  public:
136  class Context;
137 
138  Evaluator()
139  : m_next(s_current), m_innermost_context(0)
140  {
141  s_current = this;
142  }
143 
144  ~Evaluator()
145  {
146  s_current = m_next;
147  }
148 
153  static Evaluator* current()
154  {
155  return s_current;
156  }
157 
166  static unsigned int depth()
167  {
168  return s_depth;
169  }
170 
177  static unsigned int depthLimit()
178  {
179  return s_depth_limit;
180  }
181 
190  static void enableExtraDepth(bool on)
191  {
192  s_depth_threshold = s_depth_limit + (on ? 500 : 0);
193  }
194 
202  static void enableProfiling(bool on)
203  {
204  s_profiling = on;
205  }
206 
212  static void enableResultPrinting(bool on)
213  {
214  R_Visible = Rboolean(on);
215  }
216 
232 #ifdef __GNUC__
233  __attribute__((hot,fastcall))
234 #endif
235  static RObject* evaluate(RObject* object, Environment* env);
236 
243  {
244  return m_innermost_context;
245  }
246 
251  static bool profiling()
252  {
253  return s_profiling;
254  }
255 
262  static bool resultPrinted()
263  {
264  return R_Visible;
265  }
266 
275  static void setDepth(unsigned int depth)
276  {
277  s_depth = depth;
278  }
279 
287  static void setDepthLimit(int depth);
288  private:
289  friend class Context; // Unnecessary in C++ 0x
290 
291  static unsigned int s_depth; // Current depth of expression evaluation
292  static unsigned int s_depth_threshold; // An error will be
293  // reported if s_depth exceeds this
294  // value. s_depth_threshold is normally
295  // equal to s_depth_limit, but may be
296  // temporarily increased above s_depth_limit
297  // to allow error reporting.
298  static unsigned int s_depth_limit; // The value (controlled
299  // by the 'expressions' R option) to
300  // which s_depth_threshold is set except
301  // during error reporting.
302  static unsigned int s_countdown; // Number of calls of
303  // Evaluator::evaluate() to go before a
304  // check is made for user interrupts.
305  static unsigned int s_countdown_start; // Value from which
306  // s_countdown starts counting down
307  static Evaluator* s_current; // The current (innermost) Evaluator
308  static bool s_profiling; // True iff profiling enabled
309 
310  Evaluator* m_next; // Next Evaluator down the stack
311  Context* m_innermost_context; // Innermost Context belonging
312  // to this Evaluator
313  };
314 
327  inline RObject* evaluate(RObject* object, Environment* env)
328  {
329  return Evaluator::evaluate(object, env);
330  }
331 }
332 
333 extern "C" {
334 #endif /* __cplusplus */
335 
345 #ifndef __cplusplus
346  SEXP Rf_eval(SEXP e, SEXP rho);
347 #else
348  inline SEXP Rf_eval(SEXP e, SEXP rho)
349  {
350  using namespace CXXR;
351  Environment* env = 0;
352  if (e)
353  env = SEXP_downcast<Environment*>(rho);
354  return evaluate(e, env);
355  }
356 #endif
357 
358 #ifdef __cplusplus
359 }
360 #endif
361 
362 #endif /* EVALUATOR_H */