eval-string.scm 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. ;;; Evaluating code from users
  2. ;;; Copyright (C) 2011, 2013 Free Software Foundation, Inc.
  3. ;;;; This library is free software; you can redistribute it and/or
  4. ;;;; modify it under the terms of the GNU Lesser General Public
  5. ;;;; License as published by the Free Software Foundation; either
  6. ;;;; version 3 of the License, or (at your option) any later version.
  7. ;;;;
  8. ;;;; This library is distributed in the hope that it will be useful,
  9. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;;;; Lesser General Public License for more details.
  12. ;;;;
  13. ;;;; You should have received a copy of the GNU Lesser General Public
  14. ;;;; License along with this library; if not, write to the Free Software
  15. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16. ;;; Code:
  17. (define-module (ice-9 eval-string)
  18. #:use-module (system base compile)
  19. #:use-module (system base language)
  20. #:use-module (system vm loader)
  21. #:replace (eval-string))
  22. (define (ensure-language x)
  23. (if (language? x)
  24. x
  25. (lookup-language x)))
  26. (define* (read-and-eval port #:key (lang (current-language)))
  27. (parameterize ((current-language (ensure-language lang)))
  28. (define (read)
  29. ((language-reader (current-language)) port (current-module)))
  30. (define (eval exp)
  31. ((language-evaluator (current-language)) exp (current-module)))
  32. (let ((exp (read)))
  33. (if (eof-object? exp)
  34. ;; The behavior of read-and-compile and of the old
  35. ;; eval-string.
  36. *unspecified*
  37. (let lp ((exp exp))
  38. (call-with-values
  39. (lambda () (eval exp))
  40. (lambda vals
  41. (let ((next (read)))
  42. (cond
  43. ((eof-object? next)
  44. (apply values vals))
  45. (else
  46. (lp next)))))))))))
  47. (define* (eval-string str #:key
  48. (module (current-module))
  49. (file #f)
  50. (line #f)
  51. (column #f)
  52. (lang (current-language))
  53. (compile? #f))
  54. (define (maybe-with-module module thunk)
  55. (if module
  56. (save-module-excursion
  57. (lambda ()
  58. (set-current-module module)
  59. (thunk)))
  60. (thunk)))
  61. (let ((lang (ensure-language lang)))
  62. (call-with-input-string
  63. str
  64. (lambda (port)
  65. (maybe-with-module
  66. module
  67. (lambda ()
  68. (if module
  69. (set-current-module module))
  70. (if file
  71. (set-port-filename! port file))
  72. (if line
  73. (set-port-line! port line))
  74. (if column
  75. (set-port-column! port line))
  76. (if (or compile? (not (language-evaluator lang)))
  77. ((load-thunk-from-memory
  78. (read-and-compile port #:from lang #:to 'bytecode)))
  79. (read-and-eval port #:lang lang))))))))