lib/keyword.c (kw_parseempty): Use correct variable scanning `kwval' list.
[sod] / src / frontend.lisp
... / ...
CommitLineData
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(eval-when (:compile-toplevel :load-toplevel :execute)
27 (handler-bind ((warning #'muffle-warning))
28 (cl:defpackage #:sod-frontend
29 (:use #:common-lisp #:sod-utilities #:optparse #:sod #:sod-parser))))
30
31(cl:in-package #:sod-frontend)
32
33;;;--------------------------------------------------------------------------
34;;; The main program.
35
36(defvar-unbound *option-parser*
37 "The program's main option parser.")
38
39(eval-when (:compile-toplevel :load-toplevel :execute)
40 (defopthandler dirpath (var arg) ()
41 "Convert the argument into a pathname with a directory component
42 and no file component, suitable for merging."
43
44 ;; This is really fiddly and annoying. Unix pathnames don't tell you
45 ;; whether the thing named is meant to be a directory or not, and
46 ;; implementations differ as to how they cope with pathnames which do or
47 ;; don't name directories when they're expecting files, or vice versa.
48
49 (let ((path (ignore-errors (pathname arg))))
50 (cond ((null path)
51 ;; The namestring couldn't be parsed, or something else went
52 ;; horribly wrong.
53
54 (option-parse-error "Can't parse `~A' as a path" arg))
55
56 #+unix
57 ((or (pathname-name path) (pathname-type path))
58 ;; If this is Unix, or similar, then stick the filename piece on
59 ;; the end of the directory and hope that was sensible.
60
61 (setf var (make-pathname
62 :name nil :type nil :defaults path
63 :directory (append (or (pathname-directory path)
64 (list :relative))
65 (list (file-namestring path))))))
66
67 (t
68 ;; This actually looks like a plain directory name.
69
70 (setf var path))))))
71
72(defun update-usage ()
73 (setf *usage* (simple-usage *options* "SOURCES...")))
74
75(export 'augment-options)
76(defun augment-options (options)
77 "Add OPTIONS to the program's options list."
78 (asetf *options* (append it options))
79 (setf (op-options *option-parser*) *options*)
80 (update-usage))
81
82(use-package "SOD-FRONTEND" "SOD-USER")
83
84(export 'main)
85(defun main ()
86
87 ;; Initialize the argument parser.
88 (set-command-line-arguments)
89
90 ;; Collect information from the command line options.
91 (let ((output-reasons nil)
92 (output-path (make-pathname :directory '(:relative)))
93 (backtracep nil)
94 (builtinsp nil)
95 (stdoutp nil)
96 (track-deps-p nil)
97 (args nil))
98
99 ;; Option definitions.
100 (define-program
101 :help "Process SOD input files to produce (e.g.) C output."
102 :version *sod-version*
103 :options (options
104 (help-options :short-version #\V)
105 "Translator options"
106 (#\I "include" (:arg "DIR")
107 ("Search DIR for module imports.")
108 (list *module-dirs* 'string))
109 ("backtrace"
110 ("Print a Lisp backtrace on error (for debugging).")
111 (set backtracep))
112 ("builtins"
113 ("Process the builtin `sod-base' module.")
114 (set builtinsp))
115 (#\d "directory" (:arg "DIR")
116 ("Write output files to DIR.")
117 (dirpath output-path))
118 (#\e "eval" (:arg "LISP")
119 ("Evaluate raw Lisp code.")
120 (lambda (lisp)
121 (handler-case
122 (let ((*package* (find-package "SOD-USER"))
123 (token (cons 'token nil)))
124 (with-input-from-string (in lisp)
125 (loop (let ((form (read in nil token)))
126 (when (eq form token) (return))
127 (eval form)))))
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 (#\M "track-dependencies"
149 "Write make(1) fragments recording dependencies."
150 (set track-deps-p))
151 (#\p "stdout"
152 ("Write output files to standard output.")
153 (set stdoutp))
154 (#\t "type" (:arg "OUT-TYPE")
155 ("Produce output of type OUT-TYPE.")
156 (list output-reasons 'keyword))))
157 (update-usage)
158
159 ;; Actually parse the options.
160 (let ((*option-parser* (make-option-parser)))
161 (unless (and (option-parse-try
162 (do-options (:parser *option-parser*)
163 (nil (rest)
164 (setf args rest))))
165 (or builtinsp args))
166 (die-usage)))
167
168 ;; Do the main parsing job.
169 (labels ((hack-module (module)
170 ;; Process the MODULE, writing out the generated code.
171
172 ;; Work through each output type in turn.
173 (dolist (reason output-reasons)
174
175 ;; Arrange to be able to recover from errors.
176 (restart-case
177 (cond
178
179 (stdoutp
180 ;; If we're writing to stdout then use
181 ;; `output-type-pathname' to check the output type
182 ;; for us.
183
184 (output-type-pathname reason)
185 (output-module module reason *standard-output*))
186
187 (t
188 ;; Otherwise we have to construct an output
189 ;; filename the hard way.
190 (with-open-file
191 (stream
192 (module-output-file module reason output-path)
193 :direction :output
194 :if-exists :supersede
195 :if-does-not-exist :create)
196 (output-module module reason stream))
197
198 (when track-deps-p
199 (write-dependency-file module reason
200 output-path))))
201
202 ;; Error recovery.
203 (continue ()
204 :report (lambda (stream)
205 (format stream
206 "Skip output type `~(~A~)'"
207 reason))
208 nil))))
209
210 (hack-modules ()
211
212 ;; If there are no output types then there's nothing to do.
213 (unless output-reasons
214 (error "No output types given: nothing to do"))
215
216 ;; If we're writing the builtin module then now seems like a
217 ;; good time to do that.
218 (when builtinsp
219 (hack-module *builtin-module*))
220
221 ;; Parse and write out the remaining modules.
222 (dolist (arg args)
223 (let ((module (read-module arg)))
224 (when (zerop (module-errors module))
225 (hack-module module))))))
226
227 (if backtracep (hack-modules)
228 (multiple-value-bind (hunoz nerror nwarn)
229 (count-and-report-errors ()
230 (with-default-error-location
231 ((make-file-location *program-name*))
232 (hack-modules)))
233 (declare (ignore hunoz))
234 (when (or (plusp nerror) (plusp nwarn))
235 (format *error-output* "~A: Finished with~
236 ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~
237 ~[~:; ~:*~D warning~:P~]~%"
238 *program-name* nerror nwarn))
239 (uiop:quit (if (plusp nerror) 2 0)))))))
240
241;;;----- That's all, folks --------------------------------------------------