File: theme-d-address-environment.scm

package info (click to toggle)
theme-d 1.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 12,784 kB
  • sloc: lisp: 47,684; sh: 4,200; makefile: 455; ansic: 319
file content (247 lines) | stat: -rw-r--r-- 8,131 bytes parent folder | download
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
;; Copyright (C) 2008-2013 Tommi Höynälänmaa
;; Distributed under GNU General Public License version 3,
;; see file doc/GPL-3.


;; *** Linker environment ***


(import (rnrs exceptions)
	(srfi srfi-1)
	(th-scheme-utilities stdutils)
	(th-scheme-utilities hrecord))


(define gl-i-address-env-size 10000)
(define gl-i-local-address-env-size 50)

(define-hrecord-type <address-environment> ()
  address-env-parent
  ht-bindings)


(define (construct-address-environment ae-parent i-size)
  (let ((ht-bindings (make-hash-table i-size)))
    (make-hrecord <address-environment> ae-parent ht-bindings)))


(define (check-binding sym obj-value)
  (if (and (symbol? sym)
	   (or (hrecord-is-instance? obj-value <normal-variable>)
	       (hrecord-is-instance? obj-value <keyword>)
	       (is-target-object? obj-value))
	   (is-address? (hfield-ref obj-value 'address))
	   (check-address (hfield-ref obj-value 'address)))
      #t
      (raise 'invalid-binding)))


(define (make-address-environment compiler-root-env)
  (assert (hrecord-is-instance? compiler-root-env <environment>))
  (assert (null? (hfield-ref compiler-root-env 'parent)))
  (let* ((address-env
	  (construct-address-environment '() gl-i-address-env-size))
	 (ht-bindings (hfield-ref address-env 'ht-bindings)))
    (hash-for-each
     (lambda (sym-key obj-value)
       (dvar1-set! sym-key)
       (dvar2-set! obj-value)
       (check-binding sym-key obj-value)
       (let ((address (hfield-ref obj-value 'address)))
	 (hashx-set! address-hash address-assoc ht-bindings
		     address obj-value)))
     (hfield-ref compiler-root-env 'ht))
    address-env))


(define (address-env-bindings-search ht-bindings address)
  (assert (hash-table? ht-bindings))
  (assert (hrecord-is-instance? address <address>))
  (hashx-ref address-hash address-assoc ht-bindings address))


(define (address-env-get-item address-env address)
  (assert (hrecord-is-instance? address-env <address-environment>))
  (assert (hrecord-is-instance? address <address>))
  (let ((x (address-env-bindings-search
	    (hfield-ref address-env 'ht-bindings)
	    address)))
    (if x
	x
	(let ((address-env-parent (hfield-ref address-env 'address-env-parent)))
	  (if (not-null? address-env-parent)
	      (address-env-get-item address-env-parent address)
	      #f)))))


(define (address-env-address-exists? address-env address)
  (assert (hrecord-is-instance? address-env <address-environment>))
  (assert (hrecord-is-instance? address <address>))
  (if (address-env-get-item address-env address) #t #f))


(define (address-env-bindings-search-general ht-bindings pred)
  (assert (hash-table? ht-bindings))
  (assert (procedure? pred))
  (let ((var-result #f))
    (hash-for-each
     (lambda (address var)
       (if (pred address var)
	   (set! var-result var)))
     ht-bindings)
    var-result))


(define (address-env-get-item-general address-env pred)
  (assert (hrecord-is-instance? address-env <address-environment>))
  (assert (procedure? pred))
  (let ((x (address-env-bindings-search-general
	    (hfield-ref address-env 'ht-bindings)
	    pred)))
    (if x
	x
	(let ((address-env-parent (hfield-ref address-env 'address-env-parent)))
	  (if (not-null? address-env-parent)
	      (address-env-get-item-general address-env-parent pred)
	      #f)))))


(define (address-env-add-binding! address-env location)
  (assert (hrecord-is-instance? address-env <address-environment>))
  (assert (or (is-target-object? location) (is-normal-variable? location)))
  (let ((ht-bindings (hfield-ref address-env 'ht-bindings))
	(address (hfield-ref location 'address)))
    (hashx-set! address-hash address-assoc ht-bindings address location)))


(define (address-env-add-binding2! address-env address location)
  (assert (hrecord-is-instance? address-env <address-environment>))
  (assert (or (is-target-object? location) (is-normal-variable? location)))
  (let ((ht-bindings (hfield-ref address-env 'ht-bindings)))
    (hashx-set! address-hash address-assoc ht-bindings address location)))


;; HUOM: tarkistetaan vain lähin ympäristö hierarkiassa
(define (address-env-bind-object! binder address-env rebind? to)
  (if rebind?
      (let ((to-old (address-env-bindings-search
		     (hfield-ref address-env 'ht-bindings)
		     (hfield-ref to 'address))))
	(assert (not (eq? to-old #f)))
	(let ((old-type (get-entity-type to-old))
	      (new-type (get-entity-type to)))
	  (if (and
	       (is-t-subtype-fwd? binder new-type old-type)
	       (= (length (tno-field-ref new-type 'l-all-fields))
		  (length (tno-field-ref old-type 'l-all-fields))))
	      (begin
		(set-object1! to-old to)
		to-old)
	      (begin
		(dvar1-set! to)
		(dvar2-set! to-old)
		(dvar3-set! binder)
		(let ((address (hfield-ref to 'address)))
		  (raise (list 'forward-definition-type-mismatch
			       (cons 's-name
				     (hfield-ref address 'source-name))
			       (cons 'module
				     (hfield-ref address 'module))
			       (cons 'tt-actual new-type)
			       (cons 'tt-declared old-type))))))))
      (begin
	(address-env-add-binding! address-env to)
	to)))


;; HUOM: tarkistetaan vain lähin ympäristö hierarkiassa
(define (address-env-bind-object2! binder address-env rebind? address to)
  (if rebind?
      (let ((to-old (address-env-bindings-search
		     (hfield-ref address-env 'ht-bindings)
		     address)))
	(assert (not (eq? to-old #f)))
	(let ((old-type (get-entity-type to-old))
	      (new-type (get-entity-type to)))
	  (if (and
	       (is-t-subtype-fwd? binder new-type old-type)
	       (= (length (tno-field-ref new-type 'l-all-fields))
		  (length (tno-field-ref old-type 'l-all-fields))))
	      (begin
		(set-object1! to-old to)
		to-old)
	      (begin
		(dvar1-set! to)
		(dvar2-set! to-old)
		(dvar3-set! binder)
		(raise 'forward-definition-type-mismatch-4)))))
      (begin
	(address-env-add-binding2! address-env address to)
	to)))


;; HUOM: tarkistetaan vain lähin ympäristö hierarkiassa
(define (address-env-bind-variable! binder address-env rebind? variable)
  (if rebind?
      (let* ((old-variable
	      (address-env-bindings-search
	       (hfield-ref address-env 'ht-bindings)
	       (hfield-ref variable 'address))))
	(assert (not (eq? old-variable #f)))
	(let ((old-type (get-entity-type old-variable))
	      (new-type (get-entity-type variable)))
	  (if (is-t-subtype-fwd? binder new-type old-type)
	      (begin
		;; Should we update old-variable.address, too?
		(hfield-set! old-variable 'type new-type)
		(hfield-set! old-variable 'exact-type?
			     (hfield-ref variable 'exact-type?))
		(hfield-set! old-variable 'read-only?
			     (hfield-ref variable 'read-only?))
		(hfield-set! old-variable 'forward-decl?
			     (hfield-ref variable 'forward-decl?))
		(hfield-set! old-variable 'value '())
		(hfield-set! old-variable 'value-expr
			     (hfield-ref variable 'value-expr))
		old-variable)
	      (begin
		(dvar1-set! variable)
		(dvar2-set! old-type)
		(dvar3-set! new-type)
		(dvar4-set! binder)
		(raise 'forward-definition-type-mismatch-1)))))
      (begin
	(address-env-add-binding! address-env variable)
	variable)))


(define (construct-local-address-env parent-env local-bindings)
  (assert (hrecord-is-instance? parent-env <address-environment>))
  (assert (list? local-bindings))
  (let* ((address-env
	  (construct-address-environment
	   parent-env gl-i-local-address-env-size))
	 (ht-bindings (hfield-ref address-env 'ht-bindings)))
    (for-each
     (lambda (var)
       (let ((address (hfield-ref var 'address)))
	 (hashx-set! address-hash address-assoc ht-bindings
		     address var)))
     local-bindings)
    address-env))

(define (construct-local-address-env2 parent-env local-bindings)
  (assert (hrecord-is-instance? parent-env <address-environment>))
  (assert (list? local-bindings))
  (let* ((address-env
	  (construct-address-environment
	   parent-env gl-i-local-address-env-size))
	 (ht-bindings (hfield-ref address-env 'ht-bindings)))
    (for-each
     (lambda (p-binding)
       (hashx-set! address-hash address-assoc ht-bindings
		   (car p-binding) (cdr p-binding)))
     local-bindings)
    address-env))