node-letrec.scm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey, Mike Sperber
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/node/node-util.scm
  8. ;;;
  9. ;;; This file contains miscellaneous utilities for accessing and modifying the
  10. ;;; node tree.
  11. ;;;
  12. (define-module (ps-compiler node node-letrec)
  13. #:use-module (prescheme scheme48)
  14. #:use-module (ps-compiler node let-nodes)
  15. #:use-module (ps-compiler node node)
  16. #:use-module (ps-compiler node node-util)
  17. #:use-module (ps-compiler node primop)
  18. #:export (put-in-letrec make-letrec))
  19. ;;-------------------------------------------------------------------------------
  20. ;; Bind VARS to VALUES using letrec at CALL. If CALL is already a letrec
  21. ;; call, just add to it, otherwise make a new one.
  22. (define (put-in-letrec vars values call)
  23. (cond ((eq? 'letrec2 (primop-id (call-primop call)))
  24. (let ((binder (node-parent call)))
  25. (mark-changed call)
  26. (for-each (lambda (var)
  27. (set-variable-binder! var binder))
  28. vars)
  29. (set-lambda-variables! binder
  30. (append (lambda-variables binder) vars))
  31. (for-each (lambda (value)
  32. (append-call-arg call value))
  33. values)))
  34. (else
  35. (move-body
  36. call
  37. (lambda (call)
  38. (receive (letrec-call letrec-cont)
  39. (make-letrec vars values)
  40. (attach-body letrec-cont call)
  41. letrec-call))))))
  42. (define (make-letrec vars vals)
  43. (let ((cont (make-lambda-node 'c 'cont '())))
  44. (let-nodes ((call (letrec1 1 l2))
  45. (l2 ((x #f) . vars) (letrec2 1 cont (* x) . vals)))
  46. (values call cont))))