2d20fbc762a5ed97bea961ea0c2be1b4f82c6d74
[sod] / src / frontend.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; User interface
4 ;;;
5 ;;; (c) 2013 Straylight/Edgeware
6 ;;;
7
8 ;;;----- Licensing notice ---------------------------------------------------
9 ;;;
10 ;;; This file is part of the Sensible Object Design, an object system for C.
11 ;;;
12 ;;; SOD is free software; you can redistribute it and/or modify
13 ;;; it under the terms of the GNU General Public License as published by
14 ;;; the Free Software Foundation; either version 2 of the License, or
15 ;;; (at your option) any later version.
16 ;;;
17 ;;; SOD is distributed in the hope that it will be useful,
18 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;;; GNU General Public License for more details.
21 ;;;
22 ;;; You should have received a copy of the GNU General Public License
23 ;;; along with SOD; if not, write to the Free Software Foundation,
24 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26 (cl:defpackage #:sod-frontend
27 (:use #:common-lisp #:sod-utilities #:optparse #:sod #:sod-parser)
28 (:shadowing-import-from #:optparse #:int))
29
30 (cl:in-package #:sod-frontend)
31
32 ;;;--------------------------------------------------------------------------
33 ;;; Preparation for dumping.
34
35 (clear-the-decks)
36 (exercise)
37
38 ;;;--------------------------------------------------------------------------
39 ;;; The main program.
40
41 (defvar-unbound *option-parser*
42 "The program's main option parser.")
43
44 (eval-when (:compile-toplevel :load-toplevel :execute)
45 (defopthandler dirpath (var arg) ()
46 "Convert the argument into a pathname with a directory component
47 and no file component, suitable for merging."
48
49 ;; This is really fiddly and annoying. Unix pathnames don't tell you
50 ;; whether the thing named is meant to be a directory or not, and
51 ;; implementations differ as to how they cope with pathnames which do or
52 ;; don't name directories when they're expecting files, or vice versa.
53
54 (let ((path (ignore-errors (pathname arg))))
55 (cond ((null path)
56 ;; The namestring couldn't be parsed, or something else went
57 ;; horribly wrong.
58
59 (option-parse-error "Can't parse `~A' as a path" arg))
60
61 #+unix
62 ((or (pathname-name path) (pathname-type path))
63 ;; If this is Unix, or similar, then stick the filename piece on
64 ;; the end of the directory and hope that was sensible.
65
66 (setf var (make-pathname
67 :name nil :type nil :defaults path
68 :directory (append (or (pathname-directory path)
69 (list :relative))
70 (list (file-namestring path))))))
71
72 (t
73 ;; This actually looks like a plain directory name.
74
75 (setf var path))))))
76
77 (defun update-usage ()
78 (setf *usage* (simple-usage *options* "SOURCES...")))
79
80 (export 'augment-options)
81 (defun augment-options (options)
82 "Add OPTIONS to the program's options list."
83 (asetf *options* (append it options))
84 (setf (op-options *option-parser*) *options*)
85 (update-usage))
86
87 (use-package "SOD-FRONTEND" "SOD-USER")
88
89 (export 'main)
90 (defun main ()
91
92 ;; Initialize the argument parser.
93 (set-command-line-arguments)
94
95 ;; Collect information from the command line options.
96 (let ((output-reasons nil)
97 (output-path (make-pathname :directory '(:relative)))
98 (backtracep nil)
99 (builtinsp nil)
100 (stdoutp nil)
101 (args nil))
102
103 ;; Option definitions.
104 (define-program
105 :help "Process SOD input files to produce (e.g.) C output."
106 :version *sod-version*
107 :options (options
108 (help-options :short-version #\V)
109 "Translator options"
110 (#\I "include" (:arg "DIR")
111 ("Search DIR for module imports.")
112 (list *module-dirs* 'string))
113 ("backtrace"
114 ("Print a Lisp backtrace on error (for debugging).")
115 (set backtracep))
116 ("builtins"
117 ("Process the builtin `sod-base' module.")
118 (set builtinsp))
119 (#\d "directory" (:arg "DIR")
120 ("Write output files to DIR.")
121 (dirpath output-path))
122 (#\e "eval" (:arg "LISP")
123 ("Evaluate raw Lisp code.")
124 (lambda (lisp)
125 (handler-case
126 (let ((*package* (find-package "SOD-USER")))
127 (eval (read-from-string lisp)))
128 (error (error)
129 (option-parse-error "~A" error)))))
130 (#\l "load" (:arg "FILE")
131 ("Load a file of Lisp code.")
132 (lambda (file)
133 (let ((file (merge-pathnames file
134 (make-pathname
135 :type "LISP"
136 :case :common))))
137 (handler-case
138 (let ((*package* (find-package "SOD-USER")))
139 (find-file *default-pathname-defaults* file
140 "Lisp file"
141 (lambda (path true)
142 (declare (ignore path))
143 (load true
144 :verbose nil
145 :print nil))))
146 (error (error)
147 (option-parse-error "~A" error))))))
148 (#\p "stdout"
149 ("Write output files to standard output.")
150 (set stdoutp))
151 (#\t "type" (:arg "OUT-TYPE")
152 ("Produce output of type OUT-TYPE.")
153 (list output-reasons 'keyword))))
154 (update-usage)
155
156 ;; Actually parse the options.
157 (let ((*option-parser* (make-option-parser)))
158 (unless (and (option-parse-try
159 (do-options (:parser *option-parser*)
160 (nil (rest)
161 (setf args rest))))
162 (or builtinsp args))
163 (die-usage)))
164
165 ;; Do the main parsing job.
166 (labels ((hack-module (module)
167 ;; Process the MODULE, writing out the generated code.
168
169 ;; Work through each output type in turn.
170 (dolist (reason output-reasons)
171
172 ;; Arrange to be able to recover from errors.
173 (restart-case
174
175 ;; Collect information for constructing the output
176 ;; filenames here. In particular,
177 ;; `output-type-pathname' will sanity-check the
178 ;; output type for us, which is useful even if
179 ;; we're writing to stdout.
180 (let ((outpath (output-type-pathname reason))
181 (modpath (module-name module)))
182
183 (if stdoutp
184
185 ;; If we're writing to stdout then just do
186 ;; that.
187 (output-module module reason
188 *standard-output*)
189
190 ;; Otherwise we have to construct an output
191 ;; filename the hard way.
192 (with-open-file
193 (stream
194 (reduce #'merge-pathnames
195 (list output-path
196 outpath
197 (make-pathname
198 :directory nil
199 :defaults modpath))
200 :from-end t)
201 :direction :output
202 :if-exists :supersede
203 :if-does-not-exist :create)
204 (output-module module reason stream))))
205
206 ;; Error recovery.
207 (continue ()
208 :report (lambda (stream)
209 (format stream
210 "Skip output type `~(~A~)'"
211 reason))
212 nil))))
213
214 (hack-modules ()
215
216 ;; If there are no output types then there's nothing to do.
217 (unless output-reasons
218 (error "No output types given: nothing to do"))
219
220 ;; If we're writing the builtin module then now seems like a
221 ;; good time to do that.
222 (when builtinsp
223 (hack-module *builtin-module*))
224
225 ;; Parse and write out the remaining modules.
226 (dolist (arg args)
227 (let ((module (read-module arg)))
228 (when (zerop (module-errors module))
229 (hack-module module))))))
230
231 (if backtracep (hack-modules)
232 (multiple-value-bind (hunoz nerror nwarn)
233 (count-and-report-errors ()
234 (with-default-error-location
235 ((make-file-location *program-name*))
236 (hack-modules)))
237 (declare (ignore hunoz))
238 (when (or (plusp nerror) (plusp nwarn))
239 (format *error-output* "~A: Finished with~
240 ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~
241 ~[~:; ~:*~D warning~:P~]~%"
242 *program-name* nerror nwarn))
243 (exit (if (plusp nerror) 2 0)))))))
244
245 ;;;----- That's all, folks --------------------------------------------------