This repository has been archived by the owner on Oct 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhydra.ss
1407 lines (1362 loc) · 65 KB
/
hydra.ss
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 simple stack-based VM for Digamma
;; Meant to support both interpretation in Vesta as
;; well as compilation with E'
;; copyright 2011 Stefan Edwards; please see the LICENSE
;; file for details
;; TODO:
;; - variable arity functions should have some easier method for generating
;; their code, so as to reduce boiler plate. List, <, +, &c. have a lot of
;; repeated code in the code-generation side, that could be handled if they
;; could simply be marked as "variable arity" to the compiler. This arity marker
;; could be Z+ (integer >= 1) or -1 for true "variable arity", and they could be
;; given handlers for it. Simple with a syntax for "generate-variable-arity-primitive"
;; - DONE: good compilation mechanism for hydra@eval
;; - DONE: method for hydra@vm to manage things like (cons (car (cons 1 2)) (cdr (1 2)))
;; which it cannot currently do because we need to rotate the stack (wait, do we?)
;; (cons (car (cons 1 '())) (cdr 4 '())):
;; (4) ;; nil
;; (3 4) ;; load 4
;; (2) ;; cons
;; (1) ;; cdr
;; (4) ;; nil
;; (3 1) ;; load 1
;; (2) ;; cons
;; (0) ;; car
;; (2) ;; cons
;; works, so this isn't blocked. \o/
;; - lambda lifting: it would be nice if lambdas were lifted to avoid allocation as
;; much as possible
;; - define-instruction syntax that can be used to populate hydra@vm as well as
;; clean up redundancies in code (like manual calls to hydra@vm in each instruction)
;; - define a clean method of boxed representations of types, one that can be used from
;; Vesta or E'. Not sure if this is a decent use of time here, in a VM, since that
;; should be a function of the run time, not the interpreter (we wouldn't want Ceres
;; & Hydra to have different representations, for instance). Still, it would be nice
;; if errors & other types can be simply encoded '(error "error"); SRFI-9, esp. if it
;; has E' support, might be a good option (need to unbox types in E' though, for the
;; most efficient representation, as well as unions).
;; - Make all instructions support (type . value) pairs, so as to avoid a situation where
;; the user enters the code (0 (list 1 2 3)) and recieves the value 1 back, because:
;; (load 0)
;; (call) ;; call integer 0, since integers -> primitives, this can add some weird behavior.
;; - DONE: Just noticed that the way the CALL operand is implemented, the stack will no longer
;; hold the parameters. Need to walk over the params to a lambda & bind variables from the
;; stack before moving to running the code...
;; - add a debug variable, so that you could ,debug-engine at the REPL, and I wouldn't have to comment/uncomment
;; debug lines every time I wanted to check out what's going on underneath the hood
;; - DONE fix this:
;; h; 0
;; #<primitive-procedure 0>
;; it should be:
;; h; 0
;; 0
;; h; car
;; #<primitive-procedure 0>
;;
;; - DONE: I wonder if this should re-write to %define, so that I don't have
;; to do anything fancy with eval... There are three cases:
;; (define f literal)
;; (define f (fn (x) (+ x x)))
;; (define f (car '(1 2 3 4 5)))
;; the first two *can* be handled OOB by hydra@eval, but the third
;; cannot really be handled properly. It should be re-written to
;; (load (1 2 3 4 5))
;; (car)
;; (load f)
;; (%define)
;; - SRFIs to be added: 9, 22, 34, 35, 36, 57, 60, 89, 88 (already done, via Vesta's run time)
;; mini-prelude
;; should be removed once Enyalios supports load better...
(define (caar x) (car (car x)))
(define (cadr x) (car (cdr x)))
(define (cdar x) (cdr (car x)))
(define (cddr x) (cdr (cdr x)))
(define (caaar x) (car (car (car x))))
(define (caadr x) (car (car (cdr x))))
(define (cadar x) (car (cdr (car x))))
(define (caddr x) (car (cdr (cdr x))))
(define (cdaar x) (cdr (car (car x))))
(define (cdadr x) (cdr (car (cdr x))))
(define (cddar x) (cdr (cdr (car x))))
(define (cdddr x) (cdr (cdr (cdr x))))
(define (caaaar x) (car (car (car (car x)))))
(define (caaadr x) (car (car (car (cdr x)))))
(define (caadar x) (car (car (cdr (car x)))))
(define (caaddr x) (car (car (cdr (cdr x)))))
(define (cadaar x) (car (cdr (car (car x)))))
(define (cadadr x) (car (cdr (car (cdr x)))))
(define (caddar x) (car (cdr (cdr (car x)))))
(define (cadddr x) (car (cdr (cdr (cdr x)))))
(define (cdaaar x) (cdr (car (car (car x)))))
(define (cdaadr x) (cdr (car (car (cdr x)))))
(define (cdadar x) (cdr (car (cdr (car x)))))
(define (cdaddr x) (cdr (car (cdr (cdr x)))))
(define (cddaar x) (cdr (cdr (car (car x)))))
(define (cddadr x) (cdr (cdr (car (cdr x)))))
(define (cdddar x) (cdr (cdr (cdr (car x)))))
(define (cddddr x) (cdr (cdr (cdr (cdr x)))))
(define (null? n) (eq? n '()))
(define (pair? n) (eq? (type n) "Pair"))
(define (vector? n) (eq? (type n) "Vector"))
(define (dict? n) (eq? (type n) "Dictionary"))
(define (symbol? n) (eq? (type n) "Symbol"))
(define (key? n) (eq? (type n) "Key"))
(define (number? n) (eq? (type n) "Number"))
(define (string? n) (eq? (type n) "String"))
(define (bool? n) (eq? (type n) "Boolean"))
(define (goal? n) (eq? (type n) "Goal"))
(define (not x)
(cond
(eq? x #s) #u
(eq? x #f) #t
(eq? x #u) #s
else #f))
(define (zero? n) (= n 0))
(define (eof-object? n) (eq? n #e))
(define (void? x) (eq? x #v))
(define (zip xs ys)
(if (null? xs)
'()
(cons (cons (car xs) (cons (car ys) '())) (zip (cdr xs) (cdr ys)))))
(define (list-copy l)
" really, should be included from SRFI-1, but this simply makes a copy
of the spine of a pair
"
(if (null? l)
l
(cons (car l) (list-copy (cdr l)))))
(define (cadddar x) (car (cdddar x)))
;; naieve map, foreach, append-map
(define (map proc c)
(if (empty? c)
c
(ccons (proc (first c)) (map proc (rest c)))))
(define (foreach proc c)
(if (empty? c)
#v
(begin
(proc (first c))
(foreach proc (rest c)))))
(define (append-map f x)
(if (null? x)
x
(append (f (car x)) (append-map f (cdr x)))))
;; end mini-prelude.
(define (hydra@instruction c)
(car c))
(define (hydra@operand c)
(cadr c))
(define (dict-copy k dict new-dict)
"shallow copy a dictionary"
(if (null? k)
new-dict
(begin
(cset! new-dict (car k) (nth dict (car k)))
(dict-copy (cdr k) dict new-dict))))
(define (build-environment environment stack params)
"Adds a new window to the environment, removes |params| items from the stack
and binds those values in the new window. It returns a list of environment and
the new stack."
;; rough match; doesn't take into account optional parameters.
;; would probably be better to have an inner function that iterates over
;; the parameters & returns those that match. It would then be easier to
;; have optional parameters...
;; Oh! Don't just use environment, use (copy-dict environment)
;; actually, need to copy entire frame? ugh. That's going to be ugly
(let ((ls (length stack)) (lp (length params)) (nu-env (dict)))
(if (< ls lp)
(error "non-optional parameters are not statisfied by stack items in build-environment")
(if (= lp 0)
(list (cons nu-env environment) (cdr stack))
(begin
(foreach
(lambda (x)
(cset! nu-env (car x) (cadr x)))
(zip params (cslice stack 0 lp)))
(list (cons nu-env environment) (cslice stack lp ls)))))))
(define (copy-code code ip offset)
" copies the spine of code, but at ip & ip+1, insert %nop instructions
instead, over-writing the call/cc & load-lambda instructions therein.
"
(cond
(null? code) '()
(= offset (- ip 1)) (append (list '(107) '(107)) (copy-code (cddr code) ip (+ offset 2)))
else (append (list (car code)) (copy-code (cdr code) ip (+ offset 1)))))
(define (hydra@vm code env ip stack dump)
" process the actual instructions of a code object; the basic idea is that
the user enters:
h; (car (cdr (cons 1 (cons 2 '()))))
which is compiled by hydra@eval into:
(4) ;; nil
(3 2) ;; load 2
(2) ;; cons
(3 1) ;; load 1
(2) ;; cons
(1) ;; cdr
(0) ;; car
which hydra@vm can then interpret in a tail-call fashion.
There might be better ways to store the actual VM codes themselves
other than pairs of pairs (even vector of pairs would be more efficient really)
and it might be worth it to add two collexion-neutral primitives, cappend &
cappend!, to the collexion API. Also, adding named-enumerations to the language,
even if at the syntactic level, would be helpful. If (enumerate OPCAR OPCDR ...)
even compiled to
#define OPCAR 0
#define OPCDR 1
// ...
It would be more useful, and the names could be used throughout (and checked!)."
;; if this is moved to an IP-based (instruction pointer)
;; system, it might be easier to do this using a named-let rather than iterating
;; at the top level. E' should be able to lift named-lets pretty easily into whiles
;; and that would fit pretty well here. Also, moving to an inner-loop with named-let
;; means I can alliviate some of the duplication here; code & env are rarely modified
;; so it would also make sense to not have to pass them on every call. A lot of
;; duplication...
;; also, I would really like to have these all defined using a Scheme48-style
;; define-operator, since that would be much cleaner than what is seen below.
;; Syntax could expand the full list of operators in place here, and it would make
;; expanding the set of operators *much* easier than it currently is.
;(display "stack: ")
;(display stack)
;(newline)
;(display "code: ")
;(display code)
;(newline)
;(display "ip: ")
;(display ip)
;(newline)
(cond
(or (eq? (type (car stack)) "Error")
(hydra@error? (car stack)))
(car stack)
(>= ip (length code))
(if (null? dump)
(car stack)
(hydra@vm (caar dump) (cadar dump) (+ (caddar dump) 1) (cons (car stack) (cadddar dump)) (cdr dump)))
else
(let* ((c (nth code ip))
(instr (hydra@instruction c)))
;(display "current instruction: ")
;(display (nth code ip))
;(newline)
(cond ;; case would make a lot of sense here...
(eq? instr 0) ;; car
(hydra@vm code
env
(+ ip 1)
(cons (car (car stack)) (cdr stack)) dump)
(eq? instr 1) ;; cdr
(hydra@vm code
env
(+ ip 1)
(cons (cdr (car stack)) (cdr stack)) dump)
(eq? instr 2) ;; cons
(hydra@vm code
env
(+ ip 1)
(cons (cons (car stack)
(cadr stack))
(cddr stack)) dump)
(eq? instr 3) ;; load
(hydra@vm code
env
(+ ip 1)
(cons (hydra@operand c) stack) dump)
(eq? instr 4) ;; nil
(hydra@vm code
env
(+ ip 1)
(cons '() stack) dump)
(eq? instr 5) ;; -
(hydra@vm code
env
(+ ip 1)
(cons (- (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 6) ;; +
(hydra@vm code
env
(+ ip 1)
(cons (+ (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 7) ;; *
(hydra@vm code
env
(+ ip 1)
(cons (* (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 8) ;; /
(hydra@vm code
env
(+ ip 1)
(cons (/ (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 9) ;; <
(hydra@vm code
env
(+ ip 1)
(cons (< (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 10) ;; >
(hydra@vm code
env
(+ ip 1)
(cons (> (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 11) ;; <=
(hydra@vm code
env
(+ ip 1)
(cons (<= (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 12) ;; >=
(hydra@vm code
env
(+ ip 1)
(cons (>= (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 13) ;; length
(hydra@vm code
env
(+ ip 1)
(cons (length (car stack)) (cdr stack)) dump)
(eq? instr 14) ;; exact?
(hydra@vm code
env
(+ ip 1)
(cons (exact? (car stack)) (cdr stack)) dump)
(eq? instr 15) ;; inexact?
(hydra@vm code
env
(+ ip 1)
(cons (inexact? (car stack)) (cdr stack)) dump)
(eq? instr 16) ;; display
(begin
(display (car stack))
(hydra@vm code
env
(+ ip 1)
(cons #v (cdr stack)) dump))
(eq? instr 18) ;; real?
(hydra@vm code
env
(+ ip 1)
(cons (real? (car stack)) (cdr stack)) dump)
(eq? instr 19) ;; integer?
(hydra@vm code
env
(+ ip 1)
(cons (integer? (car stack)) (cdr stack)) dump)
(eq? instr 20) ;; complex?
(hydra@vm code
env
(+ ip 1)
(cons (complex? (car stack)) (cdr stack)) dump)
(eq? instr 21) ;; rational?
(hydra@vm code
env
(+ ip 1)
(cons (rational? (car stack)) (cdr stack)) dump)
(eq? instr 22) ;; gcd
(hydra@vm code
env
(+ ip 1)
(cons (gcd (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 23) ;; lcm
(hydra@vm code
env
(+ ip 1)
(cons (lcm (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 24) ;; numerator
(hydra@vm code
env
(+ ip 1)
(cons (numerator (car stack)) (cdr stack)) dump)
(eq? instr 25) ;; denomenator
(hydra@vm code
env
(+ ip 1)
(cons (denomenator (car stack)) (cdr stack)) dump)
(eq? instr 26) ;; =
(hydra@vm code
env
(+ ip 1)
(cons (= (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 27) ;; eq?
(hydra@vm code
env
(+ ip 1)
(cons (eq? (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 28) ;; jump
(hydra@vm code
env
(+ ip (hydra@operand c))
stack dump)
(eq? instr 29) ;; cmp
(if (car stack) ;; if the top of the stack is true
(hydra@vm code env (+ ip 1) (cdr stack) dump) ;; jump to the <then> portion
(hydra@vm code env (+ ip (hydra@operand c)) (cdr stack) dump))
(eq? instr 30) ;; call
(cond
(hydra@lambda? (car stack))
;; create a list from the current registers, cons this to dump, and
;; recurse over hydra@vm.
;; need to support CALLing primitives too, since they could be passed
;; in to HOFs...
(let ((env-and-stack (build-environment (nth (cadar stack) 0) (cdr stack) (nth (cadar stack) 2))))
(hydra@vm
(nth (cadar stack) 1)
(car env-and-stack)
0 '()
(cons (list code env ip (cadr env-and-stack)) dump)))
(hydra@primitive? (car stack)) ;; if primitives stored arity, slicing would be easy...
(begin
(display "in hydra@primitive\n\t")
(display (car stack))
(display "\n")
#t)
;;(hydra@procedure? (car stack))
;; #t
else
(begin
(display "in <else> of CALL\n")
#f))
(eq? instr 31) ;; environment-load; there is never a raw #f, so this is safe
(with r (hydra@lookup (hydra@operand c) env)
(if (eq? r #f)
#f
(hydra@vm
code
env
(+ ip 1)
(cons r stack)
dump)))
(eq? instr 32) ;; tail-call
(if (and (not (null? stack)) (eq? (caar stack) 'compiled-lambda))
(hydra@vm
(nth (cdar stack) 0)
(nth (cdar stack) 1)
0 '()
dump)
#f)
(eq? instr 33) ;; %define
(begin
(hydra@add-env! (car stack) (cadr stack) env)
(hydra@vm
code env (+ ip 1)
(cons #v stack)
dump))
(eq? instr 34) ;; %set!
(begin
(hydra@set-env! (car stack) (cadr stack) env)
(hydra@vm
code env (+ ip 1)
(cons #v stack)
dump))
(eq? instr 35) ;; ceil
(hydra@vm code
env
(+ ip 1)
(cons (ceil (car stack)) (cdr stack)) dump)
(eq? instr 36) ;; floor
(hydra@vm code
env
(+ ip 1)
(cons (floor (car stack)) (cdr stack)) dump)
(eq? instr 37) ;; truncate
(hydra@vm code
env
(+ ip 1)
(cons (truncate (car stack)) (cdr stack)) dump)
(eq? instr 38) ;; round
(hydra@vm code
env
(+ ip 1)
(cons (round (car stack)) (cdr stack)) dump)
(eq? instr 39) ;; inexact->exact
(hydra@vm code
env
(+ ip 1)
(cons (inexact->exact (car stack)) (cdr stack)) dump)
(eq? instr 40) ;; quotient
(hydra@vm code
env
(+ ip 1)
(cons (quotient (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 41) ;; modulo
(hydra@vm code
env
(+ ip 1)
(cons (modulo (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 42) ;; &
(hydra@vm code
env
(+ ip 1)
(cons (& (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 43) ;; |
(hydra@vm code
env
(+ ip 1)
(cons (| (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 44) ;; ^
(hydra@vm code
env
(+ ip 1)
(cons (^ (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 45) ;; ~
(hydra@vm code
env
(+ ip 1)
(cons (~ (car stack)) (cdr stack)) dump)
(eq? instr 46) ;; %list
;; take N items off the stack, create a list, and return it
(hydra@vm code
env
(+ ip 1)
(cons
(cslice (cdr stack) 0 (car stack))
(cslice (cdr stack) (car stack) (- (length stack) 1))) dump)
(eq? instr 47) ;; %vector
;; take N items off the stack, create a list, and return it
(hydra@vm code
env
(+ ip 1)
(cons
(coerce (cslice (cdr stack) 0 (car stack)) 'vector)
(cslice (cdr stack) (car stack) (- (length stack) 1))) dump)
(eq? instr 48) ;; %make-vector
(hydra@vm code
env
(+ ip 1)
(cons (make-vector (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 49) ;; %make-string
(hydra@vm code
env
(+ ip 1)
(cons (make-string (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 50) ;; %string
(hydra@vm code
env
(+ ip 1)
(cons
(apply string (cslice (cdr stack) 0 (car stack)))
(cslice (cdr stack) (car stack) (- (length stack) 1))) dump)
(eq? instr 51) ;; %append
(hydra@vm code
env
(+ ip 1)
(cons
(apply append (cslice (cdr stack) 0 (car stack)))
(cslice (cdr stack) (car stack) (- (length stack) 1))) dump)
(eq? instr 52) ;; first
(hydra@vm code
env
(+ ip 1)
(cons (first (car stack)) (cdr stack)) dump)
(eq? instr 53) ;; rest
(hydra@vm code
env
(+ ip 1)
(cons (rest (car stack)) (cdr stack)) dump)
(eq? instr 54) ;; ccons
(hydra@vm code
env
(+ ip 1)
(cons (ccons (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 55) ;; %nth
(hydra@vm code
env
(+ ip 1)
(cons (nth (caddr stack) (cadr stack) (car stack)) (cdddr stack)) dump)
(eq? instr 56) ;; keys
(hydra@vm code
env
(+ ip 1)
(cons (keys (car stack)) (cdr stack)) dump)
(eq? instr 57) ;; partial-key?
(hydra@vm code
env
(+ ip 1)
(cons (partial-key? (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 58) ;; cset!
(begin
(cset! (caddr stack) (cadr stack) (car stack))
(hydra@vm code
env
(+ ip 1)
(cons #v (cdddr stack)) dump))
(eq? instr 59) ;; empty?
(hydra@vm code
env
(+ ip 1)
(cons (empty? (car stack)) (cdr stack)) dump)
(eq? instr 60) ;; gensym
(hydra@vm code
env
(+ ip 1)
(cons (gensym (car stack)) (cdr stack)) dump)
(eq? instr 61) ;; imag-part
(hydra@vm code
env
(+ ip 1)
(cons (imag-part (car stack)) (cdr stack)) dump)
(eq? instr 62) ;; real-part
(hydra@vm code
env
(+ ip 1)
(cons (real-part (car stack)) (cdr stack)) dump)
(eq? instr 63) ;; make-rectangular
(hydra@vm code
env
(+ ip 1)
(cons (make-rectangular (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 64) ;; make-polar
(hydra@vm code
env
(+ ip 1)
(cons (make-polar (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 65) ;; magnitude
(hydra@vm code
env
(+ ip 1)
(cons (magnitude (car stack)) (cdr stack)) dump)
(eq? instr 66) ;; argument
(hydra@vm code
env
(+ ip 1)
(cons (argument (car stack)) (cdr stack)) dump)
(eq? instr 67) ;; conjugate
(hydra@vm code
env
(+ ip 1)
(cons (conjugate (car stack)) (cdr stack)) dump)
(eq? instr 68) ;; conjugate
(hydra@vm code
env
(+ ip 1)
(cons (conjugate! (car stack)) (cdr stack)) dump)
(eq? instr 69) ;; polar->rectangular
(hydra@vm code
env
(+ ip 1)
(cons (polar->rectangular (car stack)) (cdr stack)) dump)
(eq? instr 70) ;; rectangular->polar
(hydra@vm code
env
(+ ip 1)
(cons (rectangular->polar (car stack)) (cdr stack)) dump)
(eq? instr 71) ;; sin
(hydra@vm code
env
(+ ip 1)
(cons (sin (car stack)) (cdr stack)) dump)
(eq? instr 72) ;; cos
(hydra@vm code
env
(+ ip 1)
(cons (cos (car stack)) (cdr stack)) dump)
(eq? instr 73) ;; tan
(hydra@vm code
env
(+ ip 1)
(cons (tan (car stack)) (cdr stack)) dump)
(eq? instr 74) ;; asin
(hydra@vm code
env
(+ ip 1)
(cons (asin (car stack)) (cdr stack)) dump)
(eq? instr 75) ;; acos
(hydra@vm code
env
(+ ip 1)
(cons (acos (car stack)) (cdr stack)) dump)
(eq? instr 76) ;; atan
(hydra@vm code
env
(+ ip 1)
(cons (atan (car stack)) (cdr stack)) dump)
(eq? instr 77) ;; atan2
(hydra@vm code
env
(+ ip 1)
(cons (atan2 (cadr stack) (car stack)) (cddr stack)) dump)
(eq? instr 78) ;; sinh
(hydra@vm code
env
(+ ip 1)
(cons (sinh (car stack)) (cdr stack)) dump)
(eq? instr 79) ;; cosh
(hydra@vm code
env
(+ ip 1)
(cons (cosh (car stack)) (cdr stack)) dump)
(eq? instr 80) ;; tanh
(hydra@vm code
env
(+ ip 1)
(cons (tanh (car stack)) (cdr stack)) dump)
(eq? instr 81) ;; exp
(hydra@vm code
env
(+ ip 1)
(cons (exp (car stack)) (cdr stack)) dump)
(eq? instr 82) ;; ln
(hydra@vm code
env
(+ ip 1)
(cons (ln (car stack)) (cdr stack)) dump)
(eq? instr 83) ;; abs
(hydra@vm code
env
(+ ip 1)
(cons (abs (car stack)) (cdr stack)) dump)
(eq? instr 84) ;; sqrt
(hydra@vm code
env
(+ ip 1)
(cons (sqrt (car stack)) (cdr stack)) dump)
(eq? instr 85) ;; exp2
(hydra@vm code
env
(+ ip 1)
(cons (exp2 (car stack)) (cdr stack)) dump)
(eq? instr 86) ;; expm1
(hydra@vm code
env
(+ ip 1)
(cons (expm1 (car stack)) (cdr stack)) dump)
(eq? instr 87) ;; log2
(hydra@vm code
env
(+ ip 1)
(cons (log2 (car stack)) (cdr stack)) dump)
(eq? instr 88) ;; log10
(hydra@vm code
env
(+ ip 1)
(cons (log10 (car stack)) (cdr stack)) dump)
(eq? instr 89) ;; <<
(hydra@vm code
env
(+ ip 1)
(cons (<< (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 90) ;; >>
(hydra@vm code
env
(+ ip 1)
(cons (>> (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 91) ;; %string-append
(hydra@vm code
env
(+ ip 1)
(cons (exp2 (car stack)) (cdr stack)) dump)
(eq? instr 92) ;; assq
(hydra@vm code
env
(+ ip 1)
(cons (assq (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 93) ;; memq
(hydra@vm code
env
(+ ip 1)
(cons (memq (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 94) ;; %dict
(hydra@vm code
env
(+ ip 1)
(cons (exp2 (car stack)) (cdr stack)) dump)
(eq? instr 95) ;; make-dict
(hydra@vm code
env
(+ ip 1)
(cons (dict) stack) dump)
(eq? instr 96) ;; dict-has?
(hydra@vm code
env
(+ ip 1)
(cons (dict-has? (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 97) ;; coerce
(hydra@vm code
env
(+ ip 1)
(cons (coerce (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 98) ;; cupdate
(hydra@vm code
env
(+ ip 1)
(cons (cupdate (car stack) (cadr stack) (caddr stack)) (cdddr stack)) dump)
(eq? instr 99) ;; cslice
(hydra@vm code
env
(+ ip 1)
(cons (cslice (car stack) (cadr stack) (caddr stack)) (cdddr stack)) dump)
(eq? instr 100) ;; tconc!
(hydra@vm code
env
(+ ip 1)
(cons (exp2 (car stack)) (cdr stack)) dump)
(eq? instr 101) ;; make-tconc
(hydra@vm code
env
(+ ip 1)
(cons (exp2 (car stack)) (cdr stack)) dump)
(eq? instr 102) ;; tconc-list
(hydra@vm code
env
(+ ip 1)
(cons (exp2 (car stack)) (cdr stack)) dump)
(eq? instr 103) ;; tconc->pair
(hydra@vm code
env
(+ ip 1)
(cons (exp2 (car stack)) (cdr stack)) dump)
(eq? instr 104) ;; tconc-splice
(hydra@vm code
env
(+ ip 1)
(cons (exp2 (car stack)) (cdr stack)) dump)
(eq? instr 105) ;; rationalize
(hydra@vm code
env
(+ ip 1)
(cons (rationalize (car stack) (cadr stack)) (cddr stack)) dump)
(eq? instr 106) ;; call/cc
(let ((retcode (hydra@vm (cons (list 3 (car stack)) (list (list 30)))
env
0
(cons (list 'continuation (copy-code code ip 0) ip env stack dump) '())
'())))
(hydra@vm code
env
(+ ip 1)
(cons retcode (cdr stack))
dump))
(eq? instr 107) ;; %nop
(hydra@vm code
env
(+ ip 1)
stack
dump)
(eq? instr 108) ;; %ap
(let ((cont-code (car stack))
(v (cadr stack)))
(hydra@vm
(nth cont-code 1)
(nth cont-code 3)
0
(cons
v
(nth cont-code 4))
(nth cont-code 5)))
(eq? instr 109) ;; %makeclosure
;; makeclosure should rebuild the lambda that it is "enclosing"
;; to ensure clean enclosing, rather than using cset, which just
;; gives us the same problem we have now... duh (well, when you
;; think about it, sure, but the naive approach? sure).
(begin
;;(cset! (cadar stack) 0 env)
(hydra@vm
code
env
(+ ip 1)
(cons
(list 'compiled-lambda
(vector
(list-copy env)
(nth (cadar stack) 1)
(nth (cadar stack) 2)))
(cdr stack))
dump))))))
; syntax to make the above nicer:
; (define-instruction := "numeq" '() '() (+ ip 1) (cons (= (car stack) (cadr stack)) (cddr stack)))
; (define-instruction '() "jump" '() '() '() (if (car stack) ...))
; this should also fill in the top-level environment for those that have non-null name
; (define-instruction name short-description code-manip stack-manip IP-manip resulting code)
; (primitive 0) can still be defeated, I think:
; (define foo '(primitive 0))
; (foo '(1 2 3 4))
; Definitely can be defeated. Really, need to move to some SRFI-9-ish system that
; users cannot create their own versions of.
; there are two ways of dealing with arity, both have upsides & downsides:
; 0 - encode arity of primitives here, in the actual primitive notation.
; + this allows hydra@compile to know the proper arity, and signal an error.
; + it also means that hydra@vm has to suddenly change: it must now unpack the actual opcode
; before running (this might not be too bad...)
; 1 - encode the arity of primitives in a separate array.
; + this allows hydra@compile to remain unchanged, and only minimal changes to hydra@vm
; + this does not confer the benefits that the above does (that hydra@compile can
; know about the arity of primitives & signal failure during code generation).
(define *tlenv* '({
:car (primitive . 0) ;; (primitive . 0)
:cdr (primitive . 1)
:cons (primitive . 2)
:%load (primitive . 3) ;; 3 is load a value onto the stack
:%nil (primitive . 4) ;; 4 is push a nil onto the stack
:%- (primitive . 5) ;; primitive math operations with arity 2
:%+ (primitive . 6)
:%* (primitive . 7)
:%/ (primitive . 8)
:%< (primitive . 9)
:%> (primitive . 10)
:%<= (primitive . 11)
:%>= (primitive . 12)
:if (syntax . primitive-syntax-if)
:fn (syntax . primitive-syntax-fn)
:lambda (syntax . primitive-syntax-fn)
:begin (syntax . primitive-syntax-begin)
:quote (syntax . primitive-syntax-quote)
:quasi-quote (syntax . primitive-syntax-qquote)
:unquote (syntax . primitve-syntax-unquote)
:unquote-splice (syntax . primitive-syntax-unqsplice)
:length (primitive . 13)
:exact? (primitive . 14)
:inexact? (primitive . 15)
:display (primitive . 16)
:apply (primitive . 17)
:real? (primitive . 18)
:integer? (primitive . 19)
:complex? (primitive . 20)
:rational? (primitive . 21)
:gcd (primitive . 22)
:lcm (primitive . 23)
:numerator (primitive . 24)
:denomenator (primitive . 25)
:%= (primitive . 26) ;; probably has to place the value on stack rather than #t, #f for failure
:eq? (primitive . 27)
:%jmp (primitive . 28) ;; 28 is jump
:%cmp (primitive . 29) ;; 29 is compare
:%call (primitive . 30) ;; 30 is call
:%env-load (primitive . 31) ;; 31 is environment-load
:%tail-call (primitive . 32) ;; 32 is tail-call; is this necessary? operand to CALL?
:+ (syntax . primitive-syntax-plus) ;; variable arity syntax
:- (syntax . primitive-syntax-minus)
:* (syntax . primitive-syntax-mult)
:/ (syntax . primitive-syntax-div)
:< (syntax . primitive-syntax-lt)
:> (syntax . primitive-syntax-gt)
:<= (syntax . primitive-syntax-lte)
:>= (syntax . primitive-syntax-gte)
:= (syntax . primitive-syntax-numeq)
:define (syntax . primitive-syntax-define)
:set! (syntax . primitive-syntax-set)
:define-syntax (syntax . primitive-syntax-defsyn)
:define-macro (syntax . primitive-syntax-defmac)
:%define (primitive . 33)
:%set! (primitive . 34)
:ceil (primitive . 35)
:floor (primitive . 36)
:truncate (primitive . 37)
:round (primitive . 38)
:inexact->exact (primitive . 39)
:quotient (primitive . 40)
:modulo (primitive . 41)
:& (primitive . 42)
:| (primitive . 43)
:^ (primitive . 44)
:~ (primitive . 45)
:%list (primitive . 46)
:list (syntax . primitive-syntax-list)
:%vector (primitive . 47)
:vector (syntax . primitive-syntax-vector)
:%make-vector (primitive . 48)
:make-vector (syntax . primitive-syntax-makevector)
:%make-string (primitive . 49)
:make-string (syntax . primitive-syntax-makestring)
:%string (primitive . 50)
:string (syntax . primitive-syntax-string)
:%append (primitive . 51)
:append (syntax . primitive-syntax-append)
:first (primitive . 52)
:rest (primitive . 53)
:ccons (primitive . 54)
:nth (primitive . 55)
:keys (primitive . 56)
:partial-key? (primitive . 57)
:cset! (primitive . 58)
:empty? (primitive . 59)
:define-macro (syntax . primitive-syntax-defmac)
:gensym (primitive . 60)
:imag-part (primitive . 61)
:real-part (primitive . 62)
:make-rectangular (primitive . 63)
:make-polar (primitive . 64)
:magnitude (primitive . 65)