-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSEMANTIC.PAS
364 lines (273 loc) · 10.1 KB
/
SEMANTIC.PAS
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
{ SEMANTIC.PAS
Description:
Used by the SYNTAX unit, it provides the high-level semantic checking
as well as .ACX file output.
}
unit semantic;
interface
uses misc, linklist, xarray, token, keywords, error, expr, id_table;
{ Global Variables }
var
Type_List, Object_List: xarray_type;
Overlooked : list_type;
{ Procedures and Functions }
function classify_as(var f: progfile;
id_number: integer;
interpretation: classify_type;
ptr_to_data: pointer): integer;
procedure get_meaning(id_number: integer; var meaning: classify_type;
var number: integer);
function display_undefined : boolean;
procedure add_undefined(the_ID : integer);
function verify_expr(var f: progfile; the_expr: expr_tree) : boolean;
implementation
type
integer_ptr = ^integer;
{ classify_as
Description:
Works closely with the ID_Table to create and verify the various semantic
interpretations of identifiers, which are classified as either:
TYPE_ID: names a type definition template in the type list.
OBJECT_ID: names an object instantiation in the object list.
ATTRIBUTE_ID: an attribute identifier.
ENUMERATE_ID: an identifier like "open" or "closed" which is simply
assigned so that it can be tested.
UNDEFINED_ID: Not defined anywhere. If /K is asserted for CREATE, then
this is the value returned by default; otherwise,
ENUMERATE_ID is.
Arguments:
f (IN) -- the progfile that is being read. Since
this function is part of the first "pass",
it needs access to the file being read.
id_number (IN) -- the index in the ID table
interpretation (IN) -- one of the constants above
ptr_to_data (IN) -- if not nil, points to the data that
the identifier represents (when first
encountered)
Returns: depends on interpretation:
TYPE_ID: the index in Type_List
OBJECT_ID: the index in Object_List
ATTRIBUTE_ID: the order the identifier was declared in, i.e.
for the first attribute encountered, 1, for the
second, 2, etc.
ENUMERATE_ID: the unchanged id_number, for a simple unique number.
UNDEFINED_ID: same as ENUMERATE_ID
In any case, classify_as returns 0 if there was an error.
Such an error will have been printed by this routine, so there
will be no need for the caller to print out its own.
}
var
NullStr : string_ptr;
function classify_as(var f: progfile;
id_number: integer;
interpretation: classify_type;
ptr_to_data: pointer): integer;
var
the_id_ptr : id_rec_ptr;
error_string : string;
begin
if not index_ident(id_number, the_id_ptr) then
error_message(f, 'Attempt to classify unencountered identifier')
else
with the_id_ptr^ do begin
if id_kind = interpretation then
classify_as := id_integer
{ If the existing id_kind is the DefaultClassification, we're allowed to
change it; otherwise there's a conflict }
else if id_kind = DefaultClassification then begin
id_kind := interpretation;
id_integer := id_index;
case id_kind of
TYPE_ID: begin
append_to_xarray(Type_List, ptr_to_data);
append_to_xarray(Type_ID_List, pointer(id_name));
id_integer := Type_List.size
end;
OBJECT_ID:
if ptr_to_data = nil then
id_integer := 0
else begin
{ Object_List may have grown by unnamed objects between calls to classify_as.
Fill in the intervening spaces with "null". }
while Object_ID_List.size < Object_List.size do
append_to_xarray(Object_ID_List, pointer(NullStr));
append_to_xarray(Object_List, ptr_to_data);
append_to_xarray(Object_ID_List, pointer(id_name));
id_integer := Object_List.size
end;
ATTRIBUTE_ID: begin
append_to_xarray(Attribute_ID_List, pointer(id_name));
id_integer := Attribute_ID_List.size
end;
end { case }
end
else begin
error_string := 'Identifier type conflict: "' + id_name^ +
'" already declared as ';
case id_kind of
TYPE_ID:
error_string := error_string + 'a type';
OBJECT_ID:
error_string := error_string + 'an object';
ATTRIBUTE_ID:
error_string := error_string + 'an attribute';
ENUMERATE_ID:
error_string := error_string + 'a keyword';
end;
error_message(f, error_string);
id_integer := 0
end;
classify_as := id_integer
end { with }
end; { classify_as }
{ get_meaning
Description:
Given an ID_Table index, finds what it represents and returns an
appropriate enumerated type and index.
If /K is asserted, default return is UNDEFINED_ID; else it is ENUMERATE_ID.
Arguments:
id_number (IN) -- integer index to ID_Table
meaning (OUT) -- classification of ID
number (OUT) -- integer appropriate to classification
}
procedure get_meaning(id_number: integer; var meaning: classify_type;
var number: integer);
var
the_id_ptr: id_rec_ptr;
begin
if not index_ident(id_number, the_id_ptr) then begin
writeln('Internal error: ',
'attempt to find meaning of unencountered identifier');
halt
end
else
with the_id_ptr^ do begin
meaning := id_kind;
number := id_integer
end
end; { get_meaning }
{ add_undefined
Description:
Used for adding the number of an undefined identifier to a list to be
produced at the end of translation.
}
procedure add_undefined(the_ID : integer);
var
np : node_ptr;
ip : integer_ptr;
begin
np := find_item(Overlooked, the_ID);
if np <> nil then
inc(integer_ptr(np^.data)^)
else begin
new(np);
np^.key := the_ID;
new(ip);
ip^ := 1;
np^.data := ip;
insert_item(Overlooked, np)
end
end;
{ display_undefined
Description:
Displays the list of undefined identifiers collected with add_undefined.
}
function display_undefined : boolean;
var
np : node_ptr;
ip : integer_ptr;
id_rec : id_rec_ptr;
i, ls : integer;
exists : boolean;
begin
exists := FALSE;
np := nil;
while iterate_list(Overlooked, np) do begin
if not exists then begin
writeln('The following identifiers were not explicitly defined.');
exists := TRUE
end;
ip := integer_ptr(np^.data);
write ('Used ', ip^ : 3);
if ip^ = 1 then write(' time: ') else write(' times: ');
if index_ident(np^.key, id_rec) then
writeln(id_rec^.id_name^)
else
writeln('<unknown identifier>');
dispose(ip)
end;
dispose_list(Overlooked);
display_undefined := exists
end; { display_undefined }
{ verify_expr
Description:
Assumes that expression tree contains no OP_LPAREN nodes.
Ensures the following:
1. All OP_DOT operators have identifiers as their right-hand
arguments, which are classified as ATTRIBUTE_ID's.
2. All assignment operators have OP_DOT operators or identifiers
as their left-hand arguments, and any such identifiers are
classified as ATTRIBUTE_ID's.
This is necessary because the only way to use the OP_DOT operator is
to discover the value of some attribute, and attributes are the only
things which may be assigned to.
Arguments:
f (IN/OUT) -- program file (for logging errors)
the_expr (IN) -- expression to be verified
}
function verify_expr(var f: progfile; the_expr: expr_tree) : boolean;
var
success : boolean;
begin
success := TRUE;
with the_expr^ do
case kind of
OPER : begin
case op_name of
OP_DOT: begin
if right^.kind <> IDENT then begin
error_message(f, 'Right side of dot must be an identifier');
success := FALSE
end
else if right^.ident_kind <> ATTRIBUTE_ID then
right^.ident_int :=
classify_as(f, right^.ident_int, ATTRIBUTE_ID, nil);
right^.ident_kind := ATTRIBUTE_ID;
if right^.ident_int = 0 then success := FALSE
end;
OP_ASSIGN, OP_C_CONCAT, OP_C_MULTIPLY,
OP_C_DIVIDE, OP_C_PLUS, OP_C_MINUS:
if left^.kind = IDENT then begin
get_meaning(left^.ident_int, left^.ident_kind,
left^.ident_int);
if left^.ident_kind <> ATTRIBUTE_ID then begin
error_message(f, 'Left side of assignment is not an attribute');
success := FALSE
end
end
else if not ((left^.kind = OPER) and
(left^.op_name = OP_DOT)) then begin
error_message(f,
'Left side of assignment must reference an attribute');
success := FALSE
end
end; { case }
if success then
if Binary[op_name] then success := verify_expr(f, left);
if success then success := verify_expr(f, right)
end; { OPER}
end; { case }
verify_expr := success
end; { verify_expr }
procedure init_NullStr;
var s : string;
begin
s := 'null'; NullStr := NewConstStr(s)
end;
begin { Initialization }
new_list(Overlooked);
new_xarray(Type_List);
new_xarray(Object_List);
init_NullStr
end. { unit semantic }