eval-string.scm 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 program)
  21. #:use-module (system vm loader)
  22. #:replace (eval-string))
  23. (define (ensure-language x)
  24. (if (language? x)
  25. x
  26. (lookup-language x)))
  27. (define* (read-and-eval port #:key (lang (current-language)))
  28. (parameterize ((current-language (ensure-language lang)))
  29. (define (read)
  30. ((language-reader (current-language)) port (current-module)))
  31. (define (eval exp)
  32. ((language-evaluator (current-language)) exp (current-module)))
  33. (let ((exp (read)))
  34. (if (eof-object? exp)
  35. ;; The behavior of read-and-compile and of the old
  36. ;; eval-string.
  37. *unspecified*
  38. (let lp ((exp exp))
  39. (call-with-values
  40. (lambda () (eval exp))
  41. (lambda vals
  42. (let ((next (read)))
  43. (cond
  44. ((eof-object? next)
  45. (apply values vals))
  46. (else
  47. (lp next)))))))))))
  48. (define* (eval-string str #:key
  49. (module (current-module))
  50. (file #f)
  51. (line #f)
  52. (column #f)
  53. (lang (current-language))
  54. (compile? #f))
  55. (define (maybe-with-module module thunk)
  56. (if module
  57. (save-module-excursion
  58. (lambda ()
  59. (set-current-module module)
  60. (thunk)))
  61. (thunk)))
  62. (let ((lang (ensure-language lang)))
  63. (call-with-input-string
  64. str
  65. (lambda (port)
  66. (maybe-with-module
  67. module
  68. (lambda ()
  69. (if module
  70. (set-current-module module))
  71. (if file
  72. (set-port-filename! port file))
  73. (if line
  74. (set-port-line! port line))
  75. (if column
  76. (set-port-column! port line))
  77. (if (or compile? (not (language-evaluator lang)))
  78. ((load-thunk-from-memory
  79. (read-and-compile port #:from lang #:to 'bytecode)))
  80. (read-and-eval port #:lang lang))))))))