generate_expression(statement.condition_expression),
)
- if len(statement.if_statements) == 0:
+ if len(statement.if_statement_list) == 0:
condition_expression = '!({})'.format(condition_expression)
- if_statements = statement.else_statements
- else_statements = ()
+ if_statement_list = statement.else_statement_list
+ else_statement_list = ()
else:
- if_statements = statement.if_statements
- else_statements = statement.else_statements
+ if_statement_list = statement.if_statement_list
+ else_statement_list = statement.else_statement_list
generated_if_clause = 'if({})'.format(condition_expression)
- if len(if_statements) == 0:
- generated_if_statements = ';'
+ if len(if_statement_list) == 0:
+ generated_if_statement_list = ';'
else:
- generated_if_statements = indent('\n{{\n{}\n}}'.format(
- indent('\n'.join(generate_statement(s) for s in if_statements)),
+ generated_if_statement_list = indent('\n{{\n{}\n}}'.format(
+ indent('\n'.join(generate_statement(s) for s in if_statement_list)),
))
- if len(else_statements) == 0:
- generated_else_statements = ''
+ if len(else_statement_list) == 0:
+ generated_else_statement_list = ''
else:
- generated_else_statements = indent('\nelse\n{{\n{}\n}}'.format(
- indent('\n'.join(generate_statement(s) for s in else_statements)),
+ generated_else_statement_list = indent('\nelse\n{{\n{}\n}}'.format(
+ indent('\n'.join(generate_statement(s) for s in else_statement_list)),
))
- return generated_if_clause + generated_if_statements + generated_else_statements
+ return generated_if_clause + generated_if_statement_list + generated_else_statement_list
def generate_function_declaration(statement):
return 'Environment_set(environment, "{}", (Object){{ CLOSURE, (Instance)(Closure){{ environment, user${}$implementation }} }});'.format(statement.name, statement.name)
'NormalIfElseStatement',
[
'condition_expression',
- 'if_statements',
- 'else_statements',
+ 'if_statement_list',
+ 'else_statement_list',
],
)
return (counter, (), thing)
def normalize_integer_literal_expression(counter, expression):
+ variable = '${}'.format(counter)
return (
- counter,
- (),
- NormalIntegerLiteralExpression(integer=expression.integer),
+ counter + 1,
+ (
+ NormalVariableInitializationStatement(
+ variable=variable,
+ expression=NormalIntegerLiteralExpression(integer=expression.integer),
+ ),
+ ),
+ NormalVariableExpression(variable=variable),
)
def normalize_string_literal_expression(counter, expression):
+ variable = '${}'.format(counter)
return (
- counter,
- (),
- NormalStringLiteralExpression(string=expression.string),
+ counter + 1,
+ (
+ NormalVariableInitializationStatement(
+ variable=variable,
+ expression=NormalStringLiteralExpression(string=expression.string),
+ ),
+ ),
+ NormalVariableExpression(variable=variable),
)
def normalize_symbol_expression(counter, expression):
variable = '${}'.format(counter)
return (
counter + 1,
- (NormalVariableInitializationStatement(
- variable=variable,
- expression=NormalSymbolExpression(symbol=expression.symbol),
- ),),
+ (
+ NormalVariableInitializationStatement(
+ variable=variable,
+ expression=NormalSymbolExpression(symbol=expression.symbol),
+ ),
+ ),
NormalVariableExpression(variable=variable),
)
prestatements.append(s)
variable = '${}'.format(counter)
- prestatements.append(NormalVariableInitializationStatement(
- variable=variable,
- expression=normalized_argument,
- ))
+ prestatements.append(
+ NormalVariableInitializationStatement(
+ variable=variable,
+ expression=normalized_argument,
+ )
+ )
arguments.append(NormalVariableExpression(
variable=variable,
))
if not isinstance(function_expression, NormalVariableExpression):
function_variable = '${}'.format(counter)
- prestatements.append(NormalVariableInitializationStatement(
- variable=function_variable,
- expression=function_expression,
- ))
+ prestatements.append(
+ NormalVariableInitializationStatement(
+ variable=function_variable,
+ expression=function_expression,
+ )
+ )
function_expression = NormalVariableExpression(variable=function_variable)
counter += 1
counter += 1
right_variable = '${}'.format(counter)
counter += 1
+ center_variable = '${}'.format(counter)
+ counter += 1
root_prestatements = (
NormalVariableInitializationStatement(
variable=right_variable,
expression=right_expression,
),
+ NormalVariableInitializationStatement(
+ variable=center_variable,
+ expression=NormalInfixExpression(
+ order=expression.order,
+ operator=expression.operator,
+ left=NormalVariableExpression(variable=left_variable),
+ right=NormalVariableExpression(variable=right_variable),
+ ),
+ ),
)
return (
counter,
left_prestatements + right_prestatements + root_prestatements,
- NormalInfixExpression(
- order=expression.order,
- operator=expression.operator,
- left=NormalVariableExpression(variable=left_variable),
- right=NormalVariableExpression(variable=right_variable),
- ),
+ NormalVariableExpression(variable=center_variable),
)
def normalize_comparison_expression(counter, expression):
counter, right_prestatements, right_expression = normalize_expression(counter, expression.right)
result_variable = '${}'.format(counter)
- if_else_prestatment = NormalVariableInitializationStatement(variable=result_variable, expression=left_expression)
+ if_else_prestatment = NormalVariableInitializationStatement(
+ variable=result_variable,
+ expression=left_expression,
+ )
counter += 1
condition_expression=NormalVariableExpression(variable=result_variable)
if expression.operator == 'and':
if_else_statement = NormalIfElseStatement(
condition_expression=condition_expression,
- if_statements=short_circuited_statements,
- else_statements=(),
+ if_statement_list=short_circuited_statements,
+ else_statement_list=(),
)
elif expression.operator == 'or':
if_else_statement = NormalIfElseStatement(
condition_expression=condition_expression,
- if_statements=(),
- else_statements=short_circuited_statements,
+ if_statement_list=(),
+ else_statement_list=short_circuited_statements,
)
else:
'or_level': normalize_boolean_expression,
}[expression.order](counter, expression)
+def normalize_if_expression(counter, expression):
+ counter, condition_prestatements, condition_expression = normalize_expression(
+ counter,
+ expression.condition_expression,
+ )
+
+ result_variable = '${}'.format(counter)
+ counter += 1
+
+ counter, if_statement_list = normalize_statement_list(
+ counter,
+ expression.if_statement_list,
+ assign_result_to=result_variable,
+ )
+ counter, else_statement_list = normalize_statement_list(
+ counter,
+ expression.else_statement_list,
+ assign_result_to=result_variable,
+ )
+
+ return (
+ counter,
+ condition_prestatements + (
+ NormalVariableInitializationStatement(
+ variable=result_variable,
+ expression=NormalVariableExpression(variable='builtin$nil'),
+ ),
+ NormalIfElseStatement(
+ condition_expression=condition_expression,
+ if_statement_list=if_statement_list,
+ else_statement_list=else_statement_list,
+ ),
+ ),
+ NormalVariableExpression(variable=result_variable),
+ )
+
+
+
+
def normalize_negation_expression(counter, expression):
counter, prestatements, internal_expression = normalize_expression(counter, expression.value)
return (
counter,
- prestatements + (NormalVariableInitializationStatement(variable=internal_variable, expression=internal_expression),),
+ prestatements + (
+ NormalVariableInitializationStatement(
+ variable=internal_variable,
+ expression=internal_expression,
+ ),
+ ),
NormalNegationExpression(internal_expression=NormalVariableExpression(variable=internal_variable)),
)
NormalInfixExpression: fake_normalization,
NormalVariableExpression: fake_normalization,
parsing.FurFunctionCallExpression: normalize_function_call_expression,
+ parsing.FurIfExpression: normalize_if_expression,
parsing.FurInfixExpression: normalize_infix_expression,
parsing.FurIntegerLiteralExpression: normalize_integer_literal_expression,
parsing.FurNegationExpression: normalize_negation_expression,
)
def normalize_function_definition_statement(counter, statement):
+ _, statement_list = normalize_statement_list(
+ 0,
+ statement.statement_list,
+ assign_result_to='result',
+ )
return (
counter,
(),
NormalFunctionDefinitionStatement(
name=statement.name,
argument_name_list=statement.argument_name_list,
- statement_list=normalize_statement_list(statement.statement_list),
+ statement_list=statement_list,
),
)
}[type(statement)](counter, statement)
@util.force_generator(tuple)
-def normalize_statement_list(statement_list):
- counter = 0
+def normalize_statement_list(counter, statement_list, **kwargs):
+ assign_result_to = kwargs.pop('assign_result_to', None)
+
+ assert len(kwargs) == 0
+
+ result_statement_list = []
for statement in statement_list:
counter, prestatements, normalized = normalize_statement(counter, statement)
for s in prestatements:
- yield s
- yield normalized
+ result_statement_list.append(s)
+ result_statement_list.append(normalized)
+
+ last_statement = result_statement_list[-1]
+
+ if isinstance(last_statement, NormalExpressionStatement) and isinstance(last_statement.expression, NormalVariableExpression):
+ result_expression = result_statement_list.pop().expression
+
+ if assign_result_to is not None:
+ result_statement_list.append(
+ NormalVariableReassignmentStatement(
+ variable=assign_result_to,
+ expression=result_expression,
+ )
+ )
+
+ return (
+ counter,
+ result_statement_list,
+ )
def normalize(program):
+ _, statement_list = normalize_statement_list(0, program.statement_list)
+
return NormalProgram(
- statement_list=normalize_statement_list(program.statement_list),
+ statement_list=statement_list,
)
],
)
+FurIfExpression = collections.namedtuple(
+ 'FurIfExpression',
+ [
+ 'condition_expression',
+ 'if_statement_list',
+ 'else_statement_list',
+ ],
+)
+
def _integer_literal_expression_parser(index, tokens):
failure = (False, index, None)
return True, index, function
-_expression_parser = _or_level_expression_parser
+def _if_expression_parser(index, tokens):
+ failure = (False, index, None)
+
+ if tokens[index].match == 'if':
+ index += 1
+ else:
+ return failure
+
+ success, index, condition_expression = _or_level_expression_parser(index, tokens)
+
+ if not success:
+ raise Exception('Expected condition after "if" on line {}'.format(tokens[index].line))
+
+ if tokens[index].match == 'do':
+ index += 1
+ else:
+ raise Exception('Expected "do" after "if" on line {}'.format(tokens[index].line))
+
+
+ success, index, if_statement_list = _zero_or_more_parser(tuple, _statement_parser)(index, tokens)
+ _, index, _ = consume_newlines(index, tokens)
+
+ if tokens[index].match == 'else':
+ index += 1
+ success, index, else_statement_list = _zero_or_more_parser(tuple, _statement_parser)(index, tokens)
+ _, index, _ = consume_newlines(index, tokens)
+ else:
+ else_statement_list = ()
+
+ if tokens[index].match == 'end':
+ index += 1
+ else:
+ raise Exception('Expected "end" after "if" on line {}'.format(tokens[index].line))
+
+ return (
+ True,
+ index,
+ FurIfExpression(
+ condition_expression=condition_expression,
+ if_statement_list=if_statement_list,
+ else_statement_list=else_statement_list,
+ ),
+ )
+
+
+
+
+_expression_parser = _or_parser(
+ _or_level_expression_parser,
+ _if_expression_parser, # This should always be at the top level
+)
def _expression_statement_parser(index, tokens):
failure = (False, index, None)