4 import parsing # TODO Remove this import, as we should be normalizing everything before it gets here
6 CIntegerLiteral = collections.namedtuple(
13 CStringLiteral = collections.namedtuple(
21 CConstantExpression = collections.namedtuple(
22 'CConstantExpression',
28 CVariableExpression = collections.namedtuple(
29 'CVariableExpression',
35 CSymbolExpression = collections.namedtuple(
43 CNegationExpression = collections.namedtuple(
44 'CNegationExpression',
50 CFunctionCallForFurInfixOperator = collections.namedtuple(
51 'CFunctionCallForFurInfixOperator',
59 CFunctionCallExpression = collections.namedtuple(
60 'CFunctionCallExpression',
68 # TODO We are currently not changing variables, just preventing them from being accessed.
69 CSymbolAssignmentStatement = collections.namedtuple(
70 'CSymbolAssignmentStatement',
73 'target_symbol_list_index',
78 CArrayVariableInitializationStatement = collections.namedtuple(
79 'CArrayVariableInitializationStatement',
86 CVariableInitializationStatement = collections.namedtuple(
87 'CVariableInitializationStatement',
94 CVariableReassignmentStatement = collections.namedtuple(
95 'CVariableReassignmentStatement',
102 CExpressionStatement = collections.namedtuple(
103 'CExpressionStatement',
109 CIfElseStatement = collections.namedtuple(
112 'condition_expression',
118 CFunctionDeclaration = collections.namedtuple(
119 'CFunctionDeclaration',
125 # TODO If a function definition doesn't end with an expression, we have issues currently because we try to return statement.
126 # TODO Closures currently wrap entire defining environment, even symbols that are not used, which makes garbage collection ineffective.
127 CFunctionDefinition = collections.namedtuple(
128 'CFunctionDefinition',
131 'argument_name_list',
136 CProgram = collections.namedtuple(
140 'function_definition_list',
141 'operator_declarations',
143 'standard_libraries',
144 'string_literal_list',
152 'print': ['stdio.h'],
156 def transform_variable_expression(accumulators, expression):
157 return CVariableExpression(variable=expression.variable)
159 def transform_string_literal_expression(accumulators, expression):
160 value = expression.string
163 index = accumulators.string_literal_list.index(value)
165 index = len(accumulators.string_literal_list)
166 accumulators.string_literal_list.append(value)
168 return CStringLiteral(index=index, value=value)
170 def transform_symbol_expression(accumulators, expression):
171 if expression.symbol in ['true', 'false']:
172 return CConstantExpression(value=expression.symbol)
175 symbol_list_index = accumulators.symbol_list.index(expression.symbol)
177 symbol_list_index = len(accumulators.symbol_list)
178 accumulators.symbol_list.append(expression.symbol)
180 return CSymbolExpression(
181 symbol=expression.symbol,
182 symbol_list_index=symbol_list_index,
185 CInfixDeclaration = collections.namedtuple(
195 INFIX_OPERATOR_TO_DECLARATION = {
196 '+': CInfixDeclaration(name='add', in_type='integer', out_type='integer', operator='+'),
197 '-': CInfixDeclaration(name='subtract', in_type='integer', out_type='integer', operator='-'),
198 '*': CInfixDeclaration(name='multiply', in_type='integer', out_type='integer', operator='*'),
199 '//': CInfixDeclaration(name='integerDivide', in_type='integer', out_type='integer', operator='/'),
200 '%': CInfixDeclaration(name='modularDivide', in_type='integer', out_type='integer', operator='%'),
201 'and': CInfixDeclaration(name='and', in_type='boolean', out_type='boolean', operator='&&'),
202 'or': CInfixDeclaration(name='or', in_type='boolean', out_type='boolean', operator='||'),
203 '==': CInfixDeclaration(name='equals', in_type='integer', out_type='boolean', operator='=='),
204 '!=': CInfixDeclaration(name='notEquals', in_type='integer', out_type='boolean', operator='!='),
205 '<=': CInfixDeclaration(name='lessThanOrEqual', in_type='integer', out_type='boolean', operator='<='),
206 '>=': CInfixDeclaration(name='greaterThanOrEqual', in_type='integer', out_type='boolean', operator='>='),
207 '<': CInfixDeclaration(name='lessThan', in_type='integer', out_type='boolean', operator='<'),
208 '>': CInfixDeclaration(name='greaterThan', in_type='integer', out_type='boolean', operator='>'),
211 def transform_comparison_level_expression(accumulators, expression):
212 accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[expression.operator])
214 # Transform expressions like 1 < 2 < 3 into expressions like 1 < 2 && 2 < 3
215 if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'comparison_level':
216 left = transform_comparison_level_expression(
223 right = transform_expression(
228 # TODO Don't evaluate the middle expression twice
229 return CFunctionCallForFurInfixOperator(
232 right=CFunctionCallForFurInfixOperator(
233 name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
239 return CFunctionCallForFurInfixOperator(
240 name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
241 left=transform_expression(accumulators, expression.left),
242 right=transform_expression(accumulators, expression.right),
245 def transform_infix_expression(accumulators, expression):
246 if expression.order == 'comparison_level':
247 return transform_comparison_level_expression(accumulators, expression)
249 accumulators.operator_set.add(INFIX_OPERATOR_TO_DECLARATION[expression.operator])
251 return CFunctionCallForFurInfixOperator(
252 name=INFIX_OPERATOR_TO_DECLARATION[expression.operator].name,
253 left=transform_expression(accumulators, expression.left),
254 right=transform_expression(accumulators, expression.right),
257 def transform_integer_literal_expression(accumulators, expression):
258 return CIntegerLiteral(value=expression.integer)
260 def transform_negation_expression(accumulators, expression):
261 return CNegationExpression(
262 value=transform_expression(accumulators, expression.internal_expression),
265 def transform_expression(accumulators, expression):
266 # TODO Clean up handlers for parsing expressions
268 parsing.FurFunctionCallExpression: transform_function_call_expression,
269 parsing.FurInfixExpression: transform_infix_expression,
270 parsing.FurIntegerLiteralExpression: transform_integer_literal_expression,
271 parsing.FurNegationExpression: transform_negation_expression,
272 parsing.FurStringLiteralExpression: transform_string_literal_expression,
273 normalization.NormalFunctionCallExpression: transform_function_call_expression,
274 normalization.NormalInfixExpression: transform_infix_expression,
275 normalization.NormalIntegerLiteralExpression: transform_integer_literal_expression,
276 normalization.NormalNegationExpression: transform_negation_expression,
277 normalization.NormalStringLiteralExpression: transform_string_literal_expression,
278 normalization.NormalSymbolExpression: transform_symbol_expression,
279 normalization.NormalVariableExpression: transform_variable_expression,
280 }[type(expression)](accumulators, expression)
282 def transform_symbol_assignment_statement(accumulators, assignment_statement):
283 # TODO Check that target is not a builtin
285 symbol_list_index = accumulators.symbol_list.index(assignment_statement.target)
287 symbol_list_index = len(accumulators.symbol_list)
288 accumulators.symbol_list.append(assignment_statement.target)
290 return CSymbolAssignmentStatement(
291 target=assignment_statement.target,
292 target_symbol_list_index=symbol_list_index,
293 expression=transform_expression(
295 assignment_statement.expression,
299 def transform_function_call_expression(accumulators, function_call):
300 if isinstance(function_call.function, normalization.NormalSymbolExpression):
301 # TODO Move this check to transformation of symbol expressions so we can have builtins that aren't functions
302 if function_call.function.symbol in BUILTINS.keys():
303 # TODO Check that the builtin is actually callable
304 accumulators.builtin_set.add(function_call.function.symbol)
306 # TODO Use the symbol from SYMBOL LIST
307 return CFunctionCallExpression(
308 name=transform_expression(accumulators, function_call.function),
309 argument_count=function_call.argument_count,
310 argument_items=transform_expression(accumulators, function_call.argument_items),
313 def transform_expression_statement(accumulators, statement):
314 return CExpressionStatement(
315 expression=transform_expression(accumulators, statement.expression),
318 def transform_if_else_statement(accumulators, statement):
319 return CIfElseStatement(
320 condition_expression=transform_expression(accumulators, statement.condition_expression),
321 if_statements=tuple(transform_statement(accumulators, s) for s in statement.if_statements),
322 else_statements=tuple(transform_statement(accumulators, s) for s in statement.else_statements),
325 def transform_array_variable_initialization_statement(accumulators, statement):
326 return CArrayVariableInitializationStatement(
327 variable=statement.variable,
328 items=tuple(transform_expression(accumulators, i) for i in statement.items),
331 def transform_variable_initialization_statement(accumulators, statement):
332 return CVariableInitializationStatement(
333 variable=statement.variable,
334 expression=transform_expression(accumulators, statement.expression),
337 def transform_variable_reassignment_statement(accumulators, statement):
338 return CVariableReassignmentStatement(
339 variable=statement.variable,
340 expression=transform_expression(accumulators, statement.expression),
343 def transform_function_definition_statement(accumulators, statement):
344 # TODO Allow defining the same function in different contexts
345 if any(fd.name == statement.name for fd in accumulators.function_definition_list):
346 raise Exception('A function with name "{}" already exists'.format(statement.name))
348 # TODO Add argument names to the symbol table
349 accumulators.function_definition_list.append(CFunctionDefinition(
351 argument_name_list=statement.argument_name_list,
352 statement_list=tuple(transform_statement(accumulators, s) for s in statement.statement_list)
355 return CFunctionDeclaration(name=statement.name)
357 def transform_statement(accumulators, statement):
359 parsing.FurExpressionStatement: transform_expression_statement,
360 normalization.NormalArrayVariableInitializationStatement: transform_array_variable_initialization_statement,
361 normalization.NormalAssignmentStatement: transform_symbol_assignment_statement,
362 normalization.NormalExpressionStatement: transform_expression_statement,
363 normalization.NormalFunctionDefinitionStatement: transform_function_definition_statement,
364 normalization.NormalIfElseStatement: transform_if_else_statement,
365 normalization.NormalVariableInitializationStatement: transform_variable_initialization_statement,
366 normalization.NormalVariableReassignmentStatement: transform_variable_reassignment_statement,
367 }[type(statement)](accumulators, statement)
370 Accumulators = collections.namedtuple(
374 'function_definition_list',
377 'string_literal_list',
381 def transform(program):
382 accumulators = Accumulators(
384 function_definition_list=[],
387 string_literal_list=[],
391 transform_statement(accumulators, statement) for statement in program.statement_list
394 standard_library_set = set()
395 for builtin in accumulators.builtin_set:
396 for standard_library in BUILTINS[builtin]:
397 standard_library_set.add(standard_library)
400 builtin_set=accumulators.builtin_set,
401 function_definition_list=accumulators.function_definition_list,
402 operator_declarations=tuple(sorted(accumulators.operator_set)),
403 statements=statement_list,
404 standard_libraries=standard_library_set,
405 string_literal_list=accumulators.string_literal_list,
406 symbol_list=accumulators.symbol_list,
410 if __name__ == '__main__':