compile-tree-il.scm 2.2 KB

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