import collections
-import normalization
-import parsing
+import conversion
CIntegerLiteral = collections.namedtuple(
'CIntegerLiteral',
],
)
-CConstantExpression = collections.namedtuple(
- 'CConstantExpression',
- [
- 'value'
- ],
-)
-
CVariableExpression = collections.namedtuple(
'CVariableExpression',
[
],
)
-CNegationExpression = collections.namedtuple(
- 'CNegationExpression',
+CStructureLiteralExpression = collections.namedtuple(
+ 'CStructureLiteralExpression',
[
- 'value',
+ 'field_count',
+ 'symbol_list_variable',
+ 'value_list_variable',
],
)
-CFunctionCallForFurInfixOperator = collections.namedtuple(
- 'CFunctionCallForFurInfixOperator',
- [
- 'name',
- 'left',
- 'right',
- ],
+CPushStatement = collections.namedtuple(
+ 'CPushStatement',
+ (
+ 'expression',
+ ),
)
CFunctionCallExpression = collections.namedtuple(
'CFunctionCallExpression',
- [
- 'name',
+ (
+ 'metadata',
+ 'function_expression',
'argument_count',
- 'argument_items',
- ],
+ ),
)
+# TODO We are currently not changing variables, just preventing them from being accessed.
CSymbolAssignmentStatement = collections.namedtuple(
'CSymbolAssignmentStatement',
[
],
)
+CSymbolArrayVariableInitializationStatement = collections.namedtuple(
+ 'CSymbolArrayVariableInitializationStatement',
+ [
+ 'variable',
+ 'symbol_list',
+ 'symbol_list_indices',
+ ],
+)
+
CVariableInitializationStatement = collections.namedtuple(
'CVariableInitializationStatement',
[
'CIfElseStatement',
[
'condition_expression',
- 'if_statements',
- 'else_statements',
- ],
-)
-
-CFunctionDeclaration = collections.namedtuple(
- 'CFunctionDeclaration',
- [
- 'name',
+ 'if_statement_list',
+ 'else_statement_list',
],
)
+# TODO If a function definition doesn't end with an expression, we have issues currently because we try to return statement.
+# TODO Closures currently wrap entire defining environment, even symbols that are not used, which makes garbage collection ineffective.
CFunctionDefinition = collections.namedtuple(
'CFunctionDefinition',
[
'name',
+ 'argument_name_list',
'statement_list',
],
)
[
'builtin_set',
'function_definition_list',
+ 'operator_declarations',
'statements',
'standard_libraries',
'string_literal_list',
],
)
-EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING = {
- '==': 'equals',
- '!=': 'notEquals',
- '<=': 'lessThanOrEqual',
- '>=': 'greaterThanOrEqual',
- '<': 'lessThan',
- '>': 'greaterThan',
+BUILTINS = {
+ 'concatenate': [],
+ 'false': [],
+ 'pow': ['math.h'],
+ 'print': ['stdio.h'],
+ 'true': [],
}
-def transform_comparison_level_expression(accumulators, expression):
- # Transform expressions like 1 < 2 < 3 into expressions like 1 < 2 && 2 < 3
- if isinstance(expression.left, parsing.FurInfixExpression) and expression.left.order == 'comparison_level':
- left = transform_comparison_level_expression(
- accumulators,
- expression.left
- )
+def transform_variable_expression(accumulators, expression):
+ assert isinstance(expression, conversion.CPSVariableExpression)
+ return CVariableExpression(variable=expression.variable)
- middle = left.right
+def transform_string_literal_expression(accumulators, expression):
+ value = expression.string
- right = transform_expression(
- accumulators,
- expression.right,
- )
-
- # TODO Don't evaluate the middle expression twice
- return CFunctionCallForFurInfixOperator(
- name='and',
- left=left,
- right=CFunctionCallForFurInfixOperator(
- name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator],
- left=middle,
- right=right,
- ),
- )
-
- return CFunctionCallForFurInfixOperator(
- name=EQUALITY_LEVEL_OPERATOR_TO_FUNCTION_NAME_MAPPING[expression.operator],
- left=transform_expression(accumulators, expression.left),
- right=transform_expression(accumulators, expression.right),
- )
+ try:
+ index = accumulators.string_literal_list.index(value)
+ except ValueError:
+ index = len(accumulators.string_literal_list)
+ accumulators.string_literal_list.append(value)
-BUILTINS = {
- 'false': [],
- 'pow': ['math.h'],
- 'print': ['stdio.h'],
- 'true': [],
-}
+ return CStringLiteral(index=index, value=value)
-def transform_variable_expression(accumulators, expression):
- return CVariableExpression(variable=expression.variable)
+def transform_symbol_expression(accumulators, expression):
+ if expression.symbol in BUILTINS:
+ accumulators.builtin_set.add(expression.symbol)
+
+ try:
+ symbol_list_index = accumulators.symbol_list.index(expression.symbol)
+ except ValueError:
+ symbol_list_index = len(accumulators.symbol_list)
+ accumulators.symbol_list.append(expression.symbol)
-def transform_infix_expression(accumulators, expression):
- if expression.order == 'comparison_level':
- return transform_comparison_level_expression(accumulators, expression)
-
- INFIX_OPERATOR_TO_FUNCTION_NAME = {
- '+': 'add',
- '-': 'subtract',
- '*': 'multiply',
- '//': 'integerDivide',
- '%': 'modularDivide',
- 'and': 'and',
- 'or': 'or',
- }
-
- return CFunctionCallForFurInfixOperator(
- name=INFIX_OPERATOR_TO_FUNCTION_NAME[expression.operator],
- left=transform_expression(accumulators, expression.left),
- right=transform_expression(accumulators, expression.right),
+ return CSymbolExpression(
+ symbol=expression.symbol,
+ symbol_list_index=symbol_list_index,
)
-def transform_expression(accumulators, expression):
- if isinstance(expression, parsing.FurParenthesizedExpression):
- # Parentheses can be removed because everything in the C output is explicitly parenthesized
- return transform_expression(accumulators, expression.internal)
+def transform_integer_literal_expression(accumulators, expression):
+ return CIntegerLiteral(value=expression.integer)
+
+CListConstructExpression = collections.namedtuple(
+ 'CListConstructExpression',
+ (
+ 'allocate',
+ ),
+)
- if isinstance(expression, parsing.FurNegationExpression):
- return transform_negation_expression(accumulators, expression)
+CLambdaExpression = collections.namedtuple(
+ 'CLambdaExpression',
+ (
+ 'name',
+ ),
+)
- if isinstance(expression, parsing.FurFunctionCallExpression):
- return transform_function_call_expression(accumulators, expression)
+CListAppendStatement = collections.namedtuple(
+ 'CListAppendStatement',
+ (
+ 'list_expression',
+ 'item_expression',
+ ),
+)
- if isinstance(expression, parsing.FurSymbolExpression):
- if expression.value in ['true', 'false']:
- return CConstantExpression(value=expression.value)
+def transform_structure_literal_expression(accumulators, expression):
+ return CStructureLiteralExpression(
+ field_count=expression.field_count,
+ symbol_list_variable=expression.symbol_list_variable,
+ value_list_variable=expression.value_list_variable,
+ )
- if expression.value not in accumulators.symbol_list:
- symbol_list.append(expression.value)
+def transform_lambda_expression(accumulators, expression):
+ # TODO This function feels hacky
+ if len(accumulators.lambda_number_list) == 0:
+ accumulators.lambda_number_list.append(0)
+ else:
+ accumulators.lambda_number_list.append(accumulators.lambda_number_list[-1] + 1)
- return CSymbolExpression(
- symbol=expression.value,
- symbol_list_index=accumulators.symbol_list.index(expression.value),
- )
+ if expression.name is None:
+ name = '__lambda_{}'.format(accumulators.lambda_number_list[-1])
+ else:
+ name = expression.name
- if isinstance(expression, parsing.FurStringLiteralExpression):
- value = expression.value
+ accumulators.function_definition_list.append(CFunctionDefinition(
+ name=name,
+ argument_name_list=expression.argument_name_list,
+ statement_list=tuple(transform_statement(accumulators, s) for s in expression.statement_list),
+ ))
- try:
- index = accumulators.string_literal_list.index(value)
- except ValueError:
- index = len(accumulators.string_literal_list)
- accumulators.string_literal_list.append(value)
+ return CLambdaExpression(name=name)
- return CStringLiteral(index=index, value=value)
- LITERAL_TYPE_MAPPING = {
- parsing.FurIntegerLiteralExpression: CIntegerLiteral,
- }
+def transform_list_construct_expression(accumulators, expression):
+ return CListConstructExpression(allocate=expression.allocate)
- if type(expression) in LITERAL_TYPE_MAPPING:
- return LITERAL_TYPE_MAPPING[type(expression)](value=expression.value)
+def transform_list_append_statement(accumulators, expression):
+ return CListAppendStatement(
+ list_expression=transform_expression(accumulators, expression.list_expression),
+ item_expression=transform_expression(accumulators, expression.item_expression),
+ )
- # TODO Handle all possible types in this form
+def transform_expression(accumulators, expression):
return {
- parsing.FurInfixExpression: transform_infix_expression, # TODO Shouldn't need this
- normalization.NormalFunctionCallExpression: transform_function_call_expression,
- normalization.NormalInfixExpression: transform_infix_expression,
- normalization.NormalNegationExpression: transform_negation_expression,
- normalization.NormalVariableExpression: transform_variable_expression,
+ conversion.CPSFunctionCallExpression: transform_function_call_expression,
+ conversion.CPSIntegerLiteralExpression: transform_integer_literal_expression,
+ conversion.CPSLambdaExpression: transform_lambda_expression,
+ conversion.CPSListConstructExpression: transform_list_construct_expression,
+ conversion.CPSStructureLiteralExpression: transform_structure_literal_expression,
+ conversion.CPSStringLiteralExpression: transform_string_literal_expression,
+ conversion.CPSSymbolExpression: transform_symbol_expression,
+ conversion.CPSVariableExpression: transform_variable_expression,
}[type(expression)](accumulators, expression)
def transform_symbol_assignment_statement(accumulators, assignment_statement):
# TODO Check that target is not a builtin
- if assignment_statement.target not in accumulators.symbol_list:
+ try:
+ symbol_list_index = accumulators.symbol_list.index(assignment_statement.target)
+ except ValueError:
+ symbol_list_index = len(accumulators.symbol_list)
accumulators.symbol_list.append(assignment_statement.target)
return CSymbolAssignmentStatement(
target=assignment_statement.target,
- target_symbol_list_index=accumulators.symbol_list.index(assignment_statement.target),
+ target_symbol_list_index=symbol_list_index,
expression=transform_expression(
accumulators,
assignment_statement.expression,
),
)
-def transform_negation_expression(accumulators, expression):
- return CNegationExpression(
- value=transform_expression(accumulators, expression.internal_expression),
- )
-
def transform_function_call_expression(accumulators, function_call):
- if function_call.function.value in BUILTINS.keys():
- # TODO Check that the builtin is actually callable
- accumulators.builtin_set.add(function_call.function.value)
-
# TODO Use the symbol from SYMBOL LIST
return CFunctionCallExpression(
- name=function_call.function.value,
+ metadata=function_call.metadata,
+ function_expression=transform_expression(accumulators, function_call.function_expression),
argument_count=function_call.argument_count,
- argument_items=transform_expression(accumulators, function_call.argument_items),
)
def transform_expression_statement(accumulators, statement):
- # TODO At some point we can verify that all expression types are supported and just call transform_expression
- expression = {
- parsing.FurFunctionCallExpression: transform_function_call_expression,
- parsing.FurInfixExpression: transform_expression,
- parsing.FurIntegerLiteralExpression: transform_expression,
- parsing.FurSymbolExpression: transform_expression,
- normalization.NormalFunctionCallExpression: transform_function_call_expression,
- normalization.NormalVariableExpression: transform_expression,
- }[type(statement.expression)](accumulators, statement.expression)
-
return CExpressionStatement(
- expression=expression,
+ expression=transform_expression(accumulators, statement.expression),
)
def transform_if_else_statement(accumulators, statement):
return CIfElseStatement(
condition_expression=transform_expression(accumulators, statement.condition_expression),
- if_statements=tuple(transform_statement(accumulators, s) for s in statement.if_statements),
- else_statements=tuple(transform_statement(accumulators, s) for s in statement.else_statements),
+ if_statement_list=tuple(transform_statement(accumulators, s) for s in statement.if_statement_list),
+ else_statement_list=tuple(transform_statement(accumulators, s) for s in statement.else_statement_list),
)
def transform_array_variable_initialization_statement(accumulators, statement):
items=tuple(transform_expression(accumulators, i) for i in statement.items),
)
+def transform_symbol_array_variable_initialization_statement(accumulators, statement):
+ symbol_list_indices = []
+
+ for symbol in statement.symbol_list:
+ try:
+ symbol_list_index = accumulators.symbol_list.index(symbol)
+ except ValueError:
+ symbol_list_index = len(accumulators.symbol_list)
+ accumulators.symbol_list.append(symbol)
+
+ symbol_list_indices.append(symbol_list_index)
+
+ return CSymbolArrayVariableInitializationStatement(
+ variable=statement.variable,
+ symbol_list=statement.symbol_list,
+ symbol_list_indices=tuple(symbol_list_indices),
+ )
+
def transform_variable_initialization_statement(accumulators, statement):
return CVariableInitializationStatement(
variable=statement.variable,
expression=transform_expression(accumulators, statement.expression),
)
-def transform_function_definition_statement(accumulators, statement):
- # TODO Allow defining the same function in different contexts
- if any(fd.name == statement.name for fd in accumulators.function_definition_list):
- raise Exception('A function with name "{}" already exists'.format(statement.name))
-
- accumulators.function_definition_list.append(CFunctionDefinition(
- name=statement.name,
- statement_list=tuple(transform_statement(accumulators, s) for s in statement.statement_list)
- ))
-
- return CFunctionDeclaration(name=statement.name)
+def transform_push_statement(accumulators, statement):
+ return CPushStatement(expression=transform_expression(accumulators, statement.expression))
def transform_statement(accumulators, statement):
return {
- parsing.FurAssignmentStatement: transform_symbol_assignment_statement,
- parsing.FurExpressionStatement: transform_expression_statement,
- normalization.NormalArrayVariableInitializationStatement: transform_array_variable_initialization_statement,
- normalization.NormalExpressionStatement: transform_expression_statement,
- normalization.NormalFunctionDefinitionStatement: transform_function_definition_statement,
- normalization.NormalIfElseStatement: transform_if_else_statement,
- normalization.NormalVariableInitializationStatement: transform_variable_initialization_statement,
- normalization.NormalVariableReassignmentStatement: transform_variable_reassignment_statement,
+ conversion.CPSArrayVariableInitializationStatement: transform_array_variable_initialization_statement,
+ conversion.CPSAssignmentStatement: transform_symbol_assignment_statement,
+ conversion.CPSExpressionStatement: transform_expression_statement,
+ conversion.CPSIfElseStatement: transform_if_else_statement,
+ conversion.CPSListAppendStatement: transform_list_append_statement,
+ conversion.CPSPushStatement: transform_push_statement,
+ conversion.CPSSymbolArrayVariableInitializationStatement: transform_symbol_array_variable_initialization_statement,
+ conversion.CPSVariableInitializationStatement: transform_variable_initialization_statement,
+ conversion.CPSVariableReassignmentStatement: transform_variable_reassignment_statement,
}[type(statement)](accumulators, statement)
[
'builtin_set',
'function_definition_list',
+ 'lambda_number_list',
+ 'operator_set',
'symbol_list',
'string_literal_list',
],
accumulators = Accumulators(
builtin_set=set(),
function_definition_list=[],
+ lambda_number_list=[],
+ operator_set=set(),
symbol_list=[],
string_literal_list=[],
)
return CProgram(
builtin_set=accumulators.builtin_set,
function_definition_list=accumulators.function_definition_list,
+ operator_declarations=tuple(sorted(accumulators.operator_set)),
statements=statement_list,
standard_libraries=standard_library_set,
string_literal_list=accumulators.string_literal_list,