Debian Codegraph @

ipl/packs/loadfuncpp/iload.cpp

raw
1
2
3 /* C++ support for easy extensions to icon via loadfunc,
4 * without garbage collection difficulties.
5 * Include loadfuncpp.h and link dynamically to
6 * this, which contains the necessary glue.
7 * See iexample.cpp for typical use.
8 * Carl Sturtivant, 2008/3/17
9 */
10
11 #include <cstdio>
12 #include <cstring>
13
14 #include "loadfuncpp.h"
15 #include "iload.h"
16
17
18 /*
19 * References to the part of loadfuncpp written in Icon
20 */
21
22 //variables to refer to the Icon procedures in loadfuncpp.icn
23 static value _loadfuncpp_pathfind;
24 static value _loadfuncpp_reduce;
25 static value _loadfuncpp_create;
26 static value _loadfuncpp_activate;
27 static value _loadfuncpp_kcollections;
28 static value _loadfuncpp_kfeatures;
29 static value _loadfuncpp_kregions;
30 static value _loadfuncpp_kstorage;
31 static value _loadfuncpp_function;
32 static value _loadfuncpp_key;
33 static value _loadfuncpp_bang;
34 static value _loadfuncpp_any;
35 static value _loadfuncpp_many;
36 static value _loadfuncpp_upto;
37 static value _loadfuncpp_find;
38 static value _loadfuncpp_match;
39 static value _loadfuncpp_bal;
40 static value _loadfuncpp_move;
41 static value _loadfuncpp_tab;
42 static value _loadfuncpp_apply;
43
44 static void initialize_procs() { //called below, on load
45 _loadfuncpp_pathfind = Value::libproc("_loadfuncpp_pathfind");
46 _loadfuncpp_reduce = Value::libproc("_loadfuncpp_reduce");
47 _loadfuncpp_create = Value::libproc("_loadfuncpp_create");
48 _loadfuncpp_activate = Value::libproc("_loadfuncpp_activate");
49 _loadfuncpp_kcollections = Value::libproc("_loadfuncpp_kcollections");
50 _loadfuncpp_kfeatures = Value::libproc("_loadfuncpp_kfeatures");
51 _loadfuncpp_kregions = Value::libproc("_loadfuncpp_kregions");
52 _loadfuncpp_kstorage = Value::libproc("_loadfuncpp_kstorage");
53 _loadfuncpp_function = Value::libproc("_loadfuncpp_function");
54 _loadfuncpp_key = Value::libproc("_loadfuncpp_key");
55 _loadfuncpp_bang = Value::libproc("_loadfuncpp_bang");
56 _loadfuncpp_any = Value::libproc("_loadfuncpp_any");
57 _loadfuncpp_many = Value::libproc("_loadfuncpp_many");
58 _loadfuncpp_upto = Value::libproc("_loadfuncpp_upto");
59 _loadfuncpp_find = Value::libproc("_loadfuncpp_find");
60 _loadfuncpp_match = Value::libproc("_loadfuncpp_match");
61 _loadfuncpp_bal = Value::libproc("_loadfuncpp_bal");
62 _loadfuncpp_move = Value::libproc("_loadfuncpp_move");
63 _loadfuncpp_tab = Value::libproc("_loadfuncpp_tab");
64 _loadfuncpp_apply = Value::libproc("_loadfuncpp_apply");
65 }
66
67 //callbacks to Icon for generative keywords and functions
68 static int K_collections(value* argv) {
69 argv[0] = _loadfuncpp_kcollections.apply(Value::list());
70 return SUCCEEDED;
71 }
72
73 static int K_features(value* argv) {
74 argv[0] = _loadfuncpp_kfeatures.apply(Value::list());
75 return SUCCEEDED;
76 }
77
78 static int K_regions(value* argv) {
79 argv[0] = _loadfuncpp_kregions.apply(Value::list());
80 return SUCCEEDED;
81 }
82
83 static int K_storage(value* argv) {
84 argv[0] = _loadfuncpp_kstorage.apply(Value::list());
85 return SUCCEEDED;
86 }
87
88 static int Z_function(value* argv) {
89 argv[0] = _loadfuncpp_function.apply(Value::list());
90 return SUCCEEDED;
91 }
92
93 static int Z_key(value* argv) {
94 value arg(1,argv);
95 argv[0] = _loadfuncpp_key.apply(arg);
96 return SUCCEEDED;
97 }
98
99 static int Z_any(value* argv) {
100 value arg(4,argv);
101 argv[0] = _loadfuncpp_any.apply(arg);
102 return SUCCEEDED;
103 }
104
105 static int Z_many(value* argv) {
106 value arg(4,argv);
107 argv[0] = _loadfuncpp_many.apply(arg);
108 return SUCCEEDED;
109 }
110
111 static int Z_upto(value* argv) {
112 value arg(4,argv);
113 argv[0] = _loadfuncpp_upto.apply(arg);
114 return SUCCEEDED;
115 }
116
117 static int Z_find(value* argv) {
118 value arg(4,argv);
119 argv[0] = _loadfuncpp_find.apply(arg);
120 return SUCCEEDED;
121 }
122
123 static int Z_match(value* argv) {
124 value arg(4,argv);
125 argv[0] = _loadfuncpp_match.apply(arg);
126 return SUCCEEDED;
127 }
128
129 static int Z_bal(value* argv) {
130 value arg(6,argv);
131 argv[0] = _loadfuncpp_bal.apply(arg);
132 return SUCCEEDED;
133 }
134
135 static int Z_move(value* argv) {
136 value arg(1,argv);
137 argv[0] = _loadfuncpp_move.apply(arg);
138 return SUCCEEDED;
139 }
140
141 static int Z_tab(value* argv) {
142 value arg(1,argv);
143 argv[0] = _loadfuncpp_tab.apply(arg);
144 return SUCCEEDED;
145 }
146
147
148
149 /*
150 * Keywords and their initialization
151 */
152
153 namespace Icon {
154 //all non-graphics keywords excepting &fail, &cset (name collision with function cset)
155 keyword allocated;
156 keyword ascii;
157 keyword clock;
158 keyword collections;
159 keyword current;
160 keyword date;
161 keyword dateline;
162 keyword digits;
163 keyword dump;
164 keyword e;
165 keyword error;
166 keyword errornumber;
167 keyword errortext;
168 keyword errorvalue;
169 keyword errout;
170 keyword features;
171 keyword file;
172 keyword host;
173 keyword input;
174 keyword lcase;
175 keyword letters;
176 keyword level;
177 keyword line;
178 keyword main;
179 keyword null;
180 keyword output;
181 keyword phi;
182 keyword pi;
183 keyword pos;
184 keyword progname;
185 keyword random;
186 keyword regions;
187 keyword source;
188 keyword storage;
189 keyword subject;
190 keyword time;
191 keyword trace;
192 keyword ucase;
193 keyword version;
194 }; //namespace Icon
195
196
197 static void initialize_keywords() {
198 Icon::allocated.f = Kallocated;
199 Icon::ascii.f = Kascii;
200 Icon::clock.f = Kclock;
201 Icon::collections.f = K_collections; //generative: K_
202 Icon::current.f = Kcurrent;
203 Icon::date.f = Kdate;
204 Icon::dateline.f = Kdateline;
205 Icon::digits.f = Kdigits;
206 Icon::dump.f = Kdump;
207 Icon::e.f = Ke;
208 Icon::error.f = Kerror;
209 Icon::errornumber.f = Kerrornumber;
210 Icon::errortext.f = Kerrortext;
211 Icon::errorvalue.f = Kerrorvalue;
212 Icon::errout.f = Kerrout;
213 Icon::features.f = K_features; //generative: K_
214 Icon::file.f = Kfile;
215 Icon::host.f = Khost;
216 Icon::input.f = Kinput;
217 Icon::lcase.f = Klcase;
218 Icon::letters.f = Kletters;
219 Icon::level.f = Klevel;
220 Icon::line.f = Kline;
221 Icon::main.f = Kmain;
222 Icon::null.f = Knull;
223 Icon::output.f = Koutput;
224 Icon::phi.f = Kphi;
225 Icon::pi.f = Kpi;
226 Icon::pos.f = Kpos;
227 Icon::progname.f = Kprogname;
228 Icon::random.f = Krandom;
229 Icon::regions.f = K_regions; //generative: K_
230 Icon::source.f = Ksource;
231 Icon::storage.f = K_storage; //generative: K_
232 Icon::subject.f = Ksubject;
233 Icon::time.f = Ktime;
234 Icon::trace.f = Ktrace;
235 Icon::ucase.f = Kucase;
236 Icon::version.f = Kversion;
237 }
238
239 safe keyword::operator&() {
240 value result;
241 safecall_0(*f, result);
242 return result;
243 }
244
245 /*
246 * Implementation of the value class.
247 */
248
249 const value nullstring(NullString);
250 const value nullvalue; //statically initialized by default to &null
251 const value nullchar(NullChar);
252 const value illegal(Illegal);
253
254 value::value() {
255 //default initialization is to &null
256 dword = D_Null;
257 vword = 0;
258 }
259
260 value::value(special_value sv, const char *text) {
261 switch( sv ) {
262 case NullString:
263 dword = 0;
264 vword = (long)"";
265 break;
266 case StringLiteral:
267 dword = strlen(text);
268 vword = (long)text;
269 break;
270 case NewString:
271 dword = strlen(text);
272 vword = (long)alcstr((char*)text, dword);
273 break;
274 case NullChar:
275 dword = 1;
276 vword = (long)"\0";
277 break;
278 case Illegal:
279 dword = D_Illegal;
280 vword = 0;
281 break;
282 default:
283 dword = D_Null;
284 vword = 0;
285 }
286 }
287
288 value::value(int argc, value* argv) { //assumes these are passed in from Icon
289 safe argv0 = argv[0]; //which guarantees their GC safety
290 Ollist(argc, argv);
291 *this = argv[0];
292 argv[0] = argv0;
293 }
294
295 value::value(int n) {
296 dword = D_Integer;
297 vword = n;
298 }
299
300 value::value(long n) {
301 dword = D_Integer;
302 vword = n;
303 }
304
305 value::value(float x) {
306 dword = D_Real;
307 vword = (long)alcreal(x);
308 }
309
310 value::value(double x) {
311 dword = D_Real;
312 vword = (long)alcreal(x);
313 }
314
315 value::value(char* s) {
316 dword = strlen(s);
317 vword = (long)alcstr(s, dword);
318 }
319
320 value::value(const char* s) {
321 dword = strlen(s);
322 vword = (long)alcstr((char*)s, dword);
323 }
324
325 value::value(const char* s, long len) {
326 dword = len;
327 vword = (long)alcstr((char*)s, dword);
328 }
329
330 value::value(proc_block& pb) {
331 dword = D_Proc;
332 vword = (long)&pb;
333 }
334
335 value::value(proc_block* pbp) {
336 dword = D_Proc;
337 vword = (long)pbp;
338 }
339
340 value::value(external* ep) {
341 char* ptr = (char*)ep - sizeof(external_block)/sizeof(char);
342 dword = D_External;
343 vword = (long)ptr;
344 }
345
346 value::operator int() {
347 if( this->type() != Integer )
348 syserror("loadfuncpp: int cannot be produced from non-Integer");
349 return vword;
350 }
351
352 value::operator long() {
353 if( this->type() != Integer )
354 syserror("loadfuncpp: long cannot be produced from non-Integer");
355 return vword;
356 }
357
358 value::operator float() {
359 if( this->type() != Real )
360 syserror("loadfuncpp: double cannot be produced from non-Real");
361 return getdbl(this);
362 }
363
364 value::operator double() {
365 if( this->type() != Real )
366 syserror("loadfuncpp: double cannot be produced from non-Real");
367 return getdbl(this);
368 }
369
370 value::operator char*() {
371 if( this->type() != String )
372 syserror("loadfuncpp: char* cannot be produced from non-String");
373 return (char*)vword;
374 }
375
376 value::operator external*() {
377 if( dword != D_External ) return 0; //too ruthless
378 return (external*)((external_block*)vword + 1);
379 }
380
381 value::operator proc_block*() const {
382 if( dword != D_Proc ) return 0; //too ruthless
383 return (proc_block*)vword;
384 }
385
386 void value::dump() const {
387 fprintf(stderr, "\n%lx\n%lx\n", dword, vword);
388 fflush(stderr);
389 }
390
391 bool value::operator==(const value& v) const {
392 return dword==v.dword && vword==v.vword;
393 }
394
395 value& value::dereference() {
396 deref(this, this); //dereference in place
397 return *this;
398 }
399
400 value value::intify() { //integer representation of vword pointer
401 switch( this->type() ) {
402 default:
403 return vword;
404 case Null: case Integer: case Real:
405 return nullvalue;
406 }
407 }
408
409 bool value::isNull() {
410 return (dword & TypeMask) == T_Null;
411 }
412
413 bool value::notNull() {
414 return (dword & TypeMask) != T_Null;
415 }
416
417 value value::size() const {
418 value result;
419 safecall_1(&Osize, result, *this);
420 return result;
421 }
422
423 kind value::type() {
424 if( !( dword & F_Nqual ) ) return String;
425 if( dword & F_Var ) return Variable;
426 return kind(dword & TypeMask);
427 }
428
429 bool value::toCset() {
430 return safecall_1(&Zcset, *this, *this) == SUCCEEDED;
431 }
432
433 bool value::toInteger() {
434 return safecall_1(&Zinteger, *this, *this) == SUCCEEDED;
435 }
436
437 bool value::toReal() {
438 return safecall_1(&Zreal, *this, *this) == SUCCEEDED;
439 }
440
441 bool value::toNumeric() {
442 return safecall_1(&Znumeric, *this, *this) == SUCCEEDED;
443 }
444
445 bool value::toString() {
446 return safecall_1(&Zstring, *this, *this) == SUCCEEDED;
447 }
448
449 value value::subscript(const value& v) const {
450 value result;
451 safecall_2(&Osubsc, result, *this, v);
452 return result;
453 }
454
455 value& value::assign(const value& v) {
456 if( dword & F_Var ) //lhs value is an Icon 'Variable'
457 safecall_2(&Oasgn, *this, *this, v);
458 else {
459 dword = v.dword;
460 vword = v.vword;
461 deref(this,this); //in case rhs is an Icon 'Variable'
462 }
463 return *this;
464 }
465
466 value value::put(value x) {
467 value result;
468 safecall_v2(&Zput, result, *this, x);
469 return result;
470 }
471
472 value value::push(value x) {
473 value result;
474 safecall_v2(&Zpush, result, *this, x);
475 return result;
476 }
477
478 void value::printimage() const {
479 value result;
480 safecall_1(&Zimage, result, *this);
481 safecall_v1(&Zwrites, result, result);
482 }
483
484 int value::compare(const value& x) const {
485 return anycmp(this, &x);
486 }
487
488 value value::negative() const {
489 value result;
490 if( safecall_1(&Oneg, result, *this) == FAILED )
491 return nullvalue;
492 return result;
493 }
494
495 value value::complement() const {
496 value result;
497 if( safecall_1(&Ocompl, result, *this) == FAILED )
498 return nullvalue;
499 return result;
500 }
501
502 value value::refreshed() const {
503 value result;
504 if( safecall_1(&Orefresh, result, *this) == FAILED )
505 return nullvalue;
506 return result;
507 }
508
509 value value::random() const {
510 value result;
511 if( safecall_1(&Orandom, result, *this) == FAILED )
512 return nullvalue;
513 return result;
514 }
515
516 value value::plus(const value& x) const {
517 value result;
518 if( safecall_2(&Oplus, result, *this, x) == FAILED )
519 return nullvalue;
520 return result;
521 }
522
523 value value::minus(const value& x) const {
524 value result;
525 if( safecall_2(&Ominus, result, *this, x) == FAILED )
526 return nullvalue;
527 return result;
528 }
529
530 value value::multiply(const value& x) const {
531 value result;
532 if( safecall_2(&Omult, result, *this, x) == FAILED )
533 return nullvalue;
534 return result;
535 }
536
537 value value::divide(const value& x) const {
538 value result;
539 if( safecall_2(&Odivide, result, *this, x) == FAILED )
540 return nullvalue;
541 return result;
542 }
543
544 value value::remainder(const value& x) const {
545 value result;
546 if( safecall_2(&Omod, result, *this, x) == FAILED )
547 return nullvalue;
548 return result;
549 }
550
551 value value::power(const value& x) const {
552 value result;
553 if( safecall_2(&Opowr, result, *this, x) == FAILED )
554 return nullvalue;
555 return result;
556 }
557
558 value value::union_(const value& x) const {
559 value result;
560 if( safecall_2(&Ounion, result, *this, x) == FAILED )
561 return nullvalue;
562 return result;
563 }
564
565 value value::intersection(const value& x) const {
566 value result;
567 if( safecall_2(&Ointer, result, *this, x) == FAILED )
568 return nullvalue;
569 return result;
570 }
571
572 value value::difference(const value& x) const {
573 value result;
574 if( safecall_2(&Odiff, result, *this, x) == FAILED )
575 return nullvalue;
576 return result;
577 }
578
579 value value::concatenate(const value& x) const {
580 value result;
581 if( safecall_2(&Ocater, result, *this, x) == FAILED )
582 return nullvalue;
583 return result;
584 }
585
586 value value::listconcatenate(const value& x) const {
587 value result;
588 if( safecall_2(&Olconcat, result, *this, x) == FAILED )
589 return nullvalue;
590 return result;
591 }
592
593 value value::slice(const value& x, const value& y) const {
594 value result;
595 if( safecall_3(&Osect, result, *this, x, y) == FAILED )
596 return nullvalue;
597 return result;
598 }
599
600 value& value::swap(value& x) {
601 safecall_2(&Oswap, *this, *this, x);
602 return *this;
603 }
604
605 value value::activate(const value& x) const {
606 value arg = Value::pair(*this, x);
607 return _loadfuncpp_activate.apply(arg);
608 }
609
610 value value::apply(const value& x) const {
611 return Value::call(*this, x);
612 }
613
614
615
616 /*
617 * Implementation of the generator class
618 */
619
620 int generator::generate(value argv[]) {
621 //suspend all values generated and return the eventual signal
622 int signal = FAILED;
623 while( this->hasNext() && signal == FAILED ) {
624 argv[0] = this->giveNext();
625 signal = interp(SUSPEND, argv);
626 }
627 return signal;
628 }
629
630 bool generator::hasNext() { return false; } //empty sequence for the root class
631 value generator::giveNext() { return nullvalue; }
632
633
634
635 /*
636 * Implementation of class iterate
637 */
638
639 class wrap: public external { //an iterate object as Icon data
640 public:
641 iterate* data;
642 wrap(iterate* ip): data(ip) {}
643 };
644
645 extern "C" int update_iteration(value argv[]) {
646 external* ep = argv[1];
647 iterate* ip = ((wrap*)ep)->data;
648 argv[0] = nullvalue;
649 if( ip->wantNext(argv[2]) ) {
650 ip->takeNext(argv[2]);
651 return SUCCEEDED;
652 }
653 else return FAILED;
654 }
655
656 static proc_block updatepb("update_iteration", &update_iteration, 2);
657 static value update(updatepb);
658
659 void iterate::every(const value& g, const value& arg) {
660 value nullary(new wrap(this));
661 variadic v(nullary);
662 _loadfuncpp_reduce.apply((v,update,g,arg));
663 }
664
665 void iterate::bang(const value& x) {
666 value nullary(new wrap(this));
667 variadic v(nullary);
668 _loadfuncpp_bang.apply((v,update,x));
669 }
670
671 bool iterate::wantNext(const value& v) { return true; } //use whole sequence
672 void iterate::takeNext(const value& v) {}
673
674
675
676 /*
677 * Implementation of the safe_variable class
678 */
679 safe_variable::safe_variable() : val() {};
680
681 safe_variable::safe_variable(int n) : val(n) {};
682
683 safe_variable::safe_variable(long n) : val(n) {};
684
685 safe_variable::safe_variable(double x) : val(x) {};
686
687 safe_variable::safe_variable(value v) : val(v) {};
688
689 safe_variable::safe_variable(proc_block& pb) : val(pb) {};
690
691 safe_variable::safe_variable(proc_block* pbp) : val(pbp) {};
692
693 safe_variable::safe_variable(int argc, value* argv) : val(argc, argv) {};
694
695 inline void safe_variable::push(safe_variable*& tendlist, int numvalues) {
696 previous = tendlist;
697 num = numvalues;
698 tendlist = this;
699 }
700
701 inline void safe_variable::pop(safe_variable*& tendlist) {
702 if( tendlist == this ) { //we are at the head of the tend list
703 tendlist = tendlist->previous; //pop us off
704 return;
705 }
706 #if 0
707 if( tendlist == tend ) //warning is for safe tend list only
708 {
709 fprintf(stderr, "loadfuncpp warning: pop needed from interior of tended list\n");
710 fflush(stderr);
711 }
712 #endif
713 safe_variable *last = 0, *current = tendlist;
714 do { //search tendlist
715 last = current;
716 current = current->previous;
717 } while( current != this && current != 0);
718 if( current == 0 )
719 syserror("loadfuncpp bug: failed to find variable on tended list so as to remove it.");
720 last->previous = current->previous; //slice us out
721 }
722
723
724
725 /*
726 * Implementation of the variadic class (variable length argument list)
727 */
728
729 variadic::variadic(int n) {
730 value v(n);
731 val = Value::list(1, v);
732 push(global_tend);
733 }
734
735 variadic::variadic(long n) {
736 value v(n);
737 val = Value::list(1, v);
738 push(global_tend);
739 }
740
741 variadic::variadic(float x) {
742 value v(x);
743 val = Value::list(1, v);
744 push(global_tend);
745 }
746
747 variadic::variadic(double x) {
748 value v(x);
749 val = Value::list(1, v);
750 push(global_tend);
751 }
752
753 variadic::variadic(char* s) {
754 value v(s);
755 val = Value::list(1, v);
756 push(global_tend);
757 }
758
759 variadic::variadic(value v) {
760 val = Value::list(1, v);
761 push(global_tend);
762 }
763
764 variadic::variadic(const safe& x) {
765 val = Value::list(1, x.val);
766 push(global_tend);
767 }
768
769 variadic::variadic(const safe& x, const safe& y) {
770 val = Value::pair(x, y);
771 push(global_tend);
772 }
773
774 variadic& variadic::operator,(const safe& x) {
775 val.put(x.val);
776 return *this;
777 }
778
779 variadic::operator value() {
780 return val;
781 }
782
783 variadic::~variadic() { pop(global_tend); }
784
785
786 /*
787 * Implementation of the safe class
788 */
789
790 safe::safe() : safe_variable() { push(global_tend); }
791
792 safe::safe(const safe& x) : safe_variable(x.val) { push(global_tend); }
793
794 safe::safe(int n) : safe_variable(n) { push(global_tend); }
795
796 safe::safe(long n) : safe_variable(n) { push(global_tend); }
797
798 safe::safe(float x) : safe_variable(x) { push(global_tend); }
799
800 safe::safe(double x) : safe_variable(x) { push(global_tend); }
801
802 safe::safe(char* s) : safe_variable(s) { push(global_tend); }
803
804 safe::safe(const value& v) : safe_variable(v) { push(global_tend); }
805
806 safe::safe(const variadic& v) : safe_variable(v) { push(global_tend); }
807
808 safe::safe(proc_block& pb) : safe_variable(pb) { push(global_tend); }
809
810 safe::safe(proc_block* pbp) : safe_variable(pbp) { push(global_tend); }
811
812 safe::safe(int argc, value* argv) : safe_variable(argc, argv) { push(global_tend); }
813
814 safe::~safe() { pop(global_tend); }
815
816 safe& safe::operator=(const safe& x) {
817 val.assign(x.val); //Icon style assignment
818 return *this;
819 }
820
821 safe& safe::operator^=(const safe& x) {
822 *this = *this ^ x;
823 return *this;
824 }
825
826 safe& safe::operator+=(const safe& x) {
827 *this = *this + x;
828 return *this;
829 }
830
831 safe& safe::operator-=(const safe& x) {
832 *this = *this - x;
833 return *this;
834 }
835
836 safe& safe::operator*=(const safe& x) {
837 *this = *this * x;
838 return *this;
839 }
840
841 safe& safe::operator/=(const safe& x) {
842 *this = *this / x;
843 return *this;
844 }
845
846 safe& safe::operator%=(const safe& x) {
847 *this = *this % x;
848 return *this;
849 }
850
851 safe& safe::operator&=(const safe& x) {
852 *this = *this & x;
853 return *this;
854 }
855
856 safe& safe::operator|=(const safe& x) {
857 *this = *this | x;
858 return *this;
859 }
860
861 safe& safe::operator++() {
862 *this -= 1;
863 return *this;
864 }
865
866 safe& safe::operator--() {
867 *this += 1;
868 return *this;
869 }
870
871 safe safe::operator++(int) {
872 safe temp(*this);
873 *this += 1;
874 return temp;
875 }
876
877 safe safe::operator--(int) {
878 safe temp(*this);
879 *this -= 1;
880 return temp;
881 }
882
883 safe::operator value() const {
884 return val; //low-level copy
885 }
886
887 safe safe::operator() () {
888 value empty = Value::list();
889 return this->apply(empty);
890 }
891
892 safe safe::operator() (const safe& x) {
893 value singleton = Value::list(1, x);
894 return this->apply(singleton);
895 }
896
897 safe safe::operator()(const safe& x1, const safe& x2,
898 const safe& x3, const safe& x4,
899 const safe& x5, const safe& x6,
900 const safe& x7, const safe& x8 ) {
901 if( x3.isIllegal() )
902 return this->apply( (x1,x2) );
903 if( x4.isIllegal() )
904 return this->apply( (x1,x2,x3) );
905 if( x5.isIllegal() )
906 return this->apply( (x1,x2,x3,x4) );
907 if( x6.isIllegal() )
908 return this->apply( (x1,x2,x3,x4,x5) );
909 if( x7.isIllegal() )
910 return this->apply( (x1,x2,x3,x4,x5,x6) );
911 if( x8.isIllegal() )
912 return this->apply( (x1,x2,x3,x4,x5,x6,x7) );
913 return this->apply( (x1,x2,x3,x4,x5,x6,x7,x8) );
914 }
915
916 safe safe::operator[](const safe& x) {
917 return val.subscript(x.val);
918 }
919
920 safe operator*(const safe& x){
921 return x.val.size();
922 }
923
924 safe operator-(const safe& x){
925 return x.val.negative();
926 }
927
928 safe operator~(const safe& x){ //set complement
929 return x.val.complement();
930 }
931
932 safe operator+(const safe& x, const safe& y){
933 return x.val.plus(y.val);
934 }
935
936 safe operator-(const safe& x, const safe& y){
937 return x.val.minus(y.val);
938 }
939
940 safe operator*(const safe& x, const safe& y){
941 return x.val.multiply(y.val);
942 }
943
944 safe operator/(const safe& x, const safe& y){
945 return x.val.divide(y.val);
946 }
947
948 safe operator%(const safe& x, const safe& y){
949 return x.val.remainder(y.val);
950 }
951
952 safe operator^(const safe& x, const safe& y){ //exponentiation
953 return x.val.power(y.val);
954 }
955
956 safe operator|(const safe& x, const safe& y){ //union
957 return x.val.union_(y.val);
958 }
959
960 safe operator&(const safe& x, const safe& y){ //intersection
961 return x.val.intersection(y.val);
962 }
963
964 safe operator&&(const safe& x, const safe& y){ //set or cset difference
965 return x.val.difference(y.val);
966 }
967
968 safe operator||(const safe& x, const safe& y){ //string concatenation
969 return x.val.concatenate(y.val);
970 }
971
972 bool operator<(const safe& x, const safe& y){
973 return x.val.compare(y.val) < 0;
974 }
975
976 bool operator>(const safe& x, const safe& y){
977 return x.val.compare(y.val) > 0;
978 }
979
980 bool operator<=(const safe& x, const safe& y){
981 return x.val.compare(y.val) <= 0;
982 }
983
984 bool operator>=(const safe& x, const safe& y){
985 return x.val.compare(y.val) >= 0;
986 }
987
988 bool operator==(const safe& x, const safe& y){
989 return x.val.compare(y.val) == 0;
990 }
991
992 bool operator!=(const safe& x, const safe& y){
993 return x.val.compare(y.val) != 0;
994 }
995
996 variadic operator,(const safe& x, const safe& y){ //variadic argument list construction
997 return variadic(x.val, y.val);
998 }
999
1000 safe safe::slice(const safe& y, const safe& z){ // x[y:z]
1001 return this->val.slice(y, z);
1002 }
1003
1004 safe safe::apply(const safe& y){ // x ! y
1005 safe result;
1006 result = _loadfuncpp_apply.apply( (this->val, y.val) );
1007 return result;
1008 }
1009
1010 safe safe::listcat(const safe& y){ // x ||| y
1011 value x(*this);
1012 return x.listconcatenate(y);
1013 }
1014
1015 safe& safe::swap(safe& y){ // x :=: y
1016 value& x(this->val);
1017 value& yv(y.val);
1018 x.swap(yv);
1019 return *this;
1020 }
1021
1022 safe safe::create(){ // create !x
1023 return _loadfuncpp_create.apply(Value::list(1, *this));
1024 }
1025
1026 safe safe::create(const safe& y){ // create x!y
1027 return _loadfuncpp_create.apply(Value::pair(*this, y));
1028 }
1029
1030 safe safe::activate(const safe& y){ // y@x
1031 return _loadfuncpp_activate.apply(Value::pair(*this, y));
1032 }
1033
1034 safe safe::refresh(){ // ^x
1035 return this->val.refreshed();
1036 }
1037
1038 safe safe::random(){ // ?x
1039 return this->val.random();
1040 }
1041
1042 safe safe::dereference(){ // .x
1043 value var(this->val);
1044 var.dereference();
1045 return var;
1046 }
1047
1048 bool safe::isIllegal() const {
1049 return this->val == illegal;
1050 }
1051
1052
1053
1054 /*
1055 * iconx callback support
1056 */
1057
1058 inline int safecall_0(iconfunc *F, value& out) {
1059 struct {
1060 safe_variable tend; //contains an additional unused value
1061 value stack[1];
1062 } vars;
1063 vars.stack[0] = nullvalue;
1064 vars.tend.push(tend,2);
1065 int result = F(vars.stack);
1066 if( result == SUCCEEDED )
1067 out = vars.stack[0];
1068 vars.tend.pop(tend);
1069 return result;
1070 }
1071
1072 inline int safecall_1(iconfunc *F, value& out, const value& x1) {
1073 struct {
1074 safe_variable tend; //contains an additional unused value
1075 value stack[2];
1076 } vars;
1077 vars.stack[0] = nullvalue;
1078 vars.stack[1] = x1;
1079 vars.tend.push(tend,3);
1080 int result = F(vars.stack);
1081 if( result == SUCCEEDED )
1082 out = vars.stack[0];
1083 vars.tend.pop(tend);
1084 return result;
1085 }
1086
1087 inline int safecall_2(iconfunc *F, value& out, const value& x1, const value& x2) {
1088 struct {
1089 safe_variable tend; //contains an additional unused value
1090 value stack[3];
1091 } vars;
1092 vars.stack[0] = nullvalue;
1093 vars.stack[1] = x1;
1094 vars.stack[2] = x2;
1095 vars.tend.push(tend,4);
1096 int result = F(vars.stack);
1097 if( result == SUCCEEDED )
1098 out = vars.stack[0];
1099 vars.tend.pop(tend);
1100 return result;
1101 }
1102
1103 inline int safecall_3(iconfunc *F, value& out, const value& x1, const value& x2, const value& x3) {
1104 struct {
1105 safe_variable tend; //contains an additional unused value
1106 value stack[4];
1107 } vars;
1108 vars.stack[0] = nullvalue;
1109 vars.stack[1] = x1;
1110 vars.stack[2] = x2;
1111 vars.stack[3] = x3;
1112 vars.tend.push(tend,5);
1113 int result = F(vars.stack);
1114 if( result == SUCCEEDED )
1115 out = vars.stack[0];
1116 vars.tend.pop(tend);
1117 return result;
1118 }
1119
1120 inline int safecall_4(iconfunc *F, value& out, const value& x1, const value& x2, const value& x3, const value& x4) {
1121 struct {
1122 safe_variable tend; //contains an additional unused value
1123 value stack[5];
1124 } vars;
1125 vars.stack[0] = nullvalue;
1126 vars.stack[1] = x1;
1127 vars.stack[2] = x2;
1128 vars.stack[3] = x3;
1129 vars.stack[4] = x4;
1130 vars.tend.push(tend,6);
1131 int result = F(vars.stack);
1132 if( result == SUCCEEDED )
1133 out = vars.stack[0];
1134 vars.tend.pop(tend);
1135 return result;
1136 }
1137
1138 inline int safecall_5(iconfunc *F, value& out, const value& x1, const value& x2, const value& x3, const value& x4, const value& x5) {
1139 struct {
1140 safe_variable tend; //contains an additional unused value
1141 value stack[6];
1142 } vars;
1143 vars.stack[0] = nullvalue;
1144 vars.stack[1] = x1;
1145 vars.stack[2] = x2;
1146 vars.stack[3] = x3;
1147 vars.stack[4] = x4;
1148 vars.stack[5] = x5;
1149 vars.tend.push(tend,7);
1150 int result = F(vars.stack);
1151 if( result == SUCCEEDED )
1152 out = vars.stack[0];
1153 vars.tend.pop(tend);
1154 return result;
1155 }
1156
1157 inline int safecall_6(iconfunc *F, value& out, const value& x1, const value& x2, const value& x3, const value& x4, const value& x5, const value& x6) {
1158 struct {
1159 safe_variable tend; //contains an additional unused value
1160 value stack[7];
1161 } vars;
1162 vars.stack[0] = nullvalue;
1163 vars.stack[1] = x1;
1164 vars.stack[2] = x2;
1165 vars.stack[3] = x3;
1166 vars.stack[4] = x4;
1167 vars.stack[5] = x5;
1168 vars.stack[6] = x6;
1169 vars.tend.push(tend,8);
1170 int result = F(vars.stack);
1171 if( result == SUCCEEDED )
1172 out = vars.stack[0];
1173 vars.tend.pop(tend);
1174 return result;
1175 }
1176
1177 inline int safecall_v0(iconfvbl *F, value& out) {
1178 struct {
1179 safe_variable tend; //contains an additional unused value
1180 value stack[1];
1181 } vars;
1182 vars.stack[0] = nullvalue;
1183 vars.tend.push(tend,2);
1184 int result = F(0, vars.stack);
1185 if( result == SUCCEEDED )
1186 out = vars.stack[0];
1187 vars.tend.pop(tend);
1188 return result;
1189 }
1190
1191 inline int safecall_v1(iconfvbl *F, value& out, const value& x1) {
1192 struct {
1193 safe_variable tend; //contains an additional unused value
1194 value stack[2];
1195 } vars;
1196 vars.stack[0] = nullvalue;
1197 vars.stack[1]= x1;
1198 vars.tend.push(tend,3);
1199 int result = F(1, vars.stack);
1200 if( result == SUCCEEDED )
1201 out = vars.stack[0];
1202 vars.tend.pop(tend);
1203 return result;
1204 }
1205
1206 inline int safecall_v2(iconfvbl *F, value& out, const value& x1, const value& x2) {
1207 struct {
1208 safe_variable tend; //contains an additional unused value
1209 value stack[3];
1210 } vars;
1211 vars.stack[0] = nullvalue;
1212 vars.stack[1] = x1;
1213 vars.stack[2] = x2;
1214 vars.tend.push(tend,4);
1215 int result = F(2, vars.stack);
1216 if( result == SUCCEEDED )
1217 out = vars.stack[0];
1218 vars.tend.pop(tend);
1219 return result;
1220 }
1221
1222 inline int safecall_v3(iconfvbl *F, value& out, const value& x1, const value& x2, const value& x3) {
1223 struct {
1224 safe_variable tend; //contains an additional unused value
1225 value stack[4];
1226 } vars;
1227 vars.stack[0] = nullvalue;
1228 vars.stack[1] = x1;
1229 vars.stack[2] = x2;
1230 vars.stack[3] = x3;
1231 vars.tend.push(tend,5);
1232 int result = F(3, vars.stack);
1233 if( result == SUCCEEDED )
1234 out = vars.stack[0];
1235 vars.tend.pop(tend);
1236 return result;
1237 }
1238
1239 inline int safecall_vbl(iconfvbl* F, safe& out, const variadic& arg) {
1240 int argc = arg.val.size();
1241 //C++ makes allocating trailing variable sized arrays
1242 //inside structs difficult, so do this C-style
1243 safe_variable* pvars = (safe_variable*)malloc(sizeof(safe_variable)+(argc+1)*sizeof(value));
1244 value* stack = (value*)(pvars + 1); //get past the safe_variable at the start of the block
1245 stack[0] = nullvalue;
1246 for(int i=1; i<=argc; ++i)
1247 stack[i] = arg.val.subscript(i).dereference();
1248 pvars->push(tend, argc+2);
1249 int result = F(argc, stack);
1250 if( result == SUCCEEDED )
1251 out = stack[0];
1252 pvars->pop(tend);
1253 free(pvars);
1254 }
1255
1256
1257
1258 /*
1259 * Procedure related
1260 */
1261
1262 //Icon procedure block: used to make new Icon procedures as values to return
1263
1264 proc_block::proc_block(value procname, iconfvbl *function) {
1265 init(procname);
1266 nparam = -1; //a variable number of arguments
1267 entryp = function;
1268 }
1269
1270 proc_block::proc_block(value procname, iconfunc *function, int arity) {
1271 init(procname);
1272 nparam = arity;
1273 entryp = (iconfvbl*)function;
1274 }
1275
1276 proc_block::proc_block(value procname, iconfvbl *function, int arity) {
1277 init(procname);
1278 nparam = -1; //a variable number of arguments
1279 entryp = function;
1280 }
1281
1282 long proc_block::extra_bytes = 0;
1283
1284 extern long extl_ser; //serial number counter for alcexternal
1285
1286 static void* alcproc(long nbytes) {
1287 proc_block* pbp = (proc_block*)alcexternal(nbytes, 0, 0); //a hack for now
1288 --extl_ser;
1289 pbp->title = T_Proc;
1290 pbp->blksize = nbytes;
1291 return (void*)pbp;
1292 }
1293
1294 void* proc_block::operator new(size_t nbytes) { //allocated in Icon's block region
1295 return alcproc(nbytes + extra_bytes);
1296 }
1297
1298 void proc_block::operator delete(void*) {
1299 return; //do nothing
1300 }
1301
1302 proc_block::proc_block(proc_block* pbp) {
1303 *this = *pbp; //copy the C++ legitimate part
1304 }
1305
1306 proc_block* proc_block::bind(proc_block* pbp, const value& rec) {
1307 extra_bytes = pbp->blksize - sizeof(proc_block) + sizeof(value); //one more slot
1308 proc_block* ans = new proc_block(pbp); // copies the C++ legitimate part
1309 ans->blksize = sizeof(proc_block) + extra_bytes;
1310 extra_bytes = 0;
1311 int nsafe = ans->ndynam + ans->nparam;
1312 for( int pos=1; pos<nsafe; pos++) //copy the remainder
1313 ans->lnames[pos] = pbp->lnames[pos];
1314 ans->lnames[nsafe] = rec; //set the last array slot to rec
1315 ans->pname = "bound to record"; //improve this to use the proc name and rec image
1316 return ans;
1317 }
1318
1319 extern "C" int bindself(value argv[]) {
1320 if( argv[1].type() != Procedure ||
1321 argv[2].type() != Record ) {
1322 argv[0] = nullvalue;
1323 return FAILED;
1324 }
1325 argv[0] = proc_block::bind(argv[1], argv[2]);
1326 return SUCCEEDED;
1327 }
1328
1329
1330
1331 /*
1332 * External values related
1333 */
1334
1335 extern "C" { //these call virtual functions, so only one function list needed
1336 static int extcmp(int argc, value argv[]) {
1337 external *ep = argv[1], *ep2 = argv[2];
1338 argv[0] = ep->compare(ep2);
1339 return 0;
1340 }
1341 static int extcopy(int argc, value argv[]) {
1342 external* ep = argv[1];
1343 argv[0] = ep->copy();
1344 return 0;
1345 }
1346 static int extname(int argc, value argv[]) {
1347 external* ep = argv[1];
1348 argv[0] = ep->name();
1349 return 0;
1350 }
1351 static int extimage(int argc, value argv[]) {
1352 external* ep = argv[1];
1353 argv[0] = ep->image();
1354 return 0;
1355 }
1356 }; //end extern "C"
1357
1358 static void initialize_ftable(); //just below
1359
1360 static struct external_ftable { //C callback table for all C++ made external values
1361 iconfvbl* cmp;
1362 iconfvbl* copy;
1363 iconfvbl* name;
1364 iconfvbl* image;
1365 external_ftable() { initialize_ftable(); }
1366 } ftable;
1367
1368 static void initialize_ftable() {
1369 ftable.cmp = &extcmp;
1370 ftable.copy = &extcopy;
1371 ftable.name = &extname;
1372 ftable.image = &extimage;
1373 }
1374
1375 long external_block::extra_bytes; //silent extra parameter to external_block::new
1376
1377 static void* external_block::operator new(size_t nbytes) {
1378 return alcexternal(nbytes + extra_bytes, &ftable, 0); //extra_bytes for C++ external
1379 }
1380
1381 static void external_block::operator delete(void* p) {
1382 return; //don't delete
1383 }
1384
1385 external_block::external_block() {
1386 //val = (external*)((long*)&val + 1); //add a trashable pointer to the (to be appended) external
1387 val = 0;
1388 }
1389
1390 external_block* external::blockptr; //silent extra result of external::new for external()
1391
1392 static void* external::operator new(size_t nbytes) {
1393 external_block::extra_bytes = nbytes; //pass our requirements to external_block::new
1394 blockptr = new external_block(); //with extra_bytes; pass our requirements to external()
1395 char* ptr = (char*)blockptr + sizeof(external_block)/sizeof(char); //beginning of extra_bytes
1396 return (void*)ptr; //where the external will be appended
1397 }
1398
1399 static void external::operator delete(void* p) {
1400 return; //don't delete
1401 }
1402
1403 external::external() {
1404 id = blockptr->id; //set by new
1405 }
1406
1407 external* external::copy() {
1408 return this;
1409 }
1410
1411 value external::image() { //need new string every time!
1412 char sbuf[100];
1413 long vptr = *((long*)this);
1414 sprintf(sbuf, "external_%ld(%lX)", id, vptr);
1415 return value(NewString, sbuf);
1416 }
1417
1418 value external::name() {
1419 return value(StringLiteral, "external");
1420 }
1421
1422 long external::compare(external* ep) {
1423 return this->id - ep->id;
1424 }
1425
1426 bool value::isExternal(const value& type) { //needs external_block declaration
1427 if( dword != D_External ) return false;
1428 value result;
1429 external_block* ebp = (external_block*)vword;
1430 iconfvbl* name = (ebp->funcs)->name;
1431 value stack[2];
1432 stack[1] = *this;
1433 name(1, stack);
1434 return !stack[0].compare(type);
1435 }
1436
1437
1438
1439 /*
1440 * Startup code (on load)
1441 */
1442
1443 //new variant of loadfunc sidestepping loadfunc's glue, a three argument function
1444
1445 extern "C" int loadfuncpp(value argv[]) { //three arguments
1446 if( argv[3].isNull() ) argv[3]=-1;
1447 //assumption: a path is specified iff a slash or backslash is in the filename,
1448 if( argv[1].toString() ) {
1449 safe fname(argv[1]), fullname;
1450 int ispath = value( *(Icon::cset(fname) & Icon::cset((char*)"\\/")) );
1451 if( !ispath ) { //search FPATH for the file
1452 fullname = _loadfuncpp_pathfind.apply((fname, Icon::getenv((char*)"FPATH")));
1453 if( fullname == nullvalue ) {
1454 Icon::runerr(216, argv[1]);
1455 return FAILED;
1456 }
1457 argv[1] = value(fullname);
1458 }
1459 }
1460 return rawloadfuncpp(argv);
1461 }
1462
1463 static void replace_loadfunc() {
1464 static proc_block pb("loadfuncpp", loadfuncpp, 3); //three arguments
1465 value proc(pb), var = Value::variable("loadfunc");
1466 var.assign(proc);
1467 }
1468
1469 //set up a tend list for global variables on the tail of &main's
1470 struct safe_tend { //struct with isomorphic data footprint to a safe_variable
1471 safe_variable *previous;
1472 int num;
1473 value val;
1474 } sentinel;
1475
1476 safe_variable*& global_tend = sentinel.previous;
1477
1478 static void add_to_end(safe_variable*& tend_list) {
1479 safe_tend *last = 0, *current = (safe_tend*)tend_list;
1480 while( current != 0 ) {
1481 last = current;
1482 current = (safe_tend*)(current->previous);
1483 }
1484 if( last == 0 ) tend_list = (safe_variable*)&sentinel;
1485 else last->previous = (safe_variable*)&sentinel;
1486 }
1487
1488 static void make_global_tend_list() {
1489 sentinel.previous = 0;
1490 sentinel.num = 1;
1491 sentinel.val = nullvalue;
1492 if( k_current == k_main ) add_to_end(tend); //add to the active tend list
1493 else add_to_end( ((coexp_block*)(long(k_main)))->es_tend );
1494 }
1495
1496 struct load {
1497 load() { //startup code here
1498 replace_loadfunc(); //store loadfuncpp in global loadfunc temporarily
1499 make_global_tend_list();
1500 initialize_procs();
1501 initialize_keywords();
1502 //fprintf(stderr, "\nStartup code ran!\n");fflush(stderr);
1503 }
1504 };
1505 static load startup; //force static initialization so as to run startup code
1506
1507
1508
1509 /*
1510 * Useful helper functions
1511 */
1512
1513 namespace Value {
1514
1515 value pair(value x, value y) {
1516 value newlist;
1517 if( safecall_v2(&Ollist, newlist, x, y) == FAILED )
1518 return nullvalue;
1519 return newlist;
1520 }
1521
1522 value list(value n, value init) {
1523 value newlist;
1524 if( safecall_2(&Zlist, newlist, n, init) == FAILED )
1525 return nullvalue;
1526 return newlist;
1527 }
1528
1529 void runerr(value n, value x) {
1530 value v;
1531 safecall_v2(&Zrunerr, v, n, x);
1532 }
1533
1534 value set(value list) {
1535 value newset;
1536 if( safecall_1(&Zset, newset, list) == FAILED )
1537 return nullvalue;
1538 return newset;
1539 }
1540
1541 value table(value init) {
1542 value newtable;
1543 if( safecall_1(&Ztable, newtable, init) == FAILED )
1544 return nullvalue;
1545 return newtable;
1546 }
1547
1548 value variable(value name) {
1549 value var;
1550 if( safecall_1(&Zvariable, var, name) == FAILED )
1551 return nullvalue;
1552 return var;
1553 }
1554
1555 value proc(value name, value arity) {
1556 value procedure;
1557 if( safecall_2(&Zproc, procedure, name, arity) == FAILED )
1558 return nullvalue;
1559 return procedure;
1560 }
1561
1562 value libproc(value name, value arity) {
1563 value procedure;
1564 if( safecall_2(&Zproc, procedure, name, arity) == SUCCEEDED )
1565 return procedure;
1566 syserror("loadfuncpp: unable to find required Icon procedure through 'link loadfunc'\n");
1567 return nullvalue;
1568 }
1569
1570 }; //namespace Value
1571
1572
1573
1574 /*
1575 * Built-in Icon functions
1576 */
1577 namespace Icon {
1578 safe abs(const safe& x1) {
1579 value result;
1580 safecall_1(&Zabs, result, x1);
1581 return result;
1582 }
1583
1584 safe acos(const safe& x1) {
1585 value result;
1586 safecall_1(&Zacos, result, x1);
1587 return result;
1588 }
1589
1590 safe args(const safe& x1) {
1591 value result;
1592 safecall_1(&Zargs, result, x1);
1593 return result;
1594 }
1595
1596 safe asin(const safe& x1) {
1597 value result;
1598 safecall_1(&Zasin, result, x1);
1599 return result;
1600 }
1601
1602 safe atan(const safe& x1, const safe& x2) {
1603 value result;
1604 safecall_2(&Zatan, result, x1, x2);
1605 return result;
1606 }
1607
1608 safe center(const safe& x1, const safe& x2, const safe& x3) {
1609 value result;
1610 safecall_3(&Zcenter, result, x1, x2, x3);
1611 return result;
1612 }
1613
1614 safe char_(const safe& x1) {
1615 value result;
1616 safecall_1(&Zchar, result, x1);
1617 return result;
1618 }
1619
1620 safe chdir(const safe& x1) {
1621 value result;
1622 safecall_1(&Zchdir, result, x1);
1623 return result;
1624 }
1625
1626 safe close(const safe& x1) {
1627 value result;
1628 safecall_1(&Zclose, result, x1);
1629 return result;
1630 }
1631
1632 safe collect() {
1633 value result;
1634 safecall_0(&Zcollect, result);
1635 return result;
1636 }
1637
1638 safe copy(const safe& x1) {
1639 value result;
1640 safecall_1(&Zcopy, result, x1);
1641 return result;
1642 }
1643
1644 safe cos(const safe& x1) {
1645 value result;
1646 safecall_1(&Zcos, result, x1);
1647 return result;
1648 }
1649
1650 safe cset(const safe& x1) {
1651 value result;
1652 safecall_1(&Zcset, result, x1);
1653 return result;
1654 }
1655
1656 safe delay(const safe& x1) {
1657 value result;
1658 safecall_1(&Zdelay, result, x1);
1659 return result;
1660 }
1661
1662 safe delete_(const safe& x1, const safe& x2) {
1663 value result;
1664 safecall_2(&Zdelete, result, x1, x2);
1665 return result;
1666 }
1667
1668 safe detab(const variadic& x1) {
1669 safe result;
1670 safecall_vbl(&Zdetab, result, x1);
1671 return result;
1672 }
1673
1674 safe detab( const safe& x1, const safe& x2,
1675 const safe& x3, const safe& x4,
1676 const safe& x5, const safe& x6,
1677 const safe& x7, const safe& x8 ) {
1678 if( x3.isIllegal() )
1679 return detab( (x1,x2) );
1680 if( x4.isIllegal() )
1681 return detab( (x1,x2,x3) );
1682 if( x5.isIllegal() )
1683 return detab( (x1,x2,x3,x4) );
1684 if( x6.isIllegal() )
1685 return detab( (x1,x2,x3,x4,x5) );
1686 if( x7.isIllegal() )
1687 return detab( (x1,x2,x3,x4,x5,x6) );
1688 if( x8.isIllegal() )
1689 return detab( (x1,x2,x3,x4,x5,x6,x7) );
1690 return detab( (x1,x2,x3,x4,x5,x6,x7,x8) );
1691 }
1692
1693 safe display(const safe& x1, const safe& x2) {
1694 value result;
1695 safecall_2(&Zdisplay, result, x1, x2);
1696 return result;
1697 }
1698
1699 safe dtor(const safe& x1) {
1700 value result;
1701 safecall_1(&Zdtor, result, x1);
1702 return result;
1703 }
1704
1705 safe entab(const variadic& x1) {
1706 safe result;
1707 safecall_vbl(&Zentab, result, x1);
1708 return result;
1709 }
1710
1711 safe errorclear() {
1712 value result;
1713 safecall_0(&Zerrorclear, result);
1714 return result;
1715 }
1716
1717 safe exit(const safe& x1) {
1718 value result;
1719 safecall_1(&Zexit, result, x1);
1720 return result;
1721 }
1722
1723 safe exp(const safe& x1) {
1724 value result;
1725 safecall_1(&Zexp, result, x1);
1726 return result;
1727 }
1728
1729 safe flush(const safe& x1) {
1730 value result;
1731 safecall_1(&Zflush, result, x1);
1732 return result;
1733 }
1734
1735 safe function() {
1736 value result;
1737 safecall_0(&Z_function, result); //generative: Z_
1738 return result;
1739 }
1740
1741 safe get(const safe& x1) {
1742 value result;
1743 safecall_1(&Zget, result, x1);
1744 return result;
1745 }
1746
1747 safe getch() {
1748 value result;
1749 safecall_0(&Zgetch, result);
1750 return result;
1751 }
1752
1753 safe getche() {
1754 value result;
1755 safecall_0(&Zgetche, result);
1756 return result;
1757 }
1758
1759 safe getenv(const safe& x1) {
1760 value result;
1761 safecall_1(&Zgetenv, result, x1);
1762 return result;
1763 }
1764
1765 safe iand(const safe& x1, const safe& x2) {
1766 value result;
1767 safecall_2(&Ziand, result, x1, x2);
1768 return result;
1769 }
1770
1771 safe icom(const safe& x1) {
1772 value result;
1773 safecall_1(&Zicom, result, x1);
1774 return result;
1775 }
1776
1777 safe image(const safe& x1) {
1778 value result;
1779 safecall_1(&Zimage, result, x1);
1780 return result;
1781 }
1782
1783 safe insert(const safe& x1, const safe& x2, const safe& x3) {
1784 value result;
1785 safecall_3(&Zinsert, result, x1, x2, x3);
1786 return result;
1787 }
1788
1789 safe integer(const safe& x1) {
1790 value result;
1791 safecall_1(&Zinteger, result, x1);
1792 return result;
1793 }
1794
1795 safe ior(const safe& x1, const safe& x2) {
1796 value result;
1797 safecall_2(&Zior, result, x1, x2);
1798 return result;
1799 }
1800
1801 safe ishift(const safe& x1, const safe& x2) {
1802 value result;
1803 safecall_2(&Zishift, result, x1, x2);
1804 return result;
1805 }
1806
1807 safe ixor(const safe& x1, const safe& x2) {
1808 value result;
1809 safecall_2(&Zixor, result, x1, x2);
1810 return result;
1811 }
1812
1813 safe kbhit() {
1814 value result;
1815 safecall_0(&Zkbhit, result);
1816 return result;
1817 }
1818
1819 safe key(const safe& x1) {
1820 value result;
1821 safecall_1(&Z_key, result, x1); //generative: Z_
1822 return result;
1823 }
1824
1825 safe left(const safe& x1, const safe& x2, const safe& x3) {
1826 value result;
1827 safecall_3(&Zleft, result, x1, x2, x3);
1828 return result;
1829 }
1830
1831 safe list(const safe& x1, const safe& x2) {
1832 value result;
1833 safecall_2(&Zlist, result, x1, x2);
1834 return result;
1835 }
1836
1837 safe loadfunc(const safe& x1, const safe& x2) {
1838 value result;
1839 safecall_2(&Zloadfunc, result, x1, x2);
1840 return result;
1841 }
1842
1843 safe log(const safe& x1) {
1844 value result;
1845 safecall_1(&Zlog, result, x1);
1846 return result;
1847 }
1848
1849 safe map(const safe& x1, const safe& x2, const safe& x3) {
1850 value result;
1851 safecall_3(&Zmap, result, x1, x2, x3);
1852 return result;
1853 }
1854
1855 safe member(const safe& x1, const safe& x2) {
1856 value result;
1857 safecall_2(&Zmember, result, x1, x2);
1858 return result;
1859 }
1860
1861 safe name(const safe& x1) {
1862 value result;
1863 safecall_1(&Zname, result, x1);
1864 return result;
1865 }
1866
1867 safe numeric(const safe& x1) {
1868 value result;
1869 safecall_1(&Znumeric, result, x1);
1870 return result;
1871 }
1872
1873 safe open(const safe& x1, const safe& x2) {
1874 value result;
1875 safecall_2(&Zopen, result, x1, x2);
1876 return result;
1877 }
1878
1879 safe ord(const safe& x1) {
1880 value result;
1881 safecall_1(&Zord, result, x1);
1882 return result;
1883 }
1884
1885 safe pop(const safe& x1) {
1886 value result;
1887 safecall_1(&Zpop, result, x1);
1888 return result;
1889 }
1890
1891 safe proc(const safe& x1, const safe& x2) {
1892 value result;
1893 safecall_2(&Zproc, result, x1, x2);
1894 return result;
1895 }
1896
1897 safe pull(const safe& x1) {
1898 value result;
1899 safecall_1(&Zpull, result, x1);
1900 return result;
1901 }
1902
1903 safe push(const variadic& x1) {
1904 safe result;
1905 safecall_vbl(&Zpush, result, x1);
1906 return result;
1907 }
1908
1909 safe push( const safe& x1, const safe& x2,
1910 const safe& x3, const safe& x4,
1911 const safe& x5, const safe& x6,
1912 const safe& x7, const safe& x8 ) {
1913 if( x3.isIllegal() )
1914 return push( (x1,x2) );
1915 if( x4.isIllegal() )
1916 return push( (x1,x2,x3) );
1917 if( x5.isIllegal() )
1918 return push( (x1,x2,x3,x4) );
1919 if( x6.isIllegal() )
1920 return push( (x1,x2,x3,x4,x5) );
1921 if( x7.isIllegal() )
1922 return push( (x1,x2,x3,x4,x5,x6) );
1923 if( x8.isIllegal() )
1924 return push( (x1,x2,x3,x4,x5,x6,x7) );
1925 return push( (x1,x2,x3,x4,x5,x6,x7,x8) );
1926 }
1927
1928 safe put(const variadic& x1) {
1929 safe result;
1930 safecall_vbl(&Zput, result, x1);
1931 return result;
1932 }
1933
1934 safe put( const safe& x1, const safe& x2,
1935 const safe& x3, const safe& x4,
1936 const safe& x5, const safe& x6,
1937 const safe& x7, const safe& x8 ) {
1938 if( x3.isIllegal() )
1939 return put( (x1,x2) );
1940 if( x4.isIllegal() )
1941 return put( (x1,x2,x3) );
1942 if( x5.isIllegal() )
1943 return put( (x1,x2,x3,x4) );
1944 if( x6.isIllegal() )
1945 return put( (x1,x2,x3,x4,x5) );
1946 if( x7.isIllegal() )
1947 return put( (x1,x2,x3,x4,x5,x6) );
1948 if( x8.isIllegal() )
1949 return put( (x1,x2,x3,x4,x5,x6,x7) );
1950 return put( (x1,x2,x3,x4,x5,x6,x7,x8) );
1951 }
1952
1953 safe read(const safe& x1) {
1954 value result;
1955 safecall_1(&Zread, result, x1);
1956 return result;
1957 }
1958
1959 safe reads(const safe& x1, const safe& x2) {
1960 value result;
1961 safecall_2(&Zreads, result, x1, x2);
1962 return result;
1963 }
1964
1965 safe real(const safe& x1) {
1966 value result;
1967 safecall_1(&Zreal, result, x1);
1968 return result;
1969 }
1970
1971 safe remove(const safe& x1) {
1972 value result;
1973 safecall_1(&Zremove, result, x1);
1974 return result;
1975 }
1976
1977 safe rename(const safe& x1, const safe& x2) {
1978 value result;
1979 safecall_2(&Zrename, result, x1, x2);
1980 return result;
1981 }
1982
1983 safe repl(const safe& x1, const safe& x2) {
1984 value result;
1985 safecall_2(&Zrepl, result, x1, x2);
1986 return result;
1987 }
1988
1989 safe reverse(const safe& x1) {
1990 value result;
1991 safecall_1(&Zreverse, result, x1);
1992 return result;
1993 }
1994
1995 safe right(const safe& x1, const safe& x2, const safe& x3) {
1996 value result;
1997 safecall_3(&Zright, result, x1, x2, x3);
1998 return result;
1999 }
2000
2001 safe rtod(const safe& x1) {
2002 value result;
2003 safecall_1(&Zrtod, result, x1);
2004 return result;
2005 }
2006
2007 safe runerr(const safe& x1, const safe& x2) {
2008 value result;
2009 safecall_v2(&Zrunerr, result, x1, x2);
2010 return result;
2011 }
2012
2013 safe runerr(const safe& x1) {
2014 value result;
2015 safecall_v1(&Zrunerr, result, x1);
2016 return result;
2017 }
2018
2019 safe seek(const safe& x1, const safe& x2) {
2020 value result;
2021 safecall_2(&Zseek, result, x1, x2);
2022 return result;
2023 }
2024
2025 safe serial(const safe& x1) {
2026 value result;
2027 safecall_1(&Zserial, result, x1);
2028 return result;
2029 }
2030
2031 safe set(const safe& x1) {
2032 value result;
2033 safecall_1(&Zset, result, x1);
2034 return result;
2035 }
2036
2037 safe sin(const safe& x1) {
2038 value result;
2039 safecall_1(&Zsin, result, x1);
2040 return result;
2041 }
2042
2043 safe sort(const safe& x1, const safe& x2) {
2044 value result;
2045 safecall_2(&Zsort, result, x1, x2);
2046 return result;
2047 }
2048
2049 safe sortf(const safe& x1, const safe& x2) {
2050 value result;
2051 safecall_2(&Zsortf, result, x1, x2);
2052 return result;
2053 }
2054
2055 safe sqrt(const safe& x1) {
2056 value result;
2057 safecall_1(&Zsqrt, result, x1);
2058 return result;
2059 }
2060
2061 safe stop() {
2062 safe result, nullarg;
2063 safecall_vbl(&Zstop, result, nullarg);
2064 return result;
2065 }
2066
2067 safe stop(const variadic& x1) {
2068 safe result;
2069 safecall_vbl(&Zstop, result, x1);
2070 return result;
2071 }
2072
2073 safe stop( const safe& x1, const safe& x2,
2074 const safe& x3, const safe& x4,
2075 const safe& x5, const safe& x6,
2076 const safe& x7, const safe& x8 ) {
2077 if( x3.isIllegal() )
2078 return stop( (x1,x2) );
2079 if( x4.isIllegal() )
2080 return stop( (x1,x2,x3) );
2081 if( x5.isIllegal() )
2082 return stop( (x1,x2,x3,x4) );
2083 if( x6.isIllegal() )
2084 return stop( (x1,x2,x3,x4,x5) );
2085 if( x7.isIllegal() )
2086 return stop( (x1,x2,x3,x4,x5,x6) );
2087 if( x8.isIllegal() )
2088 return stop( (x1,x2,x3,x4,x5,x6,x7) );
2089 return stop( (x1,x2,x3,x4,x5,x6,x7,x8) );
2090 }
2091
2092 safe string(const safe& x1) {
2093 value result;
2094 safecall_1(&Zstring, result, x1);
2095 return result;
2096 }
2097
2098 safe system(const safe& x1) {
2099 value result;
2100 safecall_1(&Zsystem, result, x1);
2101 return result;
2102 }
2103
2104 safe table(const safe& x1) {
2105 value result;
2106 safecall_1(&Ztable, result, x1);
2107 return result;
2108 }
2109
2110 safe tan(const safe& x1) {
2111 value result;
2112 safecall_1(&Ztan, result, x1);
2113 return result;
2114 }
2115
2116 safe trim(const safe& x1, const safe& x2) {
2117 value result;
2118 safecall_2(&Ztrim, result, x1, x2);
2119 return result;
2120 }
2121
2122 safe type(const safe& x1) {
2123 value result;
2124 safecall_1(&Ztype, result, x1);
2125 return result;
2126 }
2127
2128 safe variable(const safe& x1) {
2129 value result;
2130 safecall_1(&Zvariable, result, x1);
2131 return result;
2132 }
2133
2134 safe where(const safe& x1) {
2135 value result;
2136 safecall_1(&Zwhere, result, x1);
2137 return result;
2138 }
2139
2140 safe write() {
2141 safe result, nullarg;
2142 safecall_vbl(&Zwrite, result, nullarg);
2143 return result;
2144 }
2145
2146 safe write(const variadic& x1) {
2147 safe result;
2148 safecall_vbl(&Zwrite, result, x1);
2149 return result;
2150 }
2151
2152 safe write( const safe& x1, const safe& x2,
2153 const safe& x3, const safe& x4,
2154 const safe& x5, const safe& x6,
2155 const safe& x7, const safe& x8 ) {
2156 if( x3.isIllegal() )
2157 return write( (x1,x2) );
2158 if( x4.isIllegal() )
2159 return write( (x1,x2,x3) );
2160 if( x5.isIllegal() )
2161 return write( (x1,x2,x3,x4) );
2162 if( x6.isIllegal() )
2163 return write( (x1,x2,x3,x4,x5) );
2164 if( x7.isIllegal() )
2165 return write( (x1,x2,x3,x4,x5,x6) );
2166 if( x8.isIllegal() )
2167 return write( (x1,x2,x3,x4,x5,x6,x7) );
2168 return write( (x1,x2,x3,x4,x5,x6,x7,x8) );
2169 }
2170
2171 safe writes(const variadic& x1) {
2172 safe result;
2173 safecall_vbl(&Zwrites, result, x1);
2174 return result;
2175 }
2176
2177 safe writes( const safe& x1, const safe& x2,
2178 const safe& x3, const safe& x4,
2179 const safe& x5, const safe& x6,
2180 const safe& x7, const safe& x8 ) {
2181 if( x3.isIllegal() )
2182 return writes( (x1,x2) );
2183 if( x4.isIllegal() )
2184 return writes( (x1,x2,x3) );
2185 if( x5.isIllegal() )
2186 return writes( (x1,x2,x3,x4) );
2187 if( x6.isIllegal() )
2188 return writes( (x1,x2,x3,x4,x5) );
2189 if( x7.isIllegal() )
2190 return writes( (x1,x2,x3,x4,x5,x6) );
2191 if( x8.isIllegal() )
2192 return writes( (x1,x2,x3,x4,x5,x6,x7) );
2193 return writes( (x1,x2,x3,x4,x5,x6,x7,x8) );
2194 }
2195
2196 //generative functions crippled to return a single value follow
2197
2198 safe any(const safe& x1, const safe& x2=nullvalue, const safe& x3=nullvalue, const safe& x4=nullvalue) {
2199 value result;
2200 safecall_4(&Z_any, result, x1, x2, x3, x4);
2201 return result;
2202 }
2203
2204 safe many(const safe& x1, const safe& x2=nullvalue, const safe& x3=nullvalue, const safe& x4=nullvalue) {
2205 value result;
2206 safecall_4(&Z_many, result, x1, x2, x3, x4);
2207 return result;
2208 }
2209
2210 safe upto(const safe& x1, const safe& x2=nullvalue, const safe& x3=nullvalue, const safe& x4=nullvalue) {
2211 value result;
2212 safecall_4(&Z_upto, result, x1, x2, x3, x4);
2213 return result;
2214 }
2215
2216 safe find(const safe& x1, const safe& x2=nullvalue, const safe& x3=nullvalue, const safe& x4=nullvalue) {
2217 value result;
2218 safecall_4(&Z_find, result, x1, x2, x3, x4);
2219 return result;
2220 }
2221
2222 safe match(const safe& x1, const safe& x2=nullvalue, const safe& x3=nullvalue, const safe& x4=nullvalue) {
2223 value result;
2224 safecall_4(&Z_match, result, x1, x2, x3, x4);
2225 return result;
2226 }
2227
2228 safe bal(const safe& x1, const safe& x2=nullvalue, const safe& x3=nullvalue, const safe& x4=nullvalue, const safe& x5=nullvalue, const safe& x6=nullvalue) {
2229 value result;
2230 safecall_6(&Z_bal, result, x1, x2, x3, x4, x5, x6);
2231 return result;
2232 }
2233
2234 safe move(const safe& x1) {
2235 value result;
2236 safecall_1(&Z_move, result, x1);
2237 return result;
2238 }
2239
2240 safe tab(const safe& x1) {
2241 value result;
2242 safecall_1(&Z_tab, result, x1);
2243 return result;
2244 }
2245
2246 }; //namespace Icon
2247
2248 /*
2249 * Useful functions
2250 */
2251
2252 //pass this on to external libraries, so they don't have to link against iconx (cygwin)
2253 void syserror(const char* s) { syserr((char *)s); }
2254
2255 value IconFile(FILE* fd, int status, char* fname) {
2256 value answer, filename(NewString, fname);
2257 answer.dword = D_File;
2258 answer.vword = (long)alcfile(fd, status, &filename);
2259 return answer;
2260 }
2261
2262 //large integer related and base64 related functions follow
2263
2264 struct bignum { //after b_bignum in rstructs.h
2265 long title;
2266 long blksize;
2267 long msd, lsd;
2268 int sign;
2269 unsigned int digit[1];
2270 };
2271
2272 //Endian/wordsize nonsense follows, to help get at bytes in the digits of Icon BigIntegers
2273
2274 //repair moves the non-zero bytes we care about in a DIGIT (see rlrgint.r)
2275 //that are in the least significant half of the bytes of a uint
2276 //into the left hand end (in RAM) of the unint in big endian order
2277
2278 //for solaris that does not define this macro
2279 #ifndef BYTE_ORDER
2280 #define BYTE_ORDER 4321
2281 #endif
2282
2283 #if BYTE_ORDER==1234 || BYTE_ORDER==4321
2284 const int DIGITBYTES=2;
2285
2286 #if BYTE_ORDER==1234
2287 inline unsigned int repair(unsigned int x) {
2288 return (x & 0x0000FF00) >> 8 | (x & 0x000000FF) << 8;
2289 }
2290 inline long bigendian(long n) {
2291 n = (n & 0xFFFF0000) >> 16 | (n & 0x0000FFFF) << 16;
2292 return (n & 0xFF00FF00) >> 8 | (n & 0x00FF00FF) << 8;
2293 }
2294 #endif
2295
2296 #if BYTE_ORDER==4321
2297 inline unsigned int repair(unsigned int x) {
2298 return x << 2;
2299 }
2300 inline long bigendian(long n) {
2301 return n;
2302 }
2303 #endif
2304
2305 #endif
2306
2307 #if BYTE_ORDER==12345678 || BYTE_ORDER==87654321
2308 const int DIGITBYTES=4;
2309
2310 #if BYTE_ORDER==12345678
2311 inline unsigned int repair(unsigned int x) {
2312 x = (x & 0x00000000FFFF0000) >> 16 | (x & 0x000000000000FFFF) << 16;
2313 return (x & 0x00000000FF00FF00) >> 8 | (x & 0x0000000000FF00FF) << 8;
2314 }
2315 inline long bigendian(long n) {
2316 n = (n & 0xFFFFFFFF00000000) >> 32 | (n & 0x00000000FFFFFFFF) << 32;
2317 n = (n & 0xFFFF0000FFFF0000) >> 16 | (n & 0x0000FFFF0000FFFF) << 16;
2318 return (n & 0xFF00FF00FF00FF00) >> 8 | (n & 0x00FF00FF00FF00FF) << 8;
2319 }
2320 #endif
2321
2322 #if BYTE_ORDER==87654321
2323 inline unsigned int repair(unsigned int x) {
2324 return x << 4;
2325 }
2326 inline long bigendian(long n) {
2327 return n;
2328 }
2329 #endif
2330
2331 #endif
2332
2333 value integertobytes(value bigint){ //get the bytes of an Icon long integer as an Icon string (ignore sign)
2334 safe n(bigint);
2335 if( n == 0 ) return nullchar;
2336 switch( bigint.type() ) {
2337 case Integer: {
2338 long x = bigint;
2339 x = bigendian(x);
2340 char *sbuf = (char *)&x;
2341 int len = sizeof(long);
2342 while( !*sbuf ) { //skip leading zeros in base 256
2343 ++sbuf;
2344 --len;
2345 }
2346 return value(sbuf, len);
2347 break;
2348 }
2349 case BigInteger: {
2350 bignum *bp = ((bignum*)(bigint.vword));
2351 unsigned int current;
2352 long pos = 0, len = (bp->lsd - bp->msd + 1) * DIGITBYTES;
2353 char *source, *buf = new char[len], *sbuf;
2354 sbuf = buf;
2355 for(long i = bp->msd; i <= bp->lsd; ++i) {
2356 current = repair(bp->digit[i]);
2357 source = (char *)&current;
2358 for(int b=0; b < DIGITBYTES; ++b)
2359 sbuf[pos++] = source[b];
2360 }
2361 while( !*sbuf ) { //skip leading zeros in base 256
2362 ++sbuf;
2363 --len;
2364 }
2365 value bytestring(sbuf, len);
2366 delete[] buf;
2367 return bytestring;
2368 }
2369 default:
2370 return nullvalue;
2371 }
2372 }
2373
2374 value bytestointeger(value bytestring){ //get the bytes of a new Icon long integer from an Icon string
2375 if( bytestring.type() != String ) return nullvalue;
2376 while( *(char*)bytestring.vword == 0 && bytestring.dword != 0 ) { //skip leading zeros
2377 --bytestring.dword;
2378 ++bytestring.vword;
2379 }
2380 safe s(bytestring);
2381 long size = value(*s);
2382 if( size == 0 ) return 0;
2383 unsigned char *bytes = (unsigned char *)((char*)bytestring);
2384 long n = 0;
2385 if( size < sizeof(long) || //doesn't overflow a signed long
2386 (size == sizeof(long) && ( bytes[0] <= 0x7F )) ) {
2387 for(int i = 0; i < size; ++i)
2388 n = (n << 8) + bytes[i];
2389 return n;
2390 }
2391 static const int RATIO = sizeof(unsigned int)/2;
2392 long len = (size + RATIO - 1)/RATIO; //number of digits
2393 bignum *bp = (bignum *)alcbignum(len);
2394 bytestring = s; //in case the allocation caused a garbage collection
2395 bytes = (unsigned char *)((char*)bytestring);
2396 long pos = 0;
2397 const int FIRST = len*RATIO==size ? RATIO : len*RATIO-size; //bytes in the first digit
2398 n = 0;
2399 for(int p=0; p < FIRST; ++p)
2400 n = (n << 8) + bytes[pos++];
2401 bp->digit[0] = n;
2402 for(long i = bp->msd + 1; i <= bp->lsd; ++i) {
2403 n = 0;
2404 for(int p=0; p < RATIO; ++p)
2405 n = (n << 8) + bytes[pos++];
2406 bp->digit[i] = n;
2407 }
2408 value answer;
2409 answer.dword = D_Lrgint;
2410 answer.vword = (long)bp;
2411 return answer;
2412 }
2413
2414 //base64 utilities
2415 typedef unsigned char uchar;
2416 static char chr[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2417
2418 //3 bytes -> four base64 chars
2419 inline void threetofour(uchar *three, uchar* four) {
2420 unsigned long n = three[0];
2421 n = (((n << 8) + three[1]) << 8) + three[2];
2422 four[3] = chr[n & 0x3F];
2423 n = n >> 6;
2424 four[2] = chr[n & 0x3F];
2425 n = n >> 6;
2426 four[1] = chr[n & 0x3F];
2427 n = n >> 6;
2428 four[0] = chr[n & 0x3F];
2429 }
2430
2431 //two trailing bytes -> four base64 chars
2432 inline void twotofour(uchar *three, uchar* four) {
2433 unsigned long n = three[0];
2434 n = ((n << 8) + three[1]) << 2;
2435 four[3] = '=';
2436 four[2] = chr[n & 0x3F];
2437 n = n >> 6;
2438 four[1] = chr[n & 0x3F];
2439 n = n >> 6;
2440 four[0] = chr[n & 0x3F];
2441 }
2442
2443 //one trailing byte -> four base64 chars
2444 inline void onetofour(uchar *three, uchar* four) {
2445 unsigned long n = three[0];
2446 n = n << 4;
2447 four[3] = four[2] = '=';
2448 four[1] = chr[n & 0x3F];
2449 n = n >> 6;
2450 four[0] = chr[n & 0x3F];
2451 }
2452
2453 //convert to base64, return the length of the encoded string
2454 inline long b64(char *in, long len, char* out) {
2455 char *start = out;
2456 long num = len/3;
2457 int rem = len%3;
2458 for(long i = 0; i < num; ++i) {
2459 threetofour((uchar*)in, (uchar*)out);
2460 in += 3;
2461 out += 4;
2462 }
2463 switch( rem ) {
2464 case 1:
2465 onetofour((uchar*)in, (uchar*)out);
2466 out += 4;
2467 break;
2468 case 2:
2469 twotofour((uchar*)in, (uchar*)out);
2470 out += 4;
2471 break;
2472 }
2473 return out - start;
2474 }
2475
2476 //constant denoting an invalid character in a putative base64 encoding
2477 static const int NONSENSE = -1;
2478
2479 //convert a base64 char into its corresponding 6 bits
2480 inline int undo(uchar ch) {
2481 switch( ch ) {
2482 default: return NONSENSE;
2483 case 'A': return 0; case 'B': return 1; case 'C': return 2; case 'D': return 3;
2484 case 'E': return 4; case 'F': return 5; case 'G': return 6; case 'H': return 7;
2485 case 'I': return 8; case 'J': return 9; case 'K': return 10; case 'L': return 11;
2486 case 'M': return 12; case 'N': return 13; case 'O': return 14; case 'P': return 15;
2487 case 'Q': return 16; case 'R': return 17; case 'S': return 18; case 'T': return 19;
2488 case 'U': return 20; case 'V': return 21; case 'W': return 22; case 'X': return 23;
2489 case 'Y': return 24; case 'Z': return 25; case 'a': return 26; case 'b': return 27;
2490 case 'c': return 28; case 'd': return 29; case 'e': return 30; case 'f': return 31;
2491 case 'g': return 32; case 'h': return 33; case 'i': return 34; case 'j': return 35;
2492 case 'k': return 36; case 'l': return 37; case 'm': return 38; case 'n': return 39;
2493 case 'o': return 40; case 'p': return 41; case 'q': return 42; case 'r': return 43;
2494 case 's': return 44; case 't': return 45; case 'u': return 46; case 'v': return 47;
2495 case 'w': return 48; case 'x': return 49; case 'y': return 50; case 'z': return 51;
2496 case '0': return 52; case '1': return 53; case '2': return 54; case '3': return 55;
2497 case '4': return 56; case '5': return 57; case '6': return 58; case '7': return 59;
2498 case '8': return 60; case '9': return 61; case '+': return 62; case '/': return 63;
2499 }
2500 }
2501
2502 //four base64 chars -> three bytes
2503 inline long unfour(uchar* four, uchar* three) {
2504 int ch;
2505 if( (ch = undo(four[0])) == NONSENSE ) return NONSENSE;
2506 long n = ch;
2507 if( (ch = undo(four[1])) == NONSENSE ) return NONSENSE;
2508 n = (n << 6) + ch;
2509 if( (ch = undo(four[2])) == NONSENSE ) return NONSENSE;
2510 n = (n << 6) + ch;
2511 if( (ch = undo(four[3])) == NONSENSE ) return NONSENSE;
2512 n = (n << 6) + ch;
2513 three[2] = n & 0xFF;
2514 n = n >> 8;
2515 three[1] = n & 0xFF;
2516 three[0] = n >> 8;
2517 }
2518
2519 //decode a base64 string; return NONSENSE if anything doesn't make strict sense
2520 inline long unb64(char* in, long len, char* out) {
2521 char* start = out;
2522 if( len == 0 ) return 0;
2523 if( len%4 != 0 ) return NONSENSE;
2524 int last = 0;
2525 if( in[len-1] == '=' ) {
2526 last = 1;
2527 if( in[len-2] == '=' ) last = 2;
2528 }
2529 if( last ) len -= 4;
2530
2531 for(long i = 0; i < len/4; ++i) {
2532 if( unfour((uchar*)in, (uchar*)out) == NONSENSE )
2533 return NONSENSE;
2534 in += 4;
2535 out += 3;
2536 }
2537 long n;
2538 int ch0, ch1, ch2;
2539 switch( last ) {
2540 case 1:
2541 if( (ch0 = undo((uchar)in[0])) == NONSENSE )
2542 return NONSENSE;
2543 if( (ch1 = undo((uchar)in[1])) == NONSENSE )
2544 return NONSENSE;
2545 if( (ch2 = undo((uchar)in[2])) == NONSENSE )
2546 return NONSENSE;
2547 n = ((((ch0 << 6) + ch1) << 6) + ch2) >> 2;
2548 out[1] = n & 0xFF;
2549 out[0] = n >> 8;
2550 out += 2;
2551 break;
2552 case 2:
2553 if( (ch0 = undo((uchar)in[0])) == NONSENSE )
2554 return NONSENSE;
2555 if( (ch1 = undo((uchar)in[1])) == NONSENSE )
2556 return NONSENSE;
2557 n = (ch0 << 6) + ch1;
2558 out[0] = n >> 4;
2559 out += 1;
2560 break;
2561 }
2562 return out - start;
2563 }
2564
2565 //convert string or integer to base64 string
2566 value base64(value x) {
2567 switch( x.type() ) {
2568 default:
2569 return nullvalue;
2570 case Integer:
2571 case BigInteger:
2572 x = integertobytes(x);
2573 case String: {
2574 char* enc = new char[4*x.dword/3+8]; //safety first
2575 long len = b64((char*)x.vword, x.dword, enc);
2576 value answer(enc, len);
2577 delete[] enc;
2578 return answer;
2579 }
2580 }
2581 }
2582
2583 //decode base64 encoding of a string
2584 value base64tostring(value s) {
2585 if( s.type() != String ||
2586 s.dword % 4 != 0)
2587 return nullvalue;
2588 if( s.dword == 0 ) return nullstring;
2589 long len;
2590 char* dec = new char[3 * s.dword/4]; //safety first
2591 if( (len = unb64((char*)s.vword, s.dword, dec)) == NONSENSE ) {
2592 delete[] dec;
2593 return nullvalue;
2594 }
2595 value answer(dec, len);
2596 delete[] dec;
2597 return answer;
2598 }
2599
2600 //decode base64 encoding of an integer
2601 value base64tointeger(value s) {
2602 return bytestointeger(base64tostring(s));
2603 }
2604
2605
2606
2607 /*
2608 * 1. Calling Icon from C++ (mostly in iloadgpx.cpp and iloadnogpx.cpp)
2609 * 2. loadfuncpp itself
2610 * 3. binding records to procedure blocks
2611 */
2612
2613 namespace ifload {
2614 //remove interference with icon/src/h/rt.h
2615 #undef D_Null
2616 #undef D_Integer
2617 #undef D_Lrgint
2618 #undef D_Real
2619 #undef D_File
2620 #undef D_Proc
2621 #undef D_External
2622 #undef Fs_Read
2623 #undef Fs_Write
2624 #undef F_Nqual
2625 #undef F_Var
2626
2627 #include "xfload.cpp" //inline linkage --- three argument raw loadfunc
2628 }; //end namespace ifload; put things that need Icon's rt.h included by xfload.cpp below here
2629
2630 //call to the modified loadfunc in xfload.cpp
2631 static int rawloadfuncpp(value argv[]) {
2632 return ifload::Z_loadfunc((ifload::dptr)argv);
2633 }
2634
2635
2636 //get the record from the bottom of an extended procedure block
2637 //(procedure bound to record) obtained from the procedure that
2638 //called our procedure self(). Fail if no record is bound.
2639 extern "C" int getbinding(value* argv) {
2640 value* pp = (value*)((ifload::pfp)->pf_argp); //get saved procedure
2641 if( pp==0 ) syserror("loadfuncpp bug: attempt to find caller of self() failed!");
2642 proc_block* pbp = *pp;
2643 int nsafe = pbp->ndynam + pbp->nparam;
2644 if( (pbp->blksize) - sizeof(proc_block) == (nsafe-1) * sizeof(value) ) {
2645 argv[0] = nullvalue;
2646 return FAILED;
2647 }
2648 argv[0] = pbp->lnames[nsafe];
2649 return SUCCEEDED;
2650 }
2651
2652
2653 #if __CYGWIN__ //cygwin linkage problem workaround
2654 namespace icall {
2655 using namespace ifload;
2656 //icall assigned from whichever of iloadgpx.so and iloadnogpx.so is loaded, on load thereof
2657 extern "C" {
2658 typedef int icallfunction(dptr procptr, dptr arglistptr, dptr result);
2659 };
2660 icallfunction *icall2;
2661 };
2662
2663 value Value::call(const value& proc, const value& arglist) {
2664 value result;
2665 (*(icall::icall2))( (icall::dptr)(&proc), (icall::dptr)(&arglist), (icall::dptr)(&result) );
2666 return result;
2667 }
2668 #endif //cygwin linkage problem workaround
2669