Skip to content

Instantly share code, notes, and snippets.

@rgm
Last active August 29, 2015 13:57
Show Gist options
  • Save rgm/9803879 to your computer and use it in GitHub Desktop.
Save rgm/9803879 to your computer and use it in GitHub Desktop.
diff --git a/metacircular.scm b/metacircular.scm
index 20542d1..97632e5 100644
--- a/metacircular.scm
+++ b/metacircular.scm
@@ -10,26 +10,43 @@
env))
((begin? exp) (eval-sequence (begin-actions exp) env))
((cond? exp) (eval (cond->if exp) env))
- ((application? exp) (apply (eval (operator exp) env)
- (list-of-values (operands exp) env)))
+ ((application? exp) (apply (actual-value (operator exp) env)
+ (operands exp)
+ env))
(else
(error "Unknown expression type -- EVAL" exp))))
(define apply-in-underlying-scheme apply) ;; need it later for primitives
-(define (apply procedure arguments)
+(define (apply procedure arguments env)
(cond ((primitive-procedure? procedure)
- (apply-primitive-procedure procedure arguments))
+ (apply-primitive-procedure procedure
+ (list-of-arg-values arguments env)))
((compound-procedure? procedure)
- (eval-sequence
- (procedure-body procedure)
- (extend-environment
- (procedure-parameters procedure)
- arguments
- (procedure-environment procedure))))
+ (eval-sequence (procedure-body procedure)
+ (extend-environment (procedure-parameters procedure)
+ (list-of-delayed-args arguments env)
+ (procedure-environment procedure))))
(else
(error "Unknown procedure type -- APPLY" procedure))))
+;;; thunks
+
+(define (actual-value exp env)
+ (force-it (eval exp env)))
+
+(define (list-of-arg-values exps env)
+ (if (no-operands? exps)
+ '()
+ (cons (actual-value (first-operand exps) env)
+ (list-of-arg-values (rest-operands exps) env))))
+
+(define (list-of-delayed-args exps env)
+ (if (no-operands? exps)
+ '()
+ (cons (delay-it (first-operand exps) env)
+ (list-of-delayed-args (rest-operands exps) env))))
+
;;; predicates/selectors
(define (self-evaluating? exp)
@@ -144,7 +161,7 @@
'ok)
(define (eval-if exp env)
- (if (eval (if-predicate exp) env)
+ (if (actual-value (if-predicate exp) env)
(eval (if-consequent exp) env)
(eval (if-alternative exp) env)))
@@ -257,8 +274,8 @@
;;; REPL
-(define input-prompt ";;; M-Eval input")
-(define output-prompt ";;; M-Eval output")
+(define input-prompt ";;; L-Eval input")
+(define output-prompt ";;; L-Eval output")
(define (prompt-for-input string)
(newline)
@@ -282,7 +299,7 @@
(define (driver-loop)
(prompt-for-input input-prompt)
(let ((input (read)))
- (let ((output (eval input the-global-environment)))
+ (let ((output (actual-value input the-global-environment)))
(announce-output output-prompt)
(user-print output)))
(driver-loop))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment