test-eval.scm 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ;;; Copyright (C) 2024 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 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. (eval (make-const #f 42) (make-empty-module))))
  32. (define-syntax test-eval
  33. (lambda (stx)
  34. (syntax-case stx ()
  35. ((_ form)
  36. (let ((repr (object->string (primitive-eval (syntax->datum #'form)))))
  37. #`(test-call #,repr
  38. (lambda (exp)
  39. (eval exp (make-empty-module)))
  40. 'form))))))
  41. (test-eval 42)
  42. (test-eval '42)
  43. (test-eval (let ((x 42) (y 100))
  44. (set! x 69)
  45. x))
  46. (test-eval (let* ((x 42) (y 100))
  47. (set! x 69)
  48. x))
  49. (test-eval ((lambda (x y) x) 42 69))
  50. (test-eval ((lambda (x y) y) 42 69))
  51. (test-eval ((case-lambda ((x) x) ((x y) y)) 42))
  52. (test-eval ((case-lambda ((x) x) ((x y) y)) 42 69))
  53. (test-eval (if #t 42 69))
  54. (test-eval (if #f 42 69))
  55. (test-eval (let ((x 42))
  56. (when #t (set! x 69))
  57. x))
  58. (test-eval (let ((x 42))
  59. (unless #t (set! x 69))
  60. x))
  61. (test-eval (let lp ((x 42))
  62. (if x
  63. (lp #f)
  64. 69)))
  65. (test-eval (letrec ((a (lambda () (b 42)))
  66. (b (lambda (x) (c x 69)))
  67. (c (lambda (x y) x)))
  68. (a)))
  69. (test-eval (cond
  70. (#f 42)
  71. (else 69)))
  72. (test-eval (cond
  73. (#t 42)
  74. (else 69)))
  75. (test-eval (cond
  76. (42)
  77. (else 69)))
  78. (test-eval (case 42
  79. ((42) #t)
  80. (else #f)))
  81. (test-eval (case 42
  82. ((69) #t)
  83. (else #f)))
  84. (test-eval (let ((x 42))
  85. 'what
  86. (define (y) (z x))
  87. (define (z q) q)
  88. (y)))
  89. (test-call "(2 3 4)"
  90. (lambda (exp)
  91. (eval exp (interaction-environment)))
  92. '(map 1+ '(1 2 3))))
  93. (test-end* "test-eval")