Debian Codegraph @

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