Skip to content

Commit

Permalink
ex5.32
Browse files Browse the repository at this point in the history
  • Loading branch information
nwg committed Dec 4, 2019
1 parent 5410809 commit 454a5d9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
18 changes: 17 additions & 1 deletion chapter-5/4/chapter-5.4.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@
(define (make-begin seq) (cons 'begin seq))

(define (application? exp) (pair? exp))
(define (application-simple? exp) (and (pair? exp) (symbol? (car exp))))
(define (operator exp) (car exp))
(define (operands exp) (cdr exp))
(define (no-operands? ops) (null? ops))
Expand Down Expand Up @@ -326,6 +327,7 @@
(list 'lambda? lambda?)
(list 'begin? begin?)
(list 'application? application?)
(list 'application-simple? application-simple?)
(list 'lookup-variable-value lookup-variable-value)
(list 'text-of-quotation text-of-quotation)
(list 'lambda-parameters lambda-parameters)
Expand Down Expand Up @@ -428,6 +430,8 @@
(branch (label ev-lambda))
(test (op begin?) (reg exp))
(branch (label ev-begin))
(test (op application-simple?) (reg exp))
(branch (label ev-application-simple))
(test (op application?) (reg exp))
(branch (label ev-application))
(goto (label unknown-expression-type))
Expand Down Expand Up @@ -460,6 +464,17 @@
(reg env))
(goto (reg continue))

ev-application-simple
;(perform (op user-print) (const "in simple"))
(save continue)
(assign unev (op operands) (reg exp))
(assign exp (op operator) (reg exp))
(assign continue (label ev-appl-simple-did-operator))
(goto (label eval-dispatch))
ev-appl-simple-did-operator
(assign proc (reg val))
(goto (label ev-appl-operands))

ev-application
(save continue)
(save env)
Expand All @@ -473,8 +488,9 @@
ev-appl-did-operator
(restore unev) ; the operands
(restore env)
(assign argl (op empty-arglist))
(assign proc (reg val)) ; the operator
ev-appl-operands
(assign argl (op empty-arglist))
(test (op no-operands?) (reg unev))
(branch (label apply-dispatch))
(save proc)
Expand Down
15 changes: 15 additions & 0 deletions chapter-5/5/ex5.32.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
2.
- Although you could build many optimizations, you can never fully match compilation because
an interpreter still has to discover what type of expression is being evaluated.
This means for the expression `(f 84 96)` we still have to go through the eval-dispatch chain
to figure out it's an application. I can't think of a way to avoid eval-dispatch in the interpreter.
Also, we still have to test for end of list when iterating for each operand.
You couldn't really special case a function of, say, exactly two args because this would amount to examining
the length of the args.
- There are certain things you could do:
A compiler can exploit the structure of the particular expression it is processing to
generate code that avoids unnecessary stack operations.
However, you could build similar discovery code into an interpreter. This would
amount to basically compiling the code of and caching the register usage of the body of each lambda.
Then you could avoid unneeded stack operations by referencing
the cached modifies list before each application.

0 comments on commit 454a5d9

Please sign in to comment.