| 1 |
|
|---|
| 2 | /* Microsoft stdext::hash_map */
|
|---|
| 3 | #define __HASH_MAP_MSVC__ 1
|
|---|
| 4 | /* google::dense_hash_map and google::sparse_hash_map */
|
|---|
| 5 | #define __HASH_MAP_GOOGLE__ 2
|
|---|
| 6 | /* The used hashing handler */
|
|---|
| 7 | #define __USE_HASH_MAP__ __HASH_MAP_GOOGLE__
|
|---|
| 8 |
|
|---|
| 9 | #undef BIGGEST_FIELD_ALIGNMENT
|
|---|
| 10 | #define BIGGEST_FIELD_ALIGNMENT 4
|
|---|
| 11 |
|
|---|
| 12 | #include <string.h>
|
|---|
| 13 | #include <tol/tol_init.h>
|
|---|
| 14 | #include <tol/tol_bfsmem.h>
|
|---|
| 15 | #include <tol/tol_bmatgra.h>
|
|---|
| 16 | #include <tol/tol_blanguag.h>
|
|---|
| 17 | #include <tol/tol_bdatgra.h>
|
|---|
| 18 | #include <tol/tol_btxtgra.h>
|
|---|
| 19 | #include <tol/tol_bdate.h>
|
|---|
| 20 | #include <tol/tol_bdtegra.h>
|
|---|
| 21 |
|
|---|
| 22 | #ifdef UNIX
|
|---|
| 23 | #define DLLEXPORT(f) f
|
|---|
| 24 | #else
|
|---|
| 25 | #define DLLEXPORT(f) __declspec(dllexport) f
|
|---|
| 26 | #endif
|
|---|
| 27 |
|
|---|
| 28 | #include <R.h>
|
|---|
| 29 |
|
|---|
| 30 | extern "C" DLLEXPORT(void) RInitTolKernel(void);
|
|---|
| 31 |
|
|---|
| 32 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 33 |
|
|---|
| 34 | extern BList R_stack;
|
|---|
| 35 | void R_concat(BList* head, BList* tail);
|
|---|
| 36 |
|
|---|
| 37 | BList R_stack;
|
|---|
| 38 |
|
|---|
| 39 | //----------------------------------------------------------------------------
|
|---|
| 40 | void R_concat(BList* head, BList* tail)
|
|---|
| 41 | //----------------------------------------------------------------------------
|
|---|
| 42 | {
|
|---|
| 43 | if (head->Cdr()) {
|
|---|
| 44 | R_concat(head->Cdr(), tail);
|
|---|
| 45 | }
|
|---|
| 46 | else {
|
|---|
| 47 | head->PutCdr(tail);
|
|---|
| 48 | }
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | //----------------------------------------------------------------------------
|
|---|
| 52 | extern "C" DLLEXPORT(void) RTolCompile(char **name, int *ok)
|
|---|
| 53 | //----------------------------------------------------------------------------
|
|---|
| 54 | {
|
|---|
| 55 | if(TOLHasBeenInitialized()==0) RInitTolKernel();
|
|---|
| 56 | char* str_lvalue = (char*)*name;
|
|---|
| 57 | BSyntaxObject * so = GraAnything()->EvaluateExpr(str_lvalue);
|
|---|
| 58 | if (so) {
|
|---|
| 59 | BList* result = NIL;
|
|---|
| 60 | BList* aux = NIL;
|
|---|
| 61 | LstFastAppend(result, aux, so);
|
|---|
| 62 | R_concat(&R_stack, result);
|
|---|
| 63 | *ok = 1;
|
|---|
| 64 | }
|
|---|
| 65 | else {
|
|---|
| 66 | *ok = 0;
|
|---|
| 67 | }
|
|---|
| 68 | }
|
|---|
| 69 |
|
|---|
| 70 | //----------------------------------------------------------------------------
|
|---|
| 71 | extern "C" DLLEXPORT(void) RTolDecompile(char **name)
|
|---|
| 72 | //----------------------------------------------------------------------------
|
|---|
| 73 | {
|
|---|
| 74 | char* str_name = (char*)*name;
|
|---|
| 75 | BList *P = R_stack.Cdr(), *prev = &R_stack;
|
|---|
| 76 | short found = 0;
|
|---|
| 77 |
|
|---|
| 78 | while (!found && P) {
|
|---|
| 79 | BSyntaxObject * so = (BSyntaxObject*)(P->Car());
|
|---|
| 80 | const char* object_name = so->Name().String();
|
|---|
| 81 | if (!strcmp(object_name, str_name)) {
|
|---|
| 82 | prev->PutCdr(P->Cdr());
|
|---|
| 83 | delete P;
|
|---|
| 84 | DESTROY(so);
|
|---|
| 85 | found = 1;
|
|---|
| 86 | break;
|
|---|
| 87 | }
|
|---|
| 88 | prev = P;
|
|---|
| 89 | P = P->Cdr();
|
|---|
| 90 | }
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 94 |
|
|---|
| 95 | //----------------------------------------------------------------------------
|
|---|
| 96 | extern "C" DLLEXPORT(void) RTOLVersion(char **version)
|
|---|
| 97 | //----------------------------------------------------------------------------
|
|---|
| 98 | {
|
|---|
| 99 | *version = (char*)TOLVersion();
|
|---|
| 100 | }
|
|---|
| 101 |
|
|---|
| 102 | //----------------------------------------------------------------------------
|
|---|
| 103 | extern "C" DLLEXPORT(void) RTOLHasBeenInitialized(int *initialized)
|
|---|
| 104 | //----------------------------------------------------------------------------
|
|---|
| 105 | {
|
|---|
| 106 | *initialized = TOLHasBeenInitialized();
|
|---|
| 107 | }
|
|---|
| 108 |
|
|---|
| 109 | //----------------------------------------------------------------------------
|
|---|
| 110 | extern "C" DLLEXPORT(void) RInitTolKernel(void)
|
|---|
| 111 | //----------------------------------------------------------------------------
|
|---|
| 112 | {
|
|---|
| 113 | InitTolKernel(NULL, 0, "vA");
|
|---|
| 114 | LoadInitLibrary(true, true);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 118 |
|
|---|
| 119 | //----------------------------------------------------------------------------
|
|---|
| 120 | extern "C" DLLEXPORT(void) RTolGetReal(char **name, double *value)
|
|---|
| 121 | //----------------------------------------------------------------------------
|
|---|
| 122 | {
|
|---|
| 123 | if(TOLHasBeenInitialized()==0) RInitTolKernel();
|
|---|
| 124 | char* str_lvalue = (char*)*name;
|
|---|
| 125 | BSyntaxObject * so = GraReal()->EvaluateExpr(str_lvalue);
|
|---|
| 126 | if (so) {
|
|---|
| 127 | BDat & d = Dat(so);
|
|---|
| 128 | *value = d.Value();
|
|---|
| 129 | DESTROY(so);
|
|---|
| 130 | }
|
|---|
| 131 | else {
|
|---|
| 132 | error("Real expression '%s' is not valid", str_lvalue);
|
|---|
| 133 | }
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | //----------------------------------------------------------------------------
|
|---|
| 137 | extern "C" DLLEXPORT(void) RTolGetText(char **name, char **value)
|
|---|
| 138 | //----------------------------------------------------------------------------
|
|---|
| 139 | {
|
|---|
| 140 | if(TOLHasBeenInitialized()==0) RInitTolKernel();
|
|---|
| 141 | char* str_lvalue = (char*)*name;
|
|---|
| 142 | BSyntaxObject * so = GraText()->EvaluateExpr(str_lvalue);
|
|---|
| 143 | if (so) {
|
|---|
| 144 | BText & d = Text(so);
|
|---|
| 145 | *value = (char *)(d.String());
|
|---|
| 146 | DESTROY(so);
|
|---|
| 147 | }
|
|---|
| 148 | else {
|
|---|
| 149 | error("Text expression '%s' is not valid", str_lvalue);
|
|---|
| 150 | }
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | //----------------------------------------------------------------------------
|
|---|
| 154 | extern "C" DLLEXPORT(void) RTolGetDate(char **name, double *value)
|
|---|
| 155 | //----------------------------------------------------------------------------
|
|---|
| 156 | {
|
|---|
| 157 | if(TOLHasBeenInitialized()==0) RInitTolKernel();
|
|---|
| 158 | char* str_lvalue = (char*)*name;
|
|---|
| 159 | BSyntaxObject * so = GraDate()->EvaluateExpr(str_lvalue);
|
|---|
| 160 | if (so) {
|
|---|
| 161 | BDate & d = Date(so);
|
|---|
| 162 | *value = (double)(d.Index());
|
|---|
| 163 | DESTROY(so);
|
|---|
| 164 | }
|
|---|
| 165 | else {
|
|---|
| 166 | error("Date expression '%s' is not valid", str_lvalue);
|
|---|
| 167 | }
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | //----------------------------------------------------------------------------
|
|---|
| 171 | extern "C" DLLEXPORT(void) RTolGetMatrix(char **name, double *value)
|
|---|
| 172 | //----------------------------------------------------------------------------
|
|---|
| 173 | {
|
|---|
| 174 | if(TOLHasBeenInitialized()==0) RInitTolKernel();
|
|---|
| 175 | char* str_lvalue = (char*)*name;
|
|---|
| 176 | BSyntaxObject * so = GraMatrix()->EvaluateExpr(str_lvalue);
|
|---|
| 177 | if (so) {
|
|---|
| 178 | BMat & m = Mat(so);
|
|---|
| 179 | int cols = m.Columns();
|
|---|
| 180 | int rows = m.Rows();
|
|---|
| 181 | for (int c=0; c<cols; ++c) {
|
|---|
| 182 | for (int r=0; r<rows; ++r) {
|
|---|
| 183 | BDat & d = m(r, c);
|
|---|
| 184 | value[c*rows+r] = d.Value();
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 | DESTROY(so);
|
|---|
| 188 | }
|
|---|
| 189 | else {
|
|---|
| 190 | error("Matrix expression '%s' is not valid", str_lvalue);
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 195 |
|
|---|
| 196 | //----------------------------------------------------------------------------
|
|---|
| 197 | extern "C" DLLEXPORT(void) RTolSetReal(char **name, int *type, double *value)
|
|---|
| 198 | //----------------------------------------------------------------------------
|
|---|
| 199 | {
|
|---|
| 200 | if(TOLHasBeenInitialized()==0) RInitTolKernel();
|
|---|
| 201 | char* str_name = (char*)*name;
|
|---|
| 202 | BDat d;
|
|---|
| 203 | if(*type == 0) {
|
|---|
| 204 | d.PutValue(*value);
|
|---|
| 205 | } else if(*type == 2) {
|
|---|
| 206 | d.PutValue(BDat::PosInf());
|
|---|
| 207 | } else if(*type == 3) {
|
|---|
| 208 | d.PutValue(BDat::NegInf());
|
|---|
| 209 | } else { // type 1
|
|---|
| 210 | d.PutKnown(BUNKNOWN);
|
|---|
| 211 | }
|
|---|
| 212 | BSyntaxObject * so = new BContensDat(str_name, d, "");
|
|---|
| 213 | if (so) {
|
|---|
| 214 | BList* result = NIL;
|
|---|
| 215 | BList* aux = NIL;
|
|---|
| 216 | LstFastAppend(result, aux, so);
|
|---|
| 217 | }
|
|---|
| 218 | }
|
|---|
| 219 |
|
|---|
| 220 | //----------------------------------------------------------------------------
|
|---|
| 221 | extern "C" DLLEXPORT(void) RTolSetText(char **name, char **value)
|
|---|
| 222 | //----------------------------------------------------------------------------
|
|---|
| 223 | {
|
|---|
| 224 | if(TOLHasBeenInitialized()==0) RInitTolKernel();
|
|---|
| 225 | char* str_name = (char*)*name;
|
|---|
| 226 | char* str_value = (char*)*value;
|
|---|
| 227 | BSyntaxObject * so = new BContensText(str_name, str_value, "");
|
|---|
| 228 | if (so) {
|
|---|
| 229 | BList* result = NIL;
|
|---|
| 230 | BList* aux = NIL;
|
|---|
| 231 | LstFastAppend(result, aux, so);
|
|---|
| 232 | }
|
|---|
| 233 | }
|
|---|
| 234 |
|
|---|
| 235 | //----------------------------------------------------------------------------
|
|---|
| 236 | extern "C" DLLEXPORT(void) RTolSetDate(char **name, int *type, double *value)
|
|---|
| 237 | //----------------------------------------------------------------------------
|
|---|
| 238 | {
|
|---|
| 239 | if(TOLHasBeenInitialized()==0) RInitTolKernel();
|
|---|
| 240 | char* str_name = (char*)*name;
|
|---|
| 241 | BDate d;
|
|---|
| 242 | if(*type == 0) {
|
|---|
| 243 | d.PutIndex(*value);
|
|---|
| 244 | } else if(*type == 2) {
|
|---|
| 245 | d.PutIndex(-115782); // Begin
|
|---|
| 246 | } else if(*type == 3) {
|
|---|
| 247 | d.PutIndex(219147); // End
|
|---|
| 248 | } else { // type 1
|
|---|
| 249 | d.PutIndex(-300000); // Unknown
|
|---|
| 250 | }
|
|---|
| 251 | BSyntaxObject * so = new BContensDate(str_name, d, "");
|
|---|
| 252 | if (so) {
|
|---|
| 253 | BList* result = NIL;
|
|---|
| 254 | BList* aux = NIL;
|
|---|
| 255 | LstFastAppend(result, aux, so);
|
|---|
| 256 | }
|
|---|
| 257 | }
|
|---|
| 258 |
|
|---|
| 259 | //////////////////////////////////////////////////////////////////////////////
|
|---|
| 260 |
|
|---|