test-eval.scm 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. ;;; Copyright (C) 2024, 2025 Igalia, S.L.
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. (use-modules (srfi srfi-64)
  15. (test utils))
  16. (test-begin "test-eval")
  17. (with-imports
  18. ((hoot tree-il)
  19. (hoot primitive-eval)
  20. (hoot modules)
  21. (hoot interaction-environment)
  22. ;; FIXME: error-handling and exceptions needed to
  23. ;; initialize $make-match-error, $raise-exception et al.
  24. (hoot error-handling)
  25. (hoot exceptions)
  26. (hoot syntax))
  27. ;; FIXME: Can't create Tree-IL in one module and eval it in
  28. ;; another, because of record generativity.
  29. (test-call "42"
  30. (lambda ()
  31. (primitive-eval (make-const #f 42) (make-empty-module)))))
  32. (with-imports
  33. ((hoot tree-il)
  34. (hoot eval)
  35. (hoot modules)
  36. (hoot interaction-environment)
  37. ;; FIXME: error-handling and exceptions needed to
  38. ;; initialize $make-match-error, $raise-exception et al.
  39. (hoot error-handling)
  40. (hoot exceptions)
  41. (hoot syntax))
  42. (define-syntax test-eval
  43. (lambda (stx)
  44. (syntax-case stx ()
  45. ((_ form)
  46. (let ((repr (object->string (primitive-eval (syntax->datum #'form)))))
  47. #`(test-call #,repr
  48. (lambda (exp)
  49. (eval exp (make-empty-module)))
  50. 'form))))))
  51. (test-eval 42)
  52. (test-eval '42)
  53. (test-eval (let ((x 42) (y 100))
  54. (set! x 69)
  55. x))
  56. (test-eval (let* ((x 42) (y 100))
  57. (set! x 69)
  58. x))
  59. (test-eval ((lambda (x y) x) 42 69))
  60. (test-eval ((lambda (x y) y) 42 69))
  61. (with-additional-imports ((scheme case-lambda))
  62. (test-eval ((case-lambda ((x) x) ((x y) y)) 42))
  63. (test-eval ((case-lambda ((x) x) ((x y) y)) 42 69)))
  64. (test-eval (if #t 42 69))
  65. (test-eval (if #f 42 69))
  66. (test-eval (let ((x 42))
  67. (when #t (set! x 69))
  68. x))
  69. (test-eval (let ((x 42))
  70. (unless #t (set! x 69))
  71. x))
  72. (test-eval (let lp ((x 42))
  73. (if x
  74. (lp #f)
  75. 69)))
  76. (test-eval (letrec ((a (lambda () (b 42)))
  77. (b (lambda (x) (c x 69)))
  78. (c (lambda (x y) x)))
  79. (a)))
  80. (test-eval (cond
  81. (#f 42)
  82. (else 69)))
  83. (test-eval (cond
  84. (#t 42)
  85. (else 69)))
  86. (test-eval (cond
  87. (42)
  88. (else 69)))
  89. (test-eval (case 42
  90. ((42) #t)
  91. (else #f)))
  92. (test-eval (case 42
  93. ((69) #t)
  94. (else #f)))
  95. (test-eval (let ((x 42))
  96. 'what
  97. (define (y) (z x))
  98. (define (z q) q)
  99. (y)))
  100. (with-additional-imports ((only (hoot numbers) 1+))
  101. (test-call "(2 3 4)"
  102. (lambda (exp)
  103. (eval exp (interaction-environment)))
  104. '(map 1+ '(1 2 3)))))
  105. (test-end* "test-eval")