-
Notifications
You must be signed in to change notification settings - Fork 8
/
atlast.c
3776 lines (3252 loc) · 85.7 KB
/
atlast.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
/*
A T L A S T
Autodesk Threaded Language Application System Toolkit
Main Interpreter and Compiler
Designed and implemented in January of 1990 by John Walker.
This program is in the public domain.
*/
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef ALIGNMENT
#include <memory.h>
#endif
/* Custom configuration. If the tag CUSTOM has been defined (usually on
the compiler call line), we include the file "atlcfig.h", which may
then define INDIVIDUALLY and select the subpackages needed for its
application. */
#ifdef CUSTOM
#include "atlcfig.h"
#endif
/* 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. */
#ifndef INDIVIDUALLY
/* #define ANSIFLOAT */ /* Synonyms for ANSI FORTH-94 floating point */
#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 DOUBLE /* Double word primitives (2DUP) */
#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 REAL /* Floating point numbers */
#define SHORTCUTA /* Shortcut integer arithmetic words */
#define SHORTCUTC /* Shortcut integer comparison */
#define STRING /* String functions */
#define SYSTEM /* System command function */
#ifndef NOMEMCHECK
#define TRACE /* Execution tracing */
#define WALKBACK /* Walkback trace */
#define WORDSUSED /* Logging of words used and unused */
#endif /* NOMEMCHECK */
#endif /* !INDIVIDUALLY */
#include "atldef.h"
#ifdef MATH
#include <math.h>
#endif
/* LINTLIBRARY */
/* 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))
/* Globals imported */
/* Data types */
typedef enum {False = 0, True = 1} Boolean;
#define EOS '\0' /* End of string character */
#define V (void) /* Force result to void */
#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). 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]))
/* Black art definition used to verify sizes of types at compile time */
#define MEMORY_MODEL_ERROR(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
/* Globals visible to calling programs */
atl_int atl_stklen = 100; /* Evaluation stack length */
atl_int atl_rstklen = 100; /* Return stack length */
atl_int atl_heaplen = 2000; /* Heap length */
atl_int atl_ltempstr = 256; /* Temporary string buffer length */
atl_int atl_ntempstr = 4; /* Number of temporary string buffers */
atl_int atl_trace = Falsity; /* Tracing if true */
atl_int atl_walkback = Truth; /* Walkback enabled if true */
atl_int atl_comment = Falsity; /* Currently ignoring a comment */
atl_int atl_redef = Truth; /* Allow redefinition without issuing
the "not unique" message. */
atl_int atl_errline = 0; /* Line where last atl_load failed */
/* Local variables */
/* The evaluation stack */
Exported stackitem *stack = NULL; /* Evaluation stack */
Exported stackitem *stk; /* Stack pointer */
Exported stackitem *stackbot; /* Stack bottom */
Exported stackitem *stacktop; /* Stack top */
/* The return stack */
Exported dictword ***rstack = NULL; /* Return stack */
Exported dictword ***rstk; /* Return stack pointer */
Exported dictword ***rstackbot; /* Return stack bottom */
Exported dictword ***rstacktop; /* Return stack top */
/* The heap */
Exported stackitem *heap = NULL; /* Allocation heap */
Exported stackitem *hptr; /* Heap allocation pointer */
Exported stackitem *heapbot; /* Bottom of heap (temp string buffer) */
Exported stackitem *heaptop; /* Top of heap */
/* The dictionary */
Exported dictword *dict = NULL; /* Dictionary chain head */
Exported dictword *dictprot = NULL; /* First protected item in dictionary */
/* The temporary string buffers */
Exported char **strbuf = NULL; /* Table of pointers to temp strings */
Exported int cstrbuf = 0; /* Current temp string */
/* The walkback trace stack */
#ifdef WALKBACK
static dictword **wback = NULL; /* Walkback trace buffer */
static dictword **wbptr; /* Walkback trace pointer */
#endif /* WALKBACK */
#ifdef MEMSTAT
Exported stackitem *stackmax; /* Stack maximum excursion */
Exported dictword ***rstackmax; /* Return stack maximum excursion */
Exported stackitem *heapmax; /* Heap maximum excursion */
#endif
#ifdef FILEIO
static char *fopenmodes[] = {
#ifdef FBmode
#define FMspecial
/* Fopen() mode table for systems that require a "b" in the
mode string for binary files. */
"", "r", "", "r+",
"", "rb", "", "r+b",
"", "", "w", "w+",
"", "", "wb", "w+b"
#endif
#ifndef FMspecial
/* Default fopen() mode table for SVID-compatible systems not
overridden by a special table above. */
"", "r", "", "r+",
"", "r", "", "r+",
"", "", "w", "w+",
"", "", "w", "w+"
#endif
};
#endif /* FILEIO */
static char tokbuf[128]; /* Token buffer */
static char *instream = NULL; /* Current input stream line */
static long tokint; /* Scanned integer */
#ifdef REAL
static atl_real tokreal; /* Scanned real number */
#ifdef ALIGNMENT
Exported atl_real rbuf0, rbuf1, rbuf2; /* Real temporary buffers */
#endif
#endif
static long base = 10; /* Number base */
Exported dictword **ip = NULL; /* Instruction pointer */
Exported dictword *curword = NULL; /* Current word being executed */
static int evalstat = ATL_SNORM; /* Evaluator status */
static Boolean defpend = False; /* Token definition pending */
static Boolean forgetpend = False; /* Forget pending */
static Boolean tickpend = False; /* Take address of next word */
static Boolean ctickpend = False; /* Compile-time tick ['] pending */
static Boolean cbrackpend = False; /* [COMPILE] pending */
Exported dictword *createword = NULL; /* Address of word pending creation */
static Boolean stringlit = False; /* String literal anticipated */
#ifdef BREAK
static Boolean broken = False; /* Asynchronous break received */
#endif
#ifdef COPYRIGHT
#ifndef lint
static
#endif
char copyright[] = "ATLAST: 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-referenced words, and,
far more importantly, keeps it from being spoofed if a user redefines
one of the words generated by the compiler. */
static 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 exword(), trouble();
#ifndef NOMEMCHECK
STATIC void notcomp(), divzero();
#endif
#ifdef WALKBACK
STATIC void pwalkback();
#endif
/* ALLOC -- Allocate memory and error upon exhaustion. */
static char *alloc(size)
unsigned int size;
{
char *cp = malloc(size);
/* printf("\nAlloc %u", size); */
if (cp == NULL) {
V fprintf(stderr, "\n\nOut of memory! %u bytes requested.\n", size);
abort();
}
return cp;
}
/* UCASE -- Force letters in string to upper case. */
static void ucase(c)
char *c;
{
char ch;
while ((ch = *c) != EOS) {
if (islower(ch))
*c = toupper(ch);
c++;
}
}
/* TOKEN -- Scan a token and return its type. */
static int token(cp)
char **cp;
{
char *sp = *cp;
while (True) {
char *tp = tokbuf;
int tl = 0;
Boolean istring = False, rstring = False;
if (atl_comment) {
while (*sp != ')') {
if (*sp == EOS) {
*cp = sp;
return TokNull;
}
sp++;
}
sp++;
atl_comment = Falsity;
}
while (isspace(*sp)) /* Skip leading blanks */
sp++;
if (*sp == '"') { /* Is this a string ? */
/* Assemble string token. */
sp++;
while (True) {
char c = *sp++;
if (c == '"') {
sp++;
*tp++ = EOS;
break;
} else if (c == EOS) {
rstring = True;
*tp++ = EOS;
break;
}
if (c == '\\') {
c = *sp++;
if (c == EOS) {
rstring = True;
break;
}
switch (c) {
case 'b':
c = '\b';
break;
case 'n':
c = '\n';
break;
case 'r':
c = '\r';
break;
case 't':
c = '\t';
break;
default:
break;
}
}
if (tl < (sizeof tokbuf) - 1) {
*tp++ = c;
tl++;
} else {
rstring = True;
}
}
istring = True;
} else {
/* Scan the next raw token */
while (True) {
char c = *sp++;
if (c == EOS || isspace(c)) {
*tp++ = EOS;
break;
}
if (tl < (sizeof tokbuf) - 1) {
*tp++ = c;
tl++;
}
}
}
*cp = --sp; /* Store end of scan pointer */
if (istring) {
if (rstring) {
#ifdef MEMMESSAGE
V printf("\nRunaway string: %s\n", tokbuf);
#endif
evalstat = ATL_RUNSTRING;
return TokNull;
}
return TokString;
}
if (tokbuf[0] == EOS)
return TokNull;
/* See if token is a comment to end of line character. If so, discard
the rest of the line and return null for this token request. */
if (strcmp(tokbuf, "\\") == 0) {
while (*sp != EOS)
sp++;
*cp = sp;
return TokNull;
}
/* See if this token is a comment open delimiter. If so, set to
ignore all characters until the matching comment close delimiter. */
if (strcmp(tokbuf, "(") == 0) {
while (*sp != EOS) {
if (*sp == ')')
break;
sp++;
}
if (*sp == ')') {
sp++;
continue;
}
atl_comment = Truth;
*cp = sp;
return TokNull;
}
/* See if the token is a number. */
if (isdigit(tokbuf[0]) || tokbuf[0] == '-') {
char tc;
char *tcp;
#ifdef USE_SSCANF
if (sscanf(tokbuf, "%li%c", &tokint, &tc) == 1)
return TokInt;
#else
tokint = strtoul(tokbuf, &tcp, 0);
if (*tcp == 0) {
return TokInt;
}
#endif
#ifdef REAL
if (sscanf(tokbuf, "%lf%c", &tokreal, &tc) == 1)
return TokReal;
#endif
}
return TokWord;
}
}
/* LOOKUP -- Look up token in the dictionary. */
static dictword *lookup(tkname)
char *tkname;
{
dictword *dw = dict;
ucase(tkname); /* Force name to upper case */
while (dw != NULL) {
if (!(dw->wname[0] & WORDHIDDEN) &&
(strcmp(dw->wname + 1, tkname) == 0)) {
#ifdef WORDSUSED
*(dw->wname) |= WORDUSED; /* Mark this word used */
#endif
break;
}
dw = dw->wnext;
}
return dw;
}
#ifdef EXPORT
#define FgetspNeeded
#endif
#ifdef FILEIO
#ifndef FgetspNeeded
#define FgetspNeeded
#endif
#endif
#ifdef FgetspNeeded
/* ATL_FGETSP -- 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 *atl_fgetsp(s, n, stream)
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')
V ungetc(ch, stream);
break;
}
if (ch == '\n') {
ch = getc(stream);
if (ch != '\r')
V ungetc(ch, stream);
break;
}
if (i < (n - 1))
s[i++] = ch;
}
s[i] = EOS;
return s;
}
#endif /* FgetspNeeded */
/* ATL_MEMSTAT -- Print memory usage summary. */
#ifdef MEMSTAT
void atl_memstat()
{
static char fmt[] = " %-12s %6ld %6ld %6ld %3ld\n";
V printf("\n Memory Usage Summary\n\n");
V printf(" Current Maximum Items Percent\n");
V printf(" Memory Area usage used allocated in use \n");
V printf(fmt, "Stack",
((long) (stk - stack)),
((long) (stackmax - stack)),
atl_stklen,
(100L * (stk - stack)) / atl_stklen);
V printf(fmt, "Return stack",
((long) (rstk - rstack)),
((long) (rstackmax - rstack)),
atl_rstklen,
(100L * (rstk - rstack)) / atl_rstklen);
V printf(fmt, "Heap",
((long) (hptr - heap)),
((long) (heapmax - heap)),
atl_heaplen,
(100L * (hptr - heap)) / atl_heaplen);
}
#endif /* MEMSTAT */
/* Primitive implementing functions. */
/* ENTER -- Enter word in dictionary. Given token for word's
name and initial values for its attributes, returns
the newly-allocated dictionary item. */
static void enter(tkname)
char *tkname;
{
/* Allocate name buffer */
createword->wname = alloc(((unsigned int) strlen(tkname) + 2));
createword->wname[0] = 0; /* Clear flags */
V strcpy(createword->wname + 1, tkname); /* Copy token to name buffer */
createword->wnext = dict; /* Chain rest of dictionary to word */
dict = 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) {
V 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 */
/* Primitive word definitions. */
#ifdef NOMEMCHECK
#define Compiling
#else
#define Compiling if (state == Falsity) {notcomp(); return;}
#endif
#define Compconst(x) Ho(1); Hstore = (stackitem) (x)
#define Skipstring ip += *((char *) ip)
prim P_plus() /* Add two numbers */
{
Sl(2);
/* printf("PLUS %lx + %lx = %lx\n", S1, S0, (S1 + S0)); */
S1 += S0;
Pop;
}
prim P_minus() /* Subtract two numbers */
{
Sl(2);
S1 -= S0;
Pop;
}
prim P_times() /* Multiply two numbers */
{
Sl(2);
S1 *= S0;
Pop;
}
prim P_div() /* Divide two numbers */
{
Sl(2);
#ifndef NOMEMCHECK
if (S0 == 0) {
divzero();
return;
}
#endif /* NOMEMCHECK */
S1 /= S0;
Pop;
}
prim P_mod() /* Take remainder */
{
Sl(2);
#ifndef NOMEMCHECK
if (S0 == 0) {
divzero();
return;
}
#endif /* NOMEMCHECK */
S1 %= S0;
Pop;
}
prim P_divmod() /* Compute quotient and remainder */
{
stackitem quot;
Sl(2);
#ifndef NOMEMCHECK
if (S0 == 0) {
divzero();
return;
}
#endif /* NOMEMCHECK */
quot = S1 / S0;
S1 %= S0;
S0 = quot;
}
prim P_min() /* Take minimum of stack top */
{
Sl(2);
S1 = min(S1, S0);
Pop;
}
prim P_max() /* Take maximum of stack top */
{
Sl(2);
S1 = max(S1, S0);
Pop;
}
prim P_neg() /* Negate top of stack */
{
Sl(1);
S0 = - S0;
}
prim P_abs() /* Take absolute value of top of stack */
{
Sl(1);
S0 = abs(S0);
}
prim P_equal() /* Test equality */
{
Sl(2);
S1 = (S1 == S0) ? Truth : Falsity;
Pop;
}
prim P_unequal() /* Test inequality */
{
Sl(2);
S1 = (S1 != S0) ? Truth : Falsity;
Pop;
}
prim P_gtr() /* Test greater than */
{
Sl(2);
S1 = (S1 > S0) ? Truth : Falsity;
Pop;
}
prim P_lss() /* Test less than */
{
Sl(2);
S1 = (S1 < S0) ? Truth : Falsity;
Pop;
}
prim P_geq() /* Test greater than or equal */
{
Sl(2);
S1 = (S1 >= S0) ? Truth : Falsity;
Pop;
}
prim P_leq() /* Test less than or equal */
{
Sl(2);
S1 = (S1 <= S0) ? Truth : Falsity;
Pop;
}
prim P_and() /* Logical and */
{
Sl(2);
/* printf("AND %lx & %lx = %lx\n", S1, S0, (S1 & S0)); */
S1 &= S0;
Pop;
}
prim P_or() /* Logical or */
{
Sl(2);
S1 |= S0;
Pop;
}
prim P_xor() /* Logical xor */
{
Sl(2);
S1 ^= S0;
Pop;
}
prim P_not() /* Logical negation */
{
Sl(1);
S0 = ~S0;
}
prim P_shift() /* Shift: value nbits -- value */
{
Sl(1);
S1 = (S0 < 0) ? (((unsigned long) S1) >> (-S0)) :
(((unsigned long) S1) << S0);
Pop;
}
#ifdef SHORTCUTA
prim P_1plus() /* Add one */
{
Sl(1);
S0++;
}
prim P_2plus() /* Add two */
{
Sl(1);
S0 += 2;
}
prim P_1minus() /* Subtract one */
{
Sl(1);
S0--;
}
prim P_2minus() /* Subtract two */
{
Sl(1);
S0 -= 2;
}
prim P_2times() /* Multiply by two */
{
Sl(1);
S0 *= 2;
}
prim P_2div() /* Divide by two */
{
Sl(1);
S0 /= 2;
}
#endif /* SHORTCUTA */
#ifdef SHORTCUTC
prim P_0equal() /* Equal to zero ? */
{
Sl(1);
S0 = (S0 == 0) ? Truth : Falsity;
}
prim P_0notequal() /* Not equal to zero ? */
{
Sl(1);
S0 = (S0 != 0) ? Truth : Falsity;
}
prim P_0gtr() /* Greater than zero ? */
{
Sl(1);
S0 = (S0 > 0) ? Truth : Falsity;
}
prim P_0lss() /* Less than zero ? */
{
Sl(1);
S0 = (S0 < 0) ? Truth : Falsity;
}
#endif /* SHORTCUTC */
/* Storage allocation (heap) primitives */
prim P_here() /* Push current heap address */
{
So(1);
Push = (stackitem) hptr;
}
prim P_bang() /* Store value into address */
{
Sl(2);
Hpc(S0);
*((stackitem *) S0) = S1;
Pop2;
}
prim P_at() /* Fetch value from address */
{
Sl(1);
Hpc(S0);
S0 = *((stackitem *) S0);
}
prim P_plusbang() /* Add value at specified address */
{
Sl(2);
Hpc(S0);
*((stackitem *) S0) += S1;
Pop2;
}
prim P_allot() /* Allocate heap bytes */
{
stackitem n;
Sl(1);
n = (S0 + (sizeof(stackitem) - 1)) / sizeof(stackitem);
Pop;
Ho(n);
hptr += n;
}
prim P_comma() /* Store one item on heap */
{
Sl(1);
Ho(1);
Hstore = S0;
Pop;
}
prim P_cbang() /* Store byte value into address */
{
Sl(2);
Hpc(S0);
*((unsigned char *) S0) = S1;
Pop2;
}
prim P_cat() /* Fetch byte value from address */
{
Sl(1);
Hpc(S0);
S0 = *((unsigned char *) S0);
}
prim P_ccomma() /* Store one byte on heap */
{
unsigned char *chp;
Sl(1);
Ho(1);
chp = ((unsigned char *) hptr);
*chp++ = S0;
hptr = (stackitem *) chp;
Pop;
}
prim P_cequal() /* Align heap pointer after storing */
{ /* a series of bytes. */
stackitem n = (((stackitem) hptr) - ((stackitem) heap)) %
(sizeof(stackitem));
if (n != 0) {
char *chp = ((char *) hptr);
chp += sizeof(stackitem) - n;
hptr = ((stackitem *) chp);
}
}
/* Variable and constant primitives */
prim P_var() /* Push body address of current word */
{
So(1);
Push = (stackitem) (((stackitem *) curword) + Dictwordl);
}
Exported void P_create() /* Create new word */
{
defpend = True; /* Set definition pending */
Ho(Dictwordl);
createword = (dictword *) hptr; /* Develop address of word */
createword->wname = NULL; /* Clear pointer to name string */
createword->wcode = P_var; /* Store default code */
hptr += Dictwordl; /* Allocate heap space for word */
}
prim P_forget() /* Forget word */
{
forgetpend = True; /* Mark forget pending */
}
prim P_variable() /* Declare variable */
{
P_create(); /* Create dictionary item */
Ho(1);
Hstore = 0; /* Initial value = 0 */
}
prim P_con() /* Push value in body */
{
So(1);
Push = *(((stackitem *) curword) + Dictwordl);
}
prim P_constant() /* Declare constant */
{
Sl(1);
P_create(); /* Create dictionary item */
createword->wcode = P_con; /* Set code to constant push */
Ho(1);
Hstore = S0; /* Store constant value in body */
Pop;
}
/* Array primitives */
#ifdef ARRAY
prim P_arraysub() /* Array subscript calculation */
{ /* sub1 sub2 ... subn -- addr */
int i, offset, esize, nsubs;
stackitem *array;
stackitem *isp;
Sl(1);
array = (((stackitem *) curword) + Dictwordl);
Hpc(array);
nsubs = *array++; /* Load number of subscripts */
esize = *array++; /* Load element size */
#ifndef NOMEMCHECK
isp = &S0;
for (i = 0; i < nsubs; i++) {
stackitem subn = *isp--;
if (subn < 0 || subn >= array[i])
trouble("Subscript out of range");
}
#endif /* NOMEMCHECK */
isp = &S0;
offset = *isp; /* Load initial offset */
for (i = 1; i < nsubs; i++)
offset = (offset * (*(++array))) + *(--isp);
Npop(nsubs - 1);
/* Calculate subscripted address. We start at the current word,
advance to the body, skip two more words for the subscript count
and the fundamental element size, then skip the subscript bounds
words (as many as there are subscripts). Then, finally, we
can add the calculated offset into the array. */
S0 = (stackitem) (((char *) (((stackitem *) curword) +
Dictwordl + 2 + nsubs)) + (esize * offset));
}
prim P_array() /* Declare array */
{ /* sub1 sub2 ... subn n esize -- array */
int i, nsubs, asize = 1;