compile-tree-il.scm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. ;;; compile-tree-il.scm -- compile Joy to tree-il.
  2. ;;; Copyright © 2016, 2020 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 compile-tree-il)
  17. #:use-module (language tree-il)
  18. #:use-module (system base pmatch)
  19. #:use-module (srfi srfi-1)
  20. #:export (compile-tree-il compile-tree-il*))
  21. ;;; Guile 2.2 changed the external representation for a procedure call
  22. ;;; from 'apply' to 'call'.
  23. (define call
  24. (if (or (> (string->number (major-version)) 2)
  25. (and (= (string->number (major-version)) 2)
  26. (>= (string->number (minor-version)) 2)))
  27. 'call
  28. 'apply))
  29. (define (location x)
  30. (and (pair? x)
  31. (let ((props (source-properties x)))
  32. (and (not (null? props))
  33. props))))
  34. (define *eval* '(language joy eval))
  35. (define (compile-factor fact)
  36. (cond
  37. ((list? fact) (map compile-factor fact))
  38. ((string? fact) (string->list fact))
  39. (else fact)))
  40. (define (compile-term term)
  41. `(const ,(map compile-factor term)))
  42. (define (compile-expr expr)
  43. (let ((sym (gensym "S-")))
  44. `(lambda ()
  45. (lambda-case ((() #f S #f () (,sym))
  46. (,call (@ (srfi srfi-1) fold)
  47. (@@ ,*eval* eval)
  48. (lexical S ,sym)
  49. ,(compile-term expr)))))))
  50. (define (process-options! opts)
  51. #t)
  52. (define (compile-tree-il expr env opts)
  53. "Compile Joy expression to Tree-IL."
  54. (call-with-values
  55. (lambda () (compile-tree-il* expr env opts))
  56. (lambda (rep env cenv)
  57. (values
  58. (parse-tree-il rep)
  59. env
  60. cenv))))
  61. (define (compile-tree-il* expr env opts)
  62. "Compile Joy expression to Tree-IL external representation."
  63. (values
  64. (begin
  65. (process-options! opts)
  66. (compile-expr expr))
  67. env
  68. env))