-
Notifications
You must be signed in to change notification settings - Fork 2
/
bst-functions.lisp
200 lines (182 loc) · 7.11 KB
/
bst-functions.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
;; A BibTeX re-implementation in Common Lisp - BST functions
;; Copyright 2001, 2002 Matthias Koeppe <[email protected]>
;;
;; This code is free software; you can redistribute it and/or
;; modify it under the terms of version 2.1 of the GNU Lesser
;; General Public License as published by the Free Software
;; Foundation or any later version, as clarified by the preamble
;; found in COPYING-preamble.txt. This preamble is in the style
;; of the Franz Inc. preamble at http://opensource.franz.com/preamble.html
;; with names and copyright holders altered accordingly.
(in-package bibtex-compiler)
;;; BST functions
(defclass side-effects ()
((side-effects-p :accessor side-effects-side-effects-p
:initarg :side-effects-p :initform nil)
;; strings designating BST functions, symbols designating Lisp
;; variables (including lexical BST variables)
(used-variables :accessor side-effects-used-variables
:initarg :used-variables :initform ())
(assigned-variables :accessor side-effects-assigned-variables
:initarg :assigned-variables :initform ())
;; unconditionally assigned variables are always assigned in the
;; form; in fact, they are assigned before they are referenced
(unconditionally-assigned-variables :accessor side-effects-unconditionally-assigned-variables
:initarg :unconditionally-assigned-variables
:initform ())
(variables-used-before-assigned :accessor side-effects-variables-used-before-assigned
:initarg :variables-used-before-assigned
:initform ()))
(:documentation
"A description of the side-effects of a computation"))
(defvar null-side-effects
(make-instance 'side-effects))
(defstruct bst-function
name
type
ignore-redefinition-p
lisp-name
argument-types
result-types
;; For use in the BST compiler:
pop-form-args
lisp-form-maker
(side-effects null-side-effects)
setter-form-maker
defun-form
lexical-p
constant-p
(num-assignments 0)
assigned-value-form
;; For use in the BST interpreter:
value ; value as a variable
body
setter)
(defvar *builtin-bst-functions* (make-hash-table :size 30 :test 'equalp))
(defun register-bst-primitive (bst-name argument-types result-types lisp-function &key (ignore-redefinition nil) (side-effects-p nil))
(setf (gethash (string bst-name) *builtin-bst-functions*)
(make-bst-function :name (string bst-name)
:type 'built-in
:lisp-name lisp-function
:argument-types argument-types
:result-types result-types
:side-effects (make-instance 'side-effects :side-effects-p side-effects-p)
:ignore-redefinition-p ignore-redefinition)))
(defmacro define-bst-primitive (bst-name arglist result-types
&key interpreted compiled
(side-effects-p nil)
(ignore-redefinition-p nil))
`(setf (gethash ,(string bst-name) *builtin-bst-functions*)
(make-bst-function :name ,(string bst-name)
:type 'built-in
:argument-types ',(mapcar #'cadr arglist)
:pop-form-args ',(mapcar #'cddr arglist)
:result-types ',result-types
,@(if interpreted
`(:lisp-name
#'(lambda ,(mapcar #'car arglist)
,@(if (symbolp bst-name)
`((block ,bst-name
,interpreted))
(list interpreted))))
())
,@(if compiled
`(:lisp-form-maker
#'(lambda ,(mapcar #'car arglist)
,compiled))
())
:side-effects (make-instance 'side-effects :side-effects-p ,side-effects-p)
:ignore-redefinition-p ,ignore-redefinition-p)))
(defun register-bst-entry (entry func-type type default-value hash-table)
(setq entry (string entry))
(setf (gethash entry hash-table)
(make-bst-function :name entry
:lisp-name #'(lambda ()
(bib-entry-ref entry *bib-entry* default-value))
:lisp-form-maker #'(lambda ()
`(bib-entry-ref ,entry *bib-entry* ,default-value))
:setter #'(lambda (value) (setf (bib-entry-ref entry *bib-entry*)
value))
:setter-form-maker #'(lambda (value-form)
`(setf (bib-entry-ref ,entry *bib-entry*)
,value-form))
:side-effects (make-instance 'side-effects :used-variables (list entry)
:variables-used-before-assigned (list entry))
:type func-type
:argument-types '()
:result-types (list type))))
(defun register-bst-global-var (variable lisp-name func-type type
initial-value hash-table
&key (constant-p nil))
(let ((variable (string variable)))
(setf (gethash variable hash-table)
(make-bst-function :name variable
:lisp-name lisp-name
:setter #'(lambda (value)
(setf (bst-function-value
(gethash variable hash-table))
value))
:lisp-form-maker #'(lambda () lisp-name)
:setter-form-maker #'(lambda (value-form)
`(setq ,lisp-name ,value-form))
:side-effects (make-instance 'side-effects :used-variables (list variable)
:variables-used-before-assigned (list variable))
:assigned-value-form initial-value
:constant-p constant-p
:type func-type
:argument-types '()
:result-types (list type)
:value initial-value))))
(defvar *bst-functions* nil)
(defun builtin-bst-functions ()
(let ((table (make-hash-table :test 'equalp)))
(loop for key being each hash-key of *builtin-bst-functions*
do (setf (gethash key table)
(gethash key *builtin-bst-functions*)))
table))
(defvar *bst-package* nil
"A temporary package where the BST compiler puts the symbols
generated for the BibTeX style file")
(defun bst-name-to-lisp-name (bst-name &optional (type :function))
(setq bst-name (substitute #\- #\. (string-upcase (string bst-name))))
(ecase type
((:function :lexical)
(if (string-equal bst-name "T")
(gentemp "TEMP" *bst-package*)
(bst-intern bst-name)))
((:constant)
(bst-intern (concatenate 'string "+" bst-name "+")))
((:variable)
(bst-intern (concatenate 'string "*" bst-name "*")))))
(defun bst-intern (name)
"Intern NAME into *BST-PACKAGE*, shadowing imported symbols."
(multiple-value-bind (symbol type)
(find-symbol name *bst-package*)
(case type
((nil :inherited)
(shadow name *bst-package*))))
(intern name *bst-package*))
(defun check-for-already-defined-function (name)
(unless (symbolp name)
(error "~A is not a valid identifier" name))
(let ((function (gethash (string name) *bst-functions*)))
(when function
(unless (bst-function-ignore-redefinition-p function)
(error "~A is already a ~A function name"
name (bst-function-type function))))
function))
(defun get-bst-function-of-type (name &optional (type-list t))
"Check whether NAME is the name of a BST function, whose type is one
contained in TYPE-LIST. If so, return that function. Otherwise,
signal an error and don't return."
(let ((function (gethash (string name) *bst-functions*)))
(unless function
(error "~A is an unknown function" name))
(when (and (not (eql type-list t))
(not (member (bst-function-type function)
type-list)))
(error "~A has bad function type" name))
function))
;;; Local Variables:
;;; eval: (put 'define-bst-primitive 'lisp-indent-function 3)
;;; End: