-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhh_builtins.def
1271 lines (1088 loc) · 42.7 KB
/
hh_builtins.def
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
/* This file is part of Hedgehog LISP.
* Copyright (C) 2003, 2004, 2005, 2006 Oliotalo Ltd.
* See file LICENSE.LGPL for pertinent licensing conditions.
*
* Authors: Kenneth Oksanen <cessu@iki.fi>
* Lars Wirzenius <liw@iki.fi>
*/
/* This file defines all of Hedgehog LISP's builtin functions and
special forms, such as `if', `+', `strrstr', etc.
Only macros and `def' and `fn' are treated elsewhere.
Syntax: BUILTIN(lisp_name, c_name, doc_string, args, code_gen)
*/
MODULE("Special forms")
BUILTIN("set", set,
"Create and bind a local variable. May appear only in an expression "
"sequence, such as `do' or in the body of `def', `fn', or the "
"top-level. The variable is valid only during the rest of "
"the sequence, not before the `set', or outside the surrounding "
"expression sequence.",
ARG(variable, SYMBOL_CELL, "Variable name.")
ARG(value, EMPTY_CELL, "Bound value."),
{
/* If we are in a sequence, then this has already been
handled in `hh_gen_list'. Otherwise it is an error. */
hh_fatal(expr, "`set' can only appear in an expression sequence "
"after `do', `def' or `fn'");
})
BUILTIN("quote", quote,
"Quote, i.e. prevent the given form from being evaluated.",
ARG(form, EMPTY_CELL, "Form."),
{
if (expr->arity != 2)
hh_fatal(expr, "`quote' expects exactly one argument");
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_load;
code->u.imm.value = hh_gen_quote(expr->u.ast[1]);
})
BUILTIN("if", if,
"Evaluate either branch depending on the condition.",
ARG(cond, EMPTY_CELL, "Condition.")
ARG(then, EMPTY_CELL, "Evaluated if condition true.")
ARG(else, EMPTY_CELL, "Evaluated if condition false (optional)."),
{
/* Evaluate condition, leaving the truth value into `accu'. */
hh_gen(expr->u.ast[1], 0);
code = hh_alloc_code(expr);
code->kind = HH_BRANCH;
code->u.branch.insn = HH_IMM_branch_if_false;
hh_gen(expr->u.ast[2], is_tail);
if (expr->arity == 3) {
/* There is no else-branch. Make a label here and have
`code' branch here. */
code2 = hh_alloc_code(expr);
code2->kind = HH_LABEL;
code->u.branch.target = code2;
} else {
HH_ASSERT(expr->arity == 4); /* Should be already checked. */
/* There is an else-branch. Make here an unconditional
branch to the end of this expression and a label for
`code'. */
code2 = hh_alloc_code(expr);
code2->kind = HH_BRANCH;
code2->u.branch.insn = HH_IMM_branch;
code3 = hh_alloc_code(expr);
code3->kind = HH_LABEL;
code->u.branch.target = code3;
hh_gen(expr->u.ast[3], is_tail);
code3 = hh_alloc_code(expr);
code3->kind = HH_LABEL;
code2->u.branch.target = code3;
}
})
BUILTIN("do", do,
"Execute all expressions in a sequence, return last value.",
REMAINING_ARGS(bodies, EMPTY_CELL, "Expressions to evaluate."),
{
/* Note that the AST reader makes this into a list. */
HH_ASSERT(expr->arity == 2);
hh_gen_list(expr->u.ast[1], is_tail, 0);
})
BUILTIN("catch", catch,
"Catch an exception.",
ARG(body, EMPTY_CELL, "Protected body.")
ARG(tag, SYMBOL_CELL, "Catch tag.")
ARG(catcher, EMPTY_CELL, "Exception handler."),
{
if (expr->arity != 4)
hh_fatal(expr, "`catch' expects exactly three arguments: "
"protected body, a symbol that represents the catch tag, "
"and the catching body");
if (expr->u.ast[2]->arity != HH_AST_SYMBOL)
hh_fatal(expr->u.ast[2], "Expected a symbol that represents "
"the catch tag");
if (expr->u.ast[2]->u.symbol->catch_tag == 0)
expr->u.ast[2]->u.symbol->catch_tag = ++hh_n_catch_tags;
/* Push to stack the three-word catch cell. */
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_load;
code->u.imm.value =
HH_SHORT_TO_WORD(expr->u.ast[2]->u.symbol->catch_tag);
code = hh_alloc_code(expr);
code->kind = HH_BRANCH;
code->u.branch.insn = HH_IMM_catch;
hh_sp += 3;
/* Generate code for the protected body. */
hh_gen(expr->u.ast[1], 0);
/* Not thrown, drop the catch cell, and branch to after this
`expr'. */
code2 = hh_alloc_code(expr->u.ast[2]);
code2->kind = HH_IMM;
code2->u.imm.insn = HH_IMM_drop;
code2->u.imm.value = 3;
hh_sp -= 3;
code2 = hh_alloc_code(expr->u.ast[2]);
code2->kind = HH_BRANCH;
code2->u.branch.insn = HH_IMM_branch;
/* Make a label representing the `pc' of a thrown catch. */
code3 = hh_alloc_code(expr->u.ast[2]);
code3->kind = HH_LABEL;
code->u.branch.target = code3;
/* Generate code for the catching body. */
hh_gen(expr->u.ast[3], is_tail);
/* Finally make a label where to jump after a protected body. */
code = hh_alloc_code(expr->u.ast[2]);
code->kind = HH_LABEL;
code2->u.branch.target = code;
})
BUILTIN("throw", throw,
"Throw an exception.",
ARG(tag, SYMBOL_CELL, "Catch tag."),
{
if (expr->arity != 2)
hh_fatal(expr, "`throw' expects exactly one argument, "
"the catch tag symbol");
if (expr->u.ast[1]->arity != HH_AST_SYMBOL)
hh_fatal(expr->u.ast[1], "`throw' expects the catch tag "
"to be a symbol");
if (expr->u.ast[1]->u.symbol->catch_tag == 0)
expr->u.ast[1]->u.symbol->catch_tag = ++hh_n_catch_tags;
code = hh_alloc_code(expr->u.ast[1]);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_throw;
code->u.imm.value =
HH_SHORT_TO_WORD(expr->u.ast[1]->u.symbol->catch_tag);
})
BUILTIN("apply", apply,
"Call the given function with the given argument list. Note that "
"even if the apply would be in a tail-recursive position, the call "
"to the given function is not a tail-call.",
ARG(function, EMPTY_CELL, "Function to call.")
ARG(arguments, CONS_CELL, "Argument list."),
GEN_INSN(apply, 2))
BUILTIN("tailcall", tailcall,
"Cause compile-time error if this is not a tail-recursive position.",
ARG(expr, EMPTY_CELL, "Any expression."),
{
if (!is_tail)
hh_fatal(expr, "`tailcall' in a non-tail-recursive position");
if (expr->arity != 2)
hh_fatal(expr, "`tailcall' expects exactly one argument");
hh_gen(expr->u.ast[1], 1);
})
MODULE_END
MODULE("Boolean arithmetic")
BUILTIN("and", and,
"Boolean and.",
REMAINING_ARGS(values, EMPTY_CELL, "Values interpreted as booleans."),
{
if (expr->arity == 1) {
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_load;
code->u.imm.value = HH_TRUE;
} else {
codes = malloc((expr->arity - 2) * sizeof(hh_code_t *));
if (codes == NULL)
hh_fatal(expr, "Malloc failed");
for (i = 0; i < expr->arity - 2; i++) {
hh_gen(expr->u.ast[i + 1], 0);
codes[i] = hh_alloc_code(expr);
codes[i]->kind = HH_BRANCH;
codes[i]->u.branch.insn = HH_IMM_branch_if_false;
}
hh_gen(expr->u.ast[expr->arity - 1], is_tail);
code = hh_alloc_code(expr);
code->kind = HH_LABEL;
for (i = 0; i < expr->arity - 2; i++)
codes[i]->u.branch.target = code;
free(codes);
}
})
BUILTIN("or", or,
"Boolean or.",
REMAINING_ARGS(values, EMPTY_CELL, "Values interpreted as booleans."),
{
if (expr->arity == 1) {
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_load;
code->u.imm.value = HH_FALSE;
} else {
codes = malloc((expr->arity - 2) * sizeof(hh_code_t *));
if (codes == NULL)
hh_fatal(expr, "Malloc failed");
for (i = 0; i < expr->arity - 2; i++) {
hh_gen(expr->u.ast[i + 1], 0);
codes[i] = hh_alloc_code(expr);
codes[i]->kind = HH_BRANCH;
codes[i]->u.branch.insn = HH_IMM_branch_if_true;
}
hh_gen(expr->u.ast[expr->arity - 1], is_tail);
code = hh_alloc_code(expr);
code->kind = HH_LABEL;
for (i = 0; i < expr->arity - 2; i++)
codes[i]->u.branch.target = code;
free(codes);
}
})
BUILTIN("not", not,
"Boolean not.",
ARG(value, EMPTY_CELL, "Value interpreted as boolean."),
GEN_INSN(not, 1))
MODULE_END
MODULE("List processing")
BUILTIN("cons", cons,
"Create a new cons cell.",
ARG(left, EMPTY_CELL, "Value for left slot in cons cell.")
ARG(right, EMPTY_CELL, "Value for right slot in cons cell."),
GEN_INSN(cons, 2))
BUILTIN("car", car,
"Return left slot of the cons cell argument.",
ARG(cell, CONS_CELL, "Cons cell."),
GEN_INSN(car, 1))
BUILTIN("cdr", cdr,
"Return right slot of the cons cell argument.",
ARG(cell, CONS_CELL, "Cons cell."),
GEN_INSN(cdr, 1))
BUILTIN("tuple-arity", tuple_arity,
"Return the number of fields in the tuple, or 0 if not a tuple.",
ARG(x, EMPTY_CELL, "Value"),
GEN_INSN(tuple_arity, 1))
BUILTIN("tuple-make", tuple_make,
"Create a tuple of the given values.",
REMAINING_ARGS(values, EMPTY_CELL, "Values."),
{
if (expr->arity <= 2 || expr->arity >= 16)
hh_fatal(expr, "`tuple-arity' expects from 2 to 15 arguments");
for (i = 1; i < expr->arity - 1; i++) {
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_push;
hh_sp++;
}
hh_gen(expr->u.ast[expr->arity - 1], 0);
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_tuple_make;
code->u.imm.value = expr->arity - 1;
hh_sp -= expr->arity - 2;
})
BUILTIN("tuple-make-from-list", tuple_make_from_list,
"Create a tuple of the given list of values.",
ARG(list, CONS_CELL, "List of tuple field values."),
GEN_INSN(tuple_make_from_list, 1))
BUILTIN("tuple-index", tuple_ref,
"Read the contents of the given field in the given tuple.",
ARG(tuple, EMPTY_CELL, "A sufficiently wide tuple")
ARG(i, INTEGER_CELL, "Field number, starting from zero"),
GEN_INSN(tuple_index, 2))
BUILTIN("tuple-with", tuple_with,
"Copy the tuple with a new value in the specified slot.",
ARG(tuple, EMPTY_CELL, "A sufficiently wide tuple")
ARG(i, INTEGER_CELL, "Field number, starting from zero")
ARG(value, EMPTY_CELL, "The new value"),
GEN_INSN(tuple_with, 3))
MODULE_END
MODULE("AVL-trees")
BUILTIN("avl-make-node", avl_make_node,
"Make a new AVL-tree node with the given key, value and subtrees. "
"If the height difference of the subtrees is two, the routine "
"performs necessary rotations to bring the new node into balance. "
"No rotations are made if the heights differ less, and a fatal error "
"is raised if the heights differ by three or more.",
ARG(key, EMPTY_CELL, "Key")
ARG(value, EMPTY_CELL, "Value")
ARG(left, AVL_CELL, "Left subtree")
ARG(right, AVL_CELL, "Right subtree"),
GEN_INSN(avl_make_node, 4))
BUILTIN("avl-height", avl_height,
"Get the height of the given AVL-tree node, zero for nil or for "
"something not an AVL-tree node.",
ARG(node, AVL_CELL, "AVL-tree node"),
GEN_INSN(avl_height, 1))
BUILTIN("avl-key", avl_key,
"Get the key in the given AVL-tree node.",
ARG(node, AVL_CELL, "AVL-tree node"),
GEN_INSN(avl_key, 1))
BUILTIN("avl-value", avl_value,
"Get the value in the given AVL-tree node.",
ARG(node, AVL_CELL, "AVL-tree node"),
GEN_INSN(avl_value, 1))
BUILTIN("avl-left", avl_left,
"Get the left subtree of the given AVL-tree node.",
ARG(node, AVL_CELL, "AVL-tree node"),
GEN_INSN(avl_left, 1))
BUILTIN("avl-right", avl_right,
"Get the right subtree of the given AVL-tree node.",
ARG(node, AVL_CELL, "AVL-tree node"),
GEN_INSN(avl_right, 1))
BUILTIN("default-cmpfun", default_cmpfun,
"A simple default comparison function for symbols, integers, and "
"strings. Returns -1, 0, or 1 if `a' is considered to be less than, "
"equal to, or greater than `b', respectively. Integers are "
"considered less than all symbols, which in turn are considered "
"less than strings.",
ARG(a, EMPTY_CELL, "First comparand")
ARG(b, EMPTY_CELL, "Second comparand"),
GEN_INSN(default_cmpfun, 2))
BUILTIN("default-avl-get", default_avl_get,
"A more efficient implementation of AVL tree searching "
"assuming the default key comparison function.",
ARG(tree, AVL_CELL, "AVL-tree")
ARG(key, EMPTY_CELL, "Key to be searched")
ARG(default_value, EMPTY_CELL, "Value returned when key not found"),
GEN_INSN(default_avl_get, 3))
BUILTIN("default-avl-put", default_avl_put,
"A more efficient implementation of AVL tree insertion/replacement "
"assuming the default key comparison function.",
ARG(tree, AVL_CELL, "AVL-tree")
ARG(key, EMPTY_CELL, "Key to be searched")
ARG(value, EMPTY_CELL, "Value to be inserted/replaced"),
GEN_INSN(default_avl_put, 3))
MODULE_END
MODULE("Predicates")
BUILTIN("int?", int_p,
"Is argument an integer?",
ARG(value, EMPTY_CELL, "Value of any type."),
GEN_INSN(intp, 1))
BUILTIN("string?", string_p,
"Is argument a string?",
ARG(value, EMPTY_CELL, "Value of any type."),
GEN_INSN(stringp, 1))
BUILTIN("symbol?", symbol_p,
"Is argument a symbol?",
ARG(value, EMPTY_CELL, "Value of any type."),
GEN_INSN(symbolp, 1))
BUILTIN("fn?", fn_p,
"Is argument a function?",
ARG(value, EMPTY_CELL, "Value of any type."),
GEN_INSN(fnp, 1))
BUILTIN("cons?", cons_p,
"Is argument a cons cell?",
ARG(value, EMPTY_CELL, "Value of any type."),
GEN_INSN(consp, 1))
BUILTIN("eq?", eq_p,
"Are two values, of any type, equal?",
ARG(a, EMPTY_CELL, "First value.")
ARG(b, EMPTY_CELL, "Second value."),
GEN_INSN(eqp, 2))
MODULE_END
MODULE("Comparison functions")
BUILTIN("<", lt,
"Is each argument less than the following one?",
REMAINING_ARGS(ints, INTEGER_CELL, "Integers."),
{
cmp_insn = HH_INSN_lt;
cmp_branch_insn = HH_IMM_branch_if_ge;
cmp_gen:
if (expr->arity == 1) {
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_load;
code->u.imm.value = HH_TRUE;
} else if (expr->arity == 2) {
/* Generate code for the subtree in case it has side effects. */
hh_gen(expr->u.ast[1], 0);
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_load;
code->u.imm.value = HH_TRUE;
} else {
hh_gen(expr->u.ast[1], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_push;
hh_sp++;
if (expr->arity > 3) {
codes = malloc((expr->arity - 3) * sizeof(hh_code_t *));
if (codes == NULL)
hh_fatal(expr, "Malloc failed");
} else
codes = NULL;
for (i = 0; i < expr->arity - 3; i++) {
hh_gen(expr->u.ast[i + 2], 0);
codes[i] = hh_alloc_code(expr);
codes[i]->kind = HH_BRANCH;
codes[i]->u.branch.insn = cmp_branch_insn;
}
hh_gen(expr->u.ast[expr->arity - 1], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = cmp_insn;
hh_sp--;
if (codes != NULL) {
code = hh_alloc_code(expr);
code->kind = HH_LABEL;
for (i = 0; i < expr->arity - 3; i++)
codes[i]->u.branch.target = code;
free(codes);
}
}
})
BUILTIN("<=", le,
"Is each argument less than or equal to the following one?",
REMAINING_ARGS(ints, INTEGER_CELL, "Integers."),
{
cmp_insn = HH_INSN_le;
cmp_branch_insn = HH_IMM_branch_if_gt;
goto cmp_gen;
})
BUILTIN("=", eq,
"Is each argument equal to the following one?",
REMAINING_ARGS(ints, INTEGER_CELL, "Integers."),
{
cmp_insn = HH_INSN_eq;
cmp_branch_insn = HH_IMM_branch_if_ne;
goto cmp_gen;
})
BUILTIN(">=", ge,
"Is each argument greater than or equal to the following one?",
REMAINING_ARGS(ints, INTEGER_CELL, "Integers."),
{
cmp_insn = HH_INSN_ge;
cmp_branch_insn = HH_IMM_branch_if_lt;
goto cmp_gen;
})
BUILTIN(">", gt,
"Is each argument greater than the following one?",
REMAINING_ARGS(ints, INTEGER_CELL, "Integers."),
{
cmp_insn = HH_INSN_gt;
cmp_branch_insn = HH_IMM_branch_if_le;
goto cmp_gen;
})
BUILTIN("!=", ne,
"Do the two arguments have different values?",
ARG(a, INTEGER_CELL, "First value.")
ARG(b, INTEGER_CELL, "Second value."),
GEN_INSN(ne, 2))
MODULE_END
MODULE("Arithmetic")
BUILTIN("+", add,
"Add arguments together.",
REMAINING_ARGS(ints, INTEGER_CELL, "Integer arguments."),
{
if (expr->arity == 1) {
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_load;
code->u.imm.value = HH_SHORT_TO_WORD(0);
} else {
hh_gen(expr->u.ast[1], 0);
for (i = 2; i < expr->arity; i++) {
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_push;
hh_sp++;
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_add;
hh_sp--;
}
}
})
BUILTIN("-", subtract,
"Subtract other arguments from first one, or negate if only one.",
REMAINING_ARGS(ints, INTEGER_CELL, "Integer arguments"),
{
if (expr->arity == 1)
hh_fatal(expr, "`-' must have at least one argument");
if (expr->arity == 2) {
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_load;
code->u.imm.value = HH_SHORT_TO_WORD(0);
i = 1;
} else {
hh_gen(expr->u.ast[1], 0);
i = 2;
}
while (i < expr->arity) {
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_push;
hh_sp++;
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_sub;
hh_sp--;
i++;
}
})
BUILTIN("*", mult,
"Multiply arguments together.",
REMAINING_ARGS(ints, INTEGER_CELL, "Integer arguments."),
{
if (expr->arity == 1) {
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_load;
code->u.imm.value = HH_SHORT_TO_WORD(1);
} else {
hh_gen(expr->u.ast[1], 0);
for (i = 2; i < expr->arity; i++) {
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_push;
hh_sp++;
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_mult;
hh_sp--;
}
}
})
BUILTIN("/", div,
"Divide arguments with each other.",
REMAINING_ARGS(ints, INTEGER_CELL, "Integer arguments."),
{
if (expr->arity == 1)
hh_fatal(expr, "`/' must have at least one argument");
if (expr->arity == 2) {
code = hh_alloc_code(expr);
code->kind = HH_IMM;
code->u.imm.insn = HH_IMM_load;
code->u.imm.value = HH_SHORT_TO_WORD(1);
i = 1;
} else {
hh_gen(expr->u.ast[1], 0);
i = 2;
}
while (i < expr->arity) {
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_push;
hh_sp++;
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_div;
hh_sp--;
i++;
}
})
BUILTIN("%", remainder,
"Return remainder (a modulo b).",
ARG(a, INTEGER_CELL, "The numerator.")
ARG(b, INTEGER_CELL, "The denominator."),
GEN_INSN(remainder, 2))
BUILTIN("&", bit_and,
"Bitwise and arguments together.",
REMAINING_ARGS(ints, INTEGER_CELL, "Integer arguments."),
{
if (expr->arity == 1)
hh_fatal(expr, "`&' expects at least one argument");
hh_gen(expr->u.ast[1], 0);
for (i = 2; i < expr->arity; i++) {
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_push;
hh_sp++;
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_bitand;
hh_sp--;
}
})
BUILTIN("|", bit_or,
"Bitwise or arguments together.",
REMAINING_ARGS(ints, INTEGER_CELL, "Integer arguments."),
{
if (expr->arity == 1)
hh_fatal(expr, "`|' expects at least one argument");
hh_gen(expr->u.ast[1], 0);
for (i = 2; i < expr->arity; i++) {
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_push;
hh_sp++;
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_bitor;
hh_sp--;
}
})
BUILTIN("~", bit_not,
"Bitwise not.",
ARG(value, INTEGER_CELL, "Value to be inverted."),
GEN_INSN(bitnot, 1))
BUILTIN("^", bit_xor,
"Bitwise XOR.",
REMAINING_ARGS(ints, INTEGER_CELL, "Integer arguments."),
{
if (expr->arity == 1)
hh_fatal(expr, "`^' expects at least one argument");
hh_gen(expr->u.ast[1], 0);
for (i = 2; i < expr->arity; i++) {
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_push;
hh_sp++;
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_bitxor;
hh_sp--;
}
})
BUILTIN("<<", bit_lshift,
"Bitwise left shift.",
ARG(value, INTEGER_CELL, "Value to be shifted.")
ARG(n, INTEGER_CELL, "Number of bits to shift."),
GEN_INSN(bitlshift, 2))
BUILTIN(">>", bit_rshift,
"Bitwise right shift.",
ARG(value, INTEGER_CELL, "Value to be shifted.")
ARG(n, INTEGER_CELL, "Number of bits to shift."),
GEN_INSN(bitrshift, 2))
MODULE_END
MODULE("String processing")
BUILTIN("strlen", strlen,
"Return length of string argument.",
ARG(str, STRING_CELL, "The string."),
GEN_INSN(strlen, 1))
BUILTIN("substr", substr,
"Copy part of existing string into new string.",
ARG(str, STRING_CELL, "Original string.")
ARG(pos, INTEGER_CELL, "Substring position in STR.")
ARG(len, INTEGER_CELL, "Substring length, -1 means to end of STR."),
GEN_INSN(substr, 3))
BUILTIN("strcmp", strcmp,
"Compare two strings: return negative if first is less than second, "
"positive if greater, zero if equal.",
ARG(str1, STRING_CELL, "First string.")
ARG(str2, STRING_CELL, "Second string."),
GEN_INSN(strcmp, 2))
BUILTIN("ord", ord,
"Return character code of first character in argument.",
ARG(ch, STRING_CELL, "The character (non-empty string)."),
GEN_INSN(ord, 1))
BUILTIN("chr", chr,
"Return string containing character whose code is argument.",
ARG(code, INTEGER_CELL, "The character code."),
GEN_INSN(chr, 1))
BUILTIN("strcat", strcat,
"Catenate strings.",
REMAINING_ARGS(strings, STRING_CELL, "Strings to be catenated."),
{
if (expr->arity == 1) {
/* A `strcat' without arguments was defined to return the
empty string. Modify `expr' to be a reference to the
empty string, and apply the code generation for that. */
expr->arity = HH_AST_STRING;
expr->u.string = hh_ast_string("", 0);
hh_gen(expr, is_tail);
} else {
hh_gen(expr->u.ast[1], is_tail && expr->arity == 2);
for (i = 2; i < expr->arity; i++) {
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_push;
hh_sp++;
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_strcat;
hh_sp--;
}
}
})
BUILTIN("atoi", atoi,
"Convert a string to an integer.",
ARG(str, STRING_CELL, "The string to convert.")
ARG(base, INTEGER_CELL, "Base for the number in STR."),
GEN_INSN(atoi, 2))
BUILTIN("itoa", itoa,
"Convert an integer to a string.",
ARG(value, INTEGER_CELL, "Integer to convert to string.")
ARG(base, INTEGER_CELL, "Base for the conversion."),
GEN_INSN(itoa, 2))
BUILTIN("symboltostring", symboltostring,
"Convert the name of a symbol to its string representation.",
ARG(symbol, SYMBOL_CELL, "The symbol to convert."),
GEN_INSN(symboltostring, 1))
BUILTIN("strstr", strstr,
"Return position of pattern inside string, or -1 for not found.",
ARG(string, STRING_CELL, "String to search inside.")
ARG(pattern, STRING_CELL, "String to search for."),
GEN_INSN(strstr, 2))
BUILTIN("strrstr", strrstr,
"Return last position of pattern inside string, or -1 for not found.",
ARG(string, STRING_CELL, "String to search inside.")
ARG(pattern, STRING_CELL, "String to search for."),
GEN_INSN(strrstr, 2))
BUILTIN("strsplit-last", strsplit_last,
"If SEP occurs in STRING, return a cons where car and cdr are strings "
"split at the last occurence of SEP (as if searched by strrstr)."
"Otherwise return nil.",
ARG(string, STRING_CELL, "String to be split.")
ARG(sep, STRING_CELL, "Separator."),
GEN_INSN(strsplit_last, 2))
BUILTIN("hex", hex,
"Encode bytes in a string with hexadecimals.",
ARG(string, STRING_CELL, "String to convert."),
GEN_INSN(hex, 1))
MODULE_END
MODULE("Miscellaneous")
BUILTIN("print", print,
"Write arguments to the default destination (stdout, serial, log).",
REMAINING_ARGS(values, EMPTY_CELL, "Values to print."),
{
for (i = 1; i < expr->arity; i++) {
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr->u.ast[i]);
code->kind = HH_INSN;
code->u.insn = HH_INSN_print;
}
})
BUILTIN("snprint", snprint,
"Write last argument to a string of specified length and "
"given accuracy.",
ARG(max_length, INTEGER_CELL,
"Maximum string length, use -1 for unlimited.")
ARG(accuracy, INTEGER_CELL, "Depth or accuracy of the print.")
ARG(value, EMPTY_CELL, "Value to print."),
GEN_INSN(snprint, 3))
BUILTIN("panic", panic,
"Write panic message to log and terminate program.",
REMAINING_ARGS(strings, STRING_CELL, "Values to output."),
{
for (i = 1; i < expr->arity; i++) {
hh_gen(expr->u.ast[i], 0);
code = hh_alloc_code(expr->u.ast[i]);
code->kind = HH_INSN;
code->u.insn = HH_INSN_print;
}
code = hh_alloc_code(expr);
code->kind = HH_INSN;
code->u.insn = HH_INSN_panic;
})
BUILTIN("available-mem", available_mem,
"Return the number of free bytes (not including garbage).",
,
GEN_INSN(available_mem, 0))
BUILTIN("gc", gc,
"Do a hard garbage collection. Collect all garbage. This may "
"take a some time, so you should call this only when "
"you know the program has nothing interesting to do. The Hedgehog "
"Lisp implementation collects garbage as necessary, so it is never "
"necessary to call this function. If you want to clear out large "
"amounts of garbage at once, and have nothing better to do, then "
"do call this function.",
,
GEN_INSN(gc, 0))
BUILTIN("hedgehog-version", hedgehog_version,
"Return a string giving the version of the Hedgehog LISP "
"implementation. The version consists of three decimal integers "
"separated by dots. In CVS versions, the integers may be negative.",
,
GEN_INSN(hedgehog_version, 0))
MODULE_END
MODULE("Crypto")
BUILTIN("shared-secret", shared_secret,
"Return a 32-byte string containing the shared secret (or rather its "
"256-bit cryptographic hash).",
,
GEN_INSN(shared_secret, 0))
BUILTIN("crypto-hash", crypto_hash,
"Return a 32-byte string containing the SHA-256 digest of the "
"argument string.",
ARG(data, STRING_CELL, "String to hash"),
GEN_INSN(crypto_hash, 1))
BUILTIN("crypto-cipher", crypto_chipher,
"Encrypt/decrypt the given string with the given password (with a "
"slightly beefed up variant of XXTEA).",
ARG(data, STRING_CELL, "String to en/decrypt")
ARG(encrypt, EMPTY_CELL, "True if encrypt, otherwise decrypt")
ARG(passwd, STRING_CELL, "Password, at least 16 bytes long"),
GEN_INSN(crypto_cipher, 3))
MODULE_END
#ifdef HH_UNIX
MODULE("UNIX")
BUILTIN("unix-gettimeofday", unix_gettimeofday,
"Return the current time, as a cons of seconds and microseconds since "
"the beginning of an unspecified epoch. See gettimeofday(2).",
,
GEN_INSN(unix_gettimeofday, 0))
BUILTIN("unix-fork", unix_fork,
"Perform UNIX fork.",
,
GEN_INSN(unix_fork, 0))
BUILTIN("unix-wait", unix_wait,
"Perform a non-blocking wait, return t if a child process has exited.",
,
GEN_INSN(unix_wait, 0))
BUILTIN("unix-exec", unix_exec,
"Perform UNIX execve. The first and second argument have "
"identical meaning to execve (except we expect list of strings). "
"The third argument must currently be an empty list, but may "
"in future designate changes in the environment.",
ARG(filename, STRING_CELL, "Program to execute")
ARG(argv, CONS_CELL, "List of strings passed to the program as argv")
ARG(envp, CONS_CELL, "Environment, but nil for now"),
GEN_INSN(unix_exec, 3))
BUILTIN("unix-dup2", unix_dup2,
"Call the Unix dup2 system call. Return -1 for error.",
ARG(from_fd, INTEGER_CELL, "Original file descriptor.")
ARG(to_fd, INTEGER_CELL, "New file descriptor."),
GEN_INSN(unix_dup2, 2))
BUILTIN("unix-close", unix_close,
"Close an open file descriptor on Unix.",
ARG(fd, INTEGER_CELL, "The file descriptor."),
GEN_INSN(unix_close, 1))
BUILTIN("unix-select", unix_select,
"Perform UNIX select.",
ARG(secs, INTEGER_CELL, "Max wait time seconds,")
ARG(usecs, INTEGER_CELL, "and milliseconds."),
GEN_INSN(unix_select, 2))
BUILTIN("unix-add-to-read-fds", unix_add_to_read_fds,
"Add the given fd to the next select(2)'s read fd set.",
ARG(fd, INTEGER_CELL, "File descriptor."),
GEN_INSN(unix_add_to_read_fds, 1))
BUILTIN("unix-add-to-write-fds", unix_add_to_write_fds,
"Add the given fd to the next select(2)'s write fd set.",
ARG(fd, INTEGER_CELL, "File descriptor."),
GEN_INSN(unix_add_to_write_fds, 1))
BUILTIN("unix-fd-is-readable", unix_fd_is_readable,
"Is the given fd readable according to the latest select(2).",
ARG(fd, INTEGER_CELL, "File descriptor."),
GEN_INSN(unix_fd_is_readable, 1))
BUILTIN("unix-fd-is-writable", unix_fd_is_writable,
"Is the given fd writable according to the latest select(2).",
ARG(fd, INTEGER_CELL, "File descriptor."),
GEN_INSN(unix_fd_is_writable, 1))
BUILTIN("unix-clr-fdsets", unix_clr_fdsets,
"Clear the fd sets in preparation for the next select(2).",
,
GEN_INSN(unix_clr_fdsets, 0))
BUILTIN("unix-dir-list", unix_dir_list,
"Return a list of file names in the given directory.",
ARG(dirname, STRING_CELL, "Directory name."),
GEN_INSN(unix_dir_list, 1))