-
Notifications
You must be signed in to change notification settings - Fork 5
/
pez.c
7067 lines (6213 loc) · 141 KB
/
pez.c
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
/*
Pez
Main Interpreter and Compiler
See doc/CREDITS for information about the authors.
This program is in the public domain.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#else
#include <pez_sysconfig.h>
#endif
#include <sys/types.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <regex.h>
#include <limits.h>
#include <gc/gc.h>
#include <libtcc.h>
#include <memory.h>
/*
Subpackage configuration. If INDIVIDUALLY is defined, the inclusion
of subpackages is based on whether their compile-time tags are
defined. Otherwise, we automatically enable all the subpackages.
TODO: These are all going away. Some of them to config.h, and the rest to
be made into initialization flags for interpreters (which, of course, depends
on interpreter instances being implemented first).
*/
#ifndef INDIVIDUALLY
#define ARRAY // Array subscripting words
#define BREAK // Asynchronous break facility
#define COMPILERW // Compiler-writing words
#define CONIO // Interactive console I/O
#define DEFFIELDS // Definition field access for words
#define EVALUATE // The EVALUATE primitive
#define FILEIO // File I/O primitives
#define MATH // Math functions
#define MEMMESSAGE // Print message for stack/heap errors
#define PROLOGUE // Prologue processing and auto-init
#define SYSTEM // System command functions
#define FFI // Foreign function interface
#define PROCESS // Process-level facilities
// #define NO_BOUNDS_CHECK // For the stack, heap, return stack, and arrays
#define UNRESTRICTED_POINTERS // Pointers anywhere, not just inside the heap.
#define MATH_CHECK // x/0 errors.
#define COMPILATION_SAFETY // The Compiling macro.
#define TRACE // Execution tracing
#define WALKBACK // Walkback trace
#define WORDSUSED // Logging of words used and unused
#endif // !INDIVIDUALLY
#include "pezdef.h"
#include "st.h"
#ifdef MATH
#include <math.h>
#endif
#ifdef FFI
#include <dlfcn.h>
#endif
// Macro for defining primitives that push constant values:
#define PUSH_CONSTANT(fname, constant) prim fname(pez_instance *p) { So(1);\
Push = (pez_stackitem)constant; }
/* Implicit functions (work for all numeric types). */
#ifdef abs
#undef abs
#endif
#define abs(x) ((x) < 0 ? -(x) : (x))
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) <= (b) ? (a) : (b))
// Because this one may be a little non-obvious, this expression is -1 if the
// argument is less than zero, 0 if it is zero, and 1 if it is greater than
// zero. This version tests a little faster than a clearer one would, mostly
// because it does not branch.
#define unit_scale(a) (((a) > 0) - (((a) < 0)))
/* Data types */
typedef enum { False = 0, True = 1 } Boolean;
#define Truth -1L // Stack value for truth
#define Falsity 0L // Stack value for falsity
/*
Utility definition to get an array's element count (at compile time, and
provided that you're in the same scope as the declaration). For
example:
int arr[] = {1,2,3,4,5};
...
printf("%d", ELEMENTS(arr));
would print a five. ELEMENTS("abc") can also be used to tell how many
bytes are in a string constant INCLUDING THE TRAILING NULL.
*/
#define ELEMENTS(array) (sizeof(array)/sizeof((array)[0]))
#define output_stream p->output_stk[p->output_idx]
#define input_stream p->input_stk[p->input_idx]
#ifdef COPYRIGHT
#ifndef HIGHC
#ifndef lint
static
#endif
#endif
char copyright[] = "PEZ: This program is in the public domain.";
#endif
/*
The following static cells save the compile addresses of words
generated by the compiler. They are looked up immediately after
the dictionary is created. This makes the compiler much faster
since it doesn't have to look up internally-reference words, and,
far more importantly, keeps it from being spoofed if a user redefines
one of the words generated by the compiler.
For the forseeable future, these functions don't need to vary between
instances of Pez, so it's harmless to keep them static for now.
*/
static pez_stackitem s_exit, s_lit, s_flit, s_strlit, s_dotparen,
s_qbranch, s_branch, s_xdo, s_xqdo, s_xloop, s_pxloop, s_abortq;
/* Forward functions */
static void pez_forget_during_eval(pez_instance *p, char token_buffer[]);
static void exword(pez_instance *p, pez_dictword *dw),
trouble(pez_instance *p, char *kind);
#ifdef MATH_CHECK
static void notcomp(pez_instance *p), divzero(pez_instance *p);
#endif
#ifdef WALKBACK
static void pwalkback(pez_instance *p);
#endif
/* ALLOC -- Allocate memory and error upon exhaustion. */
char *alloc(unsigned long size)
{
char *cp = (char *)GC_MALLOC(size);
if(cp == NULL) {
fprintf(stderr, "\n\nOut of memory! %lu bytes requested.\n",
size);
fflush(stderr);
abort();
}
return cp;
}
void *pez_realloc(void *ptr, long size)
{
void *p = GC_REALLOC(ptr, size);
if(!p) {
fprintf(stderr, "\n\nOut of memory! %lu bytes requested.\n",
size);
fflush(stderr);
abort();
}
return p;
}
char *pez_strdup(char *s)
{
char *dup = GC_STRDUP(s);
if(dup == NULL) {
fprintf(stderr, "\n\nOut of memory! Couldn't strdup 0x%lx!\n",
(long)s);
fflush(stderr);
abort();
}
return dup;
}
/*
Returns the concatenation of a and b.
*/
static char *pez_strcat(char *a, char *b)
{
long lena, lenb;
char *s;
lena = strlen((char *)a);
lenb = strlen((char *)b);
s = alloc(lena + lenb + 1);
if(lena)
memcpy(s, (void *)a, lena);
if(lenb)
memcpy(s + lena, (void *)b, lenb);
return s;
}
/*
Escapes honored by Pez:
\0 -> null
\a -> bell
\b -> backspace
\e -> escape
\f -> form feed
\n -> LF
\r -> CR
\t -> tab
\v -> vertical tab
Every other character maps to a literal version of itself when prefixed with
a backslash. Octal bytes (\0NNN) aren't going to be supported. Sorry, kids.
Hex bytes (\xNN) probably will.
TODO: The thing on the above line.
*/
static char string_escape_table[256] = {
0, 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, 0, 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, '\a', '\b', 99,
100, 27, '\f', 103, 104, 105, 106, 107, 108, 109,
'\n', 111, 112, 113, '\r', 115, '\t', 117, '\v', 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,
};
/*
Given the input stream, try to assemble a string
in the token buffer. These strings allow escaped characters.
*/
Boolean get_quoted_string(pez_instance *p, char **strbuf, char token_buffer[])
{
Boolean valid_string = True;
int toklen = 0;
char *sp = *strbuf;
char *tp = token_buffer;
sp++;
while(True) {
unsigned char c = *sp++;
if(c == '"') {
sp++;
*tp++ = 0;
break;
} else if(!c) {
valid_string = False;
*tp++ = 0;
break;
}
if(c == '\\') {
c = *sp++;
if(!c) {
valid_string = False;
break;
}
c = string_escape_table[c];
}
if(toklen < TOK_BUF_SZ - 1) {
*tp++ = c;
toklen++;
} else {
valid_string = False;
}
}
*strbuf = sp;
if(!valid_string) {
#ifdef MEMMESSAGE
fprintf(stderr, "\nRunaway string: %s\n", token_buffer);
#endif
p->evalstat = PEZ_RUNSTRING;
}
return valid_string;
}
/*
Given the input stream and an opening delimiter, determine
the appropriate closing delimiter and try to assemble a string
in the token buffer.
These strings don't give no never mind about no escapes
*/
Boolean get_delimited_string(pez_instance *p, char **strbuf,
char token_buffer[])
{
Boolean valid_string = True;
int toklen = 0;
char *sp = *strbuf;
char *tp = token_buffer;
char close_delim;
sp++;
switch (*sp) {
case '{' : close_delim = '}'; break;
case '(' : close_delim = ')'; break;
case '[' : close_delim = ']'; break;
case '<' : close_delim = '>'; break;
default : close_delim = *sp; break;
}
sp++;
while(True) {
char c = *sp++;
if(c == close_delim) {
sp++;
*tp++ = 0;
break;
} else if(!c) {
valid_string = False;
*tp++ = 0;
break;
}
if(toklen < TOK_BUF_SZ - 1) {
*tp++ = c;
toklen++;
} else {
valid_string = False;
}
}
*strbuf = sp;
if(!valid_string) {
#ifdef MEMMESSAGE
fprintf(stderr, "\nRunaway string: %s\n", token_buffer);
#endif
p->evalstat = PEZ_RUNSTRING;
}
return valid_string;
}
/*
Scan a token from the input stream and return its type.
It works something like this:
- If the last input string left open an inline comment, try to
close it. Failing that, pass the buck by returning TokNull.
- We're not in a comment, so drive right on by any whitespace.
- See if a string is about to happen. This is signified by
either a double quote or the backslash char. A backslash causes
the very next char to be used as the string delimiter, with
support for the usual paired delimiters.
"I am string." \{ Hear me roar.} \/LA LA LA/ puts puts puts
- If not a string, scan on until whitespace or string end.
- Next, we have to decide what to do with the token. It might
be a comment opener, either rest-of-line or open-close flavor.
- The token might be a number, as signified by a digit or minus
sign for its first char. Try to sscanf it to a TokInt or a
TokReal.
- If not otherwise identified, we have a word.
*/
static int lex(pez_instance *p, char **cp, char token_buffer[])
{
char *scanp = *cp;
while(True) {
char *tp = token_buffer;
int toklen = 0;
// handle rudely interrupted comments
if(p->comment) {
while(*scanp != ')') {
if(*scanp == 0) {
*cp = scanp;
return TokNull;
}
scanp++;
}
scanp++;
p->comment = Falsity;
}
while(isspace(*scanp)) // Say NO to leading blanks
scanp++;
if(*scanp == '"') {
Boolean valid_string =
get_quoted_string(p, &scanp, token_buffer);
*cp = --scanp;
return valid_string ? TokString : TokNull;
} else if(*scanp == '\\') { // Arbitrary string delimitation
Boolean valid_string =
get_delimited_string(p, &scanp, token_buffer);
*cp = --scanp;
return valid_string ? TokString : TokNull;
} else {
// Scan the next raw token
while(True) {
char c = *scanp++;
if(!c || isspace(c)) {
*tp++ = 0;
break;
}
if(toklen < TOK_BUF_SZ - 1) {
*tp++ = c;
toklen++;
}
}
}
*cp = --scanp;
if(!token_buffer[0])
return TokNull;
/* If token is a comment to end of line character, discard
* the rest of the line and return null for this token
* request. */
if(strcmp(token_buffer, "#") == 0 ||
strcmp(token_buffer, "#!") == 0) {
while(*scanp)
scanp++;
*cp = scanp;
return TokNull;
}
/* If this token is a comment open delimiter, set to
ignore all characters until the matching comment close
delimiter. */
if(strcmp(token_buffer, "(") == 0) {
while(*scanp) {
if(*scanp == ')')
break;
scanp++;
}
if(*scanp == ')') {
scanp++;
continue;
}
p->comment = Truth;
*cp = scanp;
return TokNull;
}
/* See if the token is a number. */
if(isdigit(token_buffer[0]) || token_buffer[0] == '-') {
char tc;
char *tcp;
#ifdef USE_SSCANF
if(sscanf(token_buffer, "%li%c", &tokint, &tc) == 1)
return TokInt;
#else
p->tokint = strtoul(token_buffer, &tcp, 0);
if(*tcp == 0) {
return TokInt;
}
#endif
if(sscanf(token_buffer, "%lf%c",
&p->tokreal, &tc) == 1) {
return TokReal;
}
}
return TokWord;
}
}
/* LOOKUP -- Look up token in the dictionary. */
static pez_dictword *lookup(pez_instance *p, char *tkname)
{
pez_dictword *dw = p->dict;
while(dw != NULL) {
if(!(dw->wname[0] & WORDHIDDEN) &&
(strcasecmp(dw->wname + 1, tkname) == 0)) {
#ifdef WORDSUSED
*(dw->wname) |= WORDUSED; // Mark this word used
#endif
break;
}
dw = dw->wnext;
}
return dw;
}
static int is_word(pez_instance *p, pez_dictword *d)
{
pez_dictword *c = p->dict;
while(c) {
if(c == d)
return 1;
c = c->wnext;
}
return 0;
}
/* Gag me with a spoon! Does no compiler but Turbo support
#if defined(x) || defined(y) ?? */
#ifdef EXPORT
#define FgetspNeeded
#endif
#ifdef FILEIO
#ifndef FgetspNeeded
#define FgetspNeeded
#endif
#endif
#ifdef FgetspNeeded
/* Portable database version of FGETS. This reads the next line into a buffer
* a la fgets(). A line is delimited by either a carriage return or a line
* feed, optionally followed by the other character of the pair. The string is
* always null terminated, and limited to the length specified - 1 (excess
* characters on the line are discarded. The string is returned, or NULL if
* end of file is encountered and no characters were stored. No end of line
* character is stored in the string buffer.
*/
Exported char *pez_fgetsp(pez_instance *p, char *s, int n, FILE *stream)
{
int i = 0, ch;
while(True) {
ch = getc(stream);
if(ch == EOF) {
if(i == 0)
return NULL;
break;
}
if(ch == '\r') {
ch = getc(stream);
if(ch != '\n')
ungetc(ch, stream);
break;
}
if(ch == '\n') {
ch = getc(stream);
if(ch != '\r')
ungetc(ch, stream);
break;
}
if(i < (n - 1))
s[i++] = ch;
}
s[i] = 0;
return s;
}
#endif // FgetspNeeded
/* PEZ_MEMSTAT -- Print memory usage summary. */
#ifdef MEMSTAT
void pez_memstat(pez_instance *p)
{
static char fmt[] = " %-12s %6ld\t%6ld\t%6ld\t%3ld\n";
printf("\n\t\t\t Memory Usage Summary\n\n"
"\t\t\t\t Current Maximum\tItems\t Percent\n"
" Memory Area\t usage\t used\tallocated in use \n");
printf(fmt, "Stack",
((long)(p->stk - p->stack)),
((long)(p->stackmax - p->stack)),
p->stklen, (100L * (p->stk - p->stack)) / p->stklen);
printf(fmt, "Float stack",
((long)(p->fstk - p->fstack)),
((long)(p->fstackmax - p->fstack)),
p->fstklen, (100L * (p->fstk - p->fstack)) / p->fstklen);
printf(fmt, "Return stack",
((long)(p->rstk - p->rstack)),
((long)(p->rstackmax - p->rstack)),
p->rstklen, (100L * (p->rstk - p->rstack)) / p->rstklen);
printf(fmt, "Heap",
((long)(p->hptr - p->heap)),
((long)(p->heapmax - p->heap)),
p->heaplen, (100L * (p->hptr - p->heap)) / p->heaplen);
fflush(stdout);
}
#endif // MEMSTAT
/* Primitive implementing functions. */
/* ENTER -- Enter word in dictionary. */
static void enter(pez_instance *p, char *tkname)
{
if(p->redef && (lookup(p, tkname) != NULL))
fprintf(stderr, "\n%s isn't unique.\n", tkname);
// Allocate name buffer
p->createword->wname = alloc(((unsigned int)strlen(tkname) + 2));
p->createword->wname[0] = 0; // Clear flags
strcpy(p->createword->wname + 1, tkname);// Copy token to name buffer
p->createword->wnext = p->dict; // Chain rest of dictionary to word
p->dict = p->createword; // Put word at head of dictionary
}
#ifdef Keyhit
/* KBQUIT -- If this system allows detecting key presses, handle
the pause, resume, and quit protocol for the word
listing facilities. */
static Boolean kbquit()
{
int key;
if((key = Keyhit()) != 0) {
printf("\nPress RETURN to stop, any other key to continue: ");
while((key = Keyhit()) == 0);
if(key == '\r' || (key == '\n'))
return True;
}
return False;
}
#endif // Keyhit
/*
Runs a regex on a string, returning 1 for a match, 0 otherwise, and filling
in all the $n variables, as well as the pre- and post-matches. For use by
rmatch, rsub, rgsub, etc. 'flags' is passed directly as 'eflags' to
regexec(3).
*/
static int regex_match(pez_instance *p, regex_t *rx, char *str, int flags)
{
int match;
int len = strlen(str);
match = !regexec(rx, str, MAX_REGEX_MATCHES, p->regex_matches, flags);
if(match) {
p->regex_prematch[0] = 0;
p->regex_prematch[1] = p->regex_matches[0].rm_so;
p->regex_postmatch[0] = p->regex_matches[0].rm_eo;
p->regex_postmatch[1] = len - p->regex_matches[0].rm_eo;
return 1;
} else {
p->regex_prematch[0] = p->regex_postmatch[0] = -1;
p->regex_prematch[1] = p->regex_postmatch[1] = 0;
return 0;
}
}
/* Primitive word definitions. */
#ifndef COMPILATION_SAFETY
#define Compiling
#else
#define Compiling if (state == Falsity) {notcomp(p); return;}
#endif
#define Compconst(x) Ho(1); Hstore = (pez_stackitem)(x)
#define Skipstring p->ip += *((char *)p->ip)
/*
( a b -- a+b )
Adds the two numbers at the top of the stack.
*/
prim P_plus(pez_instance *p)
{
Sl(2);
S1 += S0;
Pop;
}
/*
( a b -- a-b )
Subtracts the number at the top of the stack from the next number on the
stack.
*/
prim P_minus(pez_instance *p)
{
Sl(2);
S1 -= S0;
Pop;
}
/*
( a b -- a*b )
Multiplies the two numbers at the top of the stack.
*/
prim P_mul(pez_instance *p)
{
Sl(2);
S1 *= S0;
Pop;
}
/*
( a b -- a/b )
Divides the second number on the stack by the first.
*/
prim P_div(pez_instance *p)
{
Sl(2);
#ifdef MATH_CHECK
if(S0 == 0) {
divzero(p);
return;
}
#endif
S1 /= S0;
Pop;
}
/*
( a b -- a%b )
The remainder of the second number on the stack divided by the first.
*/
prim P_mod(pez_instance *p)
{
Sl(2);
#ifdef MATH_CHECK
if(S0 == 0) {
divzero(p);
return;
}
#endif
S1 %= S0;
Pop;
}
/*
( a b -- a/b a%b )
Divides the second number on the stack by the first, returning the quotient
and the remainder.
*/
prim P_divmod(pez_instance *p)
{
pez_stackitem quot;
Sl(2);
#ifdef MATH_CHECK
if(S0 == 0) {
divzero(p);
return;
}
#endif
quot = S1 / S0;
S1 %= S0;
S0 = quot;
}
/*
( a b -- min )
Returns the minimum of the top two numbers on the stack.
*/
prim P_min(pez_instance *p)
{
Sl(2);
S1 = min(S1, S0);
Pop;
}
/*
( a b -- max )
Returns the maximum of the top two numbers on the stack.
*/
prim P_max(pez_instance *p)
{
Sl(2);
S1 = max(S1, S0);
Pop;
}
/*
( a -- -a )
Negates the top number on the stack.
*/
prim P_neg(pez_instance *p)
{
Sl(1);
S0 = -S0;
}
/*
( a -- abs(a) )
Returns the absolute value of the top number on the stack.
*/
prim P_abs(pez_instance *p)
{
Sl(1);
S0 = abs(S0);
}
/*
( a b -- a=b )
Tests the top two numbers on the stack for equality.
*/
prim P_equal(pez_instance *p)
{
Sl(2);
S1 = -(S1 == S0);
Pop;
}
/*
( a b -- a<>b )
Returns true iff the top two numbers on the stack are unequal.
*/
prim P_unequal(pez_instance *p)
{
Sl(2);
S1 = -(S1 != S0);
Pop;
}
/*
( a b -- a>b )
*/
prim P_gtr(pez_instance *p)
{
Sl(2);
S1 = -(S1 > S0);
Pop;
}
/*
( a b -- a<b )
*/
prim P_lss(pez_instance *p)
{
Sl(2);
S1 = -(S1 < S0);
Pop;
}
/*
( a b -- a>=b )
*/
prim P_geq(pez_instance *p)
{
Sl(2);
S1 = -(S1 >= S0);
Pop;
}
/*
( a b -- a<=b )
*/
prim P_leq(pez_instance *p)
{
Sl(2);
S1 = -(S1 <= S0);
Pop;
}
/*
( a b -- a&b )
Returns the bitwise AND of the top two elements on the stack.
*/
prim P_and(pez_instance *p)
{
Sl(2);
S1 &= S0;
Pop;
}
/*
( a b -- a|b )
Bitwise OR of the top two elements on the stack.
*/
prim P_or(pez_instance *p)
{
Sl(2);
S1 |= S0;
Pop;
}
/*
( a b -- a^b )
Bitwise XOR of the top two elements on the stack.
*/
prim P_xor(pez_instance *p)
{
Sl(2);
S1 ^= S0;
Pop;
}
/*
( a -- ~a )
Bitwise negation of the top element on the stack.
*/
prim P_not(pez_instance *p)
{
Sl(1);
S0 = ~S0;
}
/*
( a b -- a<<b )
Arithmetic shift left of the second item on the stack by number of bits in
the top item. Shifts right if the top item is negative.
*/
prim P_shift(pez_instance *p)
{
Sl(1);
S1 = (S0 < 0) ? (((unsigned long)S1) >> (-S0)) :
(((unsigned long)S1) << S0);
Pop;
}
/*
( -- random! )
Pushes a random number onto the stack.
*/
prim P_rand(pez_instance *p)
{
Push = rand();
if(sizeof(int) < sizeof(pez_stackitem))
S0 |= (pez_stackitem)rand() << (sizeof(int) * 8);
}
prim P_1plus(pez_instance *p)
{ /* Add one */
Sl(1);
S0++;
}
prim P_2plus(pez_instance *p)
{ /* Add two */
Sl(1);
S0 += 2;
}
prim P_1minus(pez_instance *p)
{ /* Subtract one */
Sl(1);
S0--;
}
prim P_2minus(pez_instance *p)
{ /* Subtract two */
Sl(1);
S0 -= 2;
}
prim P_2mul(pez_instance *p)
{ /* Multiply by two */
Sl(1);
S0 *= 2;
}
prim P_2div(pez_instance *p)
{ /* Divide by two */
Sl(1);
S0 /= 2;
}
prim P_0equal(pez_instance *p)
{ /* Equal to zero ? */
Sl(1);
S0 = -(S0 == 0);
}
prim P_0notequal(pez_instance *p)
{ /* Not equal to zero ? */
Sl(1);
S0 = -(S0 != 0);
}
prim P_0gtr(pez_instance *p)
{ /* Greater than zero ? */
Sl(1);
S0 = -(S0 > 0);
}
prim P_0lss(pez_instance *p)
{ /* Less than zero ? */
Sl(1);
S0 = -(S0 < 0);
}
/* Storage allocation (mostly heap) primitives */
/*
( n -- addr )
Allocate n bytes of garbage-collected memory.