/
semantic_logic.py
434 lines (403 loc) · 16.9 KB
/
semantic_logic.py
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
from preloaded_data import mapping_functions
import copy
from preloaded_data import global_function
from globals import (global_data as gl, memory)
from validations import *
from Function import Function
from Variable import Variable
from GroupItem import GroupItem
from utils import array_params_to_dict
from Operator import Operator
# Redirects to the mehtod depending on the type of quadruples a function has to generate.
types = {
"both": gl.add_continuous_quadruples,
"left": gl.add_left_quadruples,
"array": gl.add_array_quadruple,
"map": gl.add_map_quadruple
}
def create_variable(variable_name, literal):
"""
Name: create_variable
Description: Creates a variable in the current scope.
Parameters:
variable_name: The name of the variable to create.
literal: The information of the content of the variable.
Returns: NA
Important methods where its called:
definition_variable: To define an array or variable.
"""
check_variable_type(variable_name, literal)
variable = gl.get_variable(variable_name)
if not variable:
# Add variable if not already declared.
new_dir = gl.get_last_data()
if literal.type == 'LIST':
(size, _, _) = literal.array_info
literal.virtual_direction = new_dir
literal.name = variable_name
literal.constant_direction = memory.get_last_constant()
memory.add_constant(new_dir, 'NUMBER')
new_variable = literal
for _ in range(0, size):
gl.add_memory(None)
else:
gl.add_memory(None)
new_variable = Variable(variable_name, literal.type, new_dir)
gl.add_variable(variable_name, new_variable)
variable = gl.get_variable(variable_name)
else:
if literal.type == 'LIST':
new_dir = gl.get_last_data()
(size, _, _) = literal.array_info
literal.virtual_direction = new_dir
literal.name = variable_name
literal.constant_direction = memory.get_last_constant()
memory.add_constant(new_dir, 'NUMBER')
new_variable = literal
for _ in range(0, size):
gl.add_memory(None)
gl.add_variable(variable_name, new_variable)
variable = gl.get_variable(variable_name)
if literal.type != 'LIST':
memory.add_assign(literal.virtual_direction,
variable.virtual_direction)
def definition_function_parameters(self, ctx, parameters):
"""
Name: definition_function_parameters
Description: Gets all the parameters of a function.
Parameters:
ctx: Holds the context of the parameters rule.
Returns: All the parameters information of a function.
Important methods where its called:
definition_function: To hold all the parameters of the function.
"""
counter = 0
new_parameters = []
for param in parameters:
new_parameters.append(self.parameters(param))
for param in new_parameters:
current_position = 0
finish = False
while not finish:
found_item = False
for (_, attr) in param.items():
if attr.pos == current_position:
attr.global_pos = counter
counter += 1
current_position += 1
found_item = True
break
if not found_item:
finish = True
return array_params_to_dict(new_parameters)
def do_function_execution(self, ctx):
"""
Name: do_function_execution
Description: Generates quadruples for user defined function calls.
Parameters:
ctx: Holds the context of the execution_function_name rule.
Returns: The name of the function and its returned direction.
Important methods where its called:
execution_function_name: To execute a user definied function.
"""
(function_name, grps) = get_execution_data(self, ctx)
func = gl.functions[function_name]
(new_function_name, groups) = prepare_arguments(
self, grps, function_name)
memory.add_quadruple(Operator.ERA, func.virtual_directon, None, None)
parameters = gl.functions[function_name].parameters
for group in groups:
for key, parameter in parameters.items():
if parameter.pos == group.pos and parameter.param == group.param:
if parameter.type == 'LIST':
(_, _, size_dir) = parameter.array_info
memory.add_quadruple(
Operator.PARAMARR, group.virtual_direction, size_dir, parameter.virtual_direction)
else:
memory.add_quadruple(
Operator.PARAM, group.virtual_direction, None, parameter.virtual_direction)
memory.add_quadruple(Operator.GOSUB, func.code_direction, None, None)
new_dir = gl.get_last_data()
gl.add_memory(None)
memory.add_quadruple(Operator.ASSIGN, func.return_direction, None, new_dir)
return (new_function_name, new_dir)
def get_id(ctx):
"""
Name: get_id
Description: Gets the id. It can be built with spaces. Ex: 'important stuff'--> 'importantstuff'
Parameters:
ctx: Holds the context of the identification2 rule.
Returns: The id of what was accessed.
Important methods where its called:
identification: To access the built id.
"""
id = ctx.Id()
if id != None:
rest_id = get_id(ctx.identification2())
return id.getText() + rest_id
return ""
def create_function(self, ctx, function_name, parameters, initial_virtual_direction, literal, return_direction):
"""
Name: create_function
Description: Creates the information for the function with its corresponding quadruples.
Parameters:
ctx: Holds the context of the definition_function rule.
function_name: The name of the function to create.
parameters: The information of the parameters of the function.
initial_virtual_direction: The virtual direction to access after Start Proc.
literal: Information about the return literal.
return_direction: The global direction used for the return.
Returns: NA
Important methods where its called:
definition_function: To create the quadruples of the function.
"""
# Ensure the new function is not already defined
check_defined_function(function_name)
code_virtual_direction = memory.get_last_code() + 1
# Add the function to the functions table
gl.functions[function_name] = Function(
function_name, literal.type, {}, parameters, False, initial_virtual_direction, code_virtual_direction, return_direction, literal.array_info)
memory.add_quadruple(Operator.PARAMEND, None, None, None)
self.function(ctx.function())
gl.current_scope = global_function
memory.add_quadruple(Operator.ENDPROC, None, None, None)
memory.local_segment.clear()
def get_group_variables(self, ctx, current_position=0):
"""
Name: get_group_variables
Description: Compiles part of the arguments of a function call.
Parameters:
ctx: Holds the context of the group2/group3 rule.
Returns: NA
Important methods where its called:
get_group_variables: To get all the arguments inside a group
group: Get the variables inside a group.
"""
if ctx.basic_literal() != None:
item = self.basic_literal(ctx.basic_literal())
new_item = GroupItem(item.type, current_position,
item.virtual_direction, None, item.array_info)
rest_items = get_group_variables(
self, ctx.group3(), current_position + 1)
return [new_item] + rest_items
return []
def prepare_arguments(self, groups, function_name):
"""
Name: prepare_arguments
Description: Gets the data needed for all arguments.
Parameters:
groups: Holds all group contexts.
function_name: The name of the function to be validated.
Returns: The name of the function and its arguments on a array of groups.
Important methods where its called:
execution_function_name: To execute a user definied function.
"""
arguments = []
for index, group in enumerate(groups):
current_arguments = self.group(group)
current_arguments = current_arguments.variables
for parameter in current_arguments:
parameter.param = index
arguments.extend(current_arguments)
check_function_match(arguments, function_name)
return (function_name, arguments)
def do_default_execution(self, ctx):
"""
Name: do_default_execution
Description: Generates the quadruples for common functions.
Parameters:
ctx: Holds the context of the execution rule.
Returns: Returns the name of the function and the last virtual direction.
Important methods where its called:
execution: To execute common functions.
"""
# Iterate all mapping functions until it finds one that exists in context
for name, operation in mapping_functions.items():
if getattr(ctx, name) and getattr(ctx, name)() != None:
new_ctx = getattr(ctx, name)()
groups = new_ctx.group()
if not isinstance(groups, (list)):
groups = [new_ctx.group()]
(function_name, parameters) = prepare_arguments(
self, groups, operation["name"])
quadruple_function = types[operation["type"]]
direction = quadruple_function(operation["operation"], parameters)
return (function_name, direction)
def do_if_execution(self, ctx):
"""
Name: do_if_execution
Description: Generates the quadruples for the if statement.
Parameters:
ctx: Holds the context of the if_execution rule.
Returns: NA
Important methods where its called:
if_execution: To execute an if statement.
do_else_execution: To execute an if statement after an else.
"""
function_name = "if(param)"
(_, parameters) = prepare_arguments(
self, [ctx.group()], function_name)
condition_dir = parameters[0].virtual_direction
memory.add_gotoFalse(condition_dir)
gl.add_jump(-1)
self.function(ctx.function())
self.else_execution(ctx)
def do_else_execution(self, ctx):
"""
Name: do_else_execution
Description: Generates the quadruples for the else statement.
Parameters:
ctx: Holds the context of the else_execution rule.
Returns: NA
Important methods where its called:
else_execution: To execute an else statement.
"""
if ctx.Else() != None:
else_dir = gl.pending_jumps.pop()
memory.fill_jump(else_dir, 1)
memory.add_goto(None)
gl.add_jump(-1)
if ctx.function() != None:
self.function(ctx.function())
else:
self.if_execution(ctx)
end_dir = gl.pending_jumps.pop()
memory.fill_jump(end_dir)
def do_iterate_execution(self, ctx):
"""
Name: do_iterate_execution
Description: Generates the quadruples for a while iterate statement.
Parameters:
ctx: Holds the context of the iterate_execution rule.
Returns: NA
Important methods where its called:
iterate_execution: To execute an iterate statement.
"""
function_name = "iteratewhile(param)"
gl.add_jump()
(_, parameters) = prepare_arguments(
self, [ctx.group()], function_name)
condition_dir = parameters[0].virtual_direction
memory.add_gotoFalse(condition_dir)
gl.add_jump(-1)
self.function(ctx.function())
begin_dir = gl.pending_jumps.pop()
end_dir = gl.pending_jumps.pop()
memory.add_goto(end_dir)
memory.fill_jump(begin_dir)
def get_parameters_data(self, ctx, param_position=0):
"""
Name: get_parameters_data
Description: Retrieves the parameters of a group of a function.
Parameters:
ctx: Holds the context of the parameters3 rule.
Returns: A list of all the parameters of a group.
Important methods where its called:
parameters: To get the information of the parameters of a group of a function.
"""
parameters = {}
if ctx.definition() != None:
(name, literal, direction) = self.defintion_parameter(ctx.definition())
parameters = get_parameters_data(
self, ctx.parameters2(), param_position + 1)
parameters[name] = GroupItem(
literal.type, param_position, None, None, literal.array_info)
if literal.type == 'LIST':
parameters[name].constant_direction = literal.constant_direction
parameters[name].virtual_direction = direction
return parameters
def get_execution_data(self, ctx):
"""
Name: get_execution_data
Description: Gets both the name of the executed function and a array of all the passed parameters in order.
Parameters:
ctx: Holds the context of the execution_function_name rule.
Returns: The name of the function in order and its parameters on a array.
Important methods where its called:
do_function_execution: To get the name of the function and its parameters in order
get_execution_data: To get all the parameters
"""
id = ""
groups = []
if ctx.identification() != None or ctx.group() != None:
if ctx.identification() != None and ctx.children[0] == ctx.identification():
id += self.identification(ctx.identification())
if ctx.group() != None:
id += "(param)"
group = ctx.group()
groups = [group]
if ctx.identification() != None and ctx.children[0] != ctx.identification():
id += self.identification(ctx.identification())
(rest_id, rest_groups) = get_execution_data(self,
ctx.execution_function_name2())
groups = groups + rest_groups
id += rest_id
return (id, groups)
def get_definition_data(self, ctx):
"""
Name: get_definition_data
Description: Creates and gets information about the new funtion.
Parameters:
ctx: Holds the context of the definition_function_name rule.
Returns: The name of the function and the context of the parameters.
Important methods where its called:
definition_function_name: To create information about the function.
get_definition_data: To get all the parameters on the definition
"""
id = ""
parameters = []
if ctx.identification() != None or ctx.parameters() != None:
if ctx.identification() != None and ctx.children[0] == ctx.identification():
# Adds part of the id
id += self.identification(ctx.identification())
if ctx.parameters() != None:
id += "(param)"
parameter = ctx.parameters()
parameters = [parameter]
if ctx.identification() != None and ctx.children[0] != ctx.identification():
# Adds part of the id
id += self.identification(ctx.identification())
# Recursively get the rest of the id and parameters
(rest_id, rest_parameters) = get_definition_data(
self, ctx.definition_function_name2())
parameters = parameters + rest_parameters
id += rest_id
return (id, parameters)
def create_literal(self, ctx):
"""
Name: create_literal
Description: Gets or creates a literal adding it to memory (if needed).
Parameters:
ctx: Holds the context of the basic_literal rule.
Returns: Information about a basic literal (string, number, etc).
Important methods where its called:
basic_literal: To create and get the information of a literal.
"""
if ctx.identification() != None:
id = self.identification(ctx.identification())
check_variable_exits(id)
return gl.get_all_variables()[id]
elif ctx.array_access() != None:
(id, direction) = self.array_access(ctx.array_access())
return Variable("", 'ANY', direction)
elif ctx.execution() != None:
(function_name, virtual_direction) = self.execution(ctx.execution())
exection_type = gl.functions[function_name].type
array_info = gl.functions[function_name].return_array_info
if virtual_direction == None:
return_direction = gl.functions[function_name].return_direction
else:
return_direction = virtual_direction
return Variable("", exection_type, return_direction, array_info)
elif ctx.String() != None:
memory.add_constant(ctx.String().getText(), "STRING")
return Variable("", "STRING", memory.get_last_constant() - 1)
elif ctx.Boolean() != None:
memory.add_constant(ctx.Boolean().getText(), "BOOLEAN")
return Variable("", "BOOLEAN", memory.get_last_constant() - 1)
elif ctx.Number() != None:
memory.add_constant(ctx.Number().getText(), "NUMBER")
return Variable("", "NUMBER", memory.get_last_constant() - 1)
elif ctx.Empty() != None:
memory.add_constant(None, "ANY")
return Variable("", "ANY", memory.get_last_constant() - 1)