-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Exercise_5_36.rkt
39 lines (36 loc) · 1.55 KB
/
Exercise_5_36.rkt
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
#lang racket/base
;; see exercise 3.8
;; compiled code evaluate subexpressions from right to left
;; from left to right:
(define (construct-arglist operand-codes) ;; don't revert operand list
(if (null? operand-codes)
(make-instruction-sequence '() '(argl)
'((assign argl (const ()))))
(let ([code-to-get-first-arg
(append-instruction-sequences
(car operand-codes)
(make-instruction-sequence '(val) '(argl)
'((assign argl (op list) (reg val)))))])
(if (null? (cdr operand-codes))
code-to-get-first-arg
(preserving '(env)
code-to-get-first-arg
(code-to-get-rest-args
(cdr operand-codes)))))))
(define (code-to-get-rest-args operand-codes)
(let ([code-for-next-arg
(preserving '(argl)
(car operand-codes)
(make-instruction-sequence
'(val argl)
'(argl)
'((assign val (op list) (reg val)) ;; ***
(assign argl
(op append) (reg argl) (reg val)))))]) ;; ***
(if (null? (cdr operand-codes))
code-for-next-arg
(preserving '(env)
code-for-next-arg
(code-to-get-rest-args (cdr operand-codes))))))
;; now the compiler don't need to reverse argument list but will
;; add an assign command in the compiled code for each argument