ui.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;;; Joy -- implementation of the Joy programming language
  2. ;;; Copyright © 2016, 2017 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 (joy ui)
  17. #:use-module (joy config)
  18. #:use-module (ice-9 format)
  19. #:use-module (srfi srfi-37)
  20. #:use-module (system base compile)
  21. #:use-module (system repl common)
  22. #:use-module (system repl repl)
  23. #:use-module (language joy write)
  24. #:export (joy-main))
  25. (define (show-bug-report-information)
  26. (format #t "
  27. Report bugs to: ~a." %joy-bug-report-address)
  28. (format #t "
  29. ~a home page: <~a>~%" %joy-package-name %joy-home-page-url))
  30. (define (show-version)
  31. "Display version information."
  32. (simple-format #t "~a (~a) ~a~%"
  33. (basename (car (command-line))) %joy-package-name %joy-version)
  34. (simple-format #t "Copyright (C) 2016, 2017 Eric Bavier <bavier@member.fsf.org>~%
  35. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
  36. ~a is free software: you are free to change and redistribute it.
  37. There is NO WARRANTY, to the extent permitted by law.
  38. "
  39. %joy-package-name))
  40. (define (show-help)
  41. (display "Usage: joy [OPTION] ... JOY-SCRIPT...")
  42. (newline)
  43. (display "
  44. -h, --help Show this message and exit.")
  45. (display "
  46. -V, --version Show the version string and exit.")
  47. (display "
  48. -I, --include=DIR Add DIR to the list of directories to
  49. search with the \"include\" operator ")
  50. (display "
  51. --debug Start in debug mode.")
  52. (newline)
  53. (show-bug-report-information))
  54. (define (warn-option-not-implemented opt name)
  55. (format (current-error-port) "
  56. joy: warning: option ~a currently not implemented." name))
  57. (define %options
  58. (list (option '(#\h "help") #f #f
  59. (λ _ (show-help) (exit 0)))
  60. (option '(#\V "version") #f #f
  61. (λ _ (show-version) (exit 0)))
  62. (option '(#\I "include") #t #f
  63. (λ (opt name arg result)
  64. (set! %load-path (cons arg %load-path))
  65. result))
  66. (option '("debug") #f #f
  67. (λ (opt name arg result)
  68. (warn-option-not-implemented opt name)
  69. result))))
  70. (define (compile-and-run filename args)
  71. (let ((f (search-path (cons (getcwd) %load-path)
  72. filename '("" ".joy"))))
  73. (if f
  74. (and=> (compiled-file-name f)
  75. (lambda (go)
  76. (begin
  77. (compile-file f #:output-file go #:from 'joy)
  78. (apply (load-compiled go) args))))
  79. (begin
  80. (format (current-error-port)
  81. "No such file: ~a~%" filename)
  82. (exit 1)))))
  83. (define (repl-welcome repl)
  84. (show-version)
  85. (newline))
  86. (module-set! (resolve-module '(system repl common))
  87. 'repl-welcome repl-welcome)
  88. (define (joy-main . args)
  89. (let ((opts (args-fold (cdr args)
  90. %options
  91. (λ (opt name arg . rest)
  92. (error "~A: unrecognized option~%"
  93. name))
  94. (λ (arg result)
  95. (cons arg result))
  96. '()))
  97. (repl (make-repl 'joy-repl)))
  98. (repl-option-set! repl 'print (lambda (repl val) (write-joy val)))
  99. (repl-option-set! repl 'value-history #f)
  100. (if (null? opts)
  101. (run-repl repl)
  102. (compile-and-run (car opts) (cdr opts)))))