Debian Codegraph @

ipl/packs/loadfuncpp/loadfuncpp.h

raw
1
2 /* C++ support for easy extensions to icon via loadfunc,
3 * without garbage collection difficulties.
4 * Include this and link to iload.cpp which
5 * contains the necessary glue.
6 * See iexample.cpp for typical use.
7 * Carl Sturtivant, 2008/3/17
8 */
9
10 #include<new>
11 #include<cstdio>
12
13 enum kind { Null, Integer, BigInteger, Real, Cset, File, Procedure, Record, List,
14 Set=10, Table=12, String, Constructor, Coexpression=18, External, Variable };
15
16 enum special_value { NullString, StringLiteral, NewString, NullChar, Illegal };
17
18 enum {
19 SUCCEEDED = 7, // Icon function call returned: A_Continue
20 FAILED = 1 // Icon function call failed: A_Resume
21 };
22
23 class value; //Icon value (descriptor)
24 class safe; //for garbage-collection-safe Icon valued C++ variables and parameters of all kinds
25 class keyword; //Icon keyword represented as an object with unary &
26 class variadic; //for garbage-collection-safe variadic function argument lists
27 class proc_block; //block specifying a procedure to iconx
28 class external_block; //block specifying an external value to iconx
29 class external_ftable; //function pointers specifying external value behavior to iconx
30 class external; //C++ Object specifying an external value
31
32 typedef int iconfunc(value argv[]); //type of icon built in functions or operators with a fixed number of arguments
33 typedef int iconfvbl(int argc, value argv[]); //type of icon built in functions with a variable number of arguments
34
35 extern const value nullvalue; //for default arguments
36 extern const value nullstring;
37 extern const value nullchar;
38 extern const value illegal; //for unwanted trailing arguments
39 extern void syserror(const char*); //fatal termination Icon-style with error message
40 #define Fs_Read 0001 // file open for reading
41 #define Fs_Write 0002 // file open for writing
42 extern value IconFile(int fd, int status, char* fname); //make an Icon file descriptor
43 extern value integertobytes(value); //get the bytes of an Icon long integer as an Icon string (ignore sign)
44 extern value bytestointeger(value); //get the bytes of a new Icon long integer from an Icon string
45 extern value base64(value); //convert string or integer to base64 encoding (string)
46 extern value base64tointeger(value); //decode base64 string to integer
47 extern value base64tostring(value); //decode base64 string to string
48
49 namespace Icon {
50 //all keywords excepting &fail, &cset (avoiding a name collision with function cset)
51 extern keyword allocated;
52 extern keyword ascii;
53 extern keyword clock;
54 extern keyword collections;
55 extern keyword current;
56 extern keyword date;
57 extern keyword dateline;
58 extern keyword digits;
59 extern keyword dump;
60 extern keyword e;
61 extern keyword error;
62 extern keyword errornumber;
63 extern keyword errortext;
64 extern keyword errorvalue;
65 extern keyword errout;
66 extern keyword features;
67 extern keyword file;
68 extern keyword host;
69 extern keyword input;
70 extern keyword lcase;
71 extern keyword letters;
72 extern keyword level;
73 extern keyword line;
74 extern keyword main;
75 extern keyword null;
76 extern keyword output;
77 extern keyword phi;
78 extern keyword pi;
79 extern keyword pos;
80 extern keyword progname;
81 extern keyword random;
82 extern keyword regions;
83 extern keyword source;
84 extern keyword storage;
85 extern keyword subject;
86 extern keyword time;
87 extern keyword trace;
88 extern keyword ucase;
89 extern keyword version;
90 }; //namespace Icon
91
92 static void initialize_keywords();
93
94 class keyword { //objects representing Icon keywords
95 friend void initialize_keywords();
96 iconfunc* f;
97 public:
98 safe operator&(); //get the keyword's value (could be an Icon 'variable')
99 };
100
101
102 class value { //a descriptor with class
103 //data members modelled after 'typedef struct { word dword, vword; } descriptor;' from icall.h
104 private:
105 long dword;
106 long vword;
107 public:
108 friend class safe;
109 friend value IconFile(FILE* fd, int status, char* fname);
110 friend value integertobytes(value);
111 friend value bytestointeger(value);
112 friend value base64(value);
113 friend value base64tointeger(value);
114 friend value base64tostring(value);
115 value(); //&null
116 value(special_value, const char* text = "");
117 value(int argc, value* argv); //makes a list of parameters passed in from Icon
118 value(int);
119 value(long);
120 value(float);
121 value(double);
122 value(char*);
123 value(const char*);
124 value(const char*, long);
125 value(proc_block&);
126 value(proc_block*);
127 value(external*);
128 operator int();
129 operator long();
130 operator float();
131 operator double();
132 operator char*();
133 operator external*();
134 operator proc_block*() const;
135 bool operator==(const value&) const;
136 value& dereference();
137 value intify();
138 bool isNull();
139 bool notNull();
140 bool isExternal(const value&);
141 value size() const;
142 kind type();
143 bool toString(); //attempted conversion in place
144 bool toCset();
145 bool toInteger();
146 bool toReal();
147 bool toNumeric();
148 value subscript(const value&) const; //produces an Icon 'variable'
149 value& assign(const value&); //dereferences Icon style
150 value put(value x = nullvalue);
151 value push(value x = nullvalue);
152 void dump() const;
153 void printimage() const;
154 int compare(const value&) const; //comparator-style result: used for Icon sorting
155 value negative() const; // -x
156 value complement() const; // ~x
157 value refreshed() const; // ^x
158 value random() const; // ?x
159 value plus(const value&) const;
160 value minus(const value&) const;
161 value multiply(const value&) const;
162 value divide(const value&) const;
163 value remainder(const value&) const;
164 value power(const value&) const;
165 value union_(const value&) const; // x ++ y
166 value intersection(const value&) const; // x ** y
167 value difference(const value&) const; // x -- y
168 value concatenate(const value&) const; // x || y
169 value listconcatenate(const value&) const;// x ||| y
170 value slice(const value&, const value&) const; // x[y:z]
171 value& swap(value&); // x :=: y
172 value activate(const value& y = nullvalue) const; // y @ x ('*this' is activated)
173 value apply(const value&) const; // x!y (must return, not fail or suspend)
174 }; //class value
175
176
177 class generator {
178 //class to inherit from for defining loadable functions that are generators
179 public:
180 int generate(value argv[]); //call to suspend everything produced by next()
181 protected: //override these, and write a constructor
182 virtual bool hasNext();
183 virtual value giveNext();
184 }; //class generator
185
186
187 class iterate {
188 //class to inherit from for iterating over f!arg or !x
189 public:
190 void every(const value& g, const value& arg); //perform the iteration over g!arg
191 void bang(const value& x); //perform the iteration over !x
192 //override these, write a constructor and the means of recovering the answer
193 virtual bool wantNext(const value& x);
194 virtual void takeNext(const value& x);
195 };
196
197
198
199 class safe_variable {
200 //data members modelled after 'struct tend_desc' from rstructs.h
201 friend class value;
202 friend inline int safecall_0(iconfunc*, value&);
203 friend inline int safecall_1(iconfunc*, value&, const value&);
204 friend inline int safecall_2(iconfunc*, value&, const value&, const value&);
205 friend inline int safecall_3(iconfunc*, value&, const value&, const value&, const value&);
206 friend inline int safecall_4(iconfunc*, value&, const value&, const value&, const value&, const value&);
207 friend inline int safecall_5(iconfunc*, value&, const value&, const value&, const value&, const value&, const value&);
208 friend inline int safecall_6(iconfunc*, value&, const value&, const value&, const value&, const value&, const value&, const value&);
209 friend inline int safecall_v0(iconfvbl*, value&);
210 friend inline int safecall_v1(iconfvbl*, value&, const value&);
211 friend inline int safecall_v2(iconfvbl*, value&, const value&, const value&);
212 friend inline int safecall_v3(iconfvbl*, value&, const value&, const value&, const value&);
213 friend inline int safecall_vbl(iconfvbl*,safe&, const variadic&);
214 protected:
215 safe_variable *previous;
216 int num;
217 value val;
218 safe_variable();
219 safe_variable(int);
220 safe_variable(long);
221 safe_variable(double);
222 safe_variable(value);
223 safe_variable(proc_block&);
224 safe_variable(proc_block*);
225 safe_variable(int, value*);
226 inline void push(safe_variable*& tendlist, int numvalues=1);
227 inline void pop(safe_variable*& tendlist);
228 }; //class safe_variable
229
230
231 class variadic: public safe_variable {
232 public:
233 variadic(int);
234 variadic(long);
235 variadic(float);
236 variadic(double);
237 variadic(char*);
238 variadic(value);
239 variadic(const safe&);
240 variadic(const safe&, const safe&);
241 variadic& operator,(const safe&);
242 operator value();
243 ~variadic();
244 }; //class variadic
245
246
247 class external_block {
248 //modelled on 'struct b_external' in icon/src/h/rstructs.h
249 friend class external;
250 friend class value;
251 static long extra_bytes; //silent extra parameter to new
252 long title;
253 long blksize;
254 long id;
255 external_ftable* funcs;
256 external* val;
257 static void* operator new(size_t); //allocated by iconx
258 static void operator delete(void*); //do nothing
259 external_block();
260 };
261
262 class external {
263 friend class value;
264 static external_block* blockptr; //silent extra result of new
265 protected:
266 long id;
267 public:
268 static void* operator new(size_t); //allocated by new external_block()
269 static void operator delete(void*); //do nothing
270 external();
271 virtual ~external() {} //root class
272 virtual long compare(external*);
273 virtual value name();
274 virtual external* copy();
275 virtual value image();
276 };
277
278
279 class safe: public safe_variable {
280 //use for a garbage collection safe icon valued safe C++ variable
281 friend class variadic;
282 friend class global;
283 public:
284 safe(); //&null
285 safe(const safe&);
286 safe(int);
287 safe(long);
288 safe(float);
289 safe(double);
290 safe(char*);
291 safe(const value&);
292 safe(const variadic&);
293 safe(proc_block&);
294 safe(proc_block*);
295 safe(int, value*); //from parameters sent in from Icon
296 ~safe();
297 safe& operator=(const safe&);
298 //augmenting assignments here
299 safe& operator+=(const safe&);
300 safe& operator-=(const safe&);
301 safe& operator*=(const safe&);
302 safe& operator/=(const safe&);
303 safe& operator%=(const safe&);
304 safe& operator^=(const safe&);
305 safe& operator&=(const safe&);
306 safe& operator|=(const safe&);
307 // ++ and -- here
308 safe& operator++();
309 safe& operator--();
310 safe operator++(int);
311 safe operator--(int);
312 //conversion to value
313 operator value() const;
314 //procedure call
315 safe operator()();
316 safe operator()(const safe&);
317 safe operator()(const safe& x1, const safe& x2,
318 const safe& x3 = illegal, const safe& x4 = illegal,
319 const safe& x5 = illegal, const safe& x6 = illegal,
320 const safe& x7 = illegal, const safe& x8 = illegal);
321 safe operator[](const safe&);
322
323 friend safe operator*(const safe&); //size
324 friend safe operator-(const safe&);
325 friend safe operator~(const safe&); //set complement
326 friend safe operator+(const safe&, const safe&);
327 friend safe operator-(const safe&, const safe&);
328 friend safe operator*(const safe&, const safe&);
329 friend safe operator/(const safe&, const safe&);
330 friend safe operator%(const safe&, const safe&);
331 friend safe operator^(const safe&, const safe&); //exponentiation
332 friend safe operator|(const safe&, const safe&); //union
333 friend safe operator&(const safe&, const safe&); //intersection
334 friend safe operator&&(const safe&, const safe&); //set or cset difference
335 friend safe operator||(const safe&, const safe&); //string concatenation
336 friend bool operator<(const safe&, const safe&);
337 friend bool operator>(const safe&, const safe&);
338 friend bool operator<=(const safe&, const safe&);
339 friend bool operator>=(const safe&, const safe&);
340 friend bool operator==(const safe&, const safe&);
341 friend bool operator!=(const safe&, const safe&);
342 friend variadic operator,(const safe&, const safe&); //variadic argument list construction
343
344 safe slice(const safe&, const safe&); // x[y:z]
345 safe apply(const safe&); // x ! y
346 safe listcat(const safe&); // x ||| y
347 safe& swap(safe&); // x :=: y
348 safe create(); // create !x
349 safe create(const safe&); // create x!y
350 safe activate(const safe& y = nullvalue); // y@x
351 safe refresh(); // ^x
352 safe random(); // ?x
353 safe dereference(); // .x
354 bool isIllegal() const; //is an illegal value used for trailing arguments
355 }; //class safe
356
357
358 //Icon built-in functions
359 namespace Icon {
360 safe abs(const safe&);
361 safe acos(const safe&);
362 safe args(const safe&);
363 safe asin(const safe&);
364 safe atan(const safe&, const safe&);
365 safe center(const safe&, const safe&, const safe&);
366 safe char_(const safe&);
367 safe chdir(const safe&);
368 safe close(const safe&);
369 safe collect();
370 safe copy(const safe&);
371 safe cos(const safe&);
372 safe cset(const safe&);
373 safe delay(const safe&);
374 safe delete_(const safe&, const safe&);
375 safe detab(const variadic&);
376 safe detab( const safe& x1, const safe& x2,
377 const safe& x3=illegal, const safe& x4=illegal,
378 const safe& x5=illegal, const safe& x6=illegal,
379 const safe& x7=illegal, const safe& x8=illegal );
380 safe display(const safe&, const safe&);
381 safe dtor(const safe&);
382 safe entab(const variadic&);
383 safe entab( const safe& x1, const safe& x2,
384 const safe& x3=illegal, const safe& x4=illegal,
385 const safe& x5=illegal, const safe& x6=illegal,
386 const safe& x7=illegal, const safe& x8=illegal );
387 safe errorclear();
388 safe exit(const safe&);
389 safe exp(const safe&);
390 safe flush(const safe&);
391 safe function(); //generative: returns a list
392 safe get(const safe&);
393 safe getch();
394 safe getche();
395 safe getenv(const safe&);
396 safe iand(const safe&, const safe&);
397 safe icom(const safe&);
398 safe image(const safe&);
399 safe insert(const safe&, const safe&, const safe&);
400 safe integer(const safe&);
401 safe ior(const safe&, const safe&);
402 safe ishift(const safe&, const safe&);
403 safe ixor(const safe&, const safe&);
404 safe kbhit();
405 safe left(const safe&, const safe&, const safe&);
406 safe list(const safe&, const safe&);
407 safe loadfunc(const safe&, const safe&);
408 safe log(const safe&);
409 safe map(const safe&, const safe&, const safe&);
410 safe member(const safe&, const safe&);
411 safe name(const safe&);
412 safe numeric(const safe&);
413 safe open(const safe&, const safe&);
414 safe ord(const safe&);
415 safe pop(const safe&);
416 safe proc(const safe&, const safe&);
417 safe pull(const safe&);
418 safe push(const variadic&);
419 safe push( const safe& x1, const safe& x2,
420 const safe& x3=illegal, const safe& x4=illegal,
421 const safe& x5=illegal, const safe& x6=illegal,
422 const safe& x7=illegal, const safe& x8=illegal );
423 safe put(const variadic&);
424 safe put( const safe& x1, const safe& x2,
425 const safe& x3=illegal, const safe& x4=illegal,
426 const safe& x5=illegal, const safe& x6=illegal,
427 const safe& x7=illegal, const safe& x8=illegal );
428 safe read(const safe&);
429 safe reads(const safe&, const safe&);
430 safe real(const safe&);
431 safe remove(const safe&);
432 safe rename(const safe&, const safe&);
433 safe repl(const safe&, const safe&);
434 safe reverse(const safe&);
435 safe right(const safe&, const safe&, const safe&);
436 safe rtod(const safe&);
437 safe runerr(const safe&, const safe&);
438 safe runerr(const safe&);
439 safe seek(const safe&, const safe&);
440 safe serial(const safe&);
441 safe set(const safe&);
442 safe sin(const safe&);
443 safe sort(const safe&, const safe&);
444 safe sortf(const safe&, const safe&);
445 safe sqrt(const safe&);
446 safe stop();
447 safe stop(const variadic&);
448 safe stop( const safe& x1, const safe& x2,
449 const safe& x3=illegal, const safe& x4=illegal,
450 const safe& x5=illegal, const safe& x6=illegal,
451 const safe& x7=illegal, const safe& x8=illegal );
452 safe string(const safe&);
453 safe system(const safe&);
454 safe table(const safe&);
455 safe tan(const safe&);
456 safe trim(const safe&, const safe&);
457 safe type(const safe&);
458 safe variable(const safe&);
459 safe where(const safe&);
460 safe write();
461 safe write(const variadic&);
462 safe write( const safe& x1, const safe& x2,
463 const safe& x3=illegal, const safe& x4=illegal,
464 const safe& x5=illegal, const safe& x6=illegal,
465 const safe& x7=illegal, const safe& x8=illegal );
466 safe writes(const variadic&);
467 safe writes( const safe& x1, const safe& x2,
468 const safe& x3=illegal, const safe& x4=illegal,
469 const safe& x5=illegal, const safe& x6=illegal,
470 const safe& x7=illegal, const safe& x8=illegal );
471 //generative functions follow, crippled to return a single value
472 safe any(const safe&, const safe&, const safe&, const safe&);
473 safe many(const safe&, const safe&, const safe&, const safe&);
474 safe upto(const safe&, const safe&, const safe&, const safe&);
475 safe find(const safe&, const safe&, const safe&, const safe&);
476 safe match(const safe&, const safe&, const safe&, const safe&);
477 safe bal(const safe&, const safe&, const safe&, const safe&, const safe&, const safe&);
478 safe move(const safe&);
479 safe tab(const safe&);
480 }; //namespace Icon
481