node-letrec.scm 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. #:use-module (ps-compiler node variable)
  19. #:export (put-in-letrec make-letrec))
  20. ;;-------------------------------------------------------------------------------
  21. ;; Bind VARS to VALUES using letrec at CALL. If CALL is already a letrec
  22. ;; call, just add to it, otherwise make a new one.
  23. (define (put-in-letrec vars values call)
  24. (cond ((eq? 'letrec2 (primop-id (call-primop call)))
  25. (let ((binder (node-parent call)))
  26. (mark-changed call)
  27. (for-each (lambda (var)
  28. (set-variable-binder! var binder))
  29. vars)
  30. (set-lambda-variables! binder
  31. (append (lambda-variables binder) vars))
  32. (for-each (lambda (value)
  33. (append-call-arg call value))
  34. values)))
  35. (else
  36. (move-body
  37. call
  38. (lambda (call)
  39. (receive (letrec-call letrec-cont)
  40. (make-letrec vars values)
  41. (attach-body letrec-cont call)
  42. letrec-call))))))
  43. (define (make-letrec vars vals)
  44. (let ((cont (make-lambda-node 'c 'cont '())))
  45. (let-nodes ((call (letrec1 1 l2))
  46. (l2 ((x #f) . vars) (letrec2 1 cont (* x) . vals)))
  47. (values call cont))))