-
Notifications
You must be signed in to change notification settings - Fork 6
/
postscript.lisp
2373 lines (2179 loc) · 59.3 KB
/
postscript.lisp
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
;;;-*- Mode: Lisp; Package: cl-user -*-
(in-package :cl-user)
; Postscript
(defparameter *table*
'(
#-avr-nano
#"
#if !defined(extensions)
// Table cross-reference functions
tbl_entry_t *tables[] = {lookup_table, NULL};
const unsigned int tablesizes[] = { arraysize(lookup_table), 0 };
const tbl_entry_t *table (int n) {
return tables[n];
}
unsigned int tablesize (int n) {
return tablesizes[n];
}
#endif"#
#"
// Table lookup functions"#
#+avr-nano
#"
/*
lookupbuiltin - looks up a string in lookup_table[], and returns the index of its entry,
or ENDFUNCTIONS if no match is found
*/
builtin_t lookupbuiltin (char* n) {
int entries = arraysize(lookup_table);
for (int entry = 0; entry < entries; entry++) {
#if defined(CPU_ATmega4809) || defined(CPU_ATtiny3227)
if (strcasecmp(n, (char*)lookup_table[entry].string) == 0)
#else
if (strcasecmp_P(n, (char*)pgm_read_ptr(&lookup_table[entry].string)) == 0)
#endif
return (builtin_t)entry;
}
return ENDFUNCTIONS;
}"#
#+avr
#"
/*
lookupbuiltin - looks up a string in lookup_table[], and returns the index of its entry,
or ENDFUNCTIONS if no match is found
*/
builtin_t lookupbuiltin (char* c) {
unsigned int end = 0, start;
for (int n=0; n<2; n++) {
start = end;
int entries = tablesize(n);
end = end + entries;
for (int i=0; i<entries; i++) {
if (strcasecmp_P(c, (char*)pgm_read_ptr(&(table(n)[i].string))) == 0)
return (builtin_t)(start + i);
}
}
return ENDFUNCTIONS;
}"#
#+(or arm riscv esp)
#"
/*
lookupbuiltin - looks up a string in lookup_table[], and returns the index of its entry,
or ENDFUNCTIONS if no match is found
*/
builtin_t lookupbuiltin (char* c) {
unsigned int end = 0, start;
for (int n=0; n<2; n++) {
start = end;
int entries = tablesize(n);
end = end + entries;
for (int i=0; i<entries; i++) {
if (strcasecmp(c, (char*)(table(n)[i].string)) == 0)
return (builtin_t)(start + i);
}
}
return ENDFUNCTIONS;
}"#
#+avr-nano
#"
/*
lookupfn - looks up the entry for name in lookup_table[], and returns the function entry point
*/
intptr_t lookupfn (builtin_t name) {
#if defined(CPU_ATmega4809) || defined(CPU_ATtiny3227)
return (intptr_t)lookup_table[name].fptr;
#else
return (intptr_t)pgm_read_ptr(&lookup_table[name].fptr);
#endif
}"#
#+avr
#"
/*
lookupfn - looks up the entry for name in lookup_table[], and returns the function entry point
*/
intptr_t lookupfn (builtin_t name) {
int n = name<tablesize(0);
return (intptr_t)pgm_read_ptr(&table(n?0:1)[n?name:name-tablesize(0)].fptr);
}"#
#+(or arm riscv esp)
#"
/*
lookupfn - looks up the entry for name in lookup_table[], and returns the function entry point
*/
intptr_t lookupfn (builtin_t name) {
int n = name<tablesize(0);
return (intptr_t)table(n?0:1)[n?name:name-tablesize(0)].fptr;
}"#
#+avr-nano
#"
/*
getminmax - gets the minmax byte from lookup_table[] whose octets specify the type of function
and minimum and maximum number of arguments for name
*/
uint8_t getminmax (builtin_t name) {
#if defined(CPU_ATmega4809) || defined(CPU_ATtiny3227)
return lookup_table[name].minmax;
#else
return pgm_read_byte(&lookup_table[name].minmax);
#endif
}"#
#+avr
#"
/*
getminmax - gets the minmax byte from lookup_table[] whose octets specify the type of function
and minimum and maximum number of arguments for name
*/
uint8_t getminmax (builtin_t name) {
int n = name<tablesize(0);
return pgm_read_byte(&table(n?0:1)[n?name:name-tablesize(0)].minmax);
}"#
#+(or arm riscv esp)
#"
/*
getminmax - gets the minmax byte from lookup_table[] whose octets specify the type of function
and minimum and maximum number of arguments for name
*/
uint8_t getminmax (builtin_t name) {
int n = name<tablesize(0);
return table(n?0:1)[n?name:name-tablesize(0)].minmax;
}"#
#"
/*
checkminmax - checks that the number of arguments nargs for name is within the range specified by minmax
*/
void checkminmax (builtin_t name, int nargs) {
if (!(name < ENDFUNCTIONS)) error2(PSTR("not a builtin"));
uint8_t minmax = getminmax(name);
if (nargs<((minmax >> 3) & 0x07)) error2(toofewargs);
if ((minmax & 0x07) != 0x07 && nargs>(minmax & 0x07)) error2(toomanyargs);
}"#
#+avr
#"
/*
lookupdoc - looks up the documentation string for the built-in function name
*/
char *lookupdoc (builtin_t name) {
int n = name<tablesize(0);
return (char*)pgm_read_ptr(&table(n?0:1)[n?name:name-tablesize(0)].doc);
}"#
#+(or arm riscv esp)
#"
/*
lookupdoc - looks up the documentation string for the built-in function name
*/
char *lookupdoc (builtin_t name) {
int n = name<tablesize(0);
return (char*)table(n?0:1)[n?name:name-tablesize(0)].doc;
}"#
#+avr
#"
/*
findsubstring - tests whether a specified substring occurs in the name of a built-in function
*/
bool findsubstring (char *part, builtin_t name) {
int n = name<tablesize(0);
PGM_P s = (char*)pgm_read_ptr(&table(n?0:1)[n?name:name-tablesize(0)].string);
int l = strlen_P(s);
int m = strlen(part);
for (int i = 0; i <= l-m; i++) {
int j = 0;
while (j < m && pgm_read_byte(&s[i+j]) == part[j]) j++;
if (j == m) return true;
}
return false;
}"#
#+(or arm riscv esp)
#"
/*
findsubstring - tests whether a specified substring occurs in the name of a built-in function
*/
bool findsubstring (char *part, builtin_t name) {
int n = name<tablesize(0);
return (strstr(table(n?0:1)[n?name:name-tablesize(0)].string, part) != NULL);
}"#
#"
void testescape () {
if (Serial.available() && Serial.read() == '~') error2(PSTR("escape!"));
}"#
#-avr-nano
#"
/*
colonp - check that a user-defined symbol starts with a colon and is therefore a keyword
*/
bool colonp (symbol_t name) {
if (!longnamep(name)) return false;
object *form = (object *)name;
if (form == NULL) return false;
return (((form->chars)>>((sizeof(int)-1)*8) & 0xFF) == ':');
}"#
#+avr-nano
#"
/*
keywordp - check that obj is a keyword
*/
bool keywordp (object *obj) {
if (!(symbolp(obj) && builtinp(obj->name))) return false;
builtin_t name = builtin(obj->name);
#if defined(CPU_ATmega4809) || defined(CPU_ATtiny3227)
PGM_P s = lookup_table[name].string;
char c = s[0];
#else
PGM_P s = (char*)pgm_read_ptr(&lookup_table[name].string);
char c = pgm_read_byte(s);
#endif
return (c == ':');
}"#
#+avr
#"
/*
keywordp - check that obj is a keyword
*/
bool keywordp (object *obj) {
if (!(symbolp(obj) && builtinp(obj->name))) return false;
builtin_t name = builtin(obj->name);
int n = name<tablesize(0);
PGM_P s = (char*)pgm_read_ptr(&table(n?0:1)[n?name:name-tablesize(0)].string);
char c = pgm_read_byte(s);
return (c == ':');
}"#
#+(or riscv arm esp)
#"
/*
keywordp - check that obj is a keyword
*/
bool keywordp (object *obj) {
if (!(symbolp(obj) && builtinp(obj->name))) return false;
builtin_t name = builtin(obj->name);
int n = name<tablesize(0);
PGM_P s = table(n?0:1)[n?name:name-tablesize(0)].string;
char c = s[0];
return (c == ':');
}"#))
(defparameter *eval*
'(#"
// Main evaluator"#
#+(or avr avr-nano)
#"
extern char __bss_end[];"#
#+arm
#"
#if defined(ARDUINO_TEENSY40) || defined(ARDUINO_TEENSY41)
#define ENDSTACK _ebss
#else
#define ENDSTACK end
#endif
extern uint32_t ENDSTACK; // Bottom of stack"#
#+riscv
#"
char end[0];"#
#+(or avr avr-nano)
#"
/*
eval - the main Lisp evaluator
*/
object *eval (object *form, object *env) {
uint8_t sp[0];
int TC=0;
EVAL:
// Enough space?
//Serial.println((uint16_t)sp - (uint16_t)__bss_end); // Find best STACKDIFF value
if ((uint16_t)sp - (uint16_t)__bss_end < STACKDIFF) { Context = NIL; error2(PSTR("stack overflow")); }
if (Freespace <= WORKSPACESIZE>>4) gc(form, env); // GC when 1/16 of workspace left
// Escape
if (tstflag(ESCAPE)) { clrflag(ESCAPE); error2(PSTR("escape!"));}
if (!tstflag(NOESC)) testescape();"#
#+arm
#"
/*
eval - the main Lisp evaluator
*/
object *eval (object *form, object *env) {
register int *sp asm ("r13");
int TC=0;
EVAL:
// Enough space?
// Serial.println((uint32_t)sp - (uint32_t)&ENDSTACK); // Find best STACKDIFF value
if (((uint32_t)sp - (uint32_t)&ENDSTACK) < STACKDIFF) { Context = NIL; error2(PSTR("stack overflow")); }
if (Freespace <= WORKSPACESIZE>>4) gc(form, env); // GC when 1/16 of workspace left
// Escape
if (tstflag(ESCAPE)) { clrflag(ESCAPE); error2(PSTR("escape!"));}
if (!tstflag(NOESC)) testescape();"#
#+riscv
#"
/*
eval - the main Lisp evaluator
*/
object *eval (object *form, object *env) {
register int *sp asm ("sp");
int TC=0;
EVAL:
// Enough space?
// Serial.println((uintptr_t)sp - (uintptr_t)end);
if ((uintptr_t)sp - (uintptr_t)end < STACKDIFF) { Context = NIL; error2(PSTR("stack overflow")); }
if (Freespace <= WORKSPACESIZE>>4) gc(form, env);
// Escape
if (tstflag(ESCAPE)) { clrflag(ESCAPE); error2(PSTR("escape!"));}
if (!tstflag(NOESC)) testescape();"#
#+ignore ; was badge
#"
/*
eval - the main Lisp evaluator
*/
object *eval (object *form, object *env) {
uint8_t sp[0];
int TC=0;
EVAL:
// Enough space?
// Serial.println((uint16_t)sp - (uint16_t)__bss_end); // Find best STACKDIFF value
if ((uint16_t)sp - (uint16_t)__bss_end < STACKDIFF) { Context = NIL; error2(PSTR("stack overflow")); }
if (Freespace <= WORKSPACESIZE>>4) gc(form, env); // GC when 1/16 of workspace left
// Escape
if (tstflag(ESCAPE)) { clrflag(ESCAPE); error2(PSTR("escape!"));}
#if defined (serialmonitor)
if (!tstflag(NOESC)) testescape();
#endif"#
#+esp
#"
/*
eval - the main Lisp evaluator
*/
object *eval (object *form, object *env) {
static unsigned long start = 0;
int TC=0;
EVAL:
#if defined(ARDUINO_ESP32C3_DEV)
if (millis() - start > 4000) { delay(1); start = millis(); }
#endif
// Enough space?
if (Freespace <= WORKSPACESIZE>>4) gc(form, env);
// Escape
if (tstflag(ESCAPE)) { clrflag(ESCAPE); error2(PSTR("escape!"));}
if (!tstflag(NOESC)) testescape();"#
#+avr-nano
#"
if (form == NULL) return nil;
if (form->type >= NUMBER && form->type <= STRING) return form;
if (symbolp(form)) {
symbol_t name = form->name;
object *pair = value(name, env);
if (pair != NULL) return cdr(pair);
pair = value(name, GlobalEnv);
if (pair != NULL) return cdr(pair);
else if (builtinp(name)) return form;
Context = NIL;
error(PSTR("undefined"), form);
}"#
#-avr-nano
#"
if (form == NULL) return nil;
if (form->type >= NUMBER && form->type <= STRING) return form;
if (symbolp(form)) {
symbol_t name = form->name;
if (colonp(name)) return form; // Keyword
object *pair = value(name, env);
if (pair != NULL) return cdr(pair);
pair = value(name, GlobalEnv);
if (pair != NULL) return cdr(pair);
else if (builtinp(name)) {
if (builtin(name) == FEATURES) return features();
return form;
}
Context = NIL;
error(PSTR("undefined"), form);
}"#
#+(or avr avr-nano arm riscv)
#"
#if defined(CODESIZE)
if (form->type == CODE) error2(PSTR("can't evaluate CODE header"));
#endif"#
#"
// It's a list
object *function = car(form);
object *args = cdr(form);
if (function == NULL) error(PSTR("illegal function"), nil);
if (!listp(args)) error(PSTR("can't evaluate a dotted pair"), args);
// List starts with a builtin symbol?
if (symbolp(function) && builtinp(function->name)) {
builtin_t name = builtin(function->name);
if ((name == LET) || (name == LETSTAR)) {
int TCstart = TC;
if (args == NULL) error2(noargument);
object *assigns = first(args);
if (!listp(assigns)) error(notalist, assigns);
object *forms = cdr(args);
object *newenv = env;
protect(newenv);
while (assigns != NULL) {
object *assign = car(assigns);
if (!consp(assign)) push(cons(assign,nil), newenv);
else if (cdr(assign) == NULL) push(cons(first(assign),nil), newenv);
else push(cons(first(assign),eval(second(assign),env)), newenv);
car(GCStack) = newenv;
if (name == LETSTAR) env = newenv;
assigns = cdr(assigns);
}
env = newenv;
unprotect();
form = tf_progn(forms,env);
TC = TCstart;
goto EVAL;
}
if (name == LAMBDA) {
if (env == NULL) return form;
object *envcopy = NULL;
while (env != NULL) {
object *pair = first(env);
if (pair != NULL) push(pair, envcopy);
env = cdr(env);
}
return cons(bsymbol(CLOSURE), cons(envcopy,args));
}
uint8_t fntype = getminmax(name)>>6;
if (fntype == SPECIAL_FORMS) {
Context = name;
checkargs(args);
return ((fn_ptr_type)lookupfn(name))(args, env);
}
if (fntype == TAIL_FORMS) {
Context = name;
checkargs(args);
form = ((fn_ptr_type)lookupfn(name))(args, env);
TC = 1;
goto EVAL;
}
if (fntype == OTHER_FORMS) error(PSTR("can't be used as a function"), function);
}
// Evaluate the parameters - result in head
object *fname = car(form);
int TCstart = TC;
object *head = cons(eval(fname, env), NULL);
protect(head); // Don't GC the result list
object *tail = head;
form = cdr(form);
int nargs = 0;
while (form != NULL){
object *obj = cons(eval(car(form),env),NULL);
cdr(tail) = obj;
tail = obj;
form = cdr(form);
nargs++;
}
function = car(head);
args = cdr(head);
if (symbolp(function)) {
builtin_t bname = builtin(function->name);
if (!builtinp(function->name)) error(PSTR("not valid here"), fname);
Context = bname;
checkminmax(bname, nargs);
object *result = ((fn_ptr_type)lookupfn(bname))(args, env);
unprotect();
return result;
}
if (consp(function)) {
symbol_t name = sym(NIL);
if (!listp(fname)) name = fname->name;
if (isbuiltin(car(function), LAMBDA)) {
form = closure(TCstart, name, function, args, &env);
unprotect();
int trace = tracing(fname->name);
if (trace) {
object *result = eval(form, env);
indent((--(TraceDepth[trace-1]))<<1, ' ', pserial);
pint(TraceDepth[trace-1], pserial);
pserial(':'); pserial(' ');
printobject(fname, pserial); pfstring(PSTR(" returned "), pserial);
printobject(result, pserial); pln(pserial);
return result;
} else {
TC = 1;
goto EVAL;
}
}
if (isbuiltin(car(function), CLOSURE)) {
function = cdr(function);
form = closure(TCstart, name, function, args, &env);
unprotect();
TC = 1;
goto EVAL;
}"#
#+avr
#"
#if defined(CODESIZE)
if (car(function)->type == CODE) {
int n = listlength(second(function));
if (nargs<n) errorsym2(fname->name, toofewargs);
if (nargs>n) errorsym2(fname->name, toomanyargs);
uint32_t entry = startblock(car(function));
unprotect();
return call(entry, n, args, env);
}
#endif"#
#+arm
#"
if (car(function)->type == CODE) {
int n = listlength(second(function));
if (nargs<n) errorsym2(fname->name, toofewargs);
if (nargs>n) errorsym2(fname->name, toomanyargs);
uint32_t entry = startblock(car(function)) + 1;
unprotect();
return call(entry, n, args, env);
}"#
#+riscv
#"
if (car(function)->type == CODE) {
int n = listlength(second(function));
if (nargs<n) errorsym2(fname->name, toofewargs);
if (nargs>n) errorsym2(fname->name, toomanyargs);
uint32_t entry = startblock(car(function));
unprotect();
return call(entry, n, args, env);
}"#
#"
}
error(PSTR("illegal function"), fname); return nil;
}"#))
(defparameter *print-functions*
'(#"
// Print functions"#
#-badge
#"
/*
pserial - prints a character to the serial port
*/
void pserial (char c) {
LastPrint = c;
if (c == '\n') Serial.write('\r');
Serial.write(c);
}"#
#+badge
#"
/*
pserial - prints a character to the serial port
*/
void pserial (char c) {
LastPrint = c;
Display(c);
#if defined (serialmonitor)
if (c == '\n') Serial.write('\r');
Serial.write(c);
#endif
}"#
#+(or avr avr-nano)
#"
const char ControlCodes[] PROGMEM = "Null\0SOH\0STX\0ETX\0EOT\0ENQ\0ACK\0Bell\0Backspace\0Tab\0Newline\0VT\0"
"Page\0Return\0SO\0SI\0DLE\0DC1\0DC2\0DC3\0DC4\0NAK\0SYN\0ETB\0CAN\0EM\0SUB\0Escape\0FS\0GS\0RS\0US\0Space\0";
/*
pcharacter - prints a character to a stream, escaping special characters if PRINTREADABLY is false
If <= 32 prints character name; eg #\Space
If < 127 prints ASCII; eg #\A
Otherwise prints decimal; eg #\234
*/
void pcharacter (uint8_t c, pfun_t pfun) {
if (!tstflag(PRINTREADABLY)) pfun(c);
else {
pfun('#'); pfun('\\');
if (c <= 32) {
PGM_P p = ControlCodes;
#if defined(CPU_ATmega4809) || defined(CPU_ATtiny3227)
while (c > 0) {p = p + strlen(p) + 1; c--; }
#else
while (c > 0) {p = p + strlen_P(p) + 1; c--; }
#endif
pfstring(p, pfun);
} else if (c < 127) pfun(c);
else pint(c, pfun);
}
}"#
#+ignore ; was badge
#"
const char ControlCodes[] PROGMEM = "Null\0SOH\0STX\0ETX\0EOT\0ENQ\0ACK\0Bell\0Backspace\0Tab\0Newline\0VT\0"
"Page\0Return\0SO\0SI\0DLE\0DC1\0DC2\0DC3\0DC4\0NAK\0SYN\0ETB\0CAN\0EM\0SUB\0Escape\0FS\0GS\0RS\0US\0Space\0";
/*
pcharacter - prints a character to a stream, escaping special characters if PRINTREADABLY is false
If <= 32 prints character name; eg #\Space
If < 127 prints ASCII; eg #\A
Otherwise prints decimal; eg #\234
*/
void pcharacter (uint8_t c, pfun_t pfun) {
if (!tstflag(PRINTREADABLY)) pfun(c);
else {
pfun('#'); pfun('\\');
if (c <= 32) {
PGM_P p = ControlCodes;
while (c > 0) {p = p + strlen_P(p) + 1; c--; }
pfstring(p, pfun);
} else if (c < 127) pfun(c);
else pint(c, pfun);
}
}"#
#+esp
#"
const char ControlCodes[] PROGMEM = "Null\0SOH\0STX\0ETX\0EOT\0ENQ\0ACK\0Bell\0Backspace\0Tab\0Newline\0VT\0"
"Page\0Return\0SO\0SI\0DLE\0DC1\0DC2\0DC3\0DC4\0NAK\0SYN\0ETB\0CAN\0EM\0SUB\0Escape\0FS\0GS\0RS\0US\0Space\0";
/*
pcharacter - prints a character to a stream, escaping special characters if PRINTREADABLY is false
If <= 32 prints character name; eg #\Space
If < 127 prints ASCII; eg #\A
Otherwise prints decimal; eg #\234
*/
void pcharacter (uint8_t c, pfun_t pfun) {
if (!tstflag(PRINTREADABLY)) pfun(c);
else {
pfun('#'); pfun('\\');
if (c <= 32) {
PGM_P p = ControlCodes;
while (c > 0) {p = p + strlen_P(p) + 1; c--; }
pfstring(p, pfun);
} else if (c < 127) pfun(c);
else pint(c, pfun);
}
}"#
#+(or arm riscv)
#"
const char ControlCodes[] PROGMEM = "Null\0SOH\0STX\0ETX\0EOT\0ENQ\0ACK\0Bell\0Backspace\0Tab\0Newline\0VT\0"
"Page\0Return\0SO\0SI\0DLE\0DC1\0DC2\0DC3\0DC4\0NAK\0SYN\0ETB\0CAN\0EM\0SUB\0Escape\0FS\0GS\0RS\0US\0Space\0";
/*
pcharacter - prints a character to a stream, escaping special characters if PRINTREADABLY is false
If <= 32 prints character name; eg #\Space
If < 127 prints ASCII; eg #\A
Otherwise prints decimal; eg #\234
*/
void pcharacter (uint8_t c, pfun_t pfun) {
if (!tstflag(PRINTREADABLY)) pfun(c);
else {
pfun('#'); pfun('\\');
if (c <= 32) {
const char *p = ControlCodes;
while (c > 0) {p = p + strlen(p) + 1; c--; }
pfstring(p, pfun);
} else if (c < 127) pfun(c);
else pint(c, pfun);
}
}"#
#"
/*
pstring - prints a C string to the specified stream
*/
void pstring (char *s, pfun_t pfun) {
while (*s) pfun(*s++);
}"#
#"
/*
plispstring - prints a Lisp string object to the specified stream
*/
void plispstring (object *form, pfun_t pfun) {
plispstr(form->name, pfun);
}
/*
plispstr - prints a Lisp string name to the specified stream
*/
void plispstr (symbol_t name, pfun_t pfun) {
object *form = (object *)name;
while (form != NULL) {
int chars = form->chars;
for (int i=(sizeof(int)-1)*8; i>=0; i=i-8) {
char ch = chars>>i & 0xFF;
if (tstflag(PRINTREADABLY) && (ch == '"' || ch == '\\')) pfun('\\');
if (ch) pfun(ch);
}
form = car(form);
}
}
/*
printstring - prints a Lisp string object to the specified stream
taking account of the PRINTREADABLY flag
*/
void printstring (object *form, pfun_t pfun) {
if (tstflag(PRINTREADABLY)) pfun('"');
plispstr(form->name, pfun);
if (tstflag(PRINTREADABLY)) pfun('"');
}"#
#+avr-nano
#"
/*
pbuiltin - prints a built-in symbol to the specified stream
*/
void pbuiltin (builtin_t name, pfun_t pfun) {
#if defined(CPU_ATmega4809) || defined(CPU_ATtiny3227)
PGM_P s = lookup_table[name].string;
#else
PGM_P s = (char*)pgm_read_ptr(&lookup_table[name].string);
#endif
while (1) {
#if defined(CPU_ATmega4809) || defined(CPU_ATtiny3227)
char c = @s++;
#else
char c = pgm_read_byte(s++);
#endif
if (c == 0) return;
pfun(c);
}
}"#
#+avr
#"
/*
pbuiltin - prints a built-in symbol to the specified stream
*/
void pbuiltin (builtin_t name, pfun_t pfun) {
int n = name<tablesize(0);
PGM_P s = (char*)pgm_read_ptr(&table(n?0:1)[n?name:name-tablesize(0)].string);
while (1) {
char c = pgm_read_byte(s++);
if (c == 0) return;
pfun(c);
}
}"#
#+(or arm riscv esp)
#"
/*
pbuiltin - prints a built-in symbol to the specified stream
*/
void pbuiltin (builtin_t name, pfun_t pfun) {
int n = name<tablesize(0);
PGM_P s = table(n?0:1)[n?name:name-tablesize(0)].string;
while (1) {
char c = *s++;
if (c == 0) return;
pfun(c);
}
}"#
#+(or avr avr-nano)
#"
/*
pradix40 - prints a radix 40 symbol to the specified stream
*/
void pradix40 (symbol_t name, pfun_t pfun) {
uint16_t x = untwist(name);
for (int d=1600; d>0; d = d/40) {
uint16_t j = x/d;
char c = fromradix40(j);
if (c == 0) return;
pfun(c); x = x - j*d;
}
}"#
#+(or arm esp riscv)
#"
/*
pradix40 - prints a radix 40 symbol to the specified stream
*/
void pradix40 (symbol_t name, pfun_t pfun) {
uint32_t x = untwist(name);
for (int d=102400000; d>0; d = d/40) {
uint32_t j = x/d;
char c = fromradix40(j);
if (c == 0) return;
pfun(c); x = x - j*d;
}
}"#
#"
/*
printsymbol - prints any symbol from a symbol object to the specified stream
*/
void printsymbol (object *form, pfun_t pfun) {
psymbol(form->name, pfun);
}"#
#+(or avr avr-nano)
#"
/*
psymbol - prints any symbol from a symbol name to the specified stream
*/
void psymbol (symbol_t name, pfun_t pfun) {
if (longnamep(name)) plispstr(name, pfun);
else {
uint16_t value = untwist(name);
if (value < PACKEDS) error2(PSTR("invalid symbol"));
else if (value >= BUILTINS) pbuiltin((builtin_t)(value-BUILTINS), pfun);
else pradix40(name, pfun);
}
}"#
#+(or arm esp riscv)
#"
/*
psymbol - prints any symbol from a symbol name to the specified stream
*/
void psymbol (symbol_t name, pfun_t pfun) {
if (longnamep(name)) plispstr(name, pfun);
else {
uint32_t value = untwist(name);
if (value < PACKEDS) error2(PSTR("invalid symbol"));
else if (value >= BUILTINS) pbuiltin((builtin_t)(value-BUILTINS), pfun);
else pradix40(name, pfun);
}
}"#
#+avr-nano
#"
/*
pfstring - prints a string from flash memory to the specified stream
*/
void pfstring (PGM_P s, pfun_t pfun) {
while (1) {
#if defined(CPU_ATmega4809) || defined(CPU_ATtiny3227)
char c = *s++;
#else
char c = pgm_read_byte(s++);
#endif
if (c == 0) return;
pfun(c);
}
}"#
#+avr
#"
/*
pfstring - prints a string from flash memory to the specified stream
*/
void pfstring (PGM_P s, pfun_t pfun) {
while (1) {
char c = pgm_read_byte(s++);
if (c == 0) return;
pfun(c);
}
}"#
#+(or arm riscv esp)
#"
/*
pfstring - prints a string from flash memory to the specified stream
*/
void pfstring (const char *s, pfun_t pfun) {
while (1) {
char c = *s++;
if (c == 0) return;
pfun(c);
}
}"#
#+msp430
#"
/*
pfstring - prints a string from flash memory to the specified stream
*/
void pfstring (PGM_P s, pfun_t pfun) {
intptr_t p = (intptr_t)s;
while (1) {
char c = pgm_read_byte(p++);
if (c == 0) return;
pfun(c);
}
}"#
#+(or avr avr-nano)
#"
/*
pint - prints an integer in decimal to the specified stream
*/
void pint (int i, pfun_t pfun) {
uint16_t j = i;
if (i<0) { pfun('-'); j=-i; }
pintbase(j, 10, pfun);
}
/*
pintbase - prints an integer in base 'base' to the specified stream
*/
void pintbase (uint16_t i, uint8_t base, pfun_t pfun) {
uint8_t lead = 0; uint16_t p = 10000;
if (base == 2) p = 0x8000; else if (base == 16) p = 0x1000;
for (uint16_t d=p; d>0; d=d/base) {
uint16_t j = i/d;
if (j!=0 || lead || d==1) { pfun((j<10) ? j+'0' : j+'W'); lead=1;}
i = i - j*d;
}
}"#
#+(or arm esp riscv)
#"
/*
pint - prints an integer in decimal to the specified stream
*/
void pint (int i, pfun_t pfun) {
uint32_t j = i;
if (i<0) { pfun('-'); j=-i; }
pintbase(j, 10, pfun);
}
/*
pintbase - prints an integer in base 'base' to the specified stream
*/
void pintbase (uint32_t i, uint8_t base, pfun_t pfun) {
int lead = 0; uint32_t p = 1000000000;
if (base == 2) p = 0x80000000; else if (base == 16) p = 0x10000000;
for (uint32_t d=p; d>0; d=d/base) {
uint32_t j = i/d;
if (j!=0 || lead || d==1) { pfun((j<10) ? j+'0' : j+'W'); lead=1;}
i = i - j*d;
}
}"#
#+(or avr avr-nano)
#"