eval.scm 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. ;;; eval.scm -- Joy runtime.
  2. ;;; Copyright © 2016 Eric Bavier <bavier@member.fsf.org>
  3. ;;;
  4. ;;; Joy is free software; you can redistribute it and/or modify it under
  5. ;;; the terms of the GNU General Public License as published by the Free
  6. ;;; Software Foundation; either version 3 of the License, or (at your
  7. ;;; option) any later version.
  8. ;;;
  9. ;;; Joy is distributed in the hope that it will be useful, but WITHOUT
  10. ;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  11. ;;; or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
  12. ;;; License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU General Public License
  15. ;;; along with Joy. If not, see <http://www.gnu.org/licenses/>.
  16. (define-module (language joy eval)
  17. #:use-module (srfi srfi-1)
  18. #:replace (eval))
  19. (define *primitives* '(language joy primitives))
  20. (define (eval x S)
  21. "Evaluate joy term X with the stack S."
  22. (cond
  23. ((symbol? x)
  24. (let ((v (or (and=> (module-variable
  25. (resolve-interface *primitives*) x)
  26. variable-ref)
  27. (module-ref (resolve-module '(joy)) x))))
  28. (if (procedure? v)
  29. ;; Joy primitives are procedures that must be applied
  30. (apply v S)
  31. ;; Variables from the 'DEFINE ...' syntax or 'def' primitive
  32. ;; are lists of factors to be evaluated.
  33. (fold eval S v))))
  34. (else
  35. (cons x S))))