PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/chaos/eval/chaos-top.lisp

https://bitbucket.org/tswd/cafeobj
Lisp | 216 lines | 141 code | 19 blank | 56 comment | 0 complexity | 8ca851b9b59deda9cc9fb32c84bcfad5 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. ;;;-*- Mode:LISP; Package:CHAOS; Base:10; Syntax:Common-lisp -*-
  2. ;;;
  3. ;;; Copyright (c) 2000-2014, Toshimi Sawada. All rights reserved.
  4. ;;;
  5. ;;; Redistribution and use in source and binary forms, with or without
  6. ;;; modification, are permitted provided that the following conditions
  7. ;;; are met:
  8. ;;;
  9. ;;; * Redistributions of source code must retain the above copyright
  10. ;;; notice, this list of conditions and the following disclaimer.
  11. ;;;
  12. ;;; * Redistributions in binary form must reproduce the above
  13. ;;; copyright notice, this list of conditions and the following
  14. ;;; disclaimer in the documentation and/or other materials
  15. ;;; provided with the distribution.
  16. ;;;
  17. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
  18. ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  21. ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  23. ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  26. ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. ;;;
  29. (in-package :chaos)
  30. #|==============================================================================
  31. System: CHAOS
  32. Module: eval
  33. File: chaos-top.lisp
  34. ==============================================================================|#
  35. ;;; == DESCRIPTION =============================================================
  36. ;;; Chaos toplevel.
  37. ;;;
  38. ;;;
  39. ;;;
  40. (defun define-builtin-module (mod-name)
  41. (let ((name (normalize-modexp mod-name)))
  42. (let ((mod (create-module name)))
  43. (setf (module-type mod) :hard)
  44. (setf (module-kind mod) :object)
  45. (add-modexp-defn name mod)
  46. mod)))
  47. ;;; GLOBAL DB INITIALIZATION
  48. (defun clear-global-db ()
  49. (setq *modules-so-far-table* nil)
  50. (setq *modexp-local-table* nil)
  51. (setq *modexp-view-table* nil)
  52. (setq *modexp-eval-table* nil)
  53. ;; (clear-all-sorts)
  54. (clear-builtin-sorts))
  55. ;;;
  56. (defvar *chaos-new* t)
  57. #+GCL
  58. (defun save-chaos (top &optional (path "./bin/chaos.exe"))
  59. (setq *chaos-new* t)
  60. (when top
  61. (defun system::top-level () (funcall top))
  62. (si::set-up-top-level)
  63. ;; (setf (symbol-function 'si::top-level) (symbol-function top))
  64. )
  65. (system::save-system path)
  66. (bye))
  67. #+CMU
  68. (defun save-chaos (top &optional (path "bin/chaos.core"))
  69. (setq *chaos-new* t)
  70. (ext:gc)
  71. (ext:purify)
  72. (ext:gc)
  73. (if top
  74. (ext:save-lisp path
  75. :purify nil
  76. :init-function top
  77. :print-herald nil
  78. )
  79. (ext:save-lisp path
  80. :purify nil
  81. :print-herald nil)))
  82. #+LUCID
  83. (defun save-chaos (top &optional (path "bin/chaos.exe"))
  84. (setq *chaos-new* t)
  85. (if top
  86. (disksave path
  87. :full-gc t
  88. :restart-function top)
  89. (disksave path
  90. :full-gc t)))
  91. #+:ccl
  92. (defun save-chaos (top &optional (path "chaos"))
  93. (setq *chaos-new* t)
  94. (if top
  95. (save-application path :toplevel-function top
  96. :size '(6144000 4196000))
  97. (save-application path
  98. :size '(6144000 4196000))))
  99. #+:ALLEGRO
  100. (defun save-chaos (top &optional (path "aobj"))
  101. (setq *chaos-new* t)
  102. (setq excl:*restart-app-function* top)
  103. (setq excl:*print-startup-message* nil)
  104. (setq excl::.dump-lisp-suppress-allegro-cl-banner. t)
  105. (dumplisp :name path :suppress-allegro-cl-banner t))
  106. #+:CLISP
  107. (defun save-chaos (top &optional (path "chaos"))
  108. (setq *chaos-new* t)
  109. (in-package :chaos)
  110. (if top
  111. (ext:saveinitmem path :quiet t :init-function top)
  112. (ext:saveinitmem path :quiet t)))
  113. #+SBCL
  114. (defun save-chaos (top &optional (path "chaos.sbcl"))
  115. (declare (ignore top))
  116. (setq *chaos-new* t)
  117. (sb-ext:save-lisp-and-die path
  118. :toplevel 'chaos::cafeobj-top-level
  119. :purify t
  120. :executable t
  121. :save-runtime-options t))
  122. ;;; PROCESS-CHAOS-INPUT
  123. ;;;
  124. (defun chaos-prompt (&optional (stream *error-output*))
  125. (let ((*standard-output* stream))
  126. (fresh-all)
  127. (flush-all)
  128. (format t "~&[")
  129. (if *last-module*
  130. (print-simple-mod-name *last-module*)
  131. (princ "*"))
  132. (princ "]> ")
  133. ))
  134. (defun handle-chaos-error (val)
  135. (if *chaos-input-source*
  136. (chaos-error val)
  137. val))
  138. (defun handle-chaos-top-error (val)
  139. (if *chaos-input-source*
  140. (chaos-to-top val)
  141. val))
  142. (defun chaos-read (&optional (stream *standard-input*))
  143. (let ((inp (read stream nil :eof nil)))
  144. (when (memq inp '(:eof eof quit :quit :q q))
  145. (return-from chaos-read :quit))
  146. inp))
  147. (defun chaos-eval-reader (stream char)
  148. (declare (ignore char))
  149. (let ((obj (read stream nil :eof t)))
  150. (if (eq obj :eof)
  151. (values)
  152. (eval-ast obj))))
  153. (defun process-chaos-input ()
  154. (let ((*print-array* nil)
  155. (*print-circle* nil)
  156. (*old-context* nil)
  157. (*show-mode* :chaos)
  158. (top-level (at-top-level)))
  159. (unless (or top-level *chaos-quiet*)
  160. (if *chaos-input-source*
  161. (with-output-simple-msg ()
  162. (format t "~&processing input : ~a~%" (namestring *chaos-input-source*)))
  163. (with-output-simple-msg ()
  164. (format t "~&processing input .......................~%")))
  165. )
  166. (let ((ast nil)
  167. (*readtable* (copy-readtable)))
  168. ;; (declare (special *readtable*))
  169. (set-macro-character #\! #'chaos-eval-reader)
  170. (block top-loop
  171. (loop
  172. (with-chaos-top-error ('handle-chaos-top-error)
  173. (with-chaos-error ('handle-chaos-error)
  174. (when top-level
  175. (chaos-prompt))
  176. (setq ast (chaos-read))
  177. ;; QUIT -----------------------------------------------------------
  178. (when (eq ast :quit)
  179. (return-from top-loop nil))
  180. ;; PROCESS INPUT COMMANDS =========================================
  181. (block process-input
  182. #||
  183. (when (eq ast '!)
  184. (setq ast (eval (chaos-read)))
  185. (when (eq ast :quit) (return-from top-loop nil)))
  186. ||#
  187. (eval-ast ast :print-generic-result)
  188. )
  189. (setq *chaos-print-errors* t)))
  190. )))))
  191. ;;; CHAOS TOP LEVEL LOOP
  192. ;;; [ast/script/lisp-form] ---> (read) ---> (eval) ---> (print)
  193. ;;;
  194. (defun chaos-top ()
  195. (catch *top-level-tag*
  196. (process-chaos-input)))
  197. ;;; EOF