| 1 |
#ifndef SOURCETOOLS_R_R_NON_STANDARD_EVALUATION_H |
| 2 |
#define SOURCETOOLS_R_R_NON_STANDARD_EVALUATION_H |
| 3 |
|
| 4 |
#include <set> |
| 5 |
#include <map> |
| 6 |
|
| 7 |
#include <sourcetools/r/RHeaders.h> |
| 8 |
#include <sourcetools/r/RCallRecurser.h> |
| 9 |
|
| 10 |
namespace sourcetools { |
| 11 |
namespace r { |
| 12 |
namespace nse { |
| 13 |
|
| 14 |
namespace detail { |
| 15 |
|
| 16 |
inline std::set<std::string> makeNsePrimitives() |
| 17 |
{ |
| 18 |
std::set<std::string> instance; |
| 19 |
|
| 20 |
instance.insert("quote"); |
| 21 |
instance.insert("substitute"); |
| 22 |
instance.insert("eval"); |
| 23 |
instance.insert("evalq"); |
| 24 |
instance.insert("lazy_dots"); |
| 25 |
|
| 26 |
return instance; |
| 27 |
} |
| 28 |
|
| 29 |
inline std::set<std::string>& nsePrimitives() |
| 30 |
{ |
| 31 |
static std::set<std::string> instance = makeNsePrimitives(); |
| 32 |
return instance; |
| 33 |
} |
| 34 |
|
| 35 |
class PerformsNonStandardEvaluationOperation |
| 36 |
: public r::CallRecurser::Operation |
| 37 |
{ |
| 38 |
public: |
| 39 |
|
| 40 |
PerformsNonStandardEvaluationOperation() |
| 41 |
: status_(false) |
| 42 |
{ |
| 43 |
} |
| 44 |
|
| 45 |
virtual void apply(SEXP dataSEXP) |
| 46 |
{ |
| 47 |
if (status_ || TYPEOF(dataSEXP) != LANGSXP) |
| 48 |
return; |
| 49 |
|
| 50 |
if ((status_ = checkCall(dataSEXP))) |
| 51 |
return; |
| 52 |
|
| 53 |
SEXP fnSEXP = CAR(dataSEXP); |
| 54 |
if (TYPEOF(fnSEXP) == SYMSXP) |
| 55 |
status_ = nsePrimitives().count(CHAR(PRINTNAME(fnSEXP))); |
| 56 |
else if (TYPEOF(fnSEXP) == STRSXP) |
| 57 |
status_ = nsePrimitives().count(CHAR(STRING_ELT(fnSEXP, 0))); |
| 58 |
|
| 59 |
} |
| 60 |
|
| 61 |
bool status() const { return status_; } |
| 62 |
|
| 63 |
private: |
| 64 |
|
| 65 |
bool checkCall(SEXP callSEXP) |
| 66 |
{ |
| 67 |
std::size_t n = Rf_length(callSEXP); |
| 68 |
if (n == 0) |
| 69 |
return false; |
| 70 |
|
| 71 |
SEXP fnSEXP = CAR(callSEXP); |
| 72 |
if (fnSEXP == Rf_install("::") || fnSEXP == Rf_install(":::")) |
| 73 |
{ |
| 74 |
SEXP lhsSEXP = CADR(callSEXP); |
| 75 |
SEXP rhsSEXP = CADDR(callSEXP); |
| 76 |
|
| 77 |
if (lhsSEXP == Rf_install("lazyeval") && rhsSEXP == Rf_install("lazy_dots")) |
| 78 |
return true; |
| 79 |
} |
| 80 |
|
| 81 |
return false; |
| 82 |
} |
| 83 |
|
| 84 |
private: |
| 85 |
bool status_; |
| 86 |
}; |
| 87 |
|
| 88 |
} // namespace detail |
| 89 |
|
| 90 |
class Database |
| 91 |
{ |
| 92 |
public: |
| 93 |
bool check(SEXP dataSEXP) |
| 94 |
{ |
| 95 |
if (contains(dataSEXP)) |
| 96 |
return get(dataSEXP); |
| 97 |
|
| 98 |
typedef detail::PerformsNonStandardEvaluationOperation Operation; |
| 99 |
scoped_ptr<Operation> operation(new Operation); |
| 100 |
|
| 101 |
r::CallRecurser recurser(dataSEXP); |
| 102 |
recurser.add(operation); |
| 103 |
recurser.run(); |
| 104 |
|
| 105 |
set(dataSEXP, operation->status()); |
| 106 |
return operation->status(); |
| 107 |
} |
| 108 |
|
| 109 |
private: |
| 110 |
|
| 111 |
bool contains(SEXP dataSEXP) |
| 112 |
{ |
| 113 |
return map_.count(address(dataSEXP)); |
| 114 |
} |
| 115 |
|
| 116 |
bool get(SEXP dataSEXP) |
| 117 |
{ |
| 118 |
return map_[address(dataSEXP)]; |
| 119 |
} |
| 120 |
|
| 121 |
void set(SEXP dataSEXP, bool value) |
| 122 |
{ |
| 123 |
map_[address(dataSEXP)] = value; |
| 124 |
} |
| 125 |
|
| 126 |
std::size_t address(SEXP dataSEXP) |
| 127 |
{ |
| 128 |
return reinterpret_cast<std::size_t>(dataSEXP); |
| 129 |
} |
| 130 |
|
| 131 |
std::map<std::size_t, bool> map_; |
| 132 |
}; |
| 133 |
|
| 134 |
inline Database& database() |
| 135 |
{ |
| 136 |
static Database instance; |
| 137 |
return instance; |
| 138 |
} |
| 139 |
|
| 140 |
inline bool performsNonStandardEvaluation(SEXP fnSEXP) |
| 141 |
{ |
| 142 |
return database().check(fnSEXP); |
| 143 |
} |
| 144 |
|
| 145 |
} // namespace nse |
| 146 |
} // namespace r |
| 147 |
} // namespace sourcetools |
| 148 |
|
| 149 |
#endif /* SOURCETOOLS_R_R_NON_STANDARD_EVALUATION_H */ |