Commit | Line | Data |
---|---|---|
1d8cc67a MW |
1 | ;;; -*-lisp-*- |
2 | ;;; | |
3 | ;;; User interface | |
4 | ;;; | |
5 | ;;; (c) 2013 Straylight/Edgeware | |
6 | ;;; | |
7 | ||
8 | ;;;----- Licensing notice --------------------------------------------------- | |
9 | ;;; | |
e0808c47 | 10 | ;;; This file is part of the Sensible Object Design, an object system for C. |
1d8cc67a MW |
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 | ||
7bfe3a37 | 26 | (cl:defpackage #:sod-frontend |
abbdf025 | 27 | (:use #:common-lisp #:sod-utilities #:optparse #:sod #:sod-parser) |
2672be80 | 28 | (:shadowing-import-from #:optparse #:int)) |
7bfe3a37 MW |
29 | |
30 | (cl:in-package #:sod-frontend) | |
1d8cc67a MW |
31 | |
32 | ;;;-------------------------------------------------------------------------- | |
65d7091b MW |
33 | ;;; Preparation for dumping. |
34 | ||
54c01772 | 35 | (clear-the-decks) |
180bfa7c | 36 | (exercise) |
65d7091b MW |
37 | |
38 | ;;;-------------------------------------------------------------------------- | |
1d8cc67a MW |
39 | ;;; The main program. |
40 | ||
abbdf025 MW |
41 | (defvar-unbound *option-parser* |
42 | "The program's main option parser.") | |
43 | ||
9ec578d9 MW |
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 | ||
1d8cc67a MW |
77 | (export 'main) |
78 | (defun main () | |
9ec578d9 MW |
79 | |
80 | ;; Initialize the argument parser. | |
1d8cc67a MW |
81 | (set-command-line-arguments) |
82 | ||
9ec578d9 MW |
83 | ;; Collect information from the command line options. |
84 | (let ((output-reasons nil) | |
85 | (output-path (make-pathname :directory '(:relative))) | |
ba8bae5f | 86 | (backtracep nil) |
9ec578d9 MW |
87 | (builtinsp nil) |
88 | (stdoutp nil) | |
89 | (args nil)) | |
90 | ||
91 | ;; Option definitions. | |
92 | (define-program | |
4aae7df0 | 93 | :help "Process SOD input files to produce (e.g.) C output." |
111dc923 | 94 | :version *sod-version* |
9ec578d9 MW |
95 | :usage "SOURCES..." |
96 | :options (options | |
97 | (help-options :short-version #\V) | |
05a2c613 | 98 | "Translator options" |
9ec578d9 MW |
99 | (#\I "include" (:arg "DIR") |
100 | ("Search DIR for module imports.") | |
101 | (list *module-dirs* 'string)) | |
ba8bae5f MW |
102 | ("backtrace" |
103 | ("Print a Lisp backtrace on error (for debugging).") | |
104 | (set backtracep)) | |
9ec578d9 MW |
105 | ("builtins" |
106 | ("Process the builtin `sod-base' module.") | |
107 | (set builtinsp)) | |
108 | (#\d "directory" (:arg "DIR") | |
109 | ("Write output files to DIR.") | |
110 | (dirpath output-path)) | |
111 | (#\p "stdout" | |
112 | ("Write output files to standard output.") | |
113 | (set stdoutp)) | |
114 | (#\t "type" (:arg "OUT-TYPE") | |
115 | ("Produce output of type OUT-TYPE.") | |
116 | (list output-reasons 'keyword)))) | |
117 | ||
118 | ;; Actually parse the options. | |
abbdf025 MW |
119 | (let ((*option-parser* (make-option-parser))) |
120 | (unless (and (option-parse-try | |
121 | (do-options (:parser *option-parser*) | |
122 | (nil (rest) | |
123 | (setf args rest)))) | |
124 | (or builtinsp args)) | |
125 | (die-usage))) | |
9ec578d9 | 126 | |
9ec578d9 | 127 | ;; Do the main parsing job. |
ba8bae5f MW |
128 | (labels ((hack-module (module) |
129 | ;; Process the MODULE, writing out the generated code. | |
130 | ||
131 | ;; Work through each output type in turn. | |
132 | (dolist (reason output-reasons) | |
133 | ||
134 | ;; Arrange to be able to recover from errors. | |
135 | (restart-case | |
136 | ||
137 | ;; Collect information for constructing the output | |
138 | ;; filenames here. In particular, | |
139 | ;; `output-type-pathname' will sanity-check the | |
140 | ;; output type for us, which is useful even if | |
141 | ;; we're writing to stdout. | |
142 | (let ((outpath (output-type-pathname reason)) | |
143 | (modpath (module-name module))) | |
144 | ||
145 | (if stdoutp | |
146 | ||
147 | ;; If we're writing to stdout then just do | |
148 | ;; that. | |
149 | (output-module module reason | |
150 | *standard-output*) | |
151 | ||
152 | ;; Otherwise we have to construct an output | |
153 | ;; filename the hard way. | |
154 | (with-open-file | |
155 | (stream | |
156 | (reduce #'merge-pathnames | |
157 | (list output-path | |
158 | outpath | |
159 | (make-pathname | |
160 | :directory nil | |
161 | :defaults modpath)) | |
162 | :from-end t) | |
163 | :direction :output | |
164 | :if-exists :supersede | |
165 | :if-does-not-exist :create) | |
166 | (output-module module reason stream)))) | |
167 | ||
168 | ;; Error recovery. | |
169 | (continue () | |
170 | :report (lambda (stream) | |
171 | (format stream | |
172 | "Skip output type `~(~A~)'" | |
173 | reason)) | |
174 | nil)))) | |
175 | ||
176 | (hack-modules () | |
177 | ||
178 | ;; If there are no output types then there's nothing to do. | |
179 | (unless output-reasons | |
180 | (error "No output types given: nothing to do")) | |
181 | ||
182 | ;; If we're writing the builtin module then now seems like a | |
183 | ;; good time to do that. | |
184 | (when builtinsp | |
185 | (hack-module *builtin-module*)) | |
186 | ||
187 | ;; Parse and write out the remaining modules. | |
188 | (dolist (arg args) | |
287e744e MW |
189 | (let ((module (read-module arg))) |
190 | (when (zerop (module-errors module)) | |
191 | (hack-module module)))))) | |
ba8bae5f MW |
192 | |
193 | (if backtracep (hack-modules) | |
194 | (multiple-value-bind (hunoz nerror nwarn) | |
195 | (count-and-report-errors () | |
196 | (with-default-error-location | |
197 | ((make-file-location *program-name*)) | |
198 | (hack-modules))) | |
199 | (declare (ignore hunoz)) | |
200 | (when (or (plusp nerror) (plusp nwarn)) | |
201 | (format *error-output* "~A: Finished with~ | |
202 | ~[~:; ~:*~D error~:P~[~:; and~]~:*~]~ | |
203 | ~[~:; ~:*~D warning~:P~]~%" | |
204 | *program-name* nerror nwarn)) | |
205 | (exit (if (plusp nerror) 2 0))))))) | |
1d8cc67a MW |
206 | |
207 | ;;;----- That's all, folks -------------------------------------------------- |