/
env.rml
340 lines (246 loc) · 8.03 KB
/
env.rml
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
(*
Copyright PELAB, Linkoping University
This file is part of Open Source Modelica (OSM).
OSM is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
OSM is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
(**
** file: env.rml
** module: Env
** description: Environmane management
**
** RCS: $Id$
**
** An environment is a stack of frames, where each frame contains a
** number of class and variable bindings.
**)
module Env:
with "absyn.rml"
with "values.rml"
with "explode.rml"
with "types.rml"
with "classinf.rml"
with "exp.rml"
type Ident = string
datatype Frame = FRAME of (Ident * Item) list
datatype Item = VAR of Types.Var * bool
| CLASS of SCode.Class * Env
| TYPE of Types.Type
type Env = Frame list
val empty_frame : Frame
val empty_env : Env
relation open_scope : Env => Env
relation extend_frame_c : (Env, SCode.Class) => Env
relation extend_frame_v : (Env, Types.Var,bool) => Env
relation extend_frame_t : (Env,Ident,Types.Type) => Env
relation print_env : Env => ()
relation print_env_graphviz : (Env * string) => ()
relation print_frame : Frame => ()
end
with "dump.rml"
with "graphviz.rml"
with "dae.rml"
(** - Values *)
val empty_frame = FRAME([])
val empty_env = []
(** - Relations *)
(** relation: open_scope
**
** Opening a new scope in the environment mans adding a new frame on
** top of the stack of frames.
**)
relation open_scope: Env => Env =
axiom open_scope(env) => empty_frame::env
(* rule
print "\nOpens scope."
-----------
open_scope(env) => empty_frame::env
*)
end
(** relation: extend_frame_c
**
** This relation adds a class definition to the environment.
**)
relation extend_frame_c : (Env, SCode.Class) => Env =
(*
rule print "extend_frame_c:\n" &
print_env env &
print " + " &
print n &
print " = " &
print_env ((FRAME((n,CLASS(c,env))::items)::fs)) &
print "\n"
----------------
extend_frame_c(env as (FRAME(items)::fs),c as SCode.CLASS(n,_,_,_))
=> ((FRAME((n,CLASS(c,env))::items)::fs))
*)
axiom extend_frame_c(env as (FRAME(items)::fs),c as SCode.CLASS(n,_,_,_))
=> ((FRAME((n,CLASS(c,env))::items)::fs))
end
(** relation: extend_frame_v
**
** This relation adds a component to the environment.
**)
relation extend_frame_v : (Env,Types.Var,bool) => Env =
(*
rule print "extend_frame_v:\n" &
print_env env &
print " + " &
print n &
print " = " &
print_env (FRAME((n,VAR(v))::items)::fs) &
print "\n"
----------------
extend_frame_v(env as (FRAME(items)::fs),v as Types.VAR(n,_,_,_,_))
=> (FRAME((n,VAR(v))::items)::fs)
*)
axiom extend_frame_v(FRAME(items)::fs,v as Types.VAR(n,_,_,_,_),b)
=> (FRAME((n,VAR(v,b))::items)::fs)
end
(** relation: extend_frame_t
**
** This relation adds a type to the environment. Types in the
** environment are used for looking up constants etc. inside class
** definitions, such as packages. For each type in the environment,
** there is a class definition with the same name in the
** environment.
**)
relation extend_frame_t : (Env,Ident,Types.Type) => Env =
(*
rule print "extend_frame_t:\n" &
print_env env &
print " + " &
print n &
print " = " &
print_env (FRAME((n,TYPE(t))::items)::fs) &
print "\n"
----------------
extend_frame_t(env as (FRAME(items)::fs), n, t)
=> (FRAME((n,TYPE(t))::items)::fs)
*)
axiom extend_frame_t(FRAME(items)::fs, n, t)
=> (FRAME((n,TYPE(t))::items)::fs)
end
relation print_env : Env => () =
rule print "Empty env\n"
-------------------
print_env []
rule print_frame fr &
print_env frs
------------
print_env fr::frs
end
relation print_frame : Frame => () =
rule print "FRAME: " &
print_frame_contents cont &
print "\n"
--------------------------------------------
print_frame FRAME(cont)
end
relation print_frame_contents: (Ident * Item) list => () =
axiom print_frame_contents []
rule print_frame_element e
-------------------------
print_frame_contents [e as (n,i)]
rule print_frame_element e & print "," & print_frame_contents rst
------------------------------------------------
print_frame_contents ((e as (n,i))::rst)
end
relation print_frame_element: (Ident * Item) => () =
rule print "v:" & print n
-------
print_frame_element ((n,VAR(_,_)))
rule print "c:" & print n
-------
print_frame_element ((n,CLASS(_,_)))
rule print "t:" & print n
-------
print_frame_element ((n,TYPE(_)))
end
relation print_env_graphviz : (Env * string) => () =
rule build_env_graphviz ((env,str)) => r &
Graphviz.dump(r)
----------------
print_env_graphviz ((env,str))
end
relation build_env_graphviz : (Env * string) => Graphviz.Node =
rule string_append ("ROOT: ", str) => str' &
build_env_graphviz_2 env => nodelist
------------------------------------
build_env_graphviz ((env,str)) => Graphviz.NODE(str',[],nodelist)
end
relation build_env_graphviz_2 : Env => Graphviz.Node list =
axiom build_env_graphviz_2 [] => []
rule build_env_graphviz_2 rest => nodelist &
build_frame_graphviz frame => node
----------------------------------
build_env_graphviz_2 frame::rest => node::nodelist
end
relation is_var_item =
axiom is_var_item ((_,VAR(_,_)))
end
relation is_class_item =
axiom is_class_item ((_,CLASS(_,_)))
end
relation is_type_item =
axiom is_type_item ((_,TYPE(_)))
end
relation build_frame_graphviz : Frame => Graphviz.Node =
rule build_item_listnode (itemlist, is_var_item,"VARS") => varsnode &
build_item_listnode (itemlist, is_class_item,"CLASSES") => classesnode &
build_item_listnode (itemlist, is_type_item,"TYPES") => typesnode &
let nodelist = [varsnode,classesnode,typesnode]
-------------------------------------------
build_frame_graphviz FRAME(itemlist) => Graphviz.NODE("FRAME",[],nodelist)
end
relation build_item_listnode : ((Ident * Item) list, (Ident * Item) => (), string)
=> Graphviz.Node =
rule DAE.get_matching (items, cond) => selitems &
build_item_listnode_2 (selitems, 1) => node
--------------------------------------------------------
build_item_listnode (items, cond, name)
=> Graphviz.NODE(name, [], [node])
end
relation build_item_listnode_2 : ((Ident * Item) list, int)
=> Graphviz.Node =
rule DAE.build_gr_strlist (items, build_item_str, 10) => (strlist, []) &
int_string count => cstr
--------------------------------------------------------
build_item_listnode_2 (items, count)
=> Graphviz.LNODE(cstr, strlist, [Graphviz.box], [])
rule DAE.build_gr_strlist (items, build_item_str, 10) => (strlist, ignored) &
int_string count => cstr &
int_add (count, 1) => count' &
build_item_listnode_2 (ignored, count') => restnode
--------------------------------------------------------
build_item_listnode_2 (items, count)
=> Graphviz.LNODE(cstr, strlist, [Graphviz.box], [restnode])
end
relation build_item_str : (Ident * Item) => string =
rule string_append ("VAR: ",id) => s
------------------------------
build_item_str ((id, VAR(_,_))) => s
rule (*build_env_graphviz env => r &*)
string_append ("CLASS: ", id) => s
----------------------------------
build_item_str ((id, CLASS(cls, env))) => s
rule string_append ("TYPE: ", id) => s
---------------------------------
build_item_str ((id, TYPE(_))) => s
end
(*
relation build_item_graphviz : (Ident, Item) => Graphviz.Node =
axiom build_item_graphviz (id, VAR(_)) => Graphviz.LNODE("VAR",[id],[],[])
axiom build_item_graphviz (id, CLASS(cls, env)) => Graphviz.LNODE("CLASS",[id],[],[])
axiom build_item_graphviz (id, TYPE(_)) => Graphviz.LNODE("TYPE",[id],[],[])
end
*)