src/final.lisp (test-module): Optionally clear the decks before parsing.
[sod] / src / final.lisp
1 ;;; -*-lisp-*-
2 ;;;
3 ;;; Finishing touches for Sod
4 ;;;
5 ;;; (c) 2015 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:in-package #:sod)
27
28 ;;;--------------------------------------------------------------------------
29 ;;; Miscellaneous details.
30
31 (export '*sod-version*)
32 (defparameter *sod-version* sod-sysdef:*version*
33 "The version of the SOD translator system, as a string.")
34
35 ;;;--------------------------------------------------------------------------
36 ;;; Debugging utilities.
37
38 (export '*debugout-pathname*)
39 (defvar *debugout-pathname* #p"debugout.c")
40
41 (export 'test-module)
42 (defun test-module (path &key reason clear)
43 "Read a module from PATH, to exercise the machinery.
44
45 If CLEAR is non-nil, then reset the translator's state before proceeding.
46
47 If REASON is non-nil, then output the module to `*debugout-pathname*' with
48 that REASON.
49
50 Return a two-element list (NERROR NWARNING) of the number of errors and
51 warnings encountered while processing the module."
52 (when clear (clear-the-decks))
53 (multiple-value-bind (module nerror nwarning)
54 (count-and-report-errors () (read-module path))
55 (when (and module reason)
56 (with-open-file (out *debugout-pathname*
57 :direction :output
58 :if-exists :supersede
59 :if-does-not-exist :create)
60 (output-module module reason out)))
61 (list nerror nwarning)))
62
63 (export 'test-parse-c-type)
64 (defun test-parse-c-type (string)
65 "Parse STRING as a C type, with optional kernel, and show the results."
66 (with-input-from-string (in string)
67 (let* ((*module-type-map* (make-hash-table))
68 (charscan (make-instance 'charbuf-scanner
69 :stream in
70 :filename "<string>"))
71 (tokscan (make-instance 'sod-token-scanner
72 :char-scanner charscan
73 :filename "<string>")))
74 (with-parser-context (token-scanner-context :scanner tokscan)
75 (multiple-value-bind (value winp consumedp)
76 (parse (seq ((decls (parse-c-type tokscan))
77 (type (parse-declarator tokscan decls :abstractp t))
78 :eof)
79 type))
80 (declare (ignore consumedp))
81 (if winp
82 (values t (car value) (cdr value)
83 (princ-to-string (car value)))
84 (values nil value)))))))
85
86 (export 'test-parser)
87 (defmacro test-parser ((scanner &key backtrace) parser input)
88 "Convenient macro for testing parsers at the REPL.
89
90 This is a macro so that the parser can use the fancy syntax. The name
91 SCANNER is bound to a `sod-token-scanner' reading tokens from the INPUT
92 string. Then the PARSER is invoked and three values are returned: the
93 result of the parse, or `nil' if the main parse failed; a list containing
94 the number of errors and warnings (respectively) reported during the
95 parse; and a list consisting of the lookahead token type and value, and a
96 string containing the untokenized remaining input.
97
98 If BACKTRACE is nil (the default) then leave errors to the calling
99 environment to sort out (e.g., by entering the Lisp debugger); otherwise,
100 catch and report them as they happen so that you can test error recovery
101 strategies."
102 (once-only (input)
103 (with-gensyms (char-scanner value winp body consumedp where nerror nwarn)
104 `(let ((,char-scanner nil) (,scanner nil))
105 (with-parser-context (token-scanner-context :scanner ,scanner)
106 (multiple-value-bind (,value ,nerror ,nwarn)
107 (flet ((,body ()
108 (setf ,char-scanner (make-string-scanner ,input)
109 ,scanner (make-instance
110 'sod-token-scanner
111 :char-scanner ,char-scanner))
112 (multiple-value-bind (,value ,winp ,consumedp)
113 (parse ,parser)
114 (declare (ignore ,consumedp))
115 (cond (,winp ,value)
116 (t (syntax-error ,scanner ,value)
117 nil)))))
118 (if ,backtrace (,body)
119 (count-and-report-errors ()
120 (with-default-error-location (,scanner)
121 (,body)))))
122 (let ((,where (scanner-capture-place ,char-scanner)))
123 (values ,value
124 (list ,nerror ,nwarn)
125 (and ,scanner (list (token-type ,scanner)
126 (token-value ,scanner)
127 (subseq ,input ,where)))))))))))
128
129 ;;;--------------------------------------------------------------------------
130 ;;; Calisthenics.
131
132 (export 'exercise)
133 (defun exercise ()
134 "Exercise the pieces of the metaobject protocol.
135
136 In some Lisps, the compiler is run the first time methods are called, to
137 do fancy just-in-time optimization things. This is great, only the
138 program doesn't actually run for very long and a lot of that work is
139 wasted because we're going to have to do it again next time the program
140 starts. Only, if we exercise the various methods, or at least a large
141 fraction of them, before we dump an image, then everything will be fast.
142
143 That's the theory anyway. Call this function before you dump an image and
144 see what happens."
145
146 (dolist (reason '(:h :c))
147 (with-output-to-string (bitbucket)
148 (output-module *builtin-module* reason bitbucket)))
149
150 (clear-the-decks))
151
152 ;;;--------------------------------------------------------------------------
153 ;;; Make sure things work after loading the system.
154
155 (clear-the-decks)
156
157 ;;;----- That's all, folks --------------------------------------------------