| 1 |
|
| 2 |
/* C++ support for easy extensions to icon via loadfunc, |
| 3 |
* without garbage collection difficulties. |
| 4 |
* Include loadfuncpp.h and link dynamically to |
| 5 |
* iload.cpp, which contains the necessary glue. |
| 6 |
* See iexample.cpp for typical use. |
| 7 |
* Carl Sturtivant, 2008/3/17 |
| 8 |
*/ |
| 9 |
|
| 10 |
|
| 11 |
#include <climits> |
| 12 |
#include <cstdlib> |
| 13 |
|
| 14 |
#if LONG_MAX == 2147483647L //32 bit icon implementation word |
| 15 |
#define D_Null 0xA0000000 |
| 16 |
#define D_Integer 0xA0000001 |
| 17 |
#define D_Lrgint 0xB0000002 |
| 18 |
#define D_Real 0xB0000003 |
| 19 |
#define D_File 0xB0000005 |
| 20 |
#define D_Proc 0xB0000006 |
| 21 |
#define D_External 0xB0000013 |
| 22 |
#define D_Illegal 0xA0000063 |
| 23 |
#define F_Nqual 0x80000000 |
| 24 |
#define F_Var 0x40000000 |
| 25 |
#else //64 bit icon implementation word |
| 26 |
#define D_Null 0xA000000000000000 |
| 27 |
#define D_Integer 0xA000000000000001 |
| 28 |
#define D_Lrgint 0xB000000000000002 |
| 29 |
#define D_Real 0xB000000000000003 |
| 30 |
#define D_File 0xB000000000000005 |
| 31 |
#define D_Proc 0xB000000000000006 |
| 32 |
#define D_External 0xB000000000000013 |
| 33 |
#define D_Illegal 0xA000000000000063 |
| 34 |
#define F_Nqual 0x8000000000000000 |
| 35 |
#define F_Var 0x4000000000000000 |
| 36 |
#endif |
| 37 |
|
| 38 |
#define T_Null 0 // null value |
| 39 |
#define T_Integer 1 // integer |
| 40 |
#define T_Lrgint 2 // long integer |
| 41 |
#define T_Real 3 // real number |
| 42 |
#define T_Cset 4 // cset |
| 43 |
#define T_File 5 // file |
| 44 |
#define T_Proc 6 // procedure |
| 45 |
#define T_Record 7 // record |
| 46 |
#define T_List 8 // list |
| 47 |
#define T_Set 10 // set |
| 48 |
#define T_Table 12 // table |
| 49 |
#define T_Coexpr 18 // coexpression |
| 50 |
#define T_External 19 // external value |
| 51 |
|
| 52 |
#define TypeMask 63 // type mask |
| 53 |
|
| 54 |
#define SUSPEND 1 // Call the interpreter suspending from a C function: G_Csusp |
| 55 |
|
| 56 |
extern "C" { //callbacks in iconx |
| 57 |
|
| 58 |
void deref(value*, value*); //dereference an icon 'variable' descriptor |
| 59 |
char* alcstr(char*, int); //allocate an icon string by copying |
| 60 |
char *alcreal(double); //allocate double by copying |
| 61 |
char *alcbignum(long); //allocate Icon large integer block w/ given number of DIGITS |
| 62 |
double getdbl(value*); //retrieve double |
| 63 |
char* alcfile(FILE *fp, int stat, value *name); |
| 64 |
int anycmp(const value*, const value*); //comparator used when sorting in Icon |
| 65 |
//alcexternal in iconx for Icon 9.5 and above |
| 66 |
external* alcexternal(long nbytes, external_ftable* ftable, external* ep); |
| 67 |
|
| 68 |
void syserr(char*); //fatally terminate Icon-style with error message |
| 69 |
|
| 70 |
int interp(int fsig, value *cargp); //the Icon interpreter, called recursively when suspending |
| 71 |
|
| 72 |
|
| 73 |
//the prototypes of all icon functions and operators in iconx needed to do the dirty work |
| 74 |
iconfunc Oasgn; |
| 75 |
iconfunc Osubsc; |
| 76 |
iconfunc Osize; |
| 77 |
iconfunc Oneg; |
| 78 |
iconfunc Ocompl; |
| 79 |
iconfunc Orefresh; |
| 80 |
iconfunc Orandom; |
| 81 |
iconfunc Oplus; |
| 82 |
iconfunc Ominus; |
| 83 |
iconfunc Omult; |
| 84 |
iconfunc Odivide; |
| 85 |
iconfunc Omod; |
| 86 |
iconfunc Opowr; |
| 87 |
iconfunc Ounion; |
| 88 |
iconfunc Ointer; |
| 89 |
iconfunc Odiff; |
| 90 |
iconfunc Ocater; |
| 91 |
iconfunc Olconcat; |
| 92 |
iconfunc Osect; |
| 93 |
iconfunc Oswap; |
| 94 |
|
| 95 |
iconfvbl Ollist; |
| 96 |
|
| 97 |
iconfunc Zloadfunc; |
| 98 |
iconfunc Zproc; |
| 99 |
iconfunc Zvariable; |
| 100 |
|
| 101 |
iconfunc Zlist; |
| 102 |
iconfunc Zset; |
| 103 |
iconfunc Ztable; |
| 104 |
|
| 105 |
iconfunc Zstring; |
| 106 |
iconfunc Zcset; |
| 107 |
iconfunc Zinteger; |
| 108 |
iconfunc Zreal; |
| 109 |
iconfunc Znumeric; |
| 110 |
|
| 111 |
iconfvbl Zput; |
| 112 |
iconfvbl Zpush; |
| 113 |
|
| 114 |
iconfvbl Zrunerr; |
| 115 |
iconfvbl Zwrites; |
| 116 |
iconfunc Zimage; |
| 117 |
|
| 118 |
iconfunc Zabs; |
| 119 |
iconfunc Zacos; |
| 120 |
iconfunc Zargs; |
| 121 |
iconfunc Zasin; |
| 122 |
iconfunc Zatan; |
| 123 |
iconfunc Zcenter; |
| 124 |
iconfunc Zchar; |
| 125 |
iconfunc Zchdir; |
| 126 |
iconfunc Zclose; |
| 127 |
iconfunc Zcollect; |
| 128 |
iconfunc Zcopy; |
| 129 |
iconfunc Zcos; |
| 130 |
iconfunc Zdelay; |
| 131 |
iconfunc Zdelete; |
| 132 |
iconfunc Zdisplay; |
| 133 |
iconfunc Zdtor; |
| 134 |
iconfunc Zerrorclear; |
| 135 |
iconfunc Zexit; |
| 136 |
iconfunc Zexp; |
| 137 |
iconfunc Zflush; |
| 138 |
iconfunc Zget; |
| 139 |
iconfunc Zgetch; |
| 140 |
iconfunc Zgetche; |
| 141 |
iconfunc Zgetenv; |
| 142 |
iconfunc Ziand; |
| 143 |
iconfunc Zicom; |
| 144 |
iconfunc Zinsert; |
| 145 |
iconfunc Zior; |
| 146 |
iconfunc Zishift; |
| 147 |
iconfunc Zixor; |
| 148 |
iconfunc Zkbhit; |
| 149 |
iconfunc Zleft; |
| 150 |
iconfunc Zlog; |
| 151 |
iconfunc Zmap; |
| 152 |
iconfunc Zmember; |
| 153 |
iconfunc Zname; |
| 154 |
iconfunc Zopen; |
| 155 |
iconfunc Zord; |
| 156 |
iconfunc Zpop; |
| 157 |
iconfunc Zpull; |
| 158 |
iconfunc Zread; |
| 159 |
iconfunc Zreads; |
| 160 |
iconfunc Zremove; |
| 161 |
iconfunc Zrename; |
| 162 |
iconfunc Zrepl; |
| 163 |
iconfunc Zreverse; |
| 164 |
iconfunc Zright; |
| 165 |
iconfunc Zrtod; |
| 166 |
iconfunc Zseek; |
| 167 |
iconfunc Zserial; |
| 168 |
iconfunc Zsin; |
| 169 |
iconfunc Zsort; |
| 170 |
iconfunc Zsortf; |
| 171 |
iconfunc Zsqrt; |
| 172 |
iconfunc Zsystem; |
| 173 |
iconfunc Ztan; |
| 174 |
iconfunc Ztrim; |
| 175 |
iconfunc Ztype; |
| 176 |
iconfunc Zwhere; |
| 177 |
|
| 178 |
iconfvbl Zdetab; |
| 179 |
iconfvbl Zentab; |
| 180 |
iconfvbl Zpush; |
| 181 |
iconfvbl Zput; |
| 182 |
iconfvbl Zstop; |
| 183 |
iconfvbl Zwrite; |
| 184 |
|
| 185 |
iconfunc Kallocated; |
| 186 |
iconfunc Kascii; |
| 187 |
iconfunc Kclock; |
| 188 |
//iconfunc Kcol; |
| 189 |
iconfunc Kcollections; |
| 190 |
//iconfunc Kcolumn; |
| 191 |
//iconfunc Kcontrol; |
| 192 |
iconfunc Kcset; |
| 193 |
iconfunc Kcurrent; |
| 194 |
iconfunc Kdate; |
| 195 |
iconfunc Kdateline; |
| 196 |
iconfunc Kdigits; |
| 197 |
iconfunc Kdump; |
| 198 |
iconfunc Ke; |
| 199 |
iconfunc Kerror; |
| 200 |
iconfunc Kerrornumber; |
| 201 |
iconfunc Kerrortext; |
| 202 |
iconfunc Kerrorvalue; |
| 203 |
iconfunc Kerrout; |
| 204 |
//iconfunc Keventcode; |
| 205 |
//iconfunc Keventsource; |
| 206 |
//iconfunc Keventvalue; |
| 207 |
iconfunc Kfail; |
| 208 |
iconfunc Kfeatures; |
| 209 |
iconfunc Kfile; |
| 210 |
iconfunc Khost; |
| 211 |
iconfunc Kinput; |
| 212 |
//iconfunc Kinterval; |
| 213 |
iconfunc Klcase; |
| 214 |
//iconfunc Kldrag; |
| 215 |
iconfunc Kletters; |
| 216 |
iconfunc Klevel; |
| 217 |
iconfunc Kline; |
| 218 |
//iconfunc Klpress; |
| 219 |
//iconfunc Klrelease; |
| 220 |
iconfunc Kmain; |
| 221 |
//iconfunc Kmdrag; |
| 222 |
//iconfunc Kmeta; |
| 223 |
//iconfunc Kmpress; |
| 224 |
//iconfunc Kmrelease; |
| 225 |
iconfunc Knull; |
| 226 |
iconfunc Koutput; |
| 227 |
iconfunc Kphi; |
| 228 |
iconfunc Kpi; |
| 229 |
iconfunc Kpos; |
| 230 |
iconfunc Kprogname; |
| 231 |
iconfunc Krandom; |
| 232 |
//iconfunc Krdrag; |
| 233 |
iconfunc Kregions; |
| 234 |
iconfunc Kresize; |
| 235 |
//iconfunc Krow; |
| 236 |
//iconfunc Krpress; |
| 237 |
//iconfunc Krrelease; |
| 238 |
//iconfunc Kshift; |
| 239 |
iconfunc Ksource; |
| 240 |
iconfunc Kstorage; |
| 241 |
iconfunc Ksubject; |
| 242 |
iconfunc Ktime; |
| 243 |
iconfunc Ktrace; |
| 244 |
iconfunc Kucase; |
| 245 |
iconfunc Kversion; |
| 246 |
iconfunc Kwindow; |
| 247 |
//iconfunc Kx; |
| 248 |
//iconfunc Ky; |
| 249 |
|
| 250 |
} //end extern "C" |
| 251 |
|
| 252 |
struct proc_block { |
| 253 |
long title; /* T_Proc */ |
| 254 |
long blksize; /* size of block */ |
| 255 |
iconfvbl *entryp; /* entry point for C routine */ |
| 256 |
long nparam; /* number of parameters */ |
| 257 |
long ndynam; /* number of dynamic locals */ |
| 258 |
long nstatic; /* number of static locals */ |
| 259 |
long fstatic; /* index (in global table) of first static */ |
| 260 |
value pname; /* procedure name (string qualifier) */ |
| 261 |
value lnames[1]; /* list of local names (qualifiers) */ |
| 262 |
private: |
| 263 |
inline void init(value procname) { |
| 264 |
title = T_Proc; |
| 265 |
blksize = sizeof(proc_block); |
| 266 |
ndynam = -1; //treat as a built-in function |
| 267 |
nstatic = 0; |
| 268 |
fstatic = 0; |
| 269 |
pname = procname; |
| 270 |
lnames[0] = nullstring; |
| 271 |
} |
| 272 |
static long extra_bytes; |
| 273 |
public: |
| 274 |
proc_block(value procname, iconfvbl *function); |
| 275 |
proc_block(value procname, iconfunc *function, int arity); |
| 276 |
proc_block(value procname, iconfvbl *function, int arity); |
| 277 |
proc_block(proc_block*); |
| 278 |
static proc_block* bind(proc_block*, const value&); |
| 279 |
static void* operator new(size_t); //allocated by iconx |
| 280 |
static void operator delete(void*); //do nothing |
| 281 |
}; |
| 282 |
|
| 283 |
struct coexp_block { |
| 284 |
long title; |
| 285 |
long size; |
| 286 |
long id; |
| 287 |
coexp_block* next; |
| 288 |
void* es_pfp; |
| 289 |
void* es_efp; |
| 290 |
void* es_gfp; |
| 291 |
safe_variable* es_tend; |
| 292 |
value* es_argp; |
| 293 |
//... |
| 294 |
}; |
| 295 |
|
| 296 |
// name/proc-block table of built-in functions |
| 297 |
struct pstrnm { char* pstrep; proc_block *pblock; }; |
| 298 |
extern pstrnm pntab[]; //table of original procedure blocks (src/runtime/data.r) |
| 299 |
extern int pnsize; //size of said table |
| 300 |
|
| 301 |
inline int safecall_0(iconfunc*, value&); |
| 302 |
inline int safecall_1(iconfunc*, value&, const value&); |
| 303 |
inline int safecall_2(iconfunc*, value&, const value&, const value&); |
| 304 |
inline int safecall_3(iconfunc*, value&, const value&, const value&, const value&); |
| 305 |
inline int safecall_4(iconfunc*, value&, const value&, const value&, const value&, const value&); |
| 306 |
inline int safecall_5(iconfunc*, value&, const value&, const value&, const value&, const value&, const value&); |
| 307 |
inline int safecall_6(iconfunc*, value&, const value&, const value&, const value&, const value&, const value&, const value&); |
| 308 |
inline int safecall_v0(iconfvbl*, value&); |
| 309 |
inline int safecall_v1(iconfvbl*, value&, const value&); |
| 310 |
inline int safecall_v2(iconfvbl*, value&, const value&, const value&); |
| 311 |
inline int safecall_v3(iconfvbl*, value&, const value&, const value&, const value&); |
| 312 |
inline int safecall_vbl(iconfvbl*,safe&, const variadic&); |
| 313 |
|
| 314 |
//iconx GC tend list |
| 315 |
extern safe_variable* tend; |
| 316 |
//our global GC tend list |
| 317 |
extern safe_variable*& global_tend; |
| 318 |
|
| 319 |
extern value k_current, k_main; //descriptors for ¤t and &main |
| 320 |
|
| 321 |
//useful helper functions |
| 322 |
namespace Value { |
| 323 |
value list(value n = (long)0, value init = nullvalue); |
| 324 |
value pair(value, value); |
| 325 |
value set(value list=nullvalue); |
| 326 |
void runerr(value i, value x = nullvalue); |
| 327 |
value table(value init = nullvalue); |
| 328 |
value variable(value name); |
| 329 |
value proc(value name, value arity = nullvalue); |
| 330 |
value libproc(value name, value arity = nullvalue); |
| 331 |
value call(const value& proc, const value& arglist); |
| 332 |
value create(const value&, const value&); // create x!y |
| 333 |
value reduce(const value&, const value&, const value&, const value&); |
| 334 |
}; //end namespace Value |
| 335 |
|
| 336 |
//raw call to the modified three argument loadfunc |
| 337 |
static int rawloadfuncpp(value argv[]); |
| 338 |
|