-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsli_control.cpp
1912 lines (1610 loc) · 53.2 KB
/
sli_control.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* slicontrol.cc
*
* This file is part of NEST
*
* Copyright (C) 2004 by
* The NEST Initiative
*
* See the file AUTHORS for details.
*
* Permission is granted to compile and modify
* this file for non-commercial use.
* See the file LICENSE for details.
*
*/
/*
SLI's control structures
*/
//#include "config.h"
//#include "sliconfig.h"
#include "sli_control.h"
#include "sli_scanner.h"
#include "sli_parser.h"
#include "sli_iostreamtype.h"
#include "sli_iostream.h"
#include "sli_stringtype.h"
#include "sli_dictstack.h"
#include "sli_functiontype.h"
//#include "sli_processes.h"
#include "sli_exceptions.h"
#include <time.h>
#include <sys/time.h> // required to fix header dependencies in OS X, HEP
#include <sys/times.h>
#include <sys/resource.h>
#include <unistd.h>
// sstream has functions std::?stringstream
// strstream has functions std::?strstream
// HEP 2002-10-06
#define HAVE_SSTREAM 1
#ifdef HAVE_SSTREAM
#include <sstream>
#else
#include <strstream>
#endif
namespace sli3
{
/*BeginDocumentation
Name: backtrace_on - enable stack backtrace on error.
Synopsis: backtrace_on -> -
Description:
This functions enables a human readable backtrace of the execution
stack. This is useful to locate where precisely an error occured. Note
that this function also disables the interpreter's tail recursion
optimization and will therefore impose a small performance
penalty. The command backtrace_off disables the stack backtrace and
re-enables tail recursion optimization.
Example:
SeeAlso:
backtrace_off
*/
void Backtrace_onFunction::execute(SLIInterpreter *i) const
{
i->backtrace_on();
i->EStack().pop();
}
/*BeginDocumentation
Name: backtrace_off - Disable the stack backtrace on error.
Synopsis: backtrace_off -> -
Description:
This functions disables the backtrace of the execution
stack and re-enables tail recursion optimization.
SeeAlso: backtrace_on
*/
void Backtrace_offFunction::execute(SLIInterpreter *i) const
{
i->backtrace_off();
i->EStack().pop();
}
void OStackdumpFunction::execute(SLIInterpreter *i) const
{
i->EStack().pop(); // never forget me!!
i->OStack().dump(std::cout);
}
void EStackdumpFunction::execute(SLIInterpreter *i) const
{
i->EStack().pop(); // never forget me!!
i->EStack().dump(std::cout);
}
/*BeginDocumentation
Name: loop - repeatedly execute a procedure
Synopsis: proc loop -
Description:
loop takes a procedure object an executes it repeatedly.
Since there is no direct termination condition, the loop
has to be terminated by calling exit.
If the procedure has to be evaluated for a certain number of
times, consider the use of repeat or for.
If some container should be iterated, consider forall or Map
SeeAlso: exit, repeat, for, forall, forallindexed, Map
*/
void LoopFunction::execute(SLIInterpreter *i) const
{
i->require_stack_load(1);
i->require_stack_type(0, sli3::proceduretype);
i->EStack().pop();
i->EStack().push(i->baselookup(i->mark_name));
i->EStack().push(i->top());
i->EStack().push(0);
i->EStack().push(i->baselookup(i->iloop_name));
i->inc_call_depth();
i->pop();
}
/*BeginDocumentation
Name: exit - exit a loop construct
Description: exit can be used to leave loop structures
like loop, repeat, for, forall, Map, etc.
in a clean way.
Remarks: This command does not exit the SLI interpreter! Use quit instead.
*/
void ExitFunction::execute(SLIInterpreter *i) const
{
size_t n=1;
size_t l=i->EStack().load();
while( ( l > n ) && !( i->EStack().pick(n++).is_of_type(sli3::marktype)));
if( n >= l)
{
i->raiseerror("EStackUnderflow");
return;
}
i->dec_call_depth();
i->EStack().pop(n);
}
/*BeginDocumentation
Name: if - conditionaly execute a procedure
Synopsis:
boolean {procedure} if -> -
boolean anytoken if -> -
Description: if executes the supplied token if the boolean
is true. The supplied token usually is a procedure.
Alternatives: Function if_ (undocumented)
-> behaviour and synopsis are the same, except that no
warnings or error messages are thrown.
Examples: 1 0 gt { (1 > 0) = } if -> (1 > 0)
SeeAlso: ifelse
*/
void IfFunction::execute(SLIInterpreter *i) const
{
// OStack: bool proc
// 1 0
i->require_stack_load(2);
if(i->pick(1)==true)
{
i->EStack().top()=i->top();
if(i->step_mode())
{
std::cerr << "if:"
<< " Executing true branch."
<< std::endl;
}
}
else
i->EStack().pop();
i->pop(2);
}
/*BeginDocumentation
Name: ifelse - conditionaly execute a procedure
Synopsis:
boolean {proc1} {proc2} ifelse -> -
boolean anytoken1 anytoken1 ifelse -> -
Description:
ifelse executes anytoken1 if the boolean is true, and anytoken2
otherwise.
The supplied tokens usually are procedures.
Alternatives: Function ifelse_ (undocumented)
-> behaviour and synopsis are the same, except that no
warnings or error messages are thrown.
Examples:
0 1 gt { (1 > 0) = } { (1 <= 0) =} ifelse -> (0 <= 1)
SeeAlso: if
*/
void IfelseFunction::execute(SLIInterpreter *i) const
{
// OStack: bool tproc fproc
// 2 1 0
i->require_stack_load(3);
i->EStack().pop();
if(i->pick(1)==true )
{
if(i->step_mode())
{
std::cerr << "if:"
<< " Executing true branch."
<< std::endl;
}
i->EStack().push(i->top());
i->pop(2);
}
else
{
if(i->step_mode())
{
std::cerr << "ifelse:"
<< " Executing false branch."
<< std::endl;
}
i->EStack().push(i->top());
}
i->pop(3);
}
/*BeginDocumentation
Name: repeat - execute a procedure n times
Synopsis: n proc repeat
Description:
repeat executes the supplied procedure n times.
The loop can be left prematurely using exit.
Note: The interation counter is not available
to the procedure. If this is desired, use for instead.
Examples:
SLI ] 3 { (Hello world) = } repeat
Hello world
Hello world
Hello world
SLI ]
SeeAlso: for, loop, exit
*/
void RepeatFunction::execute(SLIInterpreter *i) const
{
static SLIType* mark_t= i->get_type(sli3::marktype);
static SLIType* repeat_t= i->get_type(sli3::irepeattype);
i->require_stack_load(2);
i->require_stack_type(0,sli3::proceduretype);
i->require_stack_type(1,sli3::integertype);
TokenArray *proc= i->top().data_.array_val;
i->EStack().top().type_=mark_t;
i->EStack().push(i->pick(1));
i->EStack().push(i->pick(0));
i->EStack().push(i->new_token<sli3::integertype>(proc->size()));
i->EStack().push(repeat_t);
i->pop(2);
}
/*BeginDocumentation
Name: stopped - returns true if execution was stopped by stop
Synopsis:
xobj stopped -> true; if the object was aborted with stop
-> false; otherwise.
Description:
stopped is part of a pair of commands which implement the
PostScript exception mechanism.
stopped is applied to an executable object, most probably a
procedure, and returns true if this object raised a stop
signal and false otherwise.
This is accomplished by first pushing an internal name which
resolves to false and then executing the object.
stop will pop the stack down to this name and return true. If
stop is not called, the name is eventually executed and thus the
return value false.
Note that when the executable object was stopped by a call
to raiseerror, the name of the routine that caused the
error has can be found on the operand stack (see raiseerror).
The stop/stopped pair is used to implement SLI's error handling
capabilities.
Notes: stop, stopped is PostScript compatible
References: The Red Book, sec. 3.10
SeeAlso: stop, raiseerror
*/
void StoppedFunction::execute(SLIInterpreter *i) const
{
i->require_stack_load(1);
i->EStack().pop();
i->EStack().push(i->new_token<sli3::nametype>(i->istopped_name));
i->EStack().push(i->top());
i->pop();
}
/*BeginDocumentation
Name: stop - raise a stop signal
Synopsis:
stop -> -
Desctiption:
stop causes the execution stack to be popped until an
enclosing stopped context is found. Effectively, the
stopped/stop combination equals the catch/throw pair of
C++.
stop/stopped is used to implement SLI's error handling
capabilities.
Notes: stop, stopped is PostScript compatible.
References: The Red Book, sec. 3.10
SeeAlso: stopped, raiseerror
*/
void StopFunction::execute(SLIInterpreter *i) const
{
size_t l=i->EStack().load();
bool found=false;
size_t n=1;
while( ( l > n ) && !(found))
{
Token &t=i->EStack().pick(n++);
found= (t == i->istopped_name);
}
// if(i->catch_errors() && ! found)
// i->debug_mode_on();
/*
if(i->get_debug_mode() || i->show_backtrace())
{
if(i->show_backtrace() || ! found)
i->stack_backtrace(l-1);
std::cerr << "In stop: An error or stop was raised."
<<" Unrolling stack by " << n << " levels."
<< std::endl;
if(!found)
{
std::cerr << "No 'stopped' context found." << std::endl
<< "Stack unrolling will erase the execution stack." << std::endl
<< "Entering debug mode. Type '?' for help." << std::endl;
}
if(i->get_debug_mode())
{
char c=i->debug_commandline(i->EStack().top());
if(c=='i') // in interactive mode, we leave the stack as it is.
return;
}
}
*/
if(!found)
{
i->message(30,"stop",
"No stopped context was found! \n");
i->EStack().clear();
return;
}
i->push(true);
i->EStack().pop(n);
}
/*BeginDocumentation
Name: closeinput - Close current input file.
FirstVersion: 25 Jul 2005, Gewaltig
*/
void CloseinputFunction::execute(SLIInterpreter *i) const
{
size_t l=i->EStack().load();
bool found=false;
size_t n=1;
while( ( l > n ) && !(found))
found = i->EStack().pick(n++).is_of_type(sli3::xistreamtype);
/*
if(i->catch_errors() || ! found)
i->debug_mode_on();
if(i->get_debug_mode() || i->show_backtrace())
{
if(i->show_backtrace() || ! found)
i->stack_backtrace(n);
std::cerr << "In closeinput: Termination of input file requested."
<<" Unrolling stack by " << n << " levels."
<< std::endl;
if(!found)
{
std::cerr << "In closeinput: No active input file was found." << std::endl
<< "Stack unrolling will erase the execution stack." << std::endl
<< "Entering debug mode. Type '?' for help." << std::endl;
}
if(i->get_debug_mode())
{
char c=i->debug_commandline(i->EStack().top());
if(c=='i') // in interactive mode, we leave the stack as it is.
return;
}
}
*/
if(!found)
{
i->message(30,"closeinput",
"No active input file was found. \n Restarting...");
i->EStack().clear();
i->EStack().push(i->new_token<sli3::nametype>(Name("start")));
return;
}
i->EStack().pop(n);
}
/* BeginDocumentation
Name: currentname - returns the most recently resolved name
Synopsis:
currentname -> name true
-> false
Description:
currentname returns the most recently resolved name whose contents
is still under execution.
currentname is useful for error handling purposes where a procedure
has to know the name with which it has been called.
Example:
/divide
{
0 eq
{
currentname /DivisionByZero raiseerror
} if
div
} def
Note:
This function is not foolproof, as it will fail for bound procedures.
currentname evaluates certain debugging information on the execution
stack. Note that the SLI interpreter can be compiled not to generate
this information. In this case currentname fails, i.e. it will always
return false
*/
void CurrentnameFunction::execute(SLIInterpreter *i) const
{
i->EStack().pop();
/*
size_t n=0; // skip my own name
size_t l=i->EStack().load();
// top level %%lookup must belong to currentname, so
// remove it and the name.
if(i->EStack().top()==i->baselookup(i->ilookup_name))
{
assert(l>2);
n+=2;
}
bool found=false;
while( ( l > n ) && !found)
found= i->EStack().pick(n++)==i->baselookup(i->ilookup_name);
if(found)
{
i->OStack.push(i->EStack().pick(n));
i->OStack.push(true);
}
else
i->EStack().push(false);
*/
}
void DefFunction::execute(SLIInterpreter *i) const
{
i->require_stack_load(2);
i->require_stack_type(1,sli3::literaltype);
i->def(i->pick(1).data_.name_val,i->top());
i->pop(2);
i->EStack().pop();
}
/*BeginDocumentation
Name: Set - Define an association between a name and an object in the current dictionary
Synopsis:
obj literal Set -> -
[... [obj_1 ...] ... obj_n] [... [literal_1 ...] ... literal_n] Set -> -
Description:
In the first form Set is identical to def, except for the reversed parameters and
creates or modifies an entry for the literal in the current dictionary. The new value
assigned to the literal is obj.
In the second form multiple simultaneous assignments are made to the literals contained in
the second. The nesting of this array is arbitrary, indicated in the synopsis by the
inner brackets, and the same object are taken from the identical positions in first array.
Examples:
{1 2 add} /myproc Set
[4 3] [/x /y] Set
[[4 3 7] [-9 1]] [[/a /b /c] [/x /y]] Set
[[4 9] 3] [/x /y] Set
[[4 9] 3] [[/a /b] /y] Set
SeeAlso: def, undef, begin, end
*/
void SetFunction::execute(SLIInterpreter *i) const
{
i->require_stack_load(2);
i->require_stack_type(0,sli3::literaltype);
Name name(i->pick(0).data_.name_val);
i->def(name,i->pick(1));
i->pop(2);
i->EStack().pop();
}
/*BeginDocumentation
Name: load - Search for a key in each dictionary on the dictionary stack.
Synopsis: /name load -> obj
Description: Load tries to find an association for /name in each dictionary
on the dictionary stack, starting with the current (top) dictionary.
If an association is found, load pushes the associated value on the
stack. If no association is found, an UndefineName error is raised.
SeeAlso: lookup, def
*/
void LoadFunction::execute(SLIInterpreter *i) const
{
i->require_stack_load(1);
i->require_stack_type(0,sli3::literaltype);
Name name(i->top().data_.name_val);
i->top()=i->lookup(name);
i->EStack().pop();
}
/*BeginDocumentation
Name: lookup - Search for a key in each dictionay on the dictionary stack.
Synopsis: /name lookup -> obj true
-> false
Description: lookup tries to find an association for /name in each dictionary
on the dictionary stack, starting with the current (top) dictionary.
If an association is found, lookup pushes the associated value on the
stack followed by the boolean true.
If no association is found, false is pushed.
SeeAlso: load, def
*/
void LookupFunction::execute(SLIInterpreter *i) const
{
i->require_stack_load(1);
i->require_stack_type(0,sli3::literaltype);
Name name(i->top().data_.name_val);
i->EStack().pop();
i->pop();
Token content;
bool result =i->lookup(name, content);
if(result)
i->push(content);
i->push(result);
}
/*BeginDocumentation
Name: for - execute a procedure for a sequence of numbers
Synopsis: n1 s n2 proc for -> -
Description:
for repeatedly evaluates the supplied procedure for all
values from n1 to n2 in steps of s. In each iteration
proc is called with the current iteration counter as
argument.
The loop can be quit prematurely by calling exit.
If the value of the iteration counter is not needed,
use repeat instead.
Examples:
SLI ] 1 1 10 {=} for
1
2
3
4
5
6
7
8
9
10
SLI ]
SeeAlso: repeat, exit, loop
*/
void ForFunction::execute(SLIInterpreter *i) const
{
static SLIType* for_t=i->get_type(sli3::ifortype);
static SLIType* mark_t=i->get_type(sli3::marktype);
i->require_stack_load(4);
i->require_stack_type(0,sli3::proceduretype);
i->require_stack_type(1,sli3::integertype);
i->require_stack_type(2,sli3::integertype);
i->require_stack_type(3,sli3::integertype);
i->EStack().top().type_=mark_t;
i->EStack().push(i->pick(2)); // increment
i->EStack().push(i->pick(1)); // limit
i->EStack().push(i->pick(3)); // counter
i->EStack().push(i->pick(0)); // procedure
i->EStack().push(i->new_token<sli3::integertype>(i->top().data_.array_val->size()));
i->EStack().push(for_t); // %for
i->inc_call_depth();
i->pop(4);
}
/*
BeginDocumentation
Name: forall - Call a procedure for each element of a list/string/dictionary
Synopsis:
[v1 ... vn] {f} forall -> f(v1) ... f(vn)
(c1...cn) {f} forall -> f(c1) ... f(cn)
<</key1 val1 ... /keyn valn>> {f} forall -> f(/key1 val1) ... f(/keyn valn)
Parameters:
[v1,...,vn] - list of n arbitrary objects
(c1...cn) - arbitrary string
<</keyi vali>> - arbitrary dictionary
{f} - function which can operate on the elements of the
array, or on key/value pairs of the dictionary.
f is not required to return a specific number
of values.
Description:
Arrays:
For each element of the input array, forall calls f.
forall is similar to Map, however, it does not construct a
new list from the return values.
Dictionaries:
For each key/value pair of the dictionary, forall calls f.
Order on the operand stack will be: key, value. (I.e. value on top.)
*Note: The dictionary contents are copied before operation.
This can be a potentially expensive operation.*
Loops can be nested. The loop can be quit prematurely with exit.
Examples:
[1 2 3 4 5] {=} forall -> - % Print all values of the list
[1 2 3 4 5] {} forall -> 1 2 3 4 5
(abc) {=} forall -> prints 97 98 99 on separate lines
<</a 1 /b 2>> {== ==} forall -> prints 1 /a 2 /b on separate lines
Author:
Marc-Oliver Gewaltig, Ruediger Kupper (dictionary variant)
References: The Red Book
SeeAlso: Map, MapAt, MapIndexed, Table, forallindexed, NestList, FoldList, Fold, exit
*/
/******************************/
/* forall_di */
/* wrapper around forall_a */
/* implemented in SLI */
/* see typeinit.sli */
/******************************/
/******************************/
/* forall_a */
/* call: obj proc forall */
/* pick 1 0 */
/******************************/
void Forall_aFunction::execute(SLIInterpreter *i) const
{
static Token forall(i->baselookup(i->iforallarray_name));
i->require_stack_load(2);
i->require_stack_type(0,sli3::proceduretype);
i->require_stack_type(1,sli3::arraytype);
TokenArray *proc= i->top().data_.array_val;
i->EStack().pop();
i->EStack().push(i->new_token<sli3::marktype>());
i->EStack().push(i->pick(1)); // push object
i->EStack().push(i->new_token<sli3::integertype>(0)); // push array counter
i->EStack().push(i->pick(0)); // push procedure
i->EStack().push(i->new_token<sli3::integertype>(proc->size())); // push procedure counter
i->EStack().push(forall);
i->pop(2);
i->inc_call_depth();
}
/******************************/
/* forall_iter */
/* call: obj proc forall */
/* pick 1 0 */
/******************************/
// void Forall_iterFunction::execute(SLIInterpreter *i) const
// {
// i->EStack().pop();
// ProcedureDatum *proc=
// dynamic_cast<ProcedureDatum *>(i->OStack.top().datum());
// assert(proc !=NULL);
// i->EStack().push(i->baselookup(i->mark_name));
// i->EStack().push_move(i->OStack.pick(1)); // push iterator
// i->EStack().push_move(i->OStack.pick(0)); // push procedure
// i->EStack().push(i->baselookup(i->iforalliter_name));
// i->OStack.pop(2);
// i->inc_call_depth();
// }
/*
BeginDocumentation
Name: forallindexed - Call a procedure for each element of a list/string
Synopsis:
[v1,...,vn] {f} forallindexed -> f(0,v1),...,f(n-1,vn)
Parameters:
[v1,...,vn] - list of n arbitrary objects or string
{f} - function which can operate on the elements of the
array. f is not required to return a specific number
of values.
Description:
For each element of the input array, forallindexed calls f with
two arguments, the element and its index within the list/string.
forallindexed is similar to forall, only that the index of the
element is also passed to the function f.
Alternatives: Functions forallindexed_a for a list, forallindexed_s
for a string (both undocumented) -> behaviour and synopsis are the same.
Examples:
[(a) (b)] {} forallindexed -> (a) 0 (b) 1
Author:
Marc-Oliver Gewaltig
References: The Red Book
SeeAlso: Map, MapIndexed, Table, forall, NestList, FoldList
*/
/******************************/
/* forallindexed_a */
/* call: obj proc forall */
/* pick 1 0 */
/******************************/
void Forallindexed_aFunction::execute(SLIInterpreter *i) const
{
i->require_stack_load(2);
i->require_stack_type(0,sli3::proceduretype);
i->require_stack_type(1,sli3::arraytype);
TokenArray *proc= i->top().data_.array_val;
assert(proc !=0);
i->EStack().push(i->new_token<sli3::marktype>());
i->EStack().push(i->pick(1)); // push object
TokenArray *ad= i->EStack().top().data_.array_val;
assert(ad !=NULL);
i->EStack().push(i->new_token<sli3::integertype>(ad->size())); // push limit
i->EStack().push(i->new_token<sli3::integertype>(0)); // push initial counter
i->EStack().push(i->pick(0)); // push procedure
i->EStack().push(i->baselookup(i->iforallindexedarray_name));
i->inc_call_depth();
i->pop(2);
i->EStack().pop();
}
/******************************/
/* forallindexed_s */
/* call: obj proc forall */
/* pick 1 0 */
/******************************/
void Forallindexed_sFunction::execute(SLIInterpreter *i) const
{
i->require_stack_load(2);
i->require_stack_type(0,sli3::proceduretype);
i->require_stack_type(1,sli3::stringtype);
TokenArray *proc= i->top().data_.array_val;
assert(proc !=0);
i->EStack().push(i->new_token<sli3::marktype>());
i->EStack().push(i->pick(1)); // push object
SLIString *strd= i->EStack().top().data_.string_val;
assert(strd !=NULL);
i->EStack().push(i->new_token<sli3::integertype>(strd->size())); // push limit
i->EStack().push(i->new_token<sli3::integertype>(0)); // push initial counter
i->EStack().push(i->pick(0)); // push procedure
i->EStack().push(i->baselookup(i->iforallindexedstring_name));
i->inc_call_depth();
i->pop(2);
i->EStack().pop();
}
/******************************/
/* forall_s */
/* call: obj proc forall */
/* pick 1 0 */
/******************************/
void Forall_sFunction::execute(SLIInterpreter *i) const
{
i->require_stack_load(2);
i->require_stack_type(0,sli3::proceduretype);
i->require_stack_type(1,sli3::stringtype);
TokenArray *proc= i->top().data_.array_val;
assert(proc !=0);
i->EStack().push(i->new_token<sli3::marktype>());
i->EStack().push(i->pick(1)); // push object
SLIString *strd= i->EStack().top().data_.string_val;
assert(strd !=NULL);
i->EStack().push(i->new_token<sli3::integertype>(strd->size())); // push limit
i->EStack().push(i->new_token<sli3::integertype>(0)); // push initial counter
i->EStack().push(i->pick(0)); // push procedure
i->EStack().push(i->baselookup(i->iforallstring_name));
i->inc_call_depth();
i->pop(2);
i->EStack().pop();
}
/* BeginDocumentation
Name: raiseerror - raise an error to the system
Synopsis:
/command /error raiserror -> /command (side-effects see below!)
Description:
raiseerror is a SLI interface to the interpreter's error
handling mechanism (see The Red Book for details). If an error
is raised, the following actions are performed:
* the value of errordict /newerror is set to true
* the value of errordict /commandname is set to the name of the
command which raised the error
* the name of the command which raised the error is pushed on
the operand stack.
* If the value of errordict /recorstack is true,
the state of the interpreter is saved:
- the operand stack is copied to errordict /ostack
- the execution stack is copied to errordict /estack
- the dictionary stack is copied to errordict /dstack
* stop is called. Stop then tries to find an enclosing stopped
context and calls the associated function.
This mechanism is explained in detail in The PostScript Reference Manual.
Please note that when raiserror is called, the state of the stacks
should be restored to its initial state.
Examples:
/save_sqrt
{
0 gt % is the argument positive ?
{
sqrt
}
{ % else, issue an error
/save_sqrt /PositiveValueExpected raiseerror
} ifelse
} def
Bugs: lets wait...
Author: Gewaltig
Remarks: not part of PostScript, but conform to the mechanism
References: See the Red Book for PostScript's error handling facilities
SeeAlso: raiseagain, stop, stopped, errordict
*/
void RaiseerrorFunction::execute(SLIInterpreter *i) const
{
// pick : 2 1
// call : /cmd /err raiseerror
i->require_stack_load(2);
i->require_stack_type(0,sli3::literaltype);
i->require_stack_type(1,sli3::literaltype);
Name errorname(i->top().data_.name_val);
Name cmdname(i->pick(1).data_.name_val);
i->raiseerror(cmdname, errorname);
}
/* BeginDocumentation
Name: print_error - print an error based on the errordict
Synopsis:
/command print_error -> --
Description:
print_error prints a message describing the content of
the error dictionary. Please see the example below for
information about how to use this function.
Note: The errordict parameters command and message are
reset after a call to print_error. The errorname is not
reset, so every call to print_error will use the same
errorname until this parameter is redefined.
Examples:
errordict /errorname /MyError put_d
errordict /command /my_function put_d
errordict /message (Something went wrong.) put_d
/my_function print_error
Bugs:
Author:
Remarks:
References:
SeeAlso: handleerror, raiseerror, raiseagain, stop, stopped, errordict
*/
void PrinterrorFunction::execute(SLIInterpreter *i) const
{
// The name of the command that raised the error should be
// placed at the top of the OStack.
i->require_stack_load(1);
// Call print_error function.
i->print_error(i->top());
i->pop();
i->EStack().pop();
}
/* BeginDocumentation
Name: raiseagain - re-raise the last error
Synopsis: raiseagain
Description:
raiseagain re-raises a previously raised error. This is useful
if an error handler cannot cope with a particular error (e.g. a signal)
and wants to pass it to an upper level handler. Thus, nestet error handlers
are possible.
Bugs: lets wait...
Author: Gewaltig
Remarks: not part of PostScript
References: See the Red Book for PostScript's error handling facilities
SeeAlso: raiseerror, stop, stopped, errordict
*/
void RaiseagainFunction::execute(SLIInterpreter *i) const
{
i->EStack().pop();
i->raiseagain();
}
/*BeginDocumentation
Name: cycles - return the number of elapsed interpreter cycles
Synopsis: cycles -> n
*/
void CyclesFunction::execute(SLIInterpreter *i) const